Skip to content

Commit

Permalink
(WIP) Implement --meta option
Browse files Browse the repository at this point in the history
  • Loading branch information
yannoff committed Mar 9, 2024
1 parent 250f97e commit 2a4c8fc
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
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 <entrypoint> \
-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
```
21 changes: 21 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 metadata (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)
->addMeta($meta)
->publish($output)
->info('Build complete.')
;
Expand Down Expand Up @@ -149,6 +152,24 @@ protected function addFiles(array $files): self
return $this;
}

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

return $this;
}

/**
* Add banner file contents to the archive builder
*
Expand Down
26 changes: 25 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 phar metadata key/value pairs
*
* @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,26 @@ 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 PharBuilder
*/
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;
}

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

return $this;
}
}

0 comments on commit 2a4c8fc

Please sign in to comment.