#!/usr/bin/env php 0) { $arg = \array_shift($argv); switch ($arg) { case '-p': case '--package': $input = \array_shift($argv); break; case '-s': case '--source': $writeto[] = \array_shift($argv); break; case '-b': case '--base': $root = \array_shift($argv); break; case '--dry-run': $dry_run = true; break; case '--shell': $shell = \array_shift($argv); break; case '-h': case '--help': help(); } } //Defaults writeto if ($writeto === array()) { $writeto = array('src', 'bin'); } //Add root paths $input = $root . '/' . $input; foreach ($writeto as &$write) { $write = $root . '/' . $write; } //Read those JSON files if (!\file_exists($input)) fail('Package file does not exist'); $input = \json_decode(\file_get_contents($input), true); if (!$input) { fail('Invalid JSON file!'); } //Initialize the version from package file try { $version = new version($input['version']); } catch (SemVerException $e) { fail($e->getMessage()); } $version = $version->getString(); foreach ($writeto as $output) { $dir = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($output)); foreach ($dir as $file) { if (\preg_match('/[\\\\\\/]\\./', $file)) continue; //Ignore . directories $contents1 = \file_get_contents($file); $contents2 = \str_replace(array('2.0.0--', '{{{' . 'version}}}'), $version, $contents1); if ($contents1 != $contents2) { fwrite(\STDOUT, 'Writing version information to file ' . $file . \PHP_EOL); if ($shell !== null) { \system($shell . ' "' . $file . '"', $exit_code); if ($exit_code != 0) fail('Subshell exited ' . $exit_code); } if ($dry_run) { \fwrite(\STDOUT, '\\_Not writing to disk' . \PHP_EOL); } else { \file_put_contents($file, $contents2); } } } } function help() { $e = array( 'Usage: update-versions [options]' , '' , ' -p Use this file as composer.json file' , ' --package ' , ' -s Add directory to sources to scan. May be repeated.' , ' --source ' , ' -b Use this directory as base directory.' , ' --base ' , ' --shell Execute for each changed file.' , ' --dry-run Do not write files.' , '' , 'This program exits 0 on success or 1 on failure.' , 'Defaults to "--package composer.json --source src --source bin --base ."' ); echo \implode(PHP_EOL, $e); exit; } function fail($message = '') { \fwrite(\STDERR, $message . \PHP_EOL); exit(1); }