/** * Compiles slicer into a single phar file. * * @param string $pharFile */ public function compile($pharFile = 'slicer.phar') { if (file_exists($pharFile)) { unlink($pharFile); } $process = new Process('git log --pretty="%H" -n1 HEAD', __DIR__); if (0 != $process->run()) { throw new RuntimeException('Can\'t run git log. You must ensure to run compile from composer git repository clone and that git binary is available.'); } $this->version = trim($process->getOutput()); $process = new Process('git log -n1 --pretty=%ci HEAD', __DIR__); if (0 != $process->run()) { throw new RuntimeException('Can\'t run git log. You must ensure to run compile from composer git repository clone and that git binary is available.'); } $this->versionDate = new DateTime(trim($process->getOutput())); $this->versionDate->setTimezone(new DateTimeZone('UTC')); $phar = new Phar($pharFile, 0, 'slicer.phar'); $phar->setSignatureAlgorithm(Phar::SHA1); $phar->startBuffering(); /** * @param SplFileInfo $a * @param SplFileInfo $b * * @return int */ $finderSort = function ($a, $b) { return strcmp(strstr($a->getRealPath(), '\\', '/'), strstr($b->getRealPath(), '\\', '/')); }; $finder = new Finder(); $finder->files()->ignoreVCS(TRUE)->name('*.php')->notName('Compiler.php')->in(__DIR__ . '/..')->sort($finderSort); foreach ($finder as $file) { $this->addFile($phar, $file); } $finder = new Finder(); $finder->files()->name('*.json')->in(__DIR__ . '/../../res')->sort($finderSort); foreach ($finder as $file) { $this->addFile($phar, $file, FALSE); } // add vendors $finder = new Finder(); $finder->files()->ignoreVCS(TRUE)->name('*.php')->name('LICENSE')->exclude('Tests')->exclude('tests')->exclude('docs')->exclude('installed.json')->in(__DIR__ . '/../../vendor/symfony/')->in(__DIR__ . '/../../vendor/seld/jsonlint')->in(__DIR__ . '/../../vendor/psr/http-message')->in(__DIR__ . '/../../vendor/guzzlehttp')->in(__DIR__ . '/../../vendor/rappasoft')->in(__DIR__ . '/../../vendor/composer')->sort($finderSort); foreach ($finder as $file) { $this->addFile($phar, $file); } $this->addSlicerBin($phar); // Stubs $stub = $this->getStub(); $phar->setStub($stub); $phar->stopBuffering(); // disabled for interoperability with systems without gzip ext // $phar->compressFiles( Phar::GZ ); $this->addFile($phar, new SplFileInfo(__DIR__ . '/../../LICENSE'), FALSE); unset($phar); // re-sign the phar with reproducible timestamp / signature $util = new Timestamps($pharFile); $util->updateTimestamps($this->versionDate); $util->save($pharFile, Phar::SHA1); }
/** * Create an executable update phar. * * @param SplFileInfo $classFile * @param SplFileInfo $filesZip * * @return bool */ public function compileUpdatePhar(SplFileInfo $classFile, SplFileInfo $filesZip) { try { $pharFile = $this->config->getStorage()['update-dir'] . '/Update.phar'; if (file_exists($pharFile)) { unlink($pharFile); } $phar = new Phar($pharFile, 0, 'Update.phar'); $phar->setSignatureAlgorithm(Phar::SHA1); $phar->startBuffering(); // Add Update Class $path = strtr(str_replace(strtr(base_path($this->config->getStorage()['tmp-dir']) . '/', '/', '\\'), '', $classFile->getRealPath()), '\\', '/'); $content = file_get_contents($classFile->getRealPath()); $phar->addFromString($path, $content); // Add Zip File $path = 'res/files.zip'; $content = file_get_contents($filesZip->getRealPath()); $phar->addFromString($path, $content); // Add update bin script $this->addUpdateBin($phar); // Generate a Stub $stub = $this->generateUpdateStub($pharFile); $phar->setStub($stub); $phar->stopBuffering(); // re-sign the phar with reproducible timestamp / signature $util = new Timestamps($pharFile); $util->updateTimestamps((new DateTime())->format('U')); $util->save($pharFile, Phar::SHA1); return TRUE; } catch (Exception $e) { echo $e->getMessage(); } return FALSE; }
/** * Compiles coverage-comparator into a Phar file * * @param string $pharFile The full path to the file to create * * @throws \RuntimeException */ public function compile($pharFile = 'coverage-comparator.phar') { $fs = new Filesystem(); if ($fs->exists($pharFile)) { $fs->remove($pharFile); } $process = new Process('git log --pretty="%H" -n1 HEAD', __DIR__); if ($process->run() != 0) { throw new \RuntimeException('Can\'t run git log. You must ensure that compile is run from a coverage-comparator git repository clone and that git binary is available.'); } $this->version = trim($process->getOutput()); $process = new Process('git log -n1 --pretty=%ci HEAD', __DIR__); if ($process->run() != 0) { throw new \RuntimeException('Can\'t run git log. You must ensure that compile is run from a coverage-comparator git repository clone and that git binary is available.'); } $this->versionDate = new \DateTime(trim($process->getOutput())); $this->versionDate->setTimezone(new \DateTimeZone('UTC')); $process = new Process('git describe --tags --exact-match HEAD'); try { $this->version = trim($process->getOutput()); } catch (LogicException $e) { echo 'No git tags found on repository. Skipping.', "\n"; } $phar = new Phar($pharFile, 0, 'coverage-comparator.phar'); $phar->setSignatureAlgorithm(Phar::SHA1); // Start buffering Phar write operations, do not modify the Phar object on disk $phar->startBuffering(); $finderSort = function (SplFileInfo $a, SplFileInfo $b) { return strcmp(strtr($a->getRealPath(), '\\', '/'), strtr($b->getRealPath(), '\\', '/')); }; // Compile in package files $finder = new Finder(); $finder->files()->ignoreVCS(true)->name('*.php')->in(__DIR__ . '/../src/')->sort($finderSort); foreach ($finder as $file) { $this->addFile($phar, $file); } // Compile in Symfony files $finder = new Finder(); $finder->files()->ignoreVCS(true)->name('*.php')->name('LICENSE')->exclude('Tests')->exclude('tests')->exclude('docs')->in(__DIR__ . '/../vendor/symfony/')->sort($finderSort); foreach ($finder as $file) { $this->addFile($phar, $file); } // Compile in Composer autoload files $this->addFile($phar, new SplFileInfo(__DIR__ . '/../vendor/autoload.php')); $this->addFile($phar, new SplFileInfo(__DIR__ . '/../vendor/composer/autoload_namespaces.php')); $this->addFile($phar, new SplFileInfo(__DIR__ . '/../vendor/composer/autoload_psr4.php')); $this->addFile($phar, new SplFileInfo(__DIR__ . '/../vendor/composer/autoload_classmap.php')); $this->addFile($phar, new SplFileInfo(__DIR__ . '/../vendor/composer/autoload_files.php')); $this->addFile($phar, new SplFileInfo(__DIR__ . '/../vendor/composer/autoload_real.php')); if (file_exists(__DIR__ . '/../vendor/composer/include_paths.php')) { $this->addFile($phar, new SplFileInfo(__DIR__ . '/../vendor/composer/include_paths.php')); } $this->addFile($phar, new SplFileInfo(__DIR__ . '/../vendor/composer/ClassLoader.php')); $this->addComparatorBin($phar); // Stubs $phar->setStub($this->getStub()); // Stop buffering write requests to the Phar archive, and save changes to disk $phar->stopBuffering(); if (extension_loaded('zlib')) { $phar->compressFiles(Phar::GZ); } $this->addFile($phar, new SplFileInfo(__DIR__ . '/../LICENSE'), false); unset($phar); // Re-sign the Phar with reproducible timestamp / signature $util = new Timestamps($pharFile); $util->updateTimestamps($this->versionDate); $util->save($pharFile, Phar::SHA1); }
/** * Compiles composer into a single phar file * * @param string $pharFile The full path to the file to create * @throws \RuntimeException */ public function compile($pharFile = 'composer.phar') { if (file_exists($pharFile)) { unlink($pharFile); } $process = new Process('git log --pretty="%H" -n1 HEAD', __DIR__); if ($process->run() != 0) { throw new \RuntimeException('Can\'t run git log. You must ensure to run compile from composer git repository clone and that git binary is available.'); } $this->version = trim($process->getOutput()); $process = new Process('git log -n1 --pretty=%ci HEAD', __DIR__); if ($process->run() != 0) { throw new \RuntimeException('Can\'t run git log. You must ensure to run compile from composer git repository clone and that git binary is available.'); } $this->versionDate = new \DateTime(trim($process->getOutput())); $this->versionDate->setTimezone(new \DateTimeZone('UTC')); $process = new Process('git describe --tags --exact-match HEAD'); if ($process->run() == 0) { $this->version = trim($process->getOutput()); } else { // get branch-alias defined in composer.json for dev-master (if any) $localConfig = __DIR__ . '/../../composer.json'; $file = new JsonFile($localConfig); $localConfig = $file->read(); if (isset($localConfig['extra']['branch-alias']['dev-master'])) { $this->branchAliasVersion = $localConfig['extra']['branch-alias']['dev-master']; } } $phar = new \Phar($pharFile, 0, 'composer.phar'); $phar->setSignatureAlgorithm(\Phar::SHA1); $phar->startBuffering(); $finderSort = function ($a, $b) { return strcmp(strtr($a->getRealPath(), '\\', '/'), strtr($b->getRealPath(), '\\', '/')); }; $finder = new Finder(); $finder->files()->ignoreVCS(true)->name('*.php')->notName('Compiler.php')->notName('ClassLoader.php')->in(__DIR__ . '/..')->sort($finderSort); foreach ($finder as $file) { $this->addFile($phar, $file); } $this->addFile($phar, new \SplFileInfo(__DIR__ . '/Autoload/ClassLoader.php'), false); $finder = new Finder(); $finder->files()->name('*.json')->in(__DIR__ . '/../../res')->in(SpdxLicenses::getResourcesDir())->sort($finderSort); foreach ($finder as $file) { $this->addFile($phar, $file, false); } $this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../vendor/seld/cli-prompt/res/hiddeninput.exe'), false); $finder = new Finder(); $finder->files()->ignoreVCS(true)->name('*.php')->name('LICENSE')->exclude('Tests')->exclude('tests')->exclude('docs')->in(__DIR__ . '/../../vendor/symfony/')->in(__DIR__ . '/../../vendor/seld/jsonlint/')->in(__DIR__ . '/../../vendor/seld/cli-prompt/')->in(__DIR__ . '/../../vendor/justinrainbow/json-schema/')->in(__DIR__ . '/../../vendor/composer/spdx-licenses/')->in(__DIR__ . '/../../vendor/composer/semver/')->sort($finderSort); foreach ($finder as $file) { $this->addFile($phar, $file); } $this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../vendor/autoload.php')); $this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../vendor/composer/autoload_namespaces.php')); $this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../vendor/composer/autoload_psr4.php')); $this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../vendor/composer/autoload_classmap.php')); $this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../vendor/composer/autoload_real.php')); if (file_exists(__DIR__ . '/../../vendor/composer/include_paths.php')) { $this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../vendor/composer/include_paths.php')); } $this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../vendor/composer/ClassLoader.php')); $this->addComposerBin($phar); // Stubs $phar->setStub($this->getStub()); $phar->stopBuffering(); // disabled for interoperability with systems without gzip ext // $phar->compressFiles(\Phar::GZ); $this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../LICENSE'), false); unset($phar); // re-sign the phar with reproducible timestamp / signature $util = new Timestamps($pharFile); $util->updateTimestamps($this->versionDate); $util->save($pharFile, \Phar::SHA1); }
echo "Signature: ", $signature, " (from build.xml) \n"; $const = "Phar::" . strtoupper($signature); $sig = constant($const); echo "Phar Algo: ", var_export($sig, true), " ({$const})\n"; if (!is_file($file) || !is_readable($file)) { throw new RuntimeException(sprintf('Is not a file or not readable: %s', var_export($file, true))); } $timestamp = (int) `git log --format=format:%ct HEAD -1`; $threshold = 1343826993; if ($timestamp < $threshold) { $message = sprintf('Timestamp %d (%s) below threshold %d (%s).', $timestamp, date(DATE_RFC3339, $timestamp), $threshold, date(DATE_RFC3339, $threshold)); throw new RuntimeException($message); } $tmp = $file . '.tmp'; if ($tmp !== $file && file_exists($tmp)) { unlink($tmp); } if (!rename($file, $tmp)) { throw new RuntimeException(sprintf('Failed to move file %s to %s', var_export($file, true), var_export($tmp, true))); } if (!is_file($tmp)) { throw new RuntimeException('No tempfile %s for reading', var_export($tmp, true)); } $timestamps = new Timestamps($tmp); $timestamps->updateTimestamps($timestamp); $timestamps->save($file, $sig); printf("Timestamp: %d (%s)\n", $timestamp, date(DATE_RFC3339, $timestamp)); echo "SHA1.....: ", sha1_file($file), "\nMD5......: ", md5_file($file), "\n"; if (!unlink($tmp)) { throw new RuntimeException('Error deleting tempfile %s', var_export($tmp, true)); }