Skip to content

Commit

Permalink
Add --file multiple option
Browse files Browse the repository at this point in the history
  • Loading branch information
yannoff committed Mar 9, 2024
1 parent 5e0118f commit 88a459e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ chmod +x ${BINDIR}/phpcc
```
phpcc --help
phpcc --version
phpcc -e <entrypoint> -o <output> [-d dir [-d dir ...]] [-b <banner>] [-l <license>]
phpcc -e <entrypoint> -o <output> [-d dir [-d dir ...]] [-f file [-f file ...]] [-b <banner>]
```

### Options/Arguments
Expand All @@ -44,9 +44,15 @@ phpcc -e <entrypoint> -o <output> [-d dir [-d dir ...]] [-b <banner>] [-l <licen

**MANDATORY** The Phar archive output file.

#### `-f`, `--file`

Adds a single file to the archive.

_Multiple values allowed here_

#### `-d`, `--dir`

Adds a PHP sources directory to the archive.
Adds a sources directory to the archive.

_Multiple values allowed here_

Expand Down
10 changes: 10 additions & 0 deletions doc/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,13 @@ phpcc -d src:php -d src:phtml -e main.php -o bin/foobar
```bash
phpcc -e app.php -o foobar.phar -b LICENSE
```

## Example 4: Add sparse single PHP files

- Define `app.php` as the stub main entrypoint script
- Save compiled phar executable to `foobar.phar`
- Include `foo.php` and `bar.php` files to the archive

```bash
phpcc -e app.php -o foobar.phar -f foo.php -f bar.php
```
31 changes: 26 additions & 5 deletions src/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function configure()
->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')
;
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
*/
Expand All @@ -120,14 +124,31 @@ protected function addDirectories(array $dirs): self
list($directory, $extensions) = explode(':', $spec);

$wildcard = $extensions ? "*.$extensions" : 'all';
$this->info("Scanning directory <strong>$directory</strong> for <strong>$wildcard</strong> files ...");
$this->info("Scanning directory <strong>$directory</strong> for <strong>$wildcard</strong> files...");

$this->addDirectory($directory, $extensions);
}

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 <strong>$file</strong>...");
$this->addFile($file);
}

return $this;
}

/**
* Add banner file contents to the archive builder
*
Expand All @@ -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 <strong>$banner</strong> file ...");
$this->info("Loading banner contents from <strong>$banner</strong> file...");
$contents = file_get_contents($banner);
$header = $this->phpdocize($contents);

Expand All @@ -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 <strong>$output</strong> ...");
$this->info("Writing Phar archive to <strong>$output</strong>...");
$this->builder->compile($output, $compression);

return $this;
Expand Down

0 comments on commit 88a459e

Please sign in to comment.