Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 3ed4738

Browse files
author
Andrés Correa Casablanca
committed
General improvements in the installer
1 parent 8406469 commit 3ed4738

File tree

11 files changed

+241
-279
lines changed

11 files changed

+241
-279
lines changed

src/Installer/Installer.php

Lines changed: 116 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ abstract class Installer
1818
/** @var string */
1919
protected $composerCmd;
2020

21-
/**
22-
* @param null|string $composerCmd
23-
* @return Installer
24-
*/
25-
public static function getInstaller($composerCmd = null)
21+
public static function getInstaller(string $composerCmd = null): Installer
2622
{
2723
$system = System::getSystem();
2824
$composerCmd = (null !== $composerCmd) ? $composerCmd : $system->getComposerCommand();
@@ -45,11 +41,7 @@ public static function getInstaller($composerCmd = null)
4541
}
4642
}
4743

48-
/**
49-
* @param null|string $installPath
50-
* @param bool $beVerbose
51-
*/
52-
public function install($installPath = null, $beVerbose = false)
44+
public function install(string $installPath = null, bool $beVerbose = false)
5345
{
5446
$installPath = (null !== $installPath) ? $installPath : $this->getInstallPath();
5547
if (null !== $installPath && !$this->system->validatePath($installPath)) {
@@ -62,70 +54,141 @@ public function install($installPath = null, $beVerbose = false)
6254
$this->installKernel();
6355
}
6456

65-
/**
66-
* @return string
67-
*/
68-
protected abstract function getInstallPath();
69-
70-
/**
71-
*
72-
*/
73-
protected abstract function installKernel();
74-
75-
/**
76-
* @param $installPath
77-
* @return mixed
78-
*/
79-
protected abstract function executeSilentComposerCommand($installPath);
80-
81-
/**
82-
* @param string $installPath
83-
* @param bool $beVerbose
84-
*/
85-
protected function executeComposerCommand($installPath, $beVerbose = false)
57+
protected function getInstallPath(): string
58+
{
59+
return ($this->system->isRunningAsAdmin())
60+
? $this->getAdminInstallPath()
61+
: $this->getUserInstallPath();
62+
}
63+
64+
protected abstract function getAdminInstallPath(): string;
65+
66+
protected abstract function getUserInstallPath(): string;
67+
68+
protected function installKernel()
69+
{
70+
$kernelDef = json_encode([
71+
'argv' => [
72+
'php',
73+
$this->getKernelEntrypointPath(),
74+
'{connection_file}'
75+
],
76+
'display_name' => 'PHP',
77+
'language' => 'php',
78+
'env' => new \stdClass
79+
]);
80+
81+
$kernelSpecPath = ($this->system->isRunningAsAdmin())
82+
? $this->getJupyterKernelsMetadataAdminPath()
83+
: $this->getJupyterKernelsMetadatUserPath();
84+
85+
$this->system->ensurePath($kernelSpecPath);
86+
file_put_contents($kernelSpecPath.'/kernel.json', $kernelDef);
87+
}
88+
89+
protected abstract function getKernelEntryPointPath(): string;
90+
91+
protected abstract function getJupyterKernelsMetadataAdminPath(): string;
92+
93+
protected abstract function getJupyterKernelsMetadatUserPath(): string;
94+
95+
protected function executeComposerCommand(string $installPath, bool $beVerbose = false)
8696
{
8797
$composerStatus = 0;
8898

8999
$pkgsDir = $installPath.DIRECTORY_SEPARATOR.'pkgs';
90-
if (file_exists($pkgsDir)) {
91-
foreach (
92-
new \RecursiveIteratorIterator(
93-
new \RecursiveDirectoryIterator($pkgsDir, \FilesystemIterator::SKIP_DOTS),
94-
\RecursiveIteratorIterator::CHILD_FIRST
95-
) as $path
96-
) {
97-
$path->isDir() && !$path->isLink() ? rmdir($path->getPathname()) : unlink($path->getPathname());
98-
}
99-
rmdir($pkgsDir);
100-
}
100+
$this->preparePackagesDir($pkgsDir);
101101

102102
if ($beVerbose) {
103103
echo "\n";
104104
passthru(
105-
'PATH=' . getenv('PATH') . ' && ' .
106-
$this->composerCmd . ' --prefer-dist --no-interaction --working-dir="' .
107-
$installPath .'" create-project litipk/jupyter-php=0.* pkgs',
105+
$this->system->wrapCommandToAttachEnvironmentVariable(
106+
'PATH', getenv('PATH'),
107+
$this->getComposerInitCommand($pkgsDir) . ' && ' .
108+
$this->getComposerInstallCommand($pkgsDir)
109+
),
108110

109111
$composerStatus
110112
);
111113
echo "\n";
112114
} else {
113-
$composerStatus = $this->executeSilentComposerCommand($installPath);
115+
$composerStatus = $this->executeSilentComposerCommand($pkgsDir);
114116
}
115117

116-
if ($composerStatus !== 0) {
118+
if (0 !== $composerStatus) {
117119
throw new \RuntimeException('Error while trying to download Jupyter-PHP dependencies with Composer.');
118120
}
119121
}
120122

121-
/**
122-
* Installer constructor.
123-
* @param System $system
124-
* @param string $composerCmd
125-
*/
126-
protected function __construct(System $system, $composerCmd)
123+
protected function executeSilentComposerCommand(string $pkgsDir)
124+
{
125+
$composerOutputLines = [];
126+
127+
exec(
128+
$this->system->wrapCommandToAttachEnvironmentVariable(
129+
'PATH', getenv('PATH'),
130+
$this->getComposerInitCommand($pkgsDir, true) . ' && ' .
131+
$this->getComposerInstallCommand($pkgsDir, true)
132+
),
133+
134+
$composerOutputLines,
135+
$composerStatus
136+
);
137+
138+
return $composerStatus;
139+
}
140+
141+
private function getComposerInitCommand(string $pkgsDir, bool $silent = false): string
142+
{
143+
$cmd = (
144+
$this->composerCmd . ' init ' .
145+
' --no-interaction ' .
146+
' --name=jupyter-php-instance ' .
147+
' --type=project ' .
148+
' --working-dir="' . $pkgsDir . '" ' .
149+
' --require=litipk/jupyter-php=0.* '
150+
);
151+
152+
return ($silent)
153+
? $this->system->wrapCommandToNullifyItsOutput($cmd)
154+
: $cmd;
155+
}
156+
157+
private function getComposerInstallCommand(string $pkgsDir, bool $silent = false): string
158+
{
159+
$cmd = (
160+
$this->composerCmd . ' install ' .
161+
' --no-interaction ' .
162+
' --no-progress ' .
163+
' --prefer-dist ' .
164+
' --optimize-autoloader ' .
165+
' --working-dir="' . $pkgsDir . '" '
166+
);
167+
168+
return ($silent)
169+
? $this->system->wrapCommandToNullifyItsOutput($cmd . ' --no-progress ')
170+
: $cmd;
171+
}
172+
173+
protected function __construct(System $system, string $composerCmd)
127174
{
128175
$this->system = $system;
129176
$this->composerCmd = $composerCmd;
130177
}
178+
179+
protected function preparePackagesDir(string $pkgsDir)
180+
{
181+
if (file_exists($pkgsDir)) {
182+
foreach (
183+
new \RecursiveIteratorIterator(
184+
new \RecursiveDirectoryIterator($pkgsDir, \FilesystemIterator::SKIP_DOTS),
185+
\RecursiveIteratorIterator::CHILD_FIRST
186+
) as $path
187+
) {
188+
$path->isDir() && !$path->isLink() ? rmdir($path->getPathname()) : unlink($path->getPathname());
189+
}
190+
rmdir($pkgsDir);
191+
}
192+
mkdir($pkgsDir);
193+
}
131194
}

src/Installer/LinuxInstaller.php

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,18 @@ public function __construct(UnixSystem $system, $composerCmd)
1919
parent::__construct($system, $composerCmd);
2020
}
2121

