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

Commit 6c4a144

Browse files
committed
Partial work
1 parent 78bfede commit 6c4a144

File tree

8 files changed

+182
-6
lines changed

8 files changed

+182
-6
lines changed

src/Command/InstallCommand.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
final class InstallCommand extends Command
1515
{
16+
const RETURN_CODE_OK = 0;
17+
const RETURN_CODE_INSTALLER_INSTANTIATION_ERROR = 1;
18+
1619
protected function configure()
1720
{
1821
$this
@@ -44,6 +47,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
4447
$installPath = ($input->hasArgument('path')) ? $input->getArgument('path') : null;
4548
$composerCmd = ($input->hasArgument('composer_cmd')) ? $input->getArgument('composer_cmd') : null;
4649

47-
$installer = Installer::getInstaller($installPath, $composerCmd);
50+
$io = $this->getIO();
51+
52+
try {
53+
$installer = Installer::getInstaller($composerCmd, $installPath);
54+
} catch (\RuntimeException $e) {
55+
$io->writeError('ERROR: '.$e->getMessage());
56+
// TODO : Use verbosity levels to enable showing the stacktrace
57+
return self::RETURN_CODE_INSTALLER_INSTANTIATION_ERROR;
58+
}
59+
60+
61+
62+
return self::RETURN_CODE_OK;
4863
}
4964
}

src/Installer/Installer.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,56 @@
44
namespace Litipk\JupyterPhpInstaller\Installer;
55

66

7+
use Litipk\JupyterPhpInstaller\System\MacSystem;
78
use Litipk\JupyterPhpInstaller\System\System;
9+
use Litipk\JupyterPhpInstaller\System\UnixSystem;
10+
use Litipk\JupyterPhpInstaller\System\WindowsSystem;
811

912
abstract class Installer
1013
{
14+
/** @var System */
15+
protected $system;
16+
17+
/** @var string */
18+
protected $composerCmd;
19+
1120
/**
12-
* @param null|string $installPath
1321
* @param null|string $composerCmd
22+
* @param null|string $installPath
1423
* @return Installer
1524
*/
16-
public static function getInstaller($installPath = null, $composerCmd = null)
25+
public static function getInstaller($composerCmd = null, $installPath = null)
1726
{
1827
$system = System::getSystem();
28+
$composerCmd = (null !== $composerCmd) ? $composerCmd : $system->getComposerCommand();
29+
30+
if (null === $composerCmd || !$system->checkIfCommandExists($composerCmd)) {
31+
throw new \RuntimeException('Unable to find the composer executable.');
32+
}
33+
34+
if (null !== $installPath && !$system->validatePath($installPath)) {
35+
throw new \RuntimeException('Invalid install path.');
36+
}
37+
38+
if ($system instanceof UnixSystem) {
39+
return new LinuxInstaller($system, $composerCmd);
40+
} elseif ($system instanceof MacSystem) {
41+
return new MacInstaller($system, $composerCmd);
42+
} elseif ($system instanceof WindowsSystem) {
43+
return new WindowsInstaller($system, $composerCmd);
44+
} else {
45+
throw new \RuntimeException('This platform is unknown for the installer');
46+
}
47+
}
48+
49+
/**
50+
* Installer constructor.
51+
* @param System $system
52+
* @param string $composerCmd
53+
*/
54+
protected function __construct(System $system, $composerCmd)
55+
{
56+
$this->system = $system;
57+
$this->composerCmd = $composerCmd;
1958
}
2059
}

src/Installer/LinuxInstaller.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44
namespace Litipk\JupyterPhpInstaller\Installer;
55

66

7+
use Litipk\JupyterPhpInstaller\System\UnixSystem;
8+
9+
710
final class LinuxInstaller extends Installer
811
{
9-
12+
/**
13+
* LinuxInstaller constructor.
14+
* @param UnixSystem $system
15+
* @param string $composerCmd
16+
*/
17+
public function __construct(UnixSystem $system, $composerCmd)
18+
{
19+
parent::__construct($system, $composerCmd);
20+
}
1021
}

src/Installer/MacInstaller.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44
namespace Litipk\JupyterPhpInstaller\Installer;
55

