-
Notifications
You must be signed in to change notification settings - Fork 20
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 #139 from tractorcow/pulls/update-task
API Rewrite source file update task
- Loading branch information
Showing
9 changed files
with
266 additions
and
72 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,86 @@ | ||
<?php | ||
|
||
class GitMarkdownUpdater implements MarkdownUpdater | ||
{ | ||
|
||
public function update($repo, $path, $branch) | ||
{ | ||
// Ensure git is available | ||
$errors = array(); | ||
if (!$this->runCommand('git --version', $errors)) { | ||
return $errors; | ||
} | ||
|
||
// Check if we should do a git update or re-clone | ||
if (is_dir($path . '/.git')) { | ||
return $this->doFetch($path, $branch); | ||
} else { | ||
return $this->doClone($repo, $path, $branch); | ||
} | ||
} | ||
|
||
/** | ||
* Update existing git checkouts | ||
* | ||
* @param string $path | ||
* @param string $branch | ||
* @return array List of errors | ||
*/ | ||
protected function doFetch($path, $branch) { | ||
$errors = array(); | ||
$fetchCommand = sprintf( | ||
'cd %s && git fetch origin %s', | ||
escapeshellarg($path), | ||
escapeshellarg($branch) | ||
); | ||
$resetCommand = sprintf( | ||
'cd %s && git reset --hard origin/%s', | ||
escapeshellarg($path), | ||
escapeshellarg($branch) | ||
); | ||
|
||
// Run | ||
if($this->runCommand($fetchCommand, $errors)) { | ||
$this->runCommand($resetCommand, $errors); | ||
} | ||
return $errors; | ||
} | ||
|
||
/** | ||
* Clone a new git repo | ||
* @param string $repo Repo slug | ||
* @param string $path Destination path | ||
* @param string $branch Branch name | ||
* @return array | ||
*/ | ||
protected function doClone($repo, $path, $branch) { | ||
$errors = array(); | ||
$remote = "https://github.com/{$repo}.git"; | ||
$this->runCommand(sprintf( | ||
"git clone --quiet %s %s --branch %s", | ||
escapeshellarg($remote), | ||
escapeshellarg($path), | ||
escapeshellarg($branch) | ||
), $errors); | ||
return $errors; | ||
} | ||
|
||
/** | ||
* Run this command | ||
* | ||
* @param string $cmd | ||
* @param array $errors | ||
* @return bool Flag if the command was successful | ||
*/ | ||
protected function runCommand($cmd, &$errors = array()) { | ||
exec("{$cmd} 2>&1 >/dev/null", $output, $result); | ||
if($result) { | ||
$errors[] = "Error running command {$cmd}:"; | ||
foreach($output as $error) { | ||
$errors[] = ' * ' . $error; | ||
} | ||
return false; | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
interface MarkdownUpdater { | ||
|
||
/** | ||
* @param string $repo | ||
* @param string $path | ||
* @param string $branch | ||
* @return array List of any errors that occurred | ||
*/ | ||
public function update($repo, $path, $branch); | ||
} |
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,33 @@ | ||
<?php | ||
|
||
class SVNMarkdownUpdater implements MarkdownUpdater | ||
{ | ||
public function update($repo, $path, $branch) | ||
{ | ||
$errors = array(); | ||
$svnPath = "https://github.com/{$repo}.git/branches/{$branch}/docs"; | ||
$svnDest = "{$path}/docs"; | ||
$this->runCommand(sprintf( | ||
"svn export %s %s", | ||
escapeshellarg($svnPath), | ||
escapeshellarg($svnDest) | ||
), $errors); | ||
return $errors; | ||
} | ||
|
||
/** | ||
* Run this command | ||
* | ||
* @param string $cmd | ||
* @param array $errors | ||
* @return bool Flag if the command was successful | ||
*/ | ||
protected function runCommand($cmd, &$errors = array()) { | ||
exec($cmd, $output, $result); | ||
if($result) { | ||
$errors[] = "Error running command {$cmd}"; | ||
return false; | ||
} | ||
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
Oops, something went wrong.