Skip to content

Instantly share code, notes, and snippets.

@ubermuda
Created October 20, 2011 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ubermuda/1301102 to your computer and use it in GitHub Desktop.
Save ubermuda/1301102 to your computer and use it in GitHub Desktop.
A symfony2 vendors script hacked to handle svn urls and plugins for sf1.x
#!/usr/bin/env php
<?php
/*
* This file is part of the Symfony Standard Edition.
*
* (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.
*/
$rootDir = dirname(__DIR__);
$vendorDir = $rootDir.'/lib/vendor';
$pluginsDir = $rootDir.'/plugins';
array_shift($argv);
if (!isset($argv[0])) {
exit(<<<EOF
Symfony2 vendors script management.
Specify a command to run:
install: install vendors as specified in deps or deps.lock (recommended)
update: update vendors to their latest versions (as specified in deps)
EOF
);
}
if (!in_array($command = array_shift($argv), array('install', 'update'))) {
exit(sprintf("Command \"%s\" does not exist.\n", $command));
}
if (!is_dir($vendorDir)) {
mkdir($vendorDir, 0777, true);
}
if (!is_dir($pluginsDir)) {
mkdir($pluginsDir, 0777, true);
}
// versions
$versions = array();
if ('install' === $command && file_exists($rootDir.'/deps.lock')) {
foreach (file($rootDir.'/deps.lock', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
$parts = array_values(array_filter(explode(' ', $line)));
if (2 !== count($parts)) {
exit(sprintf('The deps version file is not valid (near "%s")', $line));
}
$versions[$parts[0]] = $parts[1];
}
}
$newversions = array();
$deps = parse_ini_file($rootDir.'/deps', true, INI_SCANNER_RAW);
if (false === $deps) {
exit("The deps file is not valid ini syntax. Perhaps missing a trailing newline?\n");
}
foreach ($deps as $name => $dep) {
$dep = array_map('trim', $dep);
// revision
if (isset($versions[$name])) {
$rev = $versions[$name];
} else {
$rev = isset($dep['version']) ? $dep['version'] : false;
}
// install dir
$installDirname = isset($dep['target']) ? $dep['target'] : $name;
$installDir = (substr($name, -6) == 'Plugin') ? $pluginsDir.'/'.$name : $vendorDir.'/'.$name;
if (in_array('--reinstall', $argv)) {
if (PHP_OS == 'WINNT') {
system(sprintf('rmdir /S /Q %s', escapeshellarg(realpath($installDir))));
} else {
system(sprintf('rm -rf %s', escapeshellarg($installDir)));
}
}
echo "> Installing/Updating $name in $installDir\n";
// url
if (isset($dep['git'])) {
$url = $dep['git'];
if (!is_dir($installDir)) {
system(sprintf('git clone %s %s', escapeshellarg($url), escapeshellarg($installDir)));
}
$rev = (false === $rev) ? 'origin/HEAD' : $rev;
system(sprintf('cd %s && git fetch origin && git reset --hard %s', escapeshellarg($installDir), escapeshellarg($rev)));
if ('update' === $command) {
ob_start();
system(sprintf('cd %s && git log -n 1 --format=%%H', escapeshellarg($installDir)));
$newversions[] = trim($name.' '.ob_get_clean());
}
} elseif (isset($dep['svn'])) {
$url = $dep['svn'];
if (!is_dir($installDir)) {
system(sprintf('svn checkout %s %s', escapeshellarg($url), escapeshellarg($installDir)));
}
$rev = (false === $rev) ? 'HEAD' : $rev;
system(sprintf('cd %s && svn update -r %s', escapeshellarg($installDir), escapeshellarg($rev)));
if ('update' === $command) {
ob_start();
system(sprintf('cd %s && svn log -q -l 1', escapeshellarg($installDir)));
if (preg_match('/r(\d+)/m', ob_get_clean(), $matches)) {
$newversions[] = trim($name.' '.$matches[1]);
}
}
} else {
exit(sprintf('Either the "git" or "svn" value for the "%s" dependency must be set.', $name));
}
}
// update?
if ('update' === $command) {
file_put_contents($rootDir.'/deps.lock', implode("\n", $newversions));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment