Added changed version of bin/vendors that supports git submodules
Signed-off-by: Gergely POLONKAI (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
parent
ffc9865112
commit
38e6384018
289
bin/vendors
289
bin/vendors
@ -8,170 +8,233 @@
|
|||||||
*
|
*
|
||||||
* For the full copyright and license information, please view the LICENSE
|
* For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
* @author Florian Preusner <florian@preusner.com>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
set_time_limit(0);
|
$rootDir = dirname(__DIR__);
|
||||||
|
$vendorName = 'vendor';
|
||||||
|
$vendorDir = $rootDir . '/' . $vendorName;
|
||||||
|
$submodule = false;
|
||||||
|
$cloneOptions = '';
|
||||||
|
$delete = false;
|
||||||
|
$install = true;
|
||||||
|
|
||||||
|
$commands = array(
|
||||||
|
'install' => 'install vendors as specified in deps or deps.lock (recommended)',
|
||||||
|
'update' => 'update vendors to their latest versions (as specified in deps)',
|
||||||
|
'delete' => 'remove vendors',
|
||||||
|
'reinstall' => 'delete and install',
|
||||||
|
'submodule:install' => 'install vendors as submodules',
|
||||||
|
'submodule:update' => 'update vendors to their latest versions (as specified in deps)',
|
||||||
|
'submodule:delete' => 'remove vendors',
|
||||||
|
'submodule:reinstall' => 'delete and install'
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
$rootDir = dirname(__DIR__);
|
|
||||||
$vendorDir = $rootDir.'/vendor';
|
|
||||||
|
|
||||||
array_shift($argv);
|
array_shift($argv);
|
||||||
if (!isset($argv[0])) {
|
if (!isset($argv[0]) || in_array('help', $argv)) {
|
||||||
echo(<<<EOF
|
|
||||||
Symfony2 vendors script management.
|
$help = "Symfony2 vendors script management.\n";
|
||||||
|
$help .= "Specify a command to run:\n\n";
|
||||||
|
|
||||||
Specify a command to run:
|
foreach($commands as $cmd => $info) {
|
||||||
|
|
||||||
install: install vendors as specified in deps or deps.lock (recommended)
|
$help .= str_pad($cmd, 21, ' ') . ": $info\n";
|
||||||
update: update vendors to their latest versions (as specified in deps)
|
}
|
||||||
lock: lock vendors to their current versions
|
|
||||||
|
exit($help . "\n");
|
||||||
|
|
||||||
EOF
|
|
||||||
);
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!in_array($command = array_shift($argv), array('install', 'update', 'lock'))) {
|
if (!in_array($command = array_shift($argv), array_keys($commands))) {
|
||||||
echo(sprintf('Command "%s" does not exist.', $command).PHP_EOL);
|
exit(sprintf("Command \"%s\" does not exist.\n", $command));
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Check wether this project is based on the Standard Edition that was
|
|
||||||
* shipped with vendors or not.
|
|
||||||
*/
|
|
||||||
if (is_dir($vendorDir.'/symfony') && !is_dir($vendorDir.'/symfony/.git') && !in_array('--reinstall', $argv)) {
|
|
||||||
echo(<<<EOF
|
|
||||||
Your project seems to be based on a Standard Edition that includes vendors.
|
|
||||||
|
|
||||||
Try to run ./bin/vendors install --reinstall
|
|
||||||
|
|
||||||
|
|
||||||
EOF
|
|
||||||
);
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_dir($vendorDir)) {
|
if (!is_dir($vendorDir)) {
|
||||||
mkdir($vendorDir, 0777, true);
|
mkdir($vendorDir, 0777, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (strpos($command, 'submodule') !== false) {
|
||||||
|
|
||||||
|
$submodule = true;
|
||||||
|
$command = str_replace('submodule:', '', $command);
|
||||||
|
|
||||||
|
if(!is_dir($rootDir . '/.git')) {
|
||||||
|
|
||||||
|
exit("This project is not a git repository. To use submodules it should be a repository :)\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// versions
|
// versions
|
||||||
$versions = array();
|
$versions = array();
|
||||||
if ('install' === $command && file_exists($rootDir.'/deps.lock')) {
|
if ('install' === $command && file_exists($rootDir.'/deps.lock')) {
|
||||||
foreach (file($rootDir.'/deps.lock', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
|
foreach (file($rootDir.'/deps.lock', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
|
||||||
$parts = array_values(array_filter(explode(' ', $line)));
|
$parts = array_values(array_filter(explode(' ', $line)));
|
||||||
if (2 !== count($parts)) {
|
if (2 !== count($parts)) {
|
||||||
echo(sprintf('The deps version file is not valid (near "%s")', $line).PHP_EOL);
|
exit(sprintf('The deps version file is not valid (near "%s")', $line));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
$versions[$parts[0]] = $parts[1];
|
$versions[$parts[0]] = $parts[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$newversions = array();
|
if('delete' === $command) {
|
||||||
$deps = parse_ini_file($rootDir.'/deps', true, INI_SCANNER_RAW);
|
$delete = true;
|
||||||
if (false === $deps) {
|
$install = false;
|
||||||
echo('The deps file is not valid ini syntax. Perhaps missing a trailing newline?'.PHP_EOL);
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if('reinstall' === $command) {
|
||||||
|
$delete = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$newversions = array();
|
||||||
|
$deps = parse_ini_file($rootDir.'/deps', true, INI_SCANNER_RAW);
|
||||||
foreach ($deps as $name => $dep) {
|
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'] : 'origin/HEAD';
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure to run commands on rootDir
|
||||||
|
system("cd $rootDir");
|
||||||
|
|
||||||
// install dir
|
// install dir
|
||||||
$installDir = isset($dep['target']) ? $vendorDir.'/'.$dep['target'] : $vendorDir.'/'.$name;
|
if($submodule) {
|
||||||
if (in_array('--reinstall', $argv) && realpath($installDir)) {
|
|
||||||
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
$installDir = isset($dep['target']) ? $vendorName . $dep['target'] : $vendorName . '/' . $name;
|
||||||
system(sprintf('rmdir /S /Q %s', escapeshellarg(realpath($installDir))));
|
} else {
|
||||||
} else {
|
|
||||||
system(sprintf('rm -rf %s', escapeshellarg($installDir)));
|
$installDir = isset($dep['target']) ? $vendorDir . $dep['target'] : $vendorDir . '/' . $name;
|
||||||
}
|
|
||||||
clearstatcache();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if($delete) {
|
||||||
|
|
||||||
if ('install' === $command || 'update' === $command) {
|
echo "> Removing $name\n";
|
||||||
echo PHP_EOL.'> Installing/Updating '.$name.PHP_EOL;
|
|
||||||
|
|
||||||
// url
|
if($submodule) {
|
||||||
|
|
||||||
|
deleteSubmodule($installDir, $rootDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(PHP_OS == 'WINNT') {
|
||||||
|
|
||||||
|
system('rmdir /S /Q ' . escapeshellarg(realpath($installDir)));
|
||||||
|
} else {
|
||||||
|
|
||||||
|
system('rm -rf ' . escapeshellarg($installDir));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if($install) {
|
||||||
|
|
||||||
|
echo "> Installing/Updating $name\n";
|
||||||
|
|
||||||
|
// url
|
||||||
if (!isset($dep['git'])) {
|
if (!isset($dep['git'])) {
|
||||||
echo(sprintf('The "git" value for the "%s" dependency must be set.', $name).PHP_EOL);
|
exit(sprintf('The "git" value for the "%s" dependency must be set.', $name));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
$url = $dep['git'];
|
$url = $dep['git'];
|
||||||
|
|
||||||
if ('update' === $command && is_dir($installDir)) {
|
if($submodule) {
|
||||||
ob_start();
|
|
||||||
system(sprintf('cd %s && git config --get remote.origin.url', escapeshellarg($installDir)));
|
|
||||||
$current_url = trim(ob_get_clean());
|
|
||||||
if ($current_url !== $url) {
|
|
||||||
if (PHP_OS == 'WINNT') {
|
|
||||||
system(sprintf('rmdir /S /Q %s', escapeshellarg(realpath($installDir))));
|
|
||||||
} else {
|
|
||||||
system(sprintf('rm -rf %s', escapeshellarg($installDir)));
|
|
||||||
}
|
|
||||||
clearstatcache();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!is_dir($installDir)) {
|
$cmd = sprintf('git submodule add %s %s', escapeshellarg($url), escapeshellarg($installDir));
|
||||||
system(sprintf('git clone %s %s', escapeshellarg($url), escapeshellarg($installDir)));
|
$cmd .= ' && git submodule init';
|
||||||
}
|
|
||||||
|
|
||||||
// revision
|
|
||||||
if (isset($versions[$name])) {
|
|
||||||
$rev = $versions[$name];
|
|
||||||
} else {
|
} else {
|
||||||
$rev = isset($dep['version']) ? $dep['version'] : 'origin/HEAD';
|
|
||||||
}
|
$cmd = sprintf('git clone %s %s', escapeshellarg($url), escapeshellarg($installDir));
|
||||||
|
|
||||||
$status = system(sprintf('cd %s && git status --porcelain', escapeshellarg($installDir)));
|
|
||||||
if (!empty($status)) {
|
|
||||||
echo(sprintf('"%s" has local modifications. Please revert or commit/push them before running this command again.', $name).PHP_EOL);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
$current_rev = system(sprintf('cd %s && git rev-list --max-count=1 HEAD', escapeshellarg($installDir)));
|
|
||||||
if ($current_rev === $rev) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
echo " $cmd\n";
|
||||||
|
|
||||||
|
system($cmd);
|
||||||
|
|
||||||
system(sprintf('cd %s && git fetch origin && git reset --hard %s', escapeshellarg($installDir), escapeshellarg($rev)));
|
system(sprintf('cd %s && git fetch origin && git reset --hard %s', escapeshellarg($installDir), escapeshellarg($rev)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('update' === $command || 'lock' === $command) {
|
if ('update' === $command) {
|
||||||
ob_start();
|
ob_start();
|
||||||
system(sprintf('cd %s && git log -n 1 --format=%%H', escapeshellarg($installDir)));
|
system(sprintf('cd %s && git log -n 1 --format=%%H', escapeshellarg($installDir)));
|
||||||
$newversion = trim(ob_get_clean());
|
$newversions[] = trim($name.' '.ob_get_clean());
|
||||||
|
|
||||||
ob_start();
|
|
||||||
system(sprintf('cd %s && git name-rev --tags --name-only %s', escapeshellarg($installDir), $newversion));
|
|
||||||
// remove trailing ^0 from tags, those are the tags themselves
|
|
||||||
$niceversion = preg_replace('/\^0$/', '', trim(ob_get_clean()));
|
|
||||||
|
|
||||||
// undefined is returned in case no name-rev could be found
|
|
||||||
if ('undefined' !== $niceversion) {
|
|
||||||
$newversions[] = $name.' '.$niceversion;
|
|
||||||
} else {
|
|
||||||
$newversions[] = $name.' '.$newversion;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// update?
|
// update?
|
||||||
if ('update' === $command || 'lock' === $command) {
|
if ('update' === $command) {
|
||||||
echo PHP_EOL.'> Updating deps.lock'.PHP_EOL;
|
file_put_contents($rootDir.'/deps.lock', implode("\n", $newversions));
|
||||||
|
|
||||||
file_put_contents($rootDir.'/deps.lock', implode("\n", $newversions)."\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// php on windows can't use the shebang line from system()
|
if($install) {
|
||||||
$interpreter = defined('PHP_WINDOWS_VERSION_BUILD') ? 'php.exe' : '';
|
|
||||||
|
|
||||||
// Update the bootstrap files
|
// php on windows can't use the shebang line from system()
|
||||||
system(sprintf('%s %s %s', $interpreter, escapeshellarg($rootDir.'/vendor/bundles/Sensio/Bundle/DistributionBundle/Resources/bin/build_bootstrap.php'), escapeshellarg($rootDir)));
|
$interpreter = PHP_OS == 'WINNT' ? 'php.exe' : '';
|
||||||
|
|
||||||
// Update assets
|
// Update the bootstrap files
|
||||||
system(sprintf('%s %s assets:install %s', $interpreter, escapeshellarg($rootDir.'/app/console'), escapeshellarg($rootDir.'/web/')));
|
//system(sprintf('%s %s', $interpreter, escapeshellarg($rootDir.'/bin/build_bootstrap')));
|
||||||
|
|
||||||
// Remove the cache
|
// Update assets
|
||||||
system(sprintf('%s %s cache:clear --no-warmup', $interpreter, escapeshellarg($rootDir.'/app/console')));
|
system(sprintf('%s %s assets:install --symlink %s', $interpreter, escapeshellarg($rootDir.'/app/console'), escapeshellarg($rootDir.'/web')));
|
||||||
|
|
||||||
|
// Remove the cache
|
||||||
|
system(sprintf('%s %s cache:clear --no-warmup', $interpreter, escapeshellarg($rootDir.'/app/console')));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function deleteSubmodule($name, $rootDir) {
|
||||||
|
|
||||||
|
$gitModules = $rootDir . '/.gitmodules';
|
||||||
|
$gitConfig = $rootDir . '/.git/config';
|
||||||
|
|
||||||
|
$files = array($gitModules, $gitConfig);
|
||||||
|
|
||||||
|
foreach($files as $file) :
|
||||||
|
|
||||||
|
if(file_exists($file)) {
|
||||||
|
|
||||||
|
$fileReturn = array();
|
||||||
|
$found = false;
|
||||||
|
|
||||||
|
foreach(file($file) as $line) {
|
||||||
|
|
||||||
|
if(strpos($line, '"' . $name . '"') !== false) {
|
||||||
|
|
||||||
|
$found = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($found) {
|
||||||
|
|
||||||
|
if($line{0} != '[') {
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$found = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
array_push($fileReturn, $line);
|
||||||
|
}
|
||||||
|
|
||||||
|
$handle = fopen($file, 'wt');
|
||||||
|
|
||||||
|
if($handle) {
|
||||||
|
|
||||||
|
fwrite($handle, implode($fileReturn));
|
||||||
|
fclose($handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
endforeach;
|
||||||
|
|
||||||
|
$cmd = "git rm --cached --force $name";
|
||||||
|
|
||||||
|
echo " $cmd\n";
|
||||||
|
system($cmd);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user