Skip to content

Instantly share code, notes, and snippets.

@iRedds
Created August 18, 2017 08:25
Show Gist options
  • Save iRedds/aa2ff08ec4119fe2e659c3e719a169d1 to your computer and use it in GitHub Desktop.
Save iRedds/aa2ff08ec4119fe2e659c3e719a169d1 to your computer and use it in GitHub Desktop.
<?php
// php makephar.php create - create phar from last git commit
// php makephar.php deploy [suffix number] - Unpack the phar file with the maximum / specific suffix number
$exclude = ['/^resource/', '/^Wise/'];
$chmod = 644;
if (! isset($argv[1])) {
exit;
}
if ($argv[1] == 'create') {
$time = (new \DateTime())->getTimestamp();
$package = new Phar("package.$time.phar");
exec('git show --pretty="" --name-only', $files);
$count = count($files);
$current = 0;
foreach ($files as $file) {
flush();
$current++;
$path = __DIR__ .'/'. $file;
if (file_exists($path)) {
$passed = true;
foreach($exclude as $pattern) {
if (preg_match($pattern, $file)) {
$passed = false;
break;
}
}
if (! $passed) {
echo "file excluded ($current/$count): $file\n";
continue;
}
$package->addFile($path, $file);
echo "file added ($current/$count): $file\n";;
} else {
echo "file not exists ($current/$count): $file\n";;
}
}
}
if ($argv[1] == 'deploy') {
$certain = isset($argv[2]) && is_numeric($argv[2]) ? $argv[2] : '*';
$path = __DIR__ ."/package.$certain.phar";
$files = glob($path);
if (count($files) > 1) {
usort($files, function($a, $b){
$a = str_replace('/[^\d]/', '', $a);
$b = str_replace('/[^\d]/', '', $b);
return $a < $b ? 1 : -1;
});
}
$package = new Phar($files[0], 0);
$current = 0;
$count = $package->count();
foreach (new RecursiveIteratorIterator($package) as $file) {
$current++;
// $file является объектом класса PharFileInfo, который наследует SplFileInfo
$path = preg_replace('/phar:.+\.phar/', '', $file->getPath()) . '/';
// echo file_get_contents($file->getPathName()) . "\n"; // отображает содержимое;
$dir = __DIR__ . "/$path";
if (! is_dir($dir)) {
mkdir($dir, $chmod, true);
}
file_put_contents($dir .'/'.$file->getFileName(), file_get_contents($file->getPathName()));
echo "deploy ($current/$count): " . ltrim($path, '/') . $file->getFileName() . "\n";
flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment