-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from paragonie/release-bundlers
Release Bundlers
- Loading branch information
Showing
15 changed files
with
543 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
namespace ParagonIE\Gossamer\Interfaces; | ||
|
||
interface ReleaseBundlerInterface | ||
{ | ||
/** | ||
* @param string $directory | ||
* @return self | ||
*/ | ||
public function setWorkDirectory($directory); | ||
|
||
/** | ||
* @param string $outputFile | ||
* @return bool | ||
*/ | ||
public function bundle($outputFile); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace ParagonIE\Gossamer\Release\Bundler; | ||
|
||
use Exception; | ||
use ParagonIE\Gossamer\Interfaces\ReleaseBundlerInterface; | ||
use ParagonIE\Gossamer\TypeHelperTrait; | ||
|
||
abstract class AbstractBundler implements ReleaseBundlerInterface | ||
{ | ||
use TypeHelperTrait; | ||
|
||
|
||
/** @var string $directory */ | ||
protected $directory = ''; | ||
|
||
/** | ||
* @param string $directory | ||
* @return self | ||
* @throws Exception | ||
*/ | ||
public function setWorkDirectory($directory) | ||
{ | ||
$this->assert(is_string($directory), "Argument 1 must be a string"); | ||
$this->assert(is_dir($directory), "Given path is not a directory: {$directory}"); | ||
$this->directory = $directory; | ||
return $this; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
namespace ParagonIE\Gossamer\Release\Bundler; | ||
|
||
use Exception; | ||
use ParagonIE\Gossamer\GossamerException; | ||
|
||
class GitDiffBundler extends AbstractBundler | ||
{ | ||
const IDENTIFIER_REGEX = '#[a-zA-Z0-9\-_\.]+#'; | ||
|
||
/** @var string $previousIdentifier */ | ||
protected $previousIdentifier = ''; | ||
|
||
/** | ||
* @param string $commitOrTag | ||
* @return self | ||
* @throws Exception | ||
*/ | ||
public function setPreviousIdentifier($commitOrTag) | ||
{ | ||
$this->assert(is_string($commitOrTag), "Argument 1 must be a string"); | ||
$this->assert( | ||
preg_match(self::IDENTIFIER_REGEX, $commitOrTag) > 0, | ||
"Invalid commit or tag identifier" | ||
); | ||
$this->previousIdentifier = $commitOrTag; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $current | ||
* @return string | ||
* @throws Exception | ||
*/ | ||
public function getGitDiff($current = 'HEAD') | ||
{ | ||
$this->assert(is_string($current)); | ||
$this->assert( | ||
preg_match(self::IDENTIFIER_REGEX, $current) > 0, | ||
"Invalid commit or tag identifier" | ||
); | ||
$this->assert(!empty($this->previousIdentifier)); | ||
$this->assert( | ||
preg_match(self::IDENTIFIER_REGEX, $this->previousIdentifier) > 0, | ||
"Invalid commit or tag identifier" | ||
); | ||
$workingDir = \getcwd(); | ||
|
||
// @todo Support more techniques | ||
if (is_callable('shell_exec')) { | ||
\chdir($this->directory); | ||
/** | ||
* We need to use shell_exec() to get the output of git diff. | ||
* @psalm-suppress ForbiddenCode | ||
*/ | ||
$result = (string) @shell_exec( | ||
"git diff {$this->previousIdentifier}..{$current} -G." | ||
); | ||
\chdir($workingDir); | ||
return $result; | ||
} | ||
|
||
// Failure case: Throw. | ||
throw new GossamerException("No supported git diff method found"); | ||
} | ||
|
||
/** | ||
* @param string $outputFile | ||
* @param string $current | ||
* @return bool | ||
* @throws Exception | ||
*/ | ||
public function bundle($outputFile, $current = 'HEAD') | ||
{ | ||
$diff = $this->getGitDiff($current); | ||
$this->assert(is_string($diff), "Return type of getGitDiff() must be string"); | ||
return is_int(file_put_contents($outputFile, $diff)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
namespace ParagonIE\Gossamer\Release\Bundler; | ||
|
||
use ParagonIE\Gossamer\GossamerException; | ||
use Phar; | ||
|
||
class PharBundler extends AbstractBundler | ||
{ | ||
/** @var string $defaultStubFilename */ | ||
protected $defaultStubFilename = 'default-stub.php'; | ||
|
||
/** | ||
* @param string $filename | ||
* @return self | ||
*/ | ||
public function setDefaultStubFilename($filename) | ||
{ | ||
$this->assert(is_string($filename), "Argument 1 must be a string"); | ||
$this->defaultStubFilename = $filename; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return void | ||
* @throws GossamerException | ||
*/ | ||
public function throwIfPharReadonly() | ||
{ | ||
$readOnly = (bool) ini_get('phar.readonly'); | ||
if ($readOnly) { | ||
throw new GossamerException( | ||
"Cannot work with Phars without setting readonly to false" | ||
); | ||
} | ||
} | ||
|
||
public function bundle($outputFile) | ||
{ | ||
$this->throwIfPharReadonly(); | ||
$workingDir = \getcwd(); | ||
\chdir($this->directory); | ||
|
||
$phar = new Phar($outputFile); | ||
$phar->startBuffering(); | ||
$defaultStub = $phar->createDefaultStub($this->defaultStubFilename); | ||
$phar->buildFromDirectory($this->directory); | ||
$phar->setStub('#!/usr/bin/env php' . "\n" . $defaultStub); | ||
$phar->stopBuffering(); | ||
|
||
\chdir($workingDir); | ||
return is_file($outputFile); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
namespace ParagonIE\Gossamer\Release\Bundler; | ||
|
||
use Archive_Tar; | ||
|
||
class TarBundler extends AbstractBundler | ||
{ | ||
/** @var ?string $compression */ | ||
protected $compression = null; | ||
|
||
/** | ||
* @return ?string | ||
*/ | ||
public function getCompression() | ||
{ | ||
return $this->compression; | ||
} | ||
|
||
/** | ||
* @param ?string $compress | ||
* @return self | ||
* @throws \Exception | ||
*/ | ||
public function setCompression($compress) | ||
{ | ||
if (is_null($compress)) { | ||
$this->compression = null; | ||
return $this; | ||
} | ||
$this->assert( | ||
is_string($compress), | ||
"Argument 1 must be a string or null" | ||
); | ||
$this->assert( | ||
in_array($compress, ['gz', 'bz2', 'lzma2']), | ||
"Invalid compression method: " . $compress | ||
); | ||
$this->compression = $compress; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $parent | ||
* @return array | ||
*/ | ||
public function listFilesToTar($parent) | ||
{ | ||
/** @var string $file */ | ||
$paths = []; | ||
foreach (glob($parent . '/*') as $file) { | ||
if (is_dir($file)) { | ||
$paths = array_merge($paths, $this->listFilesToTar($file)); | ||
} else { | ||
$paths[] = str_replace($this->directory . '/', '', $file); | ||
} | ||
} | ||
return $paths; | ||
} | ||
|
||
public function bundle($outputFile) | ||
{ | ||
$workingDir = \getcwd(); | ||
\chdir($this->directory); | ||
|
||
$tar = new Archive_Tar($outputFile, $this->compression); | ||
|
||
$fileList = $this->listFilesToTar($this->directory); | ||
|
||
$result = $tar->create($fileList); | ||
\chdir($workingDir); | ||
return $result; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
namespace ParagonIE\Gossamer\Release\Bundler; | ||
|
||
use Exception; | ||
use ParagonIE\Gossamer\GossamerException; | ||
use ZipArchive; | ||
|
||
class ZipBundler extends AbstractBundler | ||
{ | ||
/** | ||
* @param ZipArchive $zip | ||
* @param string $parent | ||
* @return ZipArchive | ||
*/ | ||
public function addFilesToZip(ZipArchive $zip, $parent) | ||
{ | ||
$base = str_replace($this->directory, '', $parent); | ||
$zip->addEmptyDir($base); | ||
/** @var string $file */ | ||
foreach (glob($parent . '/*') as $file) { | ||
if (is_dir($file)) { | ||
$this->addFilesToZip($zip, $file); | ||
} else { | ||
$newFile = ltrim(str_replace($this->directory, '', $file), '/'); | ||
$zip->addFile((string) $file, (string) $newFile); | ||
} | ||
} | ||
return $zip; | ||
} | ||
|
||
public function bundle($outputFile) | ||
{ | ||
$workingDir = \getcwd(); | ||
\chdir($this->directory); | ||
$zip = new ZipArchive(); | ||
if ($zip->open($outputFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) { | ||
throw new GossamerException("Cannot create zip"); | ||
} | ||
|
||
$this->addFilesToZip($zip, $this->directory); | ||
|
||
\chdir($workingDir); | ||
return $zip->close(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?php | ||
namespace ParagonIE\Gossamer\Client; | ||
namespace ParagonIE\Gossamer; | ||
|
||
use Exception; | ||
|
||
|
Oops, something went wrong.