-
-
Save mathiasverraes/1652264 to your computer and use it in GitHub Desktop.
; example deps.ini file | |
[twig] | |
git=http://github.com/fabpot/Twig.git | |
target=vendor/twig | |
version=v1.7.0 |
#!/usr/bin/env php | |
<?php | |
/* | |
* Custom version of Symfony2's vendor script | |
* Put this file in /bin and make a deps.ini file in the root, looking like this: | |
* | |
* [some_name] | |
* git=REPO | |
* target=path/to/vendor/name | |
* version=COMMITISH | |
* [some_other_name] | |
* ... | |
*/ | |
/* | |
* This file is part of the Symfony framework. | |
* | |
* (c) Fabien Potencier <[email protected]> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
set_time_limit(0); | |
$rootDir = dirname(__DIR__); | |
if (!is_dir("$rootDir/vendor")) { | |
mkdir("$rootDir/vendor", 0777, true); | |
} | |
$deps = parse_ini_file($rootDir.'/deps.ini', true, INI_SCANNER_RAW); | |
if (false === $deps) { | |
exit("The deps file is not valid ini syntax. Perhaps missing a trailing newline?\n"); | |
} | |
$children = array(); | |
foreach ($deps as $name => $dep) | |
{ | |
$pid = pcntl_fork(); | |
if ($pid == -1) { | |
die('could not fork'); | |
} else if ($pid) { | |
// we are the parent | |
$children[] = $pid; | |
} else { | |
// we are the child | |
updateDeps($rootDir, $name, $dep); | |
exit; | |
} | |
} | |
$status = null; | |
while(count($children)) { | |
pcntl_wait($status); | |
array_pop($children); | |
} | |
function updateDeps($rootDir, $name, $dep) | |
{ | |
ob_start(); | |
$dep = array_map('trim', $dep); | |
if(!isset($dep['git']) || !isset($dep['version']) || !isset($dep['target'])) { | |
echo "deps: Missing variables in [$name]".PHP_EOL; exit (1); | |
} | |
$installDir = $rootDir.'/'.$dep['target']; | |
$install = false; | |
if (!is_dir($installDir)) { | |
$install = true; | |
echo "> Installing \033[01;31m$name version {$dep['version']}\033[0m".PHP_EOL; | |
$return = system(sprintf('git clone --depth 1 %s %s', escapeshellarg($dep['git']), escapeshellarg($installDir))); | |
if(false === $return) { die('Git failed'.PHP_EOL); exit(1); } | |
} | |
if (!$install) { | |
echo "> Updating \033[01;31m$name version {$dep['version']}\033[0m".PHP_EOL; | |
} | |
$return = system(sprintf('cd %s && git fetch origin && git fetch --tags origin && git reset --hard %s', escapeshellarg($installDir), escapeshellarg($dep['version']))); | |
if(false === $return) { die('Git failed'.PHP_EOL); exit(1); } | |
// show available branches and tags that havbe a higher version number | |
$current = $dep['version']; | |
$versions = array(); | |
exec(sprintf('cd %s && git branch && git tag -l', escapeshellarg($installDir)), $versions); | |
$versions = array_filter($versions, function($version) use($current) { | |
return version_compare($current, $version, '<='); | |
}); | |
if($k = array_search($current, $versions)) { | |
$versions[$k] = "\033[01;31m".$versions[$k]."\033[0m"; // color the current one | |
} | |
echo "Versions: ".implode(' ', $versions).PHP_EOL; | |
echo PHP_EOL; | |
} |
mathiasverraes
commented
Jan 23, 2012
via email
As will other problems, it's hard to just leave them be :) Turns out it was just my skills that were a bit lacking. Got the branches and tags up and running now too, with correct authors as well :)
Great, now you can maintain your own fork of nooku on github and gradually replaces parts with http://bit.ly/zOg3Ov
Sounds like a great part-time activity! :)
I needs to learn me some more Symfony though. Haven't really touched it since Symfony 2 was in beta. What are you working with these days? Looks like a lot of it is about Symfony and Doctrine?
Added process forking for a 1000% speed increase (in a repo with about 25 deps)
About time I move this to a separate repo and refactor it for general prettiness.
Elegant! :)
So why use this script instead of git submodule?
(by elegant I of course mean quick+effective+pragmatic)
git submodules always cause problems in my experience. They confuse people who don't have a thorough understanding of git. And they're a bitch to remove. This script allows you to explicitly define what you want, at what version (be it commit, tag or branch) and where you want it. It's light, it's (as of today) fast, and everybody understands the deps.ini.