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

Commit 6c3b706

Browse files
authored
Merge pull request #14 from bstoney/master
Added support for Windows
2 parents d7b081a + 21ee6de commit 6c3b706

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

src/Installer/WindowsInstaller.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,35 @@ public function __construct(WindowsSystem $system, $composerCmd)
2424
*/
2525
protected function getInstallPath()
2626
{
27-
// TODO: Implement getInstallPath() method.
27+
$currentUser = $this->system->getCurrentUser();
28+
29+
if ('Administrator' === $currentUser) {
30+
return 'C:\ProgramData\jupyter-php';
31+
} else {
32+
return $this->system->getCurrentUserHome() . '\.jupyter-php';
33+
}
2834
}
2935

3036
/**
3137
*
3238
*/
3339
protected function installKernel()
3440
{
35-
// TODO: Implement installKernel() method.
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 = ('Administrator' === $currentUser) ?
51+
'C:\ProgramData\jupyter\kernels\jupyter-php' :
52+
$this->system->getCurrentUserHome() . '\AppData\Roaming\jupyter\kernels\jupyter-php';
53+
54+
$this->system->ensurePath($kernelSpecPath);
55+
file_put_contents($kernelSpecPath . '\kernel.json', $kernelDef);
3656
}
3757

3858
/**
@@ -41,6 +61,16 @@ protected function installKernel()
4161
*/
4262
protected function executeSilentComposerCommand($installPath)
4363
{
44-
// TODO: Implement executeSilentComposerCommand() method.
64+
$composerOutputLines = [];
65+
66+
exec(
67+
$this->composerCmd . ' --prefer-dist --no-interaction --no-progress --working-dir="' .
68+
$installPath . '" create-project litipk/jupyter-php=dev-master pkgs > nul 2>&1 ',
69+
70+
$composerOutputLines,
71+
$composerStatus
72+
);
73+
74+
return $composerStatus;
4575
}
4676
}

src/System/WindowsSystem.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,38 @@ public function getComposerCommand()
5555
*/
5656
public function validatePath($path)
5757
{
58-
// TODO: Implement validatePath() method.
58+
$absPath = $this->getAbsolutePath($path);
59+
$absPathParts = explode(DIRECTORY_SEPARATOR, $absPath);
60+
$nSteps = count($absPathParts);
61+
62+
$tmpPath = $absPathParts[0];
63+
$prevReadable = false;
64+
$prevWritable = false;
65+
66+
for ($i = 1; $i < $nSteps; $i++) {
67+
$tmpPath .= DIRECTORY_SEPARATOR . $absPathParts[$i];
68+
69+
if (file_exists($tmpPath)) {
70+
if (!is_dir($tmpPath)) {
71+
if (is_link($tmpPath)) {
72+
$linkPath = readlink($tmpPath);
73+
if (false === $linkPath || !is_dir($linkPath)) {
74+
return false;
75+
}
76+
$tmpPath = $linkPath;
77+
} else {
78+
return false;
79+
}
80+
}
81+
82+
$prevReadable = is_readable($tmpPath);
83+
$prevWritable = is_writable($tmpPath);
84+
} else {
85+
return ($prevReadable && $prevWritable);
86+
}
87+
}
88+
89+
return true;
5990
}
6091

6192
/**
@@ -64,7 +95,13 @@ public function validatePath($path)
6495
*/
6596
public function ensurePath($path)
6697
{
67-
// TODO: Implement ensurePath() method.
98+
$absPath = $this->getAbsolutePath($path);
99+
100+
if (!file_exists($absPath) && false === mkdir($absPath, 0755, true)) {
101+
throw new \RuntimeException('Unable to create the specified directory (' . $absPath . ').');
102+
}
103+
104+
return $absPath;
68105
}
69106

70107
/**
@@ -73,7 +110,7 @@ public function ensurePath($path)
73110
*/
74111
protected function isAbsolutePath($path)
75112
{
76-
// TODO: Implement isAbsolutePath() method.
113+
return preg_match('/^[a-z]\:/i', $path) === 1;
77114
}
78115

79116
/**
@@ -82,6 +119,11 @@ protected function isAbsolutePath($path)
82119
*/
83120
protected function getAbsolutePath($path)
84121
{
85-
// TODO: Implement getAbsolutePath() method.
122+
$path = $this->isAbsolutePath($path) ? $path : (getcwd() . DIRECTORY_SEPARATOR . $path);
123+
124+
// Normalise directory separators
125+
$path = preg_replace('/[\/\\\\]/u', DIRECTORY_SEPARATOR, $path);
126+
127+
return $path;
86128
}
87129
}

0 commit comments

Comments
 (0)