22-
/**
23-
* @return string
24-
*/
25-
protected function getInstallPath()
22+
protected function getAdminInstallPath(): string
2623
{
27-
$currentUser = $this->system->getCurrentUser();
28-
29-
if ('root' === $currentUser) {
30-
return '/opt/jupyter-php';
31-
} else {
32-
return $this->system->getCurrentUserHome().'/.jupyter-php';
33-
}
24+
return '/opt/jupyter-php';
3425
}
3526

36-
/**
37-
*
38-
*/
39-
protected function installKernel()
27+
protected function getUserInstallPath(): string
4028
{
41-
$kernelDef = json_encode([
42-
'argv' => ['php', $this->getInstallPath().'/pkgs/src/kernel.php', '{connection_file}'],
43-
'display_name' => 'PHP',
44-
'language' => 'php',
45-
'env' => new \stdClass
46-
]);
47-
48-
$currentUser = $this->system->getCurrentUser();
49-
50-
$kernelSpecPath = ('root' === $currentUser) ?
51-
'/usr/local/share/jupyter/kernels/jupyter-php' :
52-
$this->system->getCurrentUserHome().'/.local/share/jupyter/kernels/jupyter-php';
29+
return $this->system->getCurrentUserHome().'/.jupyter-php';
30+
}
5331

