Skip to content

Instantly share code, notes, and snippets.

@iRedds
Created August 18, 2017 08:25
Show Gist options
  • Select an option

  • Save iRedds/aa2ff08ec4119fe2e659c3e719a169d1 to your computer and use it in GitHub Desktop.

Select an option

Save iRedds/aa2ff08ec4119fe2e659c3e719a169d1 to your computer and use it in GitHub Desktop.

Revisions

  1. iRedds created this gist Aug 18, 2017.
    71 changes: 71 additions & 0 deletions makephar.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    <?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();
    }
    }