1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386:
<?php
require_once 'phing/tasks/ext/git/GitBaseTask.php';
class GuzzleSubSplitTask extends GitBaseTask
{
protected $remote = null;
protected $heads = null;
protected $tags = null;
protected $base = null;
protected $subIndicatorFile = 'composer.json';
protected $dryRun = null;
protected $noHeads = false;
protected $noTags = false;
protected $splits;
public function setRemote($str)
{
$this->remote = $str;
}
public function getRemote()
{
return $this->remote;
}
public function setHeads($str)
{
$this->heads = explode(',', $str);
}
public function getHeads()
{
return $this->heads;
}
public function setTags($str)
{
$this->tags = explode(',', $str);
}
public function getTags()
{
return $this->tags;
}
public function setBase($str)
{
$this->base = $str;
}
public function getBase()
{
return $this->base;
}
public function setSubIndicatorFile($str)
{
$this->subIndicatorFile = $str;
}
public function getSubIndicatorFile()
{
return $this->subIndicatorFile;
}
public function setDryRun($bool)
{
$this->dryRun = (bool) $bool;
}
public function getDryRun()
{
return $this->dryRun;
}
public function setNoHeads($bool)
{
$this->noHeads = (bool) $bool;
}
public function getNoHeads()
{
return $this->noHeads;
}
public function setNoTags($bool)
{
$this->noTags = (bool) $bool;
}
public function getNoTags()
{
return $this->noTags;
}
protected $client = null;
public function main()
{
$repo = $this->getRepository();
if (empty($repo)) {
throw new BuildException('"repository" is a required parameter');
}
$remote = $this->getRemote();
if (empty($remote)) {
throw new BuildException('"remote" is a required parameter');
}
chdir($repo);
$this->client = $this->getGitClient(false, $repo);
if (!is_dir('.subsplit')) {
$this->subsplitInit();
} else {
$this->subsplitUpdate();
}
$this->findSplits();
$this->verifyRepos();
$this->publish();
}
public function publish()
{
$this->log('DRY RUN ONLY FOR NOW');
$base = $this->getBase();
$base = rtrim($base, '/') . '/';
$org = $this->getOwningTarget()->getProject()->getProperty('github.org');
$splits = array();
$heads = $this->getHeads();
foreach ($heads as $head) {
foreach ($this->splits[$head] as $component => $meta) {
$splits[] = $base . $component . ':git@github.com:'. $org.'/'.$meta['repo'];
}
$cmd = 'git subsplit publish ';
$cmd .= escapeshellarg(implode(' ', $splits));
if ($this->getNoHeads()) {
$cmd .= ' --no-heads';
} else {
$cmd .= ' --heads='.$head;
}
if ($this->getNoTags()) {
$cmd .= ' --no-tags';
} else {
if ($this->getTags()) {
$cmd .= ' --tags=' . escapeshellarg(implode(' ', $this->getTags()));
}
}
passthru($cmd);
}
}
public function subsplitUpdate()
{
$repo = $this->getRepository();
$this->log('git-subsplit update...');
$cmd = $this->client->getCommand('subsplit');
$cmd->addArgument('update');
try {
$cmd->execute();
} catch (Exception $e) {
throw new BuildException('git subsplit update failed'. $e);
}
chdir($repo . '/.subsplit');
passthru('php ../composer.phar update --dev');
chdir($repo);
}
public function subsplitInit()
{
$remote = $this->getRemote();
$cmd = $this->client->getCommand('subsplit');
$this->log('running git-subsplit init ' . $remote);
$cmd->setArguments(array(
'init',
$remote
));
try {
$output = $cmd->execute();
} catch (Exception $e) {
throw new BuildException('git subsplit init failed'. $e);
}
$this->log(trim($output), Project::MSG_INFO);
$repo = $this->getRepository();
chdir($repo . '/.subsplit');
passthru('php ../composer.phar install --dev');
chdir($repo);
}
protected function findSplits()
{
$this->log("checking heads for subsplits");
$repo = $this->getRepository();
$base = $this->getBase();
$splits = array();
$heads = $this->getHeads();
if (!empty($base)) {
$base = '/' . ltrim($base, '/');
} else {
$base = '/';
}
chdir($repo . '/.subsplit');
foreach ($heads as $head) {
$splits[$head] = array();
passthru("git checkout '$head'");
$ds = new DirectoryScanner();
$ds->setBasedir($repo . '/.subsplit' . $base);
$ds->setIncludes(array('**/'.$this->subIndicatorFile));
$ds->scan();
$files = $ds->getIncludedFiles();
foreach ($files as $file) {
$pkg = file_get_contents($repo . '/.subsplit' . $base .'/'. $file);
$pkg_json = json_decode($pkg, true);
$name = $pkg_json['name'];
$component = str_replace('/composer.json', '', $file);
$tmpreponame = explode('/', $name);
$reponame = $tmpreponame[1];
$splits[$head][$component]['repo'] = $reponame;
$nscomponent = str_replace('/', '\\', $component);
$splits[$head][$component]['desc'] = "[READ ONLY] Subtree split of $nscomponent: " . $pkg_json['description'];
}
}
passthru("git checkout master");
chdir($repo);
$this->splits = $splits;
}
protected function verifyRepos()
{
$this->log('verifying GitHub target repos');
$github_org = $this->getOwningTarget()->getProject()->getProperty('github.org');
$github_creds = $this->getOwningTarget()->getProject()->getProperty('github.basicauth');
if ($github_creds == 'username:password') {
$this->log('Skipping GitHub repo checks. Update github.basicauth in build.properties to verify repos.', 1);
return;
}
$ch = curl_init('https://api.github.com/orgs/'.$github_org.'/repos?type=all');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $github_creds);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
$repos = json_decode($result, true);
$existing_repos = array();
foreach ($repos as $repo) {
$tmpreponame = explode('/', $repo['full_name']);
$reponame = $tmpreponame[1];
$existing_repos[$reponame] = $repo['description'];
}
$heads = $this->getHeads();
foreach ($heads as $head) {
foreach ($this->splits[$head] as $component => $meta) {
$reponame = $meta['repo'];
if (!isset($existing_repos[$reponame])) {
$this->log("Creating missing repo $reponame");
$payload = array(
'name' => $reponame,
'description' => $meta['desc'],
'homepage' => 'http://www.guzzlephp.org/',
'private' => true,
'has_issues' => false,
'has_wiki' => false,
'has_downloads' => true,
'auto_init' => false
);
$ch = curl_init('https://api.github.com/orgs/'.$github_org.'/repos');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $github_creds);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
echo "Response code: ".curl_getinfo($ch, CURLINFO_HTTP_CODE)."\n";
curl_close($ch);
} else {
$this->log("Repo $reponame exists", 2);
}
}
}
}
}