Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement --meta multiple option #7

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ phpcc --version
phpcc \
-e <main> \
-o <output> \
[-d dir [-d dir ...]] \
[-f file [-f file ...]] \
[-b <banner>]
[-d <dir> [-d <dir> ...]] \
[-f <file> [-f <file> ...]] \
[-b <banner>] \
[-m <metadata> [-m <metadata> ...]]
```

### Options/Arguments
Expand All @@ -51,6 +52,7 @@ Name / Shorthand | Type | Description
`--banner`, `-b` | value | Specify the filepath to the legal notice banner<br/>_Will be included in the human-readable part of the stub._ |n
`--file`, `-f` | multi | Adds a single file to the archive |n
`--dir`, `-d` | multi | Adds a sources directory to the archive<br/>_Possible dir spec formats:<br/>- `$dir` => include all files in directory<br/>- `$dir:$extension` => filter files on a specific extension_ |n
`--meta`, `-m` | multi | Adds a metadata to the archive<br/>_Metadata must be specified in the `$key:$value` format_ |n


### Examples
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ fi

sed -i "s/\$VERSION *=.*/\$VERSION = '$version';/" $main

php -dphar.readonly=0 bin/compile.php -d src:php -d vendor:php -e $main -o $phar -b .banner
php -dphar.readonly=0 bin/compile.php -d src:php -d vendor:php -e $main -o $phar -b .banner -m license:MIT -m author:yannoff -m copyright:yannoff
10 changes: 10 additions & 0 deletions doc/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ phpcc -e app.php -o foobar.phar -b LICENSE
```bash
phpcc -e app.php -o foobar.phar -f foo.php -f bar.php
```

## Example 5: Adding metadata to the archive

- Define `app.php` as the stub main entrypoint script
- Save compiled phar executable to `bin/acme`
- Add the `license` & `author` metadata to the archive

```bash
phpcc -e app.php -o bin/acme -m license:MIT -m author:yannoff
```
22 changes: 22 additions & 0 deletions src/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function configure()
->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('meta', 'm', Option::MULTI, 'Add a metadata property (eg: "-m $key:$value")')
->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 @@ -51,6 +52,7 @@ public function execute()

$dirs = $this->getOption('dir') ?? [];
$files = $this->getOption('file') ?? [];
$meta = $this->getOption('meta') ?? [];

$output = $this->getOption('output');
$main = $this->getOption('main');
Expand All @@ -60,6 +62,7 @@ public function execute()
->addFiles($files)
->addDirectories($dirs)
->setNotice($banner)
->addMetadata($meta)
->publish($output)
->info('Build complete.')
;
Expand Down Expand Up @@ -149,6 +152,25 @@ protected function addFiles(array $files): self
return $this;
}

/**
* Add a list of metadata properties to the archive builder
*
* @param string[] $definitions A list of $key:$value pairs
*
* @return self
*/
protected function addMetadata(array $definitions): self
{
foreach ($definitions as $definition) {
list($name, $value) = explode(':', $definition);
$this->info("Adding <strong>$name</strong> metadata property");
$this->info("-> $name: $value", 'grey');
$this->builder->addMetadata($name, $value);
}

return $this;
}

/**
* Add banner file contents to the archive builder
*
Expand Down
28 changes: 27 additions & 1 deletion src/PharBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ class PharBuilder
*/
protected $banner;

/**
* Store the archive metadata properties
*
* @var array
*/
protected $metadata = [];

/**
* PharBuilder factory method
*
Expand Down Expand Up @@ -124,6 +131,8 @@ public function compile(string $output, string $compression = 'GZ')
throw new LogicException("Main script {$this->main} contents must be added to the archive");
}

$this->archive->setMetadata($this->metadata);

$c = constant('Phar::' . $compression);
$this->archive->compressFiles($c);

Expand Down Expand Up @@ -181,11 +190,28 @@ protected function stub(string $main, string $banner = null): string
* @param string $file Path to the file
* @param ?string $local Optional file alias
* @param bool $minify Whether comments/spaces should be removed from contents
*
* @return self
*/
public function addFile(string $file, string $local = null, bool $minify = true)
public function addFile(string $file, string $local = null, bool $minify = true): self
{
$this->archive->addFileContents($file, $local, $minify);

return $this;
}

/**
* Add or update an archive metadata entry
*
* @param string $name
* @param ?mixed $value
*
* @return self
*/
public function addMetadata(string $name, $value = null): self
{
$this->metadata[$name] = $value;

return $this;
}
}