54-
$this->system->ensurePath($kernelSpecPath);
55-
file_put_contents($kernelSpecPath.'/kernel.json', $kernelDef);
32+
protected function getJupyterKernelsMetadatUserPath(): string
33+
{
34+
return $this->system->getCurrentUserHome().'/.local/share/jupyter/kernels/jupyter-php';
5635
}
5736
}

src/Installer/MacInstaller.php

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,23 @@
99

1010
final class MacInstaller extends UnixInstaller
1111
{
12-
/**
13-
* LinuxInstaller constructor.
14-
* @param MacSystem $system
15-
* @param string $composerCmd
16-
*/
17-
public function __construct(MacSystem $system, $composerCmd)
12+
public function __construct(MacSystem $system, string $composerCmd)
1813
{
1914
parent::__construct($system, $composerCmd);
2015
}
2116

22-
/**
23-
* @return string
24-
*/
25-
protected function getInstallPath()
17+
protected function getAdminInstallPath(): string
2618
{
27-
$currentUser = $this->system->getCurrentUser();
28-
29-
if ('root' === $currentUser) {
30-
return '/Applications/jupyter-php';
31-
} else {
32-
return $this->system->getCurrentUserHome().'/Library/jupyter-php';
33-
}
19+
return '/Applications/jupyter-php';
3420
}
3521

36-
/**
37-
*
38-
*/
39-
protected function installKernel()
22+
protected function getUserInstallPath(): string
4023
{
41-
$kernelDef = json_encode([
42-
'argv' => ['php', $this->getInstallPath().'/pkgs/src/kernel.php', '{connection_file}'],
43-
'display_name' => 'PHP',
44-
'language' => 'php',
45-
'env' => new \stdClass
46-
]);
47-
48-
$currentUser = $this->system->getCurrentUser();
49-
50-
$kernelSpecPath = ('root' === $currentUser) ?
51-
'/usr/local/share/jupyter/kernels/jupyter-php' :
52-
$this->system->getCurrentUserHome().'/Library/Jupyter/kernels/jupyter-php';
24+
return $this->system->getCurrentUserHome().'/Library/jupyter-php';
25+
}
5326

54-
$this->system->ensurePath($kernelSpecPath);
55-
file_put_contents($kernelSpecPath.'/kernel.json', $kernelDef);
27+
protected function getJupyterKernelsMetadatUserPath(): string
28+
{
29+
return $this->system->getCurrentUserHome().'/.local/share/jupyter/kernels/jupyter-php';
5630
}
5731
}

src/Installer/UnixInstaller.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,13 @@
66

77
abstract class UnixInstaller extends Installer
88
{
9-
/**
10-
* @param $installPath
11-
* @return mixed
12-
*/
13-
protected function executeSilentComposerCommand($installPath)
9+
protected function getJupyterKernelsMetadataAdminPath(): string
1410
{
15-
$composerOutputLines = [];
16-
17-
$composerOutput = exec(
18-
'PATH=' . getenv('PATH') . ' ' .
19-
$this->composerCmd . ' --prefer-dist --no-interaction --no-progress --working-dir="' .
20-
$installPath . '" create-project litipk/jupyter-php=dev-master pkgs > /dev/null 2>&1 ',
21-
22-
$composerOutputLines,
23-
$composerStatus
24-
);
11+
return '/usr/local/share/jupyter/kernels/jupyter-php';
12+
}
2513

26-
return $composerStatus;
14+
protected function getKernelEntryPointPath(): string
15+
{
16+
return $this->getInstallPath() . '/pkgs/vendor/litipk/jupyter-php/src/kernel.php';
2717
}
2818
}

0 commit comments

Comments
 (0)