Compare commits
No commits in common. "master" and "v1.0.2" have entirely different histories.
|
@ -1,29 +0,0 @@
|
|||
|
||||
name: Publish PHP Package
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*'
|
||||
|
||||
jobs:
|
||||
docker:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Get tag name
|
||||
run: echo "TAG=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
|
||||
|
||||
- name: zip and send
|
||||
run: |
|
||||
ls -la
|
||||
apt-get update -y
|
||||
apt-get install -y zip
|
||||
cd ../
|
||||
zip -r ${{ gitea.event.repository.name }}.zip ${{ gitea.event.repository.name }} -x '*.git*'
|
||||
curl -v \
|
||||
--user composer-pbl:${{ secrets.COMPOSER_PSWD }} \
|
||||
--upload-file ${{ gitea.event.repository.name }}.zip \
|
||||
https://git.bit5.ru/api/packages/bit/composer?version=${{ env.TAG }}
|
|
@ -1 +0,0 @@
|
|||
tags
|
|
@ -1,2 +0,0 @@
|
|||
## v2.3.0
|
||||
- Adding support for enviroment variables prefixed with **TASKMAN_SET_** being set as props. For example: **TASKMAN_SET_FOO=1** results in property **FOO=1**
|
235
README.md
235
README.md
|
@ -1,235 +0,0 @@
|
|||
## Taskman
|
||||
|
||||
Taskman is a simple library which allows to conveniently execute build 'tasks' written in PHP from the shell. Tasks can have dependencies on other tasks.
|
||||
|
||||
Taskman is inspired by Make, Capistrano, Ant, Maven and similar build tools.
|
||||
|
||||
The central unit of execution is 'task'. The main difference from the plain old function is the fact 'task' is executed **only once** no matter what.
|
||||
|
||||
### Examples
|
||||
|
||||
#### Hello world
|
||||
Here's a simple example of a task:
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace taskman;
|
||||
|
||||
task('hello', function()
|
||||
{
|
||||
echo "Hello world!\n";
|
||||
});
|
||||
```
|
||||
|
||||
Executing it from the shell yields:
|
||||
|
||||
```
|
||||
./gamectl hello
|
||||
***** task 'hello' start *****
|
||||
Hello world!
|
||||
***** task 'hello' done(0/0.27 sec.) *****
|
||||
***** All done (0.27 sec.) *****
|
||||
```
|
||||
|
||||
#### Real world example
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace taskman;
|
||||
|
||||
task('ultimate_build_run',
|
||||
[
|
||||
'default' => true,
|
||||
'alias' => 'urun',
|
||||
'deps' => ['autogen', 'pack_configs', 'ensure_unity_player_settings', 'unity_defines']
|
||||
], function() {});
|
||||
```
|
||||
### Get help and list all tasks
|
||||
|
||||
You can list all tasks and their definition locations using **help** task (yes, it's a task as well):
|
||||
|
||||
```
|
||||
./gamectl help
|
||||
***** task 'help' start *****
|
||||
|
||||
Usage:
|
||||
gamectl [OPTIONS] <task-name1>[,<task-name2>,..] [-D PROP1=value [-D PROP2]]
|
||||
|
||||
Available options:
|
||||
-c specify PHP script to be included (handy for setting props,config options,etc)
|
||||
-V be super verbose
|
||||
-q be quite, only system messages
|
||||
-b batch mode: be super quite, don't even output any system messages
|
||||
-- pass all options verbatim after this mark
|
||||
|
||||
Available tasks:
|
||||
---------------------------------
|
||||
apk_install_to_device (/Users/ps/dev/skeletor/gamectl.d/client.inc.php@58)
|
||||
---------------------------------
|
||||
apk_run_on_device (/Users/ps/dev/skeletor/gamectl.d/client.inc.php@67)
|
||||
---------------------------------
|
||||
autogen @deps ["unity_defines","cs_autogen","php_autogen"] (/Users/ps/dev/skeletor/gamectl.d/autogen.inc.php@6)
|
||||
---------------------------------
|
||||
bhl_autogen @deps ["bhl_make_upm_package"] (/Users/ps/dev/skeletor/gamectl.d/bhl.inc.php@19)
|
||||
***** task 'help' done(0/0.01 sec.) *****
|
||||
***** All done (0.01 sec.) *****
|
||||
```
|
||||
|
||||
You can filter tasks you want to get help for by a partial match as follows:
|
||||
|
||||
```
|
||||
./gamectl help name
|
||||
```
|
||||
|
||||
### Tasks documentation
|
||||
|
||||
#### Task declaration
|
||||
|
||||
Task must be declared using library **task** function as follows:
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace taskman;
|
||||
|
||||
task('name', function() {});
|
||||
```
|
||||
|
||||
The task above now can be invoked from the shell as follows:
|
||||
|
||||
```./gamectl name```
|
||||
|
||||
#### Task aliases
|
||||
|
||||
Task may have an alias for less typing in the shell. To specify an alias one should put it as an **alias** property of a task declaration:
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace taskman;
|
||||
|
||||
task('name', ['alias' => 'n'],
|
||||
function() {});
|
||||
```
|
||||
|
||||
You can invoke the task by the alias as follows:
|
||||
|
||||
```./gamectl n```
|
||||
|
||||
#### Task dependencies
|
||||
|
||||
Task may have an dependencies on other tasks. To specify all dependencies one should list them in **deps** section of a task declaration:
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace taskman;
|
||||
|
||||
task('c', ['deps' => ['a', 'b']],
|
||||
function() {});
|
||||
```
|
||||
|
||||
All dependencies are executed before running the specified task. Running the task above yields something as follows:
|
||||
```
|
||||
./gamectl c
|
||||
***** task 'c' start *****
|
||||
***** -task 'a' start *****
|
||||
***** -task 'a' done(0/0.18 sec.) *****
|
||||
***** -task 'b' start *****
|
||||
***** -task 'b' done(0/0.18 sec.) *****
|
||||
***** task 'c' done(0/0.18 sec.) *****
|
||||
***** All done (0.18 sec.) *****
|
||||
```
|
||||
|
||||
#### Always executed tasks
|
||||
|
||||
Sometimes it's convenient to define tasks which should be executed every time without explicit invocation. For example for setting the up the default environment, properties, etc. To achieve that one should specify the **always** property for a task:
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace taskman;
|
||||
|
||||
task('setup_env', ['always' => true],
|
||||
function() {});
|
||||
```
|
||||
|
||||
### Build properties
|
||||
|
||||
It's possible to specify miscellaneous build properties to setup a proper build environment conditions. There's a set of routines provided by taskman for properties manipulation.
|
||||
|
||||
#### Setting a property
|
||||
|
||||
Use **set** built-in function:
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace taskman;
|
||||
|
||||
task('setup_env', ['always' => true],
|
||||
function() {
|
||||
set("IOS_APP_ID", "4242jfhFD");
|
||||
});
|
||||
```
|
||||
|
||||
#### Setting a property only if it's not already set
|
||||
|
||||
Use **setor** built-in function. Property will be set only if it's not set some where before. It's a convenient pattern to have a default set of properties which can be overriden by the environment properties included from an external script.
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace taskman;
|
||||
|
||||
task('setup_env', ['always' => true],
|
||||
function() {
|
||||
setor("IOS_APP_ID", "4242jfhFD");
|
||||
});
|
||||
```
|
||||
|
||||
#### Getting a property
|
||||
|
||||
Use **get** built-in function:
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace taskman;
|
||||
|
||||
task('build_ios',
|
||||
function() {
|
||||
shell("xcode build app " . get("IOS_APP_ID"));
|
||||
});
|
||||
```
|
||||
|
||||
#### Getting a property or some default value
|
||||
|
||||
Use **getor** built-in function:
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace taskman;
|
||||
|
||||
task('build_ios',
|
||||
function() {
|
||||
shell("xcode build app " . getor("IOS_APP_ID", "3232232"));
|
||||
});
|
||||
```
|
||||
|
||||
#### Listing all build properties
|
||||
|
||||
To list all defined build properties one should use the **props** task:
|
||||
|
||||
```
|
||||
./gamectl props
|
||||
|
||||
***** task 'props' start *****
|
||||
|
||||
Available props:
|
||||
---------------------------------
|
||||
UNITY_ASSETS_DIR : '/Users/ps/dev/skeletor//unity/Assets/'
|
||||
---------------------------------
|
||||
GAME_COMPANY_NAME : 'Black Hole Light'
|
||||
***** task 'props' done(0.11/0.11 sec.) *****
|
||||
***** All done (0.11 sec.) *****
|
||||
```
|
||||
|
||||
You can filter properties you want to get information about by a partial match as follows:
|
||||
|
||||
```
|
||||
./gamectl props FOO
|
||||
```
|
415
artefact.inc.php
415
artefact.inc.php
|
@ -1,415 +0,0 @@
|
|||
<?php
|
||||
namespace taskman\artefact;
|
||||
use Exception;
|
||||
|
||||
class TaskmanArtefact
|
||||
{
|
||||
private \taskman\TaskmanTask $task;
|
||||
private string $path;
|
||||
private array $sources_fn = array();
|
||||
private array $sources_spec = array();
|
||||
private iterable $sources = array();
|
||||
private array $sources_changed = array();
|
||||
private array $sources_changed_fn = array();
|
||||
private array $sources_affected = array();
|
||||
|
||||
function __construct(\taskman\TaskmanTask $task, string $path, array $sources_spec)
|
||||
{
|
||||
$this->task = $task;
|
||||
$this->path = $path;
|
||||
$this->sources_spec = $sources_spec;
|
||||
}
|
||||
|
||||
function getTask() : \taskman\TaskmanTask
|
||||
{
|
||||
return $this->task;
|
||||
}
|
||||
|
||||
function getPath() : string
|
||||
{
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
function getSourcesSpec() : array
|
||||
{
|
||||
return $this->sources_spec;
|
||||
}
|
||||
|
||||
function getSources(int $idx) : iterable
|
||||
{
|
||||
if(isset($this->sources[$idx]))
|
||||
return $this->sources[$idx];
|
||||
|
||||
if(isset($this->sources_fn[$idx]))
|
||||
{
|
||||
$fn = $this->sources_fn[$idx];
|
||||
\taskman\log(2, "Task '{$this->task->getName()}' artefact '{$this->path}' resolving sources at $idx\n");
|
||||
$sources = $fn();
|
||||
$this->sources[$idx] = $sources;
|
||||
return $sources;
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
function setSourcesFn(int $idx, \Closure $fn)
|
||||
{
|
||||
$this->sources_fn[$idx] = $fn;
|
||||
}
|
||||
|
||||
function setSourcesChangedFn(int $idx, \Closure $fn)
|
||||
{
|
||||
$this->sources_changed_fn[$idx] = $fn;
|
||||
}
|
||||
|
||||
function getChangedSources(int $idx) : iterable
|
||||
{
|
||||
if(isset($this->sources_changed[$idx]))
|
||||
return $this->sources_changed[$idx];
|
||||
|
||||
if(isset($this->sources_changed_fn[$idx]))
|
||||
{
|
||||
$fn = $this->sources_changed_fn[$idx];
|
||||
\taskman\log(2, "Task '{$this->task->getName()}' artefact '{$this->path}' resolving changed sources at $idx\n");
|
||||
$changed = $fn();
|
||||
$this->sources_changed[$idx] = $changed;
|
||||
return $changed;
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
//obsolete
|
||||
function isSourcesNewer(int $idx) : bool
|
||||
{
|
||||
return $this->isSourcesAffected($idx);
|
||||
}
|
||||
|
||||
function isSourcesAffected(int $idx) : bool
|
||||
{
|
||||
return isset($this->sources_affected[$idx]) && $this->sources_affected[$idx];
|
||||
}
|
||||
|
||||
function getAffectedSourcesIndices() : array
|
||||
{
|
||||
return array_keys($this->sources_affected);
|
||||
}
|
||||
|
||||
function isStale() : bool
|
||||
{
|
||||
return count($this->sources_affected) > 0;
|
||||
}
|
||||
|
||||
function initSources()
|
||||
{
|
||||
$file_changes = $this->task->getFileChanges();
|
||||
|
||||
$all_src_specs = $this->getSourcesSpec();
|
||||
//let's process a convenience special case
|
||||
if(count($all_src_specs) > 0 && !is_array($all_src_specs[0]))
|
||||
$all_src_specs = [$all_src_specs];
|
||||
|
||||
foreach($all_src_specs as $src_idx => $src_spec)
|
||||
{
|
||||
//[[dir1, dir2, ..], [ext1, ext2, ..]]
|
||||
if(is_array($src_spec) && count($src_spec) == 2 &&
|
||||
is_array($src_spec[0]) && is_array($src_spec[1]))
|
||||
{
|
||||
$this->setSourcesFn($src_idx, function() use($src_spec) {
|
||||
$dir2files = array();
|
||||
foreach($src_spec[0] as $spec_dir)
|
||||
$dir2files[$spec_dir] = scan_files([$spec_dir], $src_spec[1]);
|
||||
return new TaskmanDirFiles($dir2files);
|
||||
});
|
||||
|
||||
if($file_changes != null)
|
||||
{
|
||||
$this->setSourcesChangedFn($src_idx, function() use($src_spec, $file_changes) {
|
||||
$changed = array();
|
||||
foreach($src_spec[0] as $spec_dir)
|
||||
{
|
||||
$matches = $file_changes->matchDirectory($spec_dir, $src_spec[1]);
|
||||
$changed[$spec_dir] = $matches;
|
||||
}
|
||||
return new TaskmanDirFiles($changed);
|
||||
});
|
||||
}
|
||||
}
|
||||
else if(is_array($src_spec) || $src_spec instanceof \Iterator)
|
||||
{
|
||||
$this->setSourcesFn($src_idx, fn() => $src_spec);
|
||||
|
||||
if($file_changes != null)
|
||||
{
|
||||
$this->setSourcesChangedFn($src_idx,
|
||||
fn() => $file_changes->matchFiles($this->getSources($src_idx))
|
||||
);
|
||||
}
|
||||
}
|
||||
else
|
||||
throw new Exception("Unknown artefact '{$this->getPath()}' source type" . gettype($src_spec));
|
||||
}
|
||||
}
|
||||
|
||||
function checkAffectedSources() : bool
|
||||
{
|
||||
$file_changes = $this->task->getFileChanges();
|
||||
|
||||
foreach($this->getSourcesSpec() as $src_idx => $src_spec)
|
||||
{
|
||||
$sources = $file_changes != null ? $this->getChangedSources($src_idx) : $this->getSources($src_idx);
|
||||
if(is_stale($this->getPath(), $sources))
|
||||
{
|
||||
$this->sources_affected[$src_idx] = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class TaskmanDirFiles implements \ArrayAccess, \Countable, \Iterator
|
||||
{
|
||||
/*var array<string, string[]>*/
|
||||
private array $dir2files = array();
|
||||
|
||||
private $iter_pos = 0;
|
||||
|
||||
function __construct(array $dir2files = array())
|
||||
{
|
||||
foreach($dir2files as $dir => $files)
|
||||
$this->dir2files[$dir] = $files;
|
||||
}
|
||||
|
||||
function __toString() : string
|
||||
{
|
||||
return var_export($this->dir2files, true);
|
||||
}
|
||||
|
||||
function toMap() : array
|
||||
{
|
||||
return $this->dir2files;
|
||||
}
|
||||
|
||||
function clear()
|
||||
{
|
||||
$this->dir2files = array();
|
||||
}
|
||||
|
||||
function isEmpty() : bool
|
||||
{
|
||||
return empty($this->dir2files);
|
||||
}
|
||||
|
||||
function count() : int
|
||||
{
|
||||
$total = 0;
|
||||
foreach($this->dir2files as $base_dir => $files)
|
||||
$total += count($files);
|
||||
return $total;
|
||||
}
|
||||
|
||||
function apply(callable $fn)
|
||||
{
|
||||
foreach($this->dir2files as $base_dir => $files)
|
||||
$this->dir2files[$base_dir] = $fn($base_dir, $files);
|
||||
}
|
||||
|
||||
function filter(callable $filter)
|
||||
{
|
||||
foreach($this->dir2files as $base_dir => $files)
|
||||
$this->dir2files[$base_dir] = array_filter($files, $filter);
|
||||
}
|
||||
|
||||
function forEachFile(callable $fn)
|
||||
{
|
||||
foreach($this->dir2files as $base_dir => $files)
|
||||
{
|
||||
foreach($files as $file)
|
||||
$fn($base_dir, $file);
|
||||
}
|
||||
}
|
||||
|
||||
function add(string $base_dir, string $file)
|
||||
{
|
||||
if(!isset($this->dir2files[$base_dir]))
|
||||
$this->dir2files[$base_dir] = array();
|
||||
|
||||
$this->dir2files[$base_dir][] = $file;
|
||||
}
|
||||
|
||||
//returns [[base_dir, file1], [base_dir, file2], ...]
|
||||
function getFlatArray() : array
|
||||
{
|
||||
$flat = [];
|
||||
foreach($this->dir2files as $base_dir => $files)
|
||||
{
|
||||
foreach($files as $file)
|
||||
$flat[] = [$base_dir, $file];
|
||||
}
|
||||
return $flat;
|
||||
}
|
||||
|
||||
function getAllFiles() : array
|
||||
{
|
||||
$all_files = [];
|
||||
foreach($this->dir2files as $base_dir => $files)
|
||||
$all_files = array_merge($all_files, $files);
|
||||
|
||||
return $all_files;
|
||||
}
|
||||
|
||||
//ArrayAccess interface
|
||||
function offsetExists(mixed $offset) : bool
|
||||
{
|
||||
if(!is_int($offset))
|
||||
throw new Exception("Invalid offset");
|
||||
|
||||
return $this->count() > $offset;
|
||||
}
|
||||
|
||||
function offsetGet(mixed $offset) : mixed
|
||||
{
|
||||
if(!is_int($offset))
|
||||
throw new Exception("Invalid offset");
|
||||
|
||||
foreach($this->dir2files as $base_dir => $files)
|
||||
{
|
||||
$n = count($files);
|
||||
if($offset - $n < 0)
|
||||
return $files[$offset];
|
||||
$offset -= $n;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function offsetSet(mixed $offset, mixed $value) : void
|
||||
{
|
||||
if(!is_int($offset))
|
||||
throw new Exception("Invalid offset");
|
||||
|
||||
foreach($this->dir2files as $base_dir => &$files)
|
||||
{
|
||||
$n = count($files);
|
||||
if($offset - $n < 0)
|
||||
{
|
||||
$files[$offset] = $value;
|
||||
return;
|
||||
}
|
||||
$offset -= $n;
|
||||
}
|
||||
}
|
||||
|
||||
function offsetUnset(mixed $offset) : void
|
||||
{
|
||||
if(!is_int($offset))
|
||||
throw new Exception("Invalid offset");
|
||||
|
||||
foreach($this->dir2files as $base_dir => $files)
|
||||
{
|
||||
$n = count($files);
|
||||
if($offset - $n < 0)
|
||||
{
|
||||
unset($files[$offset]);
|
||||
return;
|
||||
}
|
||||
$offset -= $n;
|
||||
}
|
||||
}
|
||||
|
||||
//Iterator interface
|
||||
function rewind() : void
|
||||
{
|
||||
$this->iter_pos = 0;
|
||||
}
|
||||
|
||||
function current() : mixed
|
||||
{
|
||||
return $this->offsetGet($this->iter_pos);
|
||||
}
|
||||
|
||||
function key() : mixed
|
||||
{
|
||||
return $this->iter_pos;
|
||||
}
|
||||
|
||||
function next() : void
|
||||
{
|
||||
++$this->iter_pos;
|
||||
}
|
||||
|
||||
function valid() : bool
|
||||
{
|
||||
return $this->offsetExists($this->iter_pos);
|
||||
}
|
||||
}
|
||||
|
||||
function is_stale(string $file, iterable $deps) : bool
|
||||
{
|
||||
if(!is_file($file))
|
||||
return true;
|
||||
|
||||
$fmtime = filemtime($file);
|
||||
|
||||
foreach($deps as $dep)
|
||||
{
|
||||
if($dep && is_file($dep) && (filemtime($dep) > $fmtime))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function scan_files(array $dirs, array $only_extensions = [], int $mode = 1) : array
|
||||
{
|
||||
$files = array();
|
||||
foreach($dirs as $dir)
|
||||
{
|
||||
if(!is_dir($dir))
|
||||
continue;
|
||||
|
||||
$dir = normalize_path($dir);
|
||||
|
||||
$iter_mode = $mode == 1 ? \RecursiveIteratorIterator::LEAVES_ONLY : \RecursiveIteratorIterator::SELF_FIRST;
|
||||
$iter = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir), $iter_mode);
|
||||
|
||||
foreach($iter as $filename => $cur)
|
||||
{
|
||||
if(($mode == 1 && !$cur->isDir()) ||
|
||||
($mode == 2 && $cur->isDir()))
|
||||
{
|
||||
if(!$only_extensions)
|
||||
$files[] = $filename;
|
||||
else
|
||||
{
|
||||
$flen = strlen($filename);
|
||||
foreach($only_extensions as $ext)
|
||||
{
|
||||
if(substr_compare($filename, $ext, $flen-strlen($ext)) === 0)
|
||||
$files[] = $filename;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $files;
|
||||
}
|
||||
|
||||
function normalize_path(string $path) : string
|
||||
{
|
||||
$path = str_replace('\\', '/', $path);
|
||||
$path = preg_replace('/\/+/', '/', $path);
|
||||
$parts = explode('/', $path);
|
||||
$absolutes = array();
|
||||
foreach($parts as $part)
|
||||
{
|
||||
if('.' == $part)
|
||||
continue;
|
||||
|
||||
if('..' == $part)
|
||||
array_pop($absolutes);
|
||||
else
|
||||
$absolutes[] = $part;
|
||||
}
|
||||
return implode(DIRECTORY_SEPARATOR, $absolutes);
|
||||
}
|
292
internal.inc.php
292
internal.inc.php
|
@ -1,292 +0,0 @@
|
|||
<?php
|
||||
namespace taskman\internal;
|
||||
use Exception;
|
||||
|
||||
function _default_logger($msg)
|
||||
{
|
||||
echo $msg;
|
||||
}
|
||||
|
||||
function _default_usage($script_name = "<taskman-script>")
|
||||
{
|
||||
echo "\nUsage:\n $script_name [OPTIONS] <task-name1>[,<task-name2>,..] [-D PROP1=value [-D PROP2]]\n\n";
|
||||
echo "Available options:\n";
|
||||
echo " -c specify PHP script to be included (handy for setting props,config options,etc)\n";
|
||||
echo " -V be super verbose\n";
|
||||
echo " -q be quite, only system messages\n";
|
||||
echo " -b batch mode: be super quite, don't even output any system messages\n";
|
||||
echo " -- pass all options verbatim after this mark\n";
|
||||
}
|
||||
|
||||
function _collect_tasks()
|
||||
{
|
||||
global $TASKMAN_TASKS;
|
||||
global $TASKMAN_TASK_ALIASES;
|
||||
global $TASKMAN_FILE_CHANGES;
|
||||
|
||||
$TASKMAN_TASKS = array();
|
||||
$TASKMAN_TASK_ALIASES = array();
|
||||
|
||||
$cands = _get_task_candidates();
|
||||
foreach($cands as $name => $args)
|
||||
{
|
||||
if(isset($TASKMAN_TASKS[$name]))
|
||||
throw new \taskman\TaskmanException("Task '$name' is already defined");
|
||||
|
||||
if(is_array($args))
|
||||
{
|
||||
$props = array();
|
||||
if(sizeof($args) > 2)
|
||||
{
|
||||
$props = $args[1];
|
||||
$func = $args[2];
|
||||
}
|
||||
else
|
||||
$func = $args[1];
|
||||
|
||||
$task = new \taskman\TaskmanTask($func, $name, $props, $TASKMAN_FILE_CHANGES);
|
||||
}
|
||||
else
|
||||
throw new Exception("Task '$name' is invalid");
|
||||
|
||||
$TASKMAN_TASKS[$name] = $task;
|
||||
|
||||
foreach($task->getAliases() as $alias)
|
||||
{
|
||||
if(isset($TASKMAN_TASKS[$alias]) || isset($TASKMAN_TASK_ALIASES[$alias]))
|
||||
throw new \taskman\TaskmanException("Alias '$alias' is already defined for task '$name'");
|
||||
$TASKMAN_TASK_ALIASES[$alias] = $task;
|
||||
}
|
||||
}
|
||||
|
||||
_validate_tasks();
|
||||
}
|
||||
|
||||
function _validate_tasks()
|
||||
{
|
||||
global $TASKMAN_TASKS;
|
||||
|
||||
foreach($TASKMAN_TASKS as $task)
|
||||
{
|
||||
try
|
||||
{
|
||||
$before = $task->getPropOr("before", "");
|
||||
if($before)
|
||||
\taskman\get_task($before)->addBeforeDep($task);
|
||||
|
||||
$after = $task->getPropOr("after", "");
|
||||
if($after)
|
||||
\taskman\get_task($after)->addAfterDep($task);
|
||||
|
||||
foreach($task->getDeps() as $dep_task)
|
||||
\taskman\get_task($dep_task);
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
throw new Exception("Task '{$task->getName()}' validation error: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function _get_task_candidates()
|
||||
{
|
||||
global $TASKMAN_CLOSURES;
|
||||
|
||||
$cands = array();
|
||||
|
||||
//get tasks defined as closures
|
||||
foreach($TASKMAN_CLOSURES as $name => $args)
|
||||
{
|
||||
$cands[$name] = $args;
|
||||
}
|
||||
|
||||
ksort($cands);
|
||||
|
||||
return $cands;
|
||||
}
|
||||
|
||||
function _resolve_callable_prop($name)
|
||||
{
|
||||
$prop = $GLOBALS['TASKMAN_PROP_' . $name];
|
||||
if(!($prop instanceof \Closure))
|
||||
return;
|
||||
$value = $prop();
|
||||
$GLOBALS['TASKMAN_PROP_' . $name] = $value;
|
||||
}
|
||||
|
||||
function _isset_task($task)
|
||||
{
|
||||
global $TASKMAN_TASKS;
|
||||
global $TASKMAN_TASK_ALIASES;
|
||||
return isset($TASKMAN_TASKS[$task]) || isset($TASKMAN_TASK_ALIASES[$task]);
|
||||
}
|
||||
|
||||
function _get_hints($task)
|
||||
{
|
||||
global $TASKMAN_TASKS;
|
||||
global $TASKMAN_TASK_ALIASES;
|
||||
$tasks = array_merge(array_keys($TASKMAN_TASKS), array_keys($TASKMAN_TASK_ALIASES));
|
||||
$found = array_filter($tasks, function($v) use($task) { return strpos($v, $task) === 0; });
|
||||
$found = array_merge($found, array_filter($tasks, function($v) use($task) { $pos = strpos($v, $task); return $pos !== false && $pos > 0; }));
|
||||
return $found;
|
||||
}
|
||||
|
||||
//e.g: run,build,zip
|
||||
function _parse_taskstr($str)
|
||||
{
|
||||
$task_spec = array();
|
||||
$items = explode(',', $str);
|
||||
foreach($items as $item)
|
||||
{
|
||||
$args = null;
|
||||
$task = $item;
|
||||
if(strpos($item, ' ') !== false)
|
||||
@list($task, $args) = explode(' ', $item, 2);
|
||||
|
||||
if($args)
|
||||
$task_spec[] = array($task, explode(' ', $args));
|
||||
else
|
||||
$task_spec[] = $task;
|
||||
}
|
||||
return $task_spec;
|
||||
}
|
||||
|
||||
function _read_env_vars()
|
||||
{
|
||||
$envs = getenv();
|
||||
|
||||
foreach($envs as $k => $v)
|
||||
{
|
||||
if(strpos($k, 'TASKMAN_SET_') === 0)
|
||||
{
|
||||
$prop_name = substr($k, 12);
|
||||
\taskman\log(0, "Setting prop '$prop_name' (with env '$k')\n");
|
||||
\taskman\set($prop_name, $v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function _process_argv(array &$argv)
|
||||
{
|
||||
global $TASKMAN_LOG_LEVEL;
|
||||
global $TASKMAN_BATCH;
|
||||
global $TASKMAN_NO_DEPS;
|
||||
global $TASKMAN_FILE_CHANGES;
|
||||
|
||||
$filtered = array();
|
||||
$process_defs = false;
|
||||
|
||||
for($i=0;$i<sizeof($argv);++$i)
|
||||
{
|
||||
$v = $argv[$i];
|
||||
|
||||
if($v == '--')
|
||||
{
|
||||
for($j=$i+1;$j<sizeof($argv);++$j)
|
||||
$filtered[] = $argv[$j];
|
||||
break;
|
||||
}
|
||||
else if($v == '-D')
|
||||
{
|
||||
$process_defs = true;
|
||||
}
|
||||
else if($v == '-V')
|
||||
{
|
||||
$TASKMAN_LOG_LEVEL = 2;
|
||||
}
|
||||
else if($v == '-q')
|
||||
{
|
||||
$TASKMAN_LOG_LEVEL = 0;
|
||||
}
|
||||
else if($v == '-b')
|
||||
{
|
||||
$TASKMAN_LOG_LEVEL = -1;
|
||||
}
|
||||
else if($v == '-l')
|
||||
{
|
||||
if(!isset($argv[$i+1]))
|
||||
throw new \taskman\TaskmanException("Log level is missing");
|
||||
|
||||
$TASKMAN_LOG_LEVEL = intval($argv[$i+1]);
|
||||
++$i;
|
||||
}
|
||||
else if($v == '-O')
|
||||
{
|
||||
$TASKMAN_NO_DEPS = true;
|
||||
}
|
||||
else if($v == '-c')
|
||||
{
|
||||
if(!isset($argv[$i+1]))
|
||||
throw new \taskman\TaskmanException("Configuration file(-c option) is missing");
|
||||
require_once($argv[$i+1]);
|
||||
++$i;
|
||||
}
|
||||
else if($v == '-F')
|
||||
{
|
||||
if(!isset($argv[$i+1]))
|
||||
throw new \taskman\TaskmanException("Argument(-F option) is missing");
|
||||
|
||||
$TASKMAN_FILE_CHANGES = \taskman\TaskmanFileChanges::parse($argv[$i+1]);
|
||||
|
||||
++$i;
|
||||
}
|
||||
else if($process_defs)
|
||||
{
|
||||
$eq_pos = strpos($v, '=');
|
||||
if($eq_pos !== false)
|
||||
{
|
||||
$def_name = substr($v, 0, $eq_pos);
|
||||
$def_value = substr($v, $eq_pos+1);
|
||||
|
||||
//TODO: this code must be more robust
|
||||
if(strtolower($def_value) === 'true')
|
||||
$def_value = true;
|
||||
else if(strtolower($def_value) === 'false')
|
||||
$def_value = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
$def_name = $v;
|
||||
$def_value = 1;
|
||||
}
|
||||
|
||||
\taskman\log(0, "Setting prop '$def_name'=" . var_export($def_value, true) . "\n");
|
||||
\taskman\set($def_name, $def_value);
|
||||
|
||||
$process_defs = false;
|
||||
}
|
||||
else
|
||||
$filtered[] = $v;
|
||||
}
|
||||
$argv = $filtered;
|
||||
}
|
||||
|
||||
function _extract_lines_from_file(string $file_path) : array
|
||||
{
|
||||
$lines = array();
|
||||
|
||||
$fh = fopen($file_path, 'r+');
|
||||
|
||||
if($fh === false)
|
||||
return $lines;
|
||||
|
||||
try
|
||||
{
|
||||
if(flock($fh, LOCK_EX))
|
||||
{
|
||||
while(($line = fgets($fh)) !== false)
|
||||
$lines[] = $line;
|
||||
|
||||
ftruncate($fh, 0);
|
||||
|
||||
flock($fh, LOCK_UN);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
fclose($fh);
|
||||
}
|
||||
|
||||
return $lines;
|
||||
}
|
||||
|
1033
taskman.inc.php
1033
taskman.inc.php
File diff suppressed because it is too large
Load Diff
|
@ -1,73 +0,0 @@
|
|||
<?php
|
||||
namespace taskman;
|
||||
use Exception;
|
||||
|
||||
task('help', function($args = array())
|
||||
{
|
||||
$filter = '';
|
||||
if(isset($args[0]))
|
||||
$filter = $args[0];
|
||||
|
||||
$maxlen = -1;
|
||||
$tasks = array();
|
||||
$all = get_tasks();
|
||||
foreach($all as $task)
|
||||
{
|
||||
if($filter && (strpos($task->getName(), $filter) === false &&
|
||||
strpos($task->getPropOr("alias", ""), $filter) === false))
|
||||
continue;
|
||||
|
||||
if(strlen($task->getName()) > $maxlen)
|
||||
$maxlen = strlen($task->getName());
|
||||
|
||||
$tasks[] = $task;
|
||||
}
|
||||
|
||||
if(!$args)
|
||||
{
|
||||
$help_func = $GLOBALS['TASKMAN_HELP_FUNC'];
|
||||
$help_func();
|
||||
echo "\n";
|
||||
}
|
||||
|
||||
echo "Available tasks:\n";
|
||||
foreach($tasks as $task)
|
||||
{
|
||||
$props_string = '';
|
||||
$pad = $maxlen - strlen($task->getName());
|
||||
foreach($task->getProps() as $name => $value)
|
||||
{
|
||||
$props_string .= str_repeat(" ", $pad) .' @' . $name . ' ' . (is_string($value) ? $value : json_encode($value)) . "\n";
|
||||
$pad = $maxlen + 1;
|
||||
}
|
||||
$props_string = rtrim($props_string);
|
||||
|
||||
echo "---------------------------------\n";
|
||||
$file = $task->getFile();
|
||||
$line = $task->getLine();
|
||||
echo " " . $task->getName() . $props_string . " ($file@$line)\n";
|
||||
}
|
||||
echo "\n";
|
||||
});
|
||||
|
||||
task('props', function($args = [])
|
||||
{
|
||||
$filter = '';
|
||||
if(isset($args[0]))
|
||||
$filter = $args[0];
|
||||
|
||||
$props = props();
|
||||
|
||||
echo "\n";
|
||||
echo "Available props:\n";
|
||||
foreach($props as $k => $v)
|
||||
{
|
||||
if($filter && stripos($k, $filter) === false)
|
||||
continue;
|
||||
|
||||
echo "---------------------------------\n";
|
||||
echo "$k : " . var_export($v, true) . "\n";
|
||||
}
|
||||
echo "\n";
|
||||
});
|
||||
|
170
util.inc.php
170
util.inc.php
|
@ -1,170 +0,0 @@
|
|||
<?php
|
||||
namespace taskman;
|
||||
use Exception;
|
||||
|
||||
function get_task(string $task) : TaskmanTask
|
||||
{
|
||||
global $TASKMAN_TASKS;
|
||||
global $TASKMAN_TASK_ALIASES;
|
||||
|
||||
if(!is_scalar($task))
|
||||
throw new TaskmanException("Bad task name");
|
||||
|
||||
if(isset($TASKMAN_TASKS[$task]))
|
||||
return $TASKMAN_TASKS[$task];
|
||||
|
||||
if(isset($TASKMAN_TASK_ALIASES[$task]))
|
||||
return $TASKMAN_TASK_ALIASES[$task];
|
||||
|
||||
throw new TaskmanException("Task with name/alias '{$task}' does not exist");
|
||||
}
|
||||
|
||||
function get($name)
|
||||
{
|
||||
if(!isset($GLOBALS['TASKMAN_PROP_' . $name]))
|
||||
throw new TaskmanException("Property '$name' is not set");
|
||||
internal\_resolve_callable_prop($name);
|
||||
return $GLOBALS['TASKMAN_PROP_' . $name];
|
||||
}
|
||||
|
||||
function getor($name, $def)
|
||||
{
|
||||
if(!isset($GLOBALS['TASKMAN_PROP_' . $name]))
|
||||
return $def;
|
||||
internal\_resolve_callable_prop($name);
|
||||
return $GLOBALS['TASKMAN_PROP_' . $name];
|
||||
}
|
||||
|
||||
function set($name, $value)
|
||||
{
|
||||
$GLOBALS['TASKMAN_PROP_' . $name] = $value;
|
||||
}
|
||||
|
||||
function setor($name, $value)
|
||||
{
|
||||
if(!isset($GLOBALS['TASKMAN_PROP_' . $name]))
|
||||
$GLOBALS['TASKMAN_PROP_' . $name] = $value;
|
||||
}
|
||||
|
||||
function is($name)
|
||||
{
|
||||
return isset($GLOBALS['TASKMAN_PROP_' . $name]);
|
||||
}
|
||||
|
||||
function del($name)
|
||||
{
|
||||
unset($GLOBALS['TASKMAN_PROP_' . $name]);
|
||||
}
|
||||
|
||||
function props()
|
||||
{
|
||||
$props = array();
|
||||
foreach($GLOBALS as $key => $value)
|
||||
{
|
||||
if(($idx = strpos($key, 'TASKMAN_PROP_')) === 0)
|
||||
{
|
||||
$name = substr($key, strlen('TASKMAN_PROP_'));
|
||||
$props[$name] = get($name);
|
||||
}
|
||||
}
|
||||
return $props;
|
||||
}
|
||||
|
||||
function task($name)
|
||||
{
|
||||
global $TASKMAN_CLOSURES;
|
||||
|
||||
if(isset($TASKMAN_CLOSURES[$name]))
|
||||
throw new TaskmanException("Task '$name' is already defined");
|
||||
|
||||
$args = func_get_args();
|
||||
$TASKMAN_CLOSURES[$name] = $args;
|
||||
}
|
||||
|
||||
function get_tasks() : array
|
||||
{
|
||||
global $TASKMAN_TASKS;
|
||||
return $TASKMAN_TASKS;
|
||||
}
|
||||
|
||||
function current_task()
|
||||
{
|
||||
global $TASKMAN_CURRENT_TASK;
|
||||
return $TASKMAN_CURRENT_TASK;
|
||||
}
|
||||
|
||||
function run($task, array $args = array())
|
||||
{
|
||||
if($task instanceof TaskmanTask)
|
||||
$task_obj = $task;
|
||||
else
|
||||
$task_obj = get_task($task);
|
||||
|
||||
return $task_obj->run($args);
|
||||
}
|
||||
|
||||
function run_many($tasks, $args = array())
|
||||
{
|
||||
foreach($tasks as $task_spec)
|
||||
{
|
||||
if(is_array($task_spec))
|
||||
run($task_spec[0], $task_spec[1]);
|
||||
else
|
||||
run($task_spec, $args);
|
||||
}
|
||||
}
|
||||
|
||||
//the lower the level the more important the message: 0 - is the highest priority
|
||||
function log(int $level, $msg)
|
||||
{
|
||||
global $TASKMAN_LOG_LEVEL;
|
||||
|
||||
if($TASKMAN_LOG_LEVEL < $level)
|
||||
return;
|
||||
|
||||
$logger = $GLOBALS['TASKMAN_LOGGER'];
|
||||
call_user_func_array($logger, array($msg));
|
||||
}
|
||||
|
||||
//obsolete
|
||||
function _log(string $msg, int $level = 1)
|
||||
{
|
||||
log($level, $msg);
|
||||
}
|
||||
|
||||
//TODO: obsolete
|
||||
function msg_dbg(string $msg)
|
||||
{
|
||||
log(2, $msg);
|
||||
}
|
||||
|
||||
//TODO: obsolete
|
||||
function msg(string $msg)
|
||||
{
|
||||
log(1, $msg);
|
||||
}
|
||||
|
||||
//TODO: obsolete
|
||||
function msg_sys(string $msg)
|
||||
{
|
||||
log(0, $msg);
|
||||
}
|
||||
|
||||
function _(string $str) : string
|
||||
{
|
||||
if(strpos($str, '%') === false)
|
||||
return $str;
|
||||
|
||||
$str = preg_replace_callback(
|
||||
'~%\(([^\)]+)\)%~',
|
||||
function($m) { return get($m[1]); },
|
||||
$str
|
||||
);
|
||||
return $str;
|
||||
}
|
||||
|
||||
function usage($script_name = "<taskman-script>")
|
||||
{
|
||||
internal\_default_usage($script_name);
|
||||
}
|
||||
|
Loading…
Reference in New Issue