diff options
author | Rob Loach <robloach@gmail.com> | 2015-02-08 09:36:53 -0500 |
---|---|---|
committer | Rob Loach <robloach@gmail.com> | 2015-02-08 09:36:53 -0500 |
commit | 56942fbeb71c41a074e293c58dd2633eda016198 (patch) | |
tree | a820ce826a9abd8225f6b1215422131564b3bb16 /src | |
parent | bc0f13b93a3b20f1640c75c9f28fe77b5a4c89ca (diff) | |
download | git-deploy-origin/HEAD.zip git-deploy-origin/HEAD.tar.gz git-deploy-origin/HEAD.tar.bz2 |
Update to allow multiple branchesHEAD0.0.5origin/masterorigin/HEADmaster
Diffstat (limited to 'src')
-rw-r--r-- | src/GitDeploy/Console/Command/DeployCommand.php | 8 | ||||
-rw-r--r-- | src/GitDeploy/GitDeploy.php | 99 |
2 files changed, 75 insertions, 32 deletions
diff --git a/src/GitDeploy/Console/Command/DeployCommand.php b/src/GitDeploy/Console/Command/DeployCommand.php index b504d4b..6a735bd 100644 --- a/src/GitDeploy/Console/Command/DeployCommand.php +++ b/src/GitDeploy/Console/Command/DeployCommand.php @@ -29,12 +29,6 @@ class DeployCommand extends Command protected function execute(InputInterface $input, OutputInterface $output) { $deploy = GitDeploy::fromFile($input->getOption('file')); - $repositories = $deploy->getRepositories(); - if (empty($repositories)) { - $output->writeln('<info>Configuration file empty.</info>'); - } - else { - $deploy->update(); - } + $deploy->deploy(); } } diff --git a/src/GitDeploy/GitDeploy.php b/src/GitDeploy/GitDeploy.php index d1c2cf6..a8c420c 100644 --- a/src/GitDeploy/GitDeploy.php +++ b/src/GitDeploy/GitDeploy.php @@ -7,11 +7,55 @@ use GitWrapper\GitWorkingCopy; class GitDeploy { - protected $repositories; + protected $projects = array(); - public function __construct($repositories = array()) + public function __construct(array $projects = array()) { - $this->repositories = $repositories; + if (!empty($projects)) { + $this->setProjects($projects); + } + } + + /** + * Parses the given projects configuration array into valid configuration. + */ + public function setProjects(array $projects = array()) + { + $output = array(); + + foreach ($projects as $name => $project) { + if (is_string($project)) { + $output[$name]['repo'] = $project; + } + elseif (is_array($project)) { + $output[$name] = $project; + } + + // Construct the default branches array if one is not given. + if (!isset($output[$name]['branches']) || !is_array($output[$name]['branches'])) { + $output[$name]['branches'] = array( + 'gh-pages' => $name, + ); + } + else { + $branches = array(); + foreach ($output[$name]['branches'] as $source => $destination) { + if (is_numeric($source)) { + $source = $destination; + $destination = $name . '-' . $source; + } + $branches[$source] = $destination; + } + $output[$name]['branches'] = $branches; + } + } + + $this->projects = $output; + } + + public function getProjects() + { + return $this->projects; } public static function fromFile($file = 'git-deploy.json') @@ -20,41 +64,46 @@ class GitDeploy if (is_file($file)) { $contents = file_get_contents($file); - $results = json_decode($contents); + $results = json_decode($contents, true); + if ($results === NULL) { + throw new \UnexpectedValueException("The given JSON file could not be parsed."); + } } return new GitDeploy($results); } - public function update() + public function deploy() { // Create the wrapper. $wrapper = new GitWrapper(); $wrapper->streamOutput(); - foreach ($this->repositories as $dir => $repo) { - // Build our git interface. - $git = null; - if (!is_dir($dir)) { - $git = $wrapper->cloneRepository($repo, $dir); - } - else { - $git = new GitWorkingCopy($wrapper, $dir); - } + // Iterate through each project. + foreach ($this->projects as $name => $project) { + // Check out all branches. + foreach ($project['branches'] as $branch => $destination) { + // Build our git interface. + $git = null; + if (!is_dir($destination)) { + $git = $wrapper->cloneRepository($project['repo'], $destination); + } + else { + $git = new GitWorkingCopy($wrapper, $destination); + } - // Fetch all the latest. - $git->fetch('--all'); + // Fetch the latest. + $git->fetch('origin'); - // Reset over to the gh-pages branch. - $git->reset('origin/gh-pages', array('hard' => true)); + // Checkout the desired branch. + $git->checkout($branch, array('force' => true)); - // Remove any extra files. - $git->clean('-d', '-f', '-x'); - } - } + // Reset any local changes. + $git->reset(array('hard' => true)); - public function getRepositories() - { - return $this->repositories; + // Pull the latest from the branch. + $git->pull('origin', $branch, array('force' => true)); + } + } } } |