Skip to content

Instantly share code, notes, and snippets.

@JanTvrdik
Last active August 29, 2015 14:28
Show Gist options
  • Save JanTvrdik/dbfb4d4673acc116c50d to your computer and use it in GitHub Desktop.
Save JanTvrdik/dbfb4d4673acc116c50d to your computer and use it in GitHub Desktop.
Nette Dev Kit
#!/bin/env bash
while read dir
do
sh -c "cd \"$dir\" && echo && pwd && $*"
done
#!/bin/env php
<?php
/**
* @license MIT https://github.com/janmarek/WebLoader/
*/
function run($cmd, $stdin = NULL, $cwd = NULL, array $env = NULL)
{
$descriptorspec = array(
0 => array('pipe', 'r'), // stdin
1 => array('pipe', 'w'), // stdout
2 => array('pipe', 'w'), // stderr
);
$pipes = array();
$proc = proc_open($cmd, $descriptorspec, $pipes, $cwd, $env);
if (!empty($stdin)) {
fwrite($pipes[0], $stdin . PHP_EOL);
}
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
$code = proc_close($proc);
if ($code != 0) {
throw new \RuntimeException($stderr, $code);
}
return $stdout;
}
function getAll()
{
static $all;
if ($all === NULL) {
$all = [];
foreach (scandir(__DIR__) as $dir) {
if ($dir[0] === '.' || !is_dir($dir)) continue;
$all[] = $dir;
}
}
return $all;
}
function filterAll($dirs)
{
return array_merge($dirs, getAll());
}
function filterSome($dirs, $some)
{
return array_merge($dirs, file($some, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
}
function filterStdin($dirs)
{
return filterSome('php://stdin');
}
function filterBranch($dirs, $branch)
{
return array_filter($dirs, function ($dir) use ($branch) {
$out = run(
sprintf('git branch --no-color --all --list %s', escapeshellarg($branch)),
NULL,
$dir
);
return $out !== '';
});
}
function filterBranchActive($dirs, $branch)
{
return array_filter($dirs, function ($dir) use ($branch) {
$out = run(
sprintf('git branch --no-color --all --list %s', escapeshellarg($branch)),
NULL,
$dir
);
return strpos($out, '*') !== FALSE;
});
}
function filterCommit($dirs, $message)
{
return array_filter($dirs, function ($dir) use ($message) {
$out = run(
sprintf('git log --fixed-strings --after=%s --oneline --grep=%s', date('Y-m-d', strtotime('-7 days')), escapeshellarg($message)),
NULL,
$dir
);
return $out !== '';
});
}
function filterSync($dirs)
{
return array_filter($dirs, function ($dir) {
$out = run(
sprintf('git log -1 --decorate --oneline --no-color'),
NULL,
$dir
);
return strpos($out, 'origin/pr/') !== FALSE;
});
}
function filterFile($dirs, $file)
{
return array_filter($dirs, function ($dir) use ($file) {
return is_file("$dir/$file");
});
}
function filterFileGrep($dirs, $grep)
{
list($file, $needle) = explode(':', $grep, 2);
$dirs = filterFile($dirs, $file);
return array_filter($dirs, function ($dir) use ($file, $needle) {
$content = file_get_contents("$dir/$file");
return strpos($content, $needle) !== FALSE;
});
}
function filterDiff($dirs, $diff)
{
return array_diff($dirs, file($diff, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
}
function filterInvert($dirs)
{
return array_diff(getAll(), $dirs);
}
$dirs = [];
foreach (array_slice($argv, 1) as $arg) {
if (substr($arg, 0, 2) === '--') {
list($filterName, $filterArg) = explode('=', substr($arg, 2), 2) + [1 => NULL];
$filterCallback = 'filter' . ucfirst($filterName);
$dirs = $filterCallback($dirs, $filterArg);
} else {
echo "Unknow option $arg\n";
exit(1);
}
}
foreach ($dirs as $dir) {
echo "$dir\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment