Last active
September 20, 2017 19:46
-
-
Save oscarotero/635ad936a502b2e6c314d36ef16da5dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is project's console commands configuration for Robo task runner. | |
* | |
* @see http://robo.li/ | |
*/ | |
class RoboFile extends \Robo\Tasks | |
{ | |
private $dir; | |
private $className; | |
private $repos = [ | |
'access-log', | |
'aura-router', | |
'aura-session', | |
'base-path', | |
'cache', | |
'client-ip', | |
'cors', | |
'csp', | |
'debugbar', | |
'encoder', | |
'error-handler', | |
'fast-route', | |
'filesystem', | |
'firewall', | |
'geolocation', | |
'honeypot', | |
'http-authentication', | |
'https', | |
'image-manipulation', | |
'method-override', | |
'minifier', | |
'negotiation', | |
'payload', | |
'php-session', | |
'proxy', | |
'recaptcha', | |
'referrer-spam', | |
'redirect', | |
'request-handler', | |
'response-time', | |
'robots', | |
'shutdown', | |
'trailing-slash', | |
'uuid', | |
'whoops', | |
'www', | |
]; | |
public function update() | |
{ | |
foreach ($this->repos as $repo) { | |
$this->dir = $repo; | |
$this->className = str_replace('-', '', ucwords($repo, '-')); | |
$this->say($repo); | |
continue; | |
$this->updateChangelogVersion(); | |
$this->push(); | |
$this->composer(function (&$composer) { | |
$composer['require']['http-interop/http-middleware'] = '^0.5'; | |
if (isset($composer['require']['middlewares/utils'])) { | |
$composer['require']['middlewares/utils'] = '~0.12'; | |
} | |
if (isset($composer['require-dev']['middlewares/utils'])) { | |
$composer['require-dev']['middlewares/utils'] = '~0.12'; | |
} | |
}); | |
$this->replaceCode( | |
'src/*.php', | |
'Interop\\Http\\ServerMiddleware\\', | |
'Interop\\Http\\Server\\' | |
); | |
$this->replaceCode( | |
'src/*.php', | |
'DelegateInterface', | |
'RequestHandlerInterface' | |
); | |
$this->replaceCode( | |
'src/*.php', | |
'$delegate', | |
'$handler' | |
); | |
$this->replaceCode( | |
'src/*.php', | |
'->process(', | |
'->handle(' | |
); | |
$this->command('composer update'); | |
$this->command('composer cs-fix'); | |
$this->command('composer test'); | |
} | |
} | |
//Copy a file from the skeleton package | |
private function copySkeletonFile($glob) | |
{ | |
foreach (glob("skeleton/$glob") as $file) { | |
$content = file_get_contents($file); | |
$content = str_replace('skeleton', $this->dir, $content); | |
$content = str_replace('Skeleton', $this->className, $content); | |
$file = str_replace('skeleton/', "{$this->dir}/", $file); | |
file_put_contents($file, $content); | |
} | |
} | |
//Replace code | |
private function replaceCode($glob, $search, $replace) | |
{ | |
foreach (glob("{$this->dir}/$glob") as $file) { | |
$code = file_get_contents($file); | |
$code = str_replace($search, $replace, $code); | |
file_put_contents($file, $code); | |
} | |
} | |
//Insert code in a specific line number | |
private function insertCode($glob, $line, $code) | |
{ | |
foreach (glob("{$this->dir}/$glob") as $file) { | |
$lines = file($file, FILE_IGNORE_NEW_LINES); | |
array_splice($lines, $line, 0, $code); | |
file_put_contents($file, implode("\n", $lines)); | |
} | |
} | |
//Append code at the end of a file | |
private function appendCode($glob, $code) | |
{ | |
foreach (glob("{$this->dir}/$glob") as $file) { | |
$content = file_get_contents($file); | |
$content = trim($content)."\n.{$code}\n"; | |
file_put_contents($file, $content); | |
} | |
} | |
//Change the composer json | |
private function composer(callable $handler) | |
{ | |
$file = $this->dir.'/composer.json'; | |
$composer = json_decode(file_get_contents($file), true); | |
$handler($composer); | |
file_put_contents($file, json_encode($composer, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)."\n"); | |
} | |
//Execute a command | |
private function command($command) | |
{ | |
$this->taskExec($command) | |
->dir($this->dir) | |
->run(); | |
} | |
//Make a commit | |
private function commit($message, $add = '.') | |
{ | |
$task = $this->taskGitStack() | |
->dir($this->dir) | |
->stopOnFail() | |
->pull() | |
->add($add) | |
->commit($message) | |
->run(); | |
} | |
//Push the changes to remote | |
private function push() | |
{ | |
$task = $this->taskGitStack() | |
->dir($this->dir) | |
->push() | |
->run(); | |
} | |
//Rename a file | |
private function rename($from, $to) | |
{ | |
rename("{$this->dir}/{$from}", "{$this->dir}/{$to}"); | |
} | |
//Get the current version | |
private function getCurrentVersion() { | |
ob_start(); | |
$this->command('git describe --tags --abbrev=0'); | |
$tag = trim(ob_get_clean()); | |
$tag = explode('.', str_replace('v', '', $tag)); | |
return array_map(function ($value) { | |
return (int) $value; | |
}, $tag); | |
} | |
//Get the next version | |
private function getNextVersion() { | |
$version = $this->getCurrentVersion(); | |
++$version[1]; | |
return $version; | |
} | |
//Replace "UNRELEASED" by the new version in the changelog | |
private function updateChangelogVersion() { | |
$version = $this->getCurrentVersion(); | |
$newVersion = $this->getNextVersion(); | |
$this->replaceCode( | |
'CHANGELOG.md', | |
'UNRELEASED', | |
"[$newVersion] - UNRELEASED" | |
); | |
$this->replaceCode( | |
'CHANGELOG.md', | |
"[$version]:", | |
"[$newVersion]: https://github.com/middlewares/$repo/compare/v{$version}...v{$newVersion}\n[$version]:" | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment