Navigation Menu

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;
}
@torkiljohnsen
Copy link

This is indeed sweet.

@torkiljohnsen
Copy link

I created a fork and added similar SVN functionality too:
https://gist.github.com/1657260

@mathiasverraes
Copy link
Author

Nice. I usually just mirror svn repo's to git though :)

@torkiljohnsen
Copy link

I had a dance with svn2git, trying to mirror this framework called Nooku Framework (not sure if you heard about it? ;) into a Git repo. Had a hard time trying to get tags, trunk and branches set up correctly. Somewhere in there I saw your name and "git submodule" appear in the activity stream, so I decided to try your script instead.

@mathiasverraes
Copy link
Author

mathiasverraes commented Jan 23, 2012 via email

@torkiljohnsen
Copy link

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 :)

@mathiasverraes
Copy link
Author

Great, now you can maintain your own fork of nooku on github and gradually replaces parts with http://bit.ly/zOg3Ov

@torkiljohnsen
Copy link

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?

@mathiasverraes
Copy link
Author

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.

@torkiljohnsen
Copy link

Elegant! :)

So why use this script instead of git submodule?

@torkiljohnsen
Copy link

(by elegant I of course mean quick+effective+pragmatic)

@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