Now supports excluding files and dirs

This commit is contained in:
Chris 2017-01-10 16:02:09 +01:00
parent 41ad908bc6
commit be93645db9
2 changed files with 35 additions and 4 deletions

View File

@ -73,8 +73,15 @@ be added through one of the rules in the `include` block.
stub "src/stub.php";
In the future, it will also be possible to exclude files matching specific
patterns.
You can follow the same approach when excluding files with the `exclude` block:
exclude {
dir ".git";
file "DONT-INCLUDE-ME-IN-PHAR";
}
The `dir` rules match against `*/<dirname>/*` while the `file` rule will match
against `*/<filename>`.
## Props
@ -153,3 +160,5 @@ into a temporary directory before running. But right now **it does nothing**.
them to the archive. This shouldn't be an issue, but if your code depends on
a specific character index in a specific php file, you might want to specify
the `verbatim` option.
* You can specify multiple `props` blocks, f.ex. one containing static values
and another one generate f.ex. build date or git revision.

View File

@ -23,6 +23,8 @@ class Manifest
protected $verbatim = false;
/** @var array Properties to be defined in the main stub */
protected $props = [];
/** @var array Patterns to exclude */
protected $excludes = [];
/**
* Read manifests from a makephar.sdl file and return all the build targets
@ -74,6 +76,18 @@ class Manifest
break;
}
break;
case 'exclude':
foreach ($child->getChildren() as $inc) switch ($inc->getTagName()) {
case 'dir':
$val = (string)$inc->getValue();
$mf->excludes[] = "*/{$val}/*";
break;
case 'file':
$val = (string)$inc->getValue();
$mf->excludes[] = "*/{$val}";
break;
}
break;
case 'props':
$props = [];
if ($src = (string)$child->getValue()) {
@ -199,10 +213,18 @@ class Manifest
log_warn("Unsupported source type: %s", $source->type);
}
}
$items = array_filter($items, function ($item) {
if (fnmatch("*/.git/*", $item->src)) return false;
$rules = $this->excludes;
$before = count($items);
$items = array_filter($items, function ($item) use ($rules) {
foreach ($rules as $rule) {
if (fnmatch($rule, $item->src)) return false;
}
return true;
});
$after = count($items);
if ($after < $before) {
log_debug("Skipped %d files matching exclusion patterns", $before-$after);
}
return $items;
}