66

7+
use Litipk\JupyterPhpInstaller\System\MacSystem;
8+
9+
710
final class MacInstaller extends Installer
811
{
9-
12+
/**
13+
* LinuxInstaller constructor.
14+
* @param MacSystem $system
15+
* @param string $composerCmd
16+
*/
17+
public function __construct(MacSystem $system, $composerCmd)
18+
{
19+
parent::__construct($system, $composerCmd);
20+
}
1021
}

src/Installer/WindowsInstaller.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44
namespace Litipk\JupyterPhpInstaller\Installer;
55

66

7+
use Litipk\JupyterPhpInstaller\System\WindowsSystem;
8+
9+
710
final class WindowsInstaller extends Installer
811
{
9-
12+
/**
13+
* LinuxInstaller constructor.
14+
* @param WindowsSystem $system
15+
* @param string $composerCmd
16+
*/
17+
public function __construct(WindowsSystem $system, $composerCmd)
18+
{
19+
parent::__construct($system, $composerCmd);
20+
}
1021
}

src/System/System.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,34 @@ public abstract function getCurrentUserHome();
4646
*/
4747
public abstract function checkIfCommandExists($cmdName);
4848

49+
/** @return string|null */
50+
public abstract function getComposerCommand();
51+
52+
/**
53+
* Returns true if the path is a "valid" path and is writable (event if the complete path does not yet exist).
54+
* @param string $path
55+
* @return boolean
56+
*/
57+
public abstract function validatePath($path);
58+
59+
/**
60+
* @param string $path
61+
* @return string The "absolute path" version of $path.
62+
*/
63+
public abstract function ensurePath($path);
64+
65+
/**
66+
* @param string $path
67+
* @return boolean
68+
*/
69+
protected abstract function isAbsolutePath($path);
70+
71+
/**
72+
* @param string $path
73+
* @return string
74+
*/
75+
protected abstract function getAbsolutePath($path);
76+
4977
/** @return integer */
5078
private static function guessOperativeSystem()
5179
{

src/System/UnixSystem.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,59 @@ public function checkIfCommandExists($cmdName)
4848

4949
return filter_var($sysResponse, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
5050
}
51+
52+
/** @return string|null */
53+
public function getComposerCommand()
54+
{
55+
$potentialCommands = [
56+
'composer', 'composer.phar', './composer.phar', './composer', './bin/composer', './bin/composer.phar',
57+
'./vendor/bin/composer', './vendor/bin/composer.phar', '../vendor/bin/composer',
58+
'../vendor/bin/composer.phar', '../../vendor/bin/composer', '../../vendor/bin/composer.phar'
59+
];
60+
61+
foreach ($potentialCommands as $potentialCommand) {
62+
if ($this->checkIfCommandExists($potentialCommand)) {
63+
return $potentialCommand;
64+
}
65+
}
66+
67+
return null;
68+
}
69+
70+
/**
71+
* Returns true if the path is a "valid" path and is writable (event if the complete path does not yet exist).
72+
* @param string $path
73+
* @return boolean
74+
*/
75+
public function validatePath($path)
76+
{
77+
$absPath = $this->getAbsolutePath($path);
78+
}
79+
80+
/**
81+
* @param string $path
82+
* @return string The "absolute path" version of $path.
83+
*/
84+
public function ensurePath($path)
85+
{
86+
87+
}
88+
89+
/**
90+
* @param string $path
91+
* @return bool
92+
*/
93+
protected function isAbsolutePath($path)
94+
{
95+
return (1 === preg_match('#^/#', $path));
96+
}
97+
98+
/**
99+
* @param string $path
100+
* @return string
101+
*/
102+
protected function getAbsolutePath($path)
103+
{
104+
return $this->isAbsolutePath($path) ? $path : (getcwd() . DIRECTORY_SEPARATOR . $path);
105+
}
51106
}

src/System/WindowsSystem.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@ public function checkIfCommandExists($cmdName)
4141
{
4242
return true;
4343
}
44+
45+
/** @return string */
46+
public function getComposerCommand()
47+
{
48+
return 'composer';
49+
}
4450
}

0 commit comments

Comments
 (0)