forked from jamescowie/composer-patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
splitting patch strategy (sh and patch files)
- Loading branch information
1 parent
acd6e01
commit ecf6617
Showing
6 changed files
with
294 additions
and
66 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,36 @@ | ||
<?php | ||
|
||
namespace Inviqa\Patch; | ||
|
||
use Symfony\Component\Process\Process; | ||
use Symfony\Component\Process\ProcessUtils; | ||
use Symfony\Component\Process\Exception\ProcessFailedException; | ||
|
||
class DotPatch extends Patch | ||
{ | ||
const TYPE = 'patch'; | ||
|
||
/** | ||
* @throws ProcessFailedException | ||
* @return boolean | ||
*/ | ||
protected function doApply() | ||
{ | ||
$patchPath = ProcessUtils::escapeArgument($this->getPatchTemporaryPath()); | ||
$process = new Process("patch -p 1 < $patchPath"); | ||
$process->mustRun(); | ||
return $process->getExitCode() === 0; | ||
} | ||
|
||
protected function canApply() | ||
{ | ||
$patchPath = ProcessUtils::escapeArgument($this->getPatchTemporaryPath()); | ||
$process = new Process("patch --dry-run -p 1 < $patchPath"); | ||
try { | ||
$process->mustRun(); | ||
return $process->getExitCode() === 0; | ||
} catch (\Exception $e) { | ||
return false; | ||
} | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace Inviqa\Patch; | ||
|
||
class Factory | ||
{ | ||
/** | ||
* @param string $patchName | ||
* @param string $patchGroup | ||
* @param array $patchDetails | ||
* @return Patch | ||
*/ | ||
public static function create($patchName, $patchGroup, array $patchDetails) | ||
{ | ||
if (empty($patchDetails['type'])) { | ||
$patchDetails['type'] = DotPatch::TYPE; | ||
} | ||
|
||
$type = $patchDetails['type'] === DotPatch::TYPE ? 'DotPatch' : 'Shell'; | ||
$patchClass = '\\Inviqa\\Patch\\' . $type; | ||
|
||
$patch = new $patchClass($patchName, $patchGroup, $patchDetails); | ||
|
||
return $patch; | ||
} | ||
} |
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,203 @@ | ||
<?php | ||
|
||
namespace Inviqa\Patch; | ||
|
||
use Symfony\Component\Console\Output\ConsoleOutput; | ||
use Symfony\Component\Console\Output\Output; | ||
|
||
abstract class Patch | ||
{ | ||
private $group; | ||
|
||
private $name; | ||
|
||
private $title; | ||
|
||
private $description; | ||
|
||
private $url; | ||
|
||
private $tempPatchFilePath; | ||
|
||
/** | ||
* @var Output | ||
*/ | ||
private $output; | ||
|
||
private $log; | ||
|
||
/** | ||
* @return boolean | ||
*/ | ||
abstract protected function doApply(); | ||
|
||
/** | ||
* @return boolean | ||
*/ | ||
abstract protected function canApply(); | ||
|
||
public function __construct($name, $group, array $details) | ||
{ | ||
$this->setName($name); | ||
$this->setGroup($group); | ||
|
||
if (!empty($details['url'])) { | ||
$this->setUrl($details['url']); | ||
} | ||
} | ||
|
||
/** | ||
* @return boolean|null | ||
* @throws \Exception | ||
*/ | ||
public final function apply() | ||
{ | ||
$namespace = $this->getNamespace(); | ||
if ($this->canApply()) { | ||
$res = (bool) $this->doApply(); | ||
|
||
if ($res) { | ||
$this->getOutput()->writeln("<info>Patch $namespace successfully applied.</info>"); | ||
} else { | ||
$this->getOutput()->writeln("<comment>Patch $namespace was not applied.</comment>"); | ||
} | ||
|
||
return $res; | ||
} | ||
$this->getOutput()->writeln("<comment>Patch $namespace skipped. Patch was already applied?</comment>"); | ||
return null; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getNamespace() | ||
{ | ||
return $this->getGroup() . '/' . $this->getName(); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getGroup() | ||
{ | ||
return $this->group; | ||
} | ||
|
||
/** | ||
* @param string $group | ||
*/ | ||
protected function setGroup($group) | ||
{ | ||
$this->group = $group; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
*/ | ||
protected function setName($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getTitle() | ||
{ | ||
return $this->title; | ||
} | ||
|
||
/** | ||
* @param string $title | ||
*/ | ||
protected function setTitle($title) | ||
{ | ||
$this->title = $title; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getDescription() | ||
{ | ||
return $this->description; | ||
} | ||
|
||
/** | ||
* @param string $description | ||
*/ | ||
protected function setDescription($description) | ||
{ | ||
$this->description = $description; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getUrl() | ||
{ | ||
return $this->url; | ||
} | ||
|
||
/** | ||
* @param string $url | ||
*/ | ||
protected function setUrl($url) | ||
{ | ||
$this->url = $url; | ||
} | ||
|
||
/** | ||
* @return string | ||
* @throws \Exception | ||
*/ | ||
protected function getPatchTemporaryPath() | ||
{ | ||
if (is_null($this->tempPatchFilePath)) { | ||
$this->getOutput()->writeln("<info>Fetching patch {$this->getNamespace()}</info>"); | ||
|
||
if (!$this->getUrl()) { | ||
return $this->tempPatchFilePath = ''; | ||
} | ||
|
||
if (!$patch = file_get_contents($this->getUrl())) { | ||
throw new \Exception("Could not get contents from {$this->getUrl()}"); | ||
} | ||
|
||
$patchFilePath = sprintf("%s/%s_%s.patch", sys_get_temp_dir(), $this->getGroup(), $this->getName()); | ||
file_put_contents($patchFilePath, $patch); | ||
|
||
$this->tempPatchFilePath = $patchFilePath; | ||
} | ||
|
||
return $this->tempPatchFilePath; | ||
} | ||
|
||
/** | ||
* @return Output | ||
*/ | ||
public function getOutput() | ||
{ | ||
if (!$this->output) { | ||
$this->output = new ConsoleOutput(); | ||
} | ||
return $this->output; | ||
} | ||
|
||
/** | ||
* @param Output $output | ||
*/ | ||
public function setOutput(Output $output) | ||
{ | ||
$this->output = $output; | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
namespace Inviqa\Patch; | ||
|
||
class Shell extends Patch | ||
{ | ||
const TYPE = 'shell'; | ||
|
||
protected function doApply() | ||
{ | ||
return true; | ||
} | ||
|
||
protected function canApply() | ||
{ | ||
return true; | ||
} | ||
} |
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