Created
November 20, 2012 09:40
-
-
Save radmiraal/4116995 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
error_reporting(E_ALL ^ E_STRICT); | |
ini_set('display_errors', 1); | |
class FetchHandler { | |
/** | |
* @var string The path to this script | |
*/ | |
protected $rootPath; | |
/** | |
* @var string The path to the Flow project root folder | |
*/ | |
protected $projectPath; | |
/** | |
* @var array List of command files to process | |
*/ | |
protected $commandFiles = array(); | |
/** | |
* Construct the FetchHandler | |
*/ | |
public function __construct() { | |
$this->projectPath = realpath(getenv('WORKSPACE') ? getenv('WORKSPACE') : $_SERVER['PWD']) . DIRECTORY_SEPARATOR; | |
$this->rootPath = $this->projectPath . implode(DIRECTORY_SEPARATOR, array('Build', 'QueuedPatches')) . DIRECTORY_SEPARATOR; | |
} | |
/** | |
* Fetch all command files and process them | |
* @return void | |
*/ | |
public function fetch() { | |
$this->getCommandFiles($this->rootPath); | |
foreach ($this->commandFiles as $commandFile) { | |
$packageDirectory = $this->projectPath . str_replace($this->rootPath, '', $commandFile); | |
echo chr(10) . chr(10) . 'PROCESSING: ' . str_replace($this->rootPath, '', $packageDirectory) . chr(10); | |
if (is_dir($packageDirectory)) { | |
$this->patchPackage($packageDirectory, $commandFile); | |
} | |
} | |
} | |
public function patchPackage($path, $commandFile) { | |
// Check current status of the submodule | |
exec(sprintf('cd %s && git status', $path), $output, $result); | |
if (isset($output[1]) && strpos($output[1], 'ahead') !== FALSE) { | |
preg_match('/by ([0-9]*) commit/', $output[1], $matches); | |
echo sprintf('REVERT: %s commit%s', $matches[1], intval($matches[1]) > 1 ? 's' : '') . chr(10); | |
exec(sprintf('cd %s && git reset --hard HEAD~' . $matches[1], $path), $output, $result); | |
} | |
$commands = explode(chr(13), file_get_contents($commandFile)); | |
foreach ($commands as $command) { | |
if (trim($command) === '') { | |
continue; | |
} | |
$cmd = sprintf('cd %s && %s', $path, $command); | |
echo sprintf('EXECUTE: %s', $command) . chr(10); | |
echo exec($cmd, $output, $result); | |
if ($result !== 0) { | |
echo 'ERROR!!!' . chr(10); | |
} | |
} | |
} | |
/** | |
* Recursively fetch all command files in a given path | |
* @param $path | |
* @return void | |
*/ | |
protected function getCommandFiles($path) { | |
$files = $this->getDirectoryContent($path); | |
foreach ($files as $file) { | |
if (is_dir($file)) { | |
$this->getCommandFiles($file . '/'); | |
} else { | |
$submodulePath = str_replace($this->rootPath, '', $file); | |
if (is_dir($this->projectPath . $submodulePath)) { | |
$this->commandFiles[] = $file; | |
} | |
} | |
} | |
} | |
/** | |
* Get all files and directories in a given path | |
* | |
* @param $path | |
* @return array | |
*/ | |
protected function getDirectoryContent($path) { | |
$content = array(); | |
if (is_dir($path)) { | |
$directoryIterator = new DirectoryIterator($path); | |
foreach ($directoryIterator as $entry) { | |
if (!$entry->isDot()) { | |
$content[] = $entry->getPathname(); | |
} | |
} | |
} | |
return $content; | |
} | |
} | |
/** | |
* Initialize the handler and fetch all changes | |
*/ | |
$handler = new FetchHandler(); | |
$handler->fetch(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment