From 88a459e7217e863509f76825d963a2f1bd39bdea Mon Sep 17 00:00:00 2001 From: Yannoff Date: Fri, 8 Mar 2024 18:44:12 +0100 Subject: [PATCH] Add `--file` multiple option --- README.md | 10 ++++++++-- doc/examples.md | 10 ++++++++++ src/Command/Compile.php | 31 ++++++++++++++++++++++++++----- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 127f18a..11f0882 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ chmod +x ${BINDIR}/phpcc ``` phpcc --help phpcc --version -phpcc -e -o [-d dir [-d dir ...]] [-b ] [-l ] +phpcc -e -o [-d dir [-d dir ...]] [-f file [-f file ...]] [-b ] ``` ### Options/Arguments @@ -44,9 +44,15 @@ phpcc -e -o [-d dir [-d dir ...]] [-b ] [-l setHelp('PHP Code compiler - Phar executable compiling utility') ->addOption('main', 'e', Option::VALUE, 'Set the PHAR stub\'s main entrypoint script') ->addOption('dir', 'd', Option::MULTI, 'Add directory contents ("-d $dir") optionally filtered on a specific file extension ("$dir:$extension")') + ->addOption('file', 'f', Option::MULTI, 'Add a single file to the archive') ->addOption('output', 'o', Option::VALUE, 'Set the compiled archive output name') ->addOption('banner', 'b', Option::VALUE, 'Load legal notice from the given banner file') ; @@ -49,11 +50,14 @@ public function execute() $banner = $this->getOption('banner') ?? ''; $dirs = $this->getOption('dir') ?? []; + $files = $this->getOption('file') ?? []; + $output = $this->getOption('output'); $main = $this->getOption('main'); $this ->initBuilder($main) + ->addFiles($files) ->addDirectories($dirs) ->setNotice($banner) ->publish($output) @@ -96,7 +100,7 @@ protected function addDirectory(string $directory, string $extensions = null) /** * Add a single file to the archive builder * - * @param string $file + * @param string $file A relative or absolute file path * */ protected function addFile(string $file) @@ -110,7 +114,7 @@ protected function addFile(string $file) /** * Add a list of directory specifications to the archive builder * - * @param array $dirs A list of directories in the form "$dir" or "$dir:$extension" + * @param string[] $dirs A list of specs in the form "$dir" or "$dir:$extension" * * @return self */ @@ -120,7 +124,7 @@ protected function addDirectories(array $dirs): self list($directory, $extensions) = explode(':', $spec); $wildcard = $extensions ? "*.$extensions" : 'all'; - $this->info("Scanning directory $directory for $wildcard files ..."); + $this->info("Scanning directory $directory for $wildcard files..."); $this->addDirectory($directory, $extensions); } @@ -128,6 +132,23 @@ protected function addDirectories(array $dirs): self return $this; } + /** + * Add a list of single files to the archive builder + * + * @param string[] $files A list of relative or absolute file paths + * + * @return self + */ + protected function addFiles(array $files): self + { + foreach ($files as $file) { + $this->info("Adding single file $file..."); + $this->addFile($file); + } + + return $this; + } + /** * Add banner file contents to the archive builder * @@ -138,7 +159,7 @@ protected function addDirectories(array $dirs): self protected function setNotice(string $banner = null): self { if (is_file($banner)) { - $this->info("Loading banner contents from $banner file ..."); + $this->info("Loading banner contents from $banner file..."); $contents = file_get_contents($banner); $header = $this->phpdocize($contents); @@ -159,7 +180,7 @@ protected function setNotice(string $banner = null): self */ protected function publish(string $output, string $compression = 'GZ'): self { - $this->info("Writing Phar archive to $output ..."); + $this->info("Writing Phar archive to $output..."); $this->builder->compile($output, $compression); return $this;