Skip to content

Instantly share code, notes, and snippets.

@mathiasverraes
Created January 21, 2012 10:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mathiasverraes/1652264 to your computer and use it in GitHub Desktop.
Save mathiasverraes/1652264 to your computer and use it in GitHub Desktop.
vendors script, replaces git submodules * MOVED TO https://github.com/Credico/git-dependency-manager *
; 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 <fabien@symfony.com>
*
* 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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment