diff --git a/src/Conso/Commands/Compile.php b/src/Conso/Commands/Compile.php index 743352f..19008fb 100644 --- a/src/Conso/Commands/Compile.php +++ b/src/Conso/Commands/Compile.php @@ -1,188 +1,204 @@ - - * @package Conso PHP Console Creator + * * @version 2.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ use Conso\Command; -use Conso\Exceptions\{CompileException,InputException}; -use Conso\Contracts\{CommandInterface,InputInterface,OutputInterface}; +use Conso\Contracts\CommandInterface; +use Conso\Contracts\InputInterface; +use Conso\Contracts\OutputInterface; +use Conso\Exceptions\CompileException; +use Conso\Exceptions\InputException; class Compile extends Command implements CommandInterface { /** - * sub commands + * sub commands. * * @var array */ protected $sub = [ - 'init' + 'init', ]; /** - * flags + * flags. * * @var array */ protected $flags = [ - '--no-shebang' + '--no-shebang', ]; /** - * command help + * command help. * * @var array */ - protected $help = [ + protected $help = [ 'sub' => [ - 'init' => 'initialize .json build file' + 'init' => 'initialize .json build file', ], 'flags' => [ - '--no-shebang' => 'no stub shebang, usefull when invoking phar from http' - ] + '--no-shebang' => 'no stub shebang, usefull when invoking phar from http', + ], ]; /** - * command description + * command description. * * @var string */ protected $description = 'Compile your package to a shareable phar file.'; /** - * set up command dependencies + * set up command dependencies. */ public function __construct() { $this->cwd = getcwd(); - $this->packageFile = $this->cwd . '/conso.json'; + $this->packageFile = $this->cwd.'/conso.json'; } /** - * execute method + * execute method. + * + * @param InputInterface $input + * @param OutputInterface $output * - * @param InputInterface $input - * @param OutputInterface $output * @return void */ - public function execute(InputInterface $input, OutputInterface $output) : void + public function execute(InputInterface $input, OutputInterface $output): void { - if(!\file_exists($this->packageFile)) + if (!\file_exists($this->packageFile)) { throw new CompileException("package file ($this->packageFile) not found"); - - if(ini_get('phar.readonly') == 1) - throw new CompileException("phar is read only mode ! it should be turned off from php.init to compile."); - + } + if (ini_get('phar.readonly') == 1) { + throw new CompileException('phar is read only mode ! it should be turned off from php.init to compile.'); + } // read package file $buildFile = (array) json_decode(file_get_contents($this->packageFile)); // validate build file $this->validateBuildFile($buildFile); - + // create a phar file if everything went well $shebang = ($input->flag('--no-shebang') === false) ? true : false; - if($this->createPhar($buildFile, $shebang)) + if ($this->createPhar($buildFile, $shebang)) { exit($output->writeLn("\npackage compiled successfully .\n\n", 'green')); - + } + $output->writeLn("error compiling package.\n", 'red'); } /** - * init package to compile + * init package to compile. * * @return void */ - public function init(InputInterface $input, OutputInterface $output) : void + public function init(InputInterface $input, OutputInterface $output): void { - if(!\is_writable($this->cwd)) + if (!\is_writable($this->cwd)) { throw new InputException("{$this->cwd} is not writable."); - - if(\file_exists($this->packageFile)) + } + if (\file_exists($this->packageFile)) { throw new InputException("build file ({$this->packageFile}) exists already."); - + } $content = [ - "src" => [ - "src/Conso", - "vendor" + 'src' => [ + 'src/Conso', + 'vendor', ], - "build" => "build", - "stub" => "conso", - "phar" => "conso.phar" + 'build' => 'build', + 'stub' => 'conso', + 'phar' => 'conso.phar', ]; - if(\file_put_contents($this->packageFile, json_encode($content, JSON_PRETTY_PRINT))) + if (\file_put_contents($this->packageFile, json_encode($content, JSON_PRETTY_PRINT))) { exit($output->writeLn("\nbuild file created successfully.\n\n", 'green')); + } $output->writeLn("error creating build file.\n", 'red'); } /** - * validate build file + * validate build file. * * @param string + * * @return void */ - private function validateBuildFile(array $file) : void + private function validateBuildFile(array $file): void { - if(!is_array($file) || count($file) < 4) - throw new CompileException("build file is not a valid json file."); - - if(!in_array('src', array_keys($file))) - throw new CompileException("source (src) directory is missing from package file."); - - if(!in_array('build', array_keys($file))) - throw new CompileException("build (build) directory is missing from package file."); - - if(!in_array('stub', array_keys($file))) - throw new CompileException("stub (stub) file is missing from package file."); - - if(!in_array('phar', array_keys($file))) - throw new CompileException("output (phar) file is missing from package file."); - - if(!is_array($file['src'])) - throw new CompileException("source (src) directory must be an array of valid directories."); - - foreach($file['src'] as $dir) - if(!is_dir($dir)) + if (!is_array($file) || count($file) < 4) { + throw new CompileException('build file is not a valid json file.'); + } + if (!in_array('src', array_keys($file))) { + throw new CompileException('source (src) directory is missing from package file.'); + } + if (!in_array('build', array_keys($file))) { + throw new CompileException('build (build) directory is missing from package file.'); + } + if (!in_array('stub', array_keys($file))) { + throw new CompileException('stub (stub) file is missing from package file.'); + } + if (!in_array('phar', array_keys($file))) { + throw new CompileException('output (phar) file is missing from package file.'); + } + if (!is_array($file['src'])) { + throw new CompileException('source (src) directory must be an array of valid directories.'); + } + foreach ($file['src'] as $dir) { + if (!is_dir($dir)) { throw new CompileException("source ($dir) directory is not a valid directory."); - - if(!is_string($file['build']) || !is_dir($file['build']) || !is_writable($file['build'])) + } + } + if (!is_string($file['build']) || !is_dir($file['build']) || !is_writable($file['build'])) { throw new CompileException("build ({$file['build']}) directory is not a valid directory."); - - if(!is_string($file['stub']) || !file_exists($file['stub'])) + } + if (!is_string($file['stub']) || !file_exists($file['stub'])) { throw new CompileException("stub file ({$file['stub']}) not found."); - - if(!is_string($file['phar']) || strlen($file['phar']) < 1 || preg_match('/\.phar$/', $file['phar']) == false) + } + if (!is_string($file['phar']) || strlen($file['phar']) < 1 || preg_match('/\.phar$/', $file['phar']) == false) { throw new CompileException("phar file ({$file['phar']}) must be a valid file ends with .phar extension."); + } } /** - * create a phar + * create a phar. + * + * @param array $rules * - * @param array $rules * @return void */ - private function createPhar(array $rules, bool $shebang = false) : bool + private function createPhar(array $rules, bool $shebang = false): bool { - $buildLocation = rtrim($rules['build'], '/') . "/package/"; + $buildLocation = rtrim($rules['build'], '/').'/package/'; // delete old build files if any deleteTree($buildLocation); // copy project - foreach($rules['src'] as $src) - copyDirectory($src, $buildLocation . rtrim($src, '/')); + foreach ($rules['src'] as $src) { + copyDirectory($src, $buildLocation.rtrim($src, '/')); + } // copy stub file - copy($rules['stub'], $buildLocation . $rules['stub']); + copy($rules['stub'], $buildLocation.$rules['stub']); // create phar - $phar = new \Phar(rtrim($rules['build'], '/') . "/" . $rules['phar']); + $phar = new \Phar(rtrim($rules['build'], '/').'/'.$rules['phar']); // start buffering. Mandatory to modify stub to add shebang $phar->startBuffering(); @@ -194,7 +210,7 @@ private function createPhar(array $rules, bool $shebang = false) : bool $phar->buildFromDirectory($buildLocation); // add shebang - $stub = "#!/usr/bin/env php \n" . $defaultStub; + $stub = "#!/usr/bin/env php \n".$defaultStub; $phar->setStub($shebang === true ? $stub : $defaultStub); // stop buffering @@ -203,8 +219,9 @@ private function createPhar(array $rules, bool $shebang = false) : bool // gzip compressin $phar->compressFiles(\Phar::GZ); - # Make the file executable - chmod(rtrim($rules['build'], '/') . "/" . $rules['phar'], 0770); + // Make the file executable + chmod(rtrim($rules['build'], '/').'/'.$rules['phar'], 0770); + return deleteTree($buildLocation); } -} \ No newline at end of file +} diff --git a/src/Conso/Testing/TestCase.php b/src/Conso/Testing/TestCase.php index d40bbe1..3103d98 100644 --- a/src/Conso/Testing/TestCase.php +++ b/src/Conso/Testing/TestCase.php @@ -37,4 +37,4 @@ public function setUp(): void $this->output->disableAnsi(); $this->output->enableTestMode(); } -} \ No newline at end of file +} diff --git a/src/Conso/hlprs.php b/src/Conso/hlprs.php index 28bf0f1..da32f1e 100644 --- a/src/Conso/hlprs.php +++ b/src/Conso/hlprs.php @@ -117,46 +117,49 @@ function commandHelp(array $command, $output) } /** - * copy directory recursively + * copy directory recursively. * * @param string $source * @param string $destination + * * @return void */ -function copyDirectory(string $source, string $destination) : void +function copyDirectory(string $source, string $destination): void { mkdir($destination, 0755, true); foreach ( $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), - \RecursiveIteratorIterator::SELF_FIRST) as $item - ) - { + \RecursiveIteratorIterator::SELF_FIRST + ) as $item + ) { if ($item->isDir()) { - mkdir($destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); + mkdir($destination.DIRECTORY_SEPARATOR.$iterator->getSubPathName()); } else { - copy($item, $destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); + copy($item, $destination.DIRECTORY_SEPARATOR.$iterator->getSubPathName()); } } } /** - * delete directory recursively + * delete directory recursively. * * @param string $dir + * * @return void */ function deleteTree(string $dir) -{ - if(!is_dir($dir)) +{ + if (!is_dir($dir)) { return false; - - $files = array_diff(scandir($dir), array('.', '..')); + } + + $files = array_diff(scandir($dir), ['.', '..']); - foreach ($files as $file) { - (is_dir("$dir/$file")) ? deleteTree("$dir/$file") : unlink("$dir/$file"); + foreach ($files as $file) { + (is_dir("$dir/$file")) ? deleteTree("$dir/$file") : unlink("$dir/$file"); } - return rmdir($dir); -} \ No newline at end of file + return rmdir($dir); +} diff --git a/tests/Unit/CommandInvokerTest.php b/tests/Unit/CommandInvokerTest.php index 3677c74..bdc69d2 100644 --- a/tests/Unit/CommandInvokerTest.php +++ b/tests/Unit/CommandInvokerTest.php @@ -14,10 +14,9 @@ * @copyright 2019 Lotfio Lakehal */ +use Conso\CommandInvoker; use Conso\Conso; use Conso\Input; -use Conso\Output; -use Conso\CommandInvoker; use Conso\Testing\TestCase; class CommandInvokerTest extends TestCase @@ -37,7 +36,7 @@ class CommandInvokerTest extends TestCase public function setUp(): void { parent::setUp(); - + $this->commands = [ [ 'name' => 'make', diff --git a/tests/Unit/CommandLinkerTest.php b/tests/Unit/CommandLinkerTest.php index fa240b0..f5cb37e 100644 --- a/tests/Unit/CommandLinkerTest.php +++ b/tests/Unit/CommandLinkerTest.php @@ -14,11 +14,10 @@ * @copyright 2019 Lotfio Lakehal */ -use Conso\Input; -use Conso\Output; use Conso\CommandLinker; -use Conso\Testing\TestCase; use Conso\Exceptions\InputException; +use Conso\Input; +use Conso\Testing\TestCase; class CommandLinkerTest extends TestCase { diff --git a/tests/Unit/ConsoTest.php b/tests/Unit/ConsoTest.php index 92e3227..2da3f85 100644 --- a/tests/Unit/ConsoTest.php +++ b/tests/Unit/ConsoTest.php @@ -16,7 +16,6 @@ use Conso\Conso; use Conso\Input; -use Conso\Output; use Conso\Testing\TestCase; class ConsoTest extends TestCase