-
-
Save ringmaster/1481722 to your computer and use it in GitHub Desktop.
Github Packager
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 | |
class Github_Packager { | |
private static $api_endpoint = 'http://github.com/api/v2/json/'; | |
private static $temp_path = './temp/'; | |
public static function list_branches ( $addon_name, $with_commits = false ) { | |
$url = self::$api_endpoint . 'repos/show/habari-extras/' . $addon_name . '/branches'; | |
$result = file_get_contents( $url ); | |
$result = json_decode( $result ); | |
// do we want to include commits hashes? | |
if ( $with_commits ) { | |
return (array)$result->branches; | |
} | |
else { | |
return array_keys( (array)$result->branches ); | |
} | |
} | |
public static function list_tags ( $addon_name, $with_commits = false ) { | |
$url = self::$api_endpoint . 'repos/show/habari-extras/' . $addon_name . '/tags'; | |
$result = file_get_contents( $url ); | |
$result = json_decode( $result ); | |
// do we want to include commits hashes? | |
if ( $with_commits ) { | |
return (array)$result->tags; | |
} | |
else { | |
return array_keys( (array)$result->tags ); | |
} | |
} | |
public static function get_branch ( $addon_name, $branch_name ) { | |
// first, get the list of branches so we know which commit to use | |
$branches = self::list_branches( $addon_name, true ); | |
if ( !isset( $branches[ $branch_name ] ) ) { | |
throw new InvalidArgumentException('The branch ' . $branch_name . ' does not exist for repo ' . $addon_name ); | |
} | |
// now clone the repo and check it out to the right point | |
self::clone_repo( $addon_name, $branches[ $branch_name ] ); | |
// package it! | |
$filename = self::package( $addon_name, $branches[ $branch_name ] ); | |
return $filename; | |
} | |
public static function get_tag ( $addon_name, $tag_name ) { | |
// first, get the list of tags so we know which commit to use | |
$tags = self::list_tags( $addon_name, true ); | |
if ( !isset( $tags[ $tag_name ] ) ) { | |
throw new InvalidArgumentException('The tag ' . $tag_name . ' does not exist for repo ' . $addon_name ); | |
} | |
// now clone the repo and check it out to the right point | |
self::clone_repo( $addon_name, $tags[ $tag_name ] ); | |
// package it! | |
$filename = self::package( $addon_name, $tags[ $tag_name ] ); | |
return $filename; | |
} | |
private static function clone_repo ( $addon_name, $commit ) { | |
// we use an abbreviated commit hash for unique naming | |
$hash = substr( $commit, 0, 8 ); | |
// build the url to the repo | |
$url = 'https://github.com/habari-extras/' . $addon_name . '.git'; | |
$dest_directory = self::$temp_path . $addon_name . '-' . $hash; | |
if ( file_exists( $dest_directory ) ) { | |
rmdir( $dest_directory ); | |
} | |
// clone the repo | |
shell_exec( 'git clone ' . $url . ' ' . $dest_directory ); | |
$orig_dir = getcwd(); | |
// move into that directory | |
chdir( $dest_directory ); | |
// check out our specific commit in a detached state | |
shell_exec( 'git checkout --detach --quiet ' . $commit ); | |
// pull in submodules | |
shell_exec( 'git submodule update --init' ); | |
// move back to the original location so self::package() can chdir properly | |
chdir( $orig_dir ); | |
} | |
private static function package ( $addon_name, $commit ) { | |
$hash = substr( $commit, 0, 8 ); | |
$orig_dir = getcwd(); | |
// move into the temp directory so our zip file ends up with a sane structure | |
chdir( self::$temp_path ); | |
$repo_name = $addon_name . '-' . $hash; | |
$dest_file = $repo_name . '.zip'; | |
// -9 = highest compression | |
// -r = recurse into directories | |
shell_exec( 'zip -9 -r ' . $dest_file . ' ' . $repo_name ); | |
// move back to the original directory so the path below is actually right | |
chdir( $orig_dir ); | |
// return the full path to the zip file | |
return realpath( self::$temp_path . $dest_file ); | |
} | |
} | |
$result = Github_Packager::get_tag('blogroll', '0.6-0.6'); | |
var_dump($result); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment