PageRenderTime 28ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/vendors

http://github.com/ornicar/lichess
PHP | 154 lines | 99 code | 31 blank | 24 comment | 27 complexity | a38cdad4f69c23a151eaf0ad1e4f6eb9 MD5 | raw file
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * This file is part of the Symfony Standard Edition.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. set_time_limit(0);
  12. $rootDir = dirname(__DIR__);
  13. $vendorDir = $rootDir.'/vendor';
  14. array_shift($argv);
  15. if (!isset($argv[0])) {
  16. exit(<<<EOF
  17. Symfony2 vendors script management.
  18. Specify a command to run:
  19. install: install vendors as specified in deps or deps.lock (recommended)
  20. update: update vendors to their latest versions (as specified in deps)
  21. lock: lock vendors to their current versions
  22. EOF
  23. );
  24. }
  25. if (!in_array($command = array_shift($argv), array('install', 'update', 'lock'))) {
  26. exit(sprintf('Command "%s" does not exist.', $command).PHP_EOL);
  27. }
  28. /*
  29. * Check wether this project is based on the Standard Edition that was
  30. * shipped with vendors or not.
  31. */
  32. if (is_dir($vendorDir.'/symfony') && !is_dir($vendorDir.'/symfony/.git') && !in_array('--reinstall', $argv)) {
  33. exit(<<<EOF
  34. Your project seems to be based on a Standard Edition that includes vendors.
  35. Try to run ./bin/vendors install --reinstall
  36. EOF
  37. );
  38. }
  39. if (!is_dir($vendorDir)) {
  40. mkdir($vendorDir, 0777, true);
  41. }
  42. // versions
  43. $versions = array();
  44. if ('install' === $command && file_exists($rootDir.'/deps.lock')) {
  45. foreach (file($rootDir.'/deps.lock', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
  46. $parts = array_values(array_filter(explode(' ', $line)));
  47. if (2 !== count($parts)) {
  48. exit(sprintf('The deps version file is not valid (near "%s")', $line).PHP_EOL);
  49. }
  50. $versions[$parts[0]] = $parts[1];
  51. }
  52. }
  53. $newversions = array();
  54. $deps = parse_ini_file($rootDir.'/deps', true, INI_SCANNER_RAW);
  55. if (false === $deps) {
  56. exit('The deps file is not valid ini syntax. Perhaps missing a trailing newline?'.PHP_EOL);
  57. }
  58. foreach ($deps as $name => $dep) {
  59. $dep = array_map('trim', $dep);
  60. // install dir
  61. $installDir = isset($dep['target']) ? $vendorDir.'/'.$dep['target'] : $vendorDir.'/'.$name;
  62. if (in_array('--reinstall', $argv)) {
  63. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  64. system(sprintf('rmdir /S /Q %s', escapeshellarg(realpath($installDir))));
  65. } else {
  66. system(sprintf('rm -rf %s', escapeshellarg($installDir)));
  67. }
  68. }
  69. if ('install' === $command || 'update' === $command) {
  70. echo '> Installing/Updating '.$name.PHP_EOL;
  71. // url
  72. if (!isset($dep['git'])) {
  73. exit(sprintf('The "git" value for the "%s" dependency must be set.', $name).PHP_EOL);
  74. }
  75. $url = $dep['git'];
  76. if (!is_dir($installDir)) {
  77. system(sprintf('git clone %s %s', escapeshellarg($url), escapeshellarg($installDir)));
  78. }
  79. // revision
  80. if (isset($versions[$name])) {
  81. $rev = $versions[$name];
  82. } else {
  83. $rev = isset($dep['version']) ? $dep['version'] : 'origin/HEAD';
  84. }
  85. $status = system(sprintf('cd %s && git status --porcelain', escapeshellarg($installDir)));
  86. if (!empty($status)) {
  87. exit(sprintf('"%s" has local modifications. Please revert or commit/push them before running this command again.', $name).PHP_EOL);
  88. }
  89. $current_rev = system(sprintf('cd %s && git rev-list --max-count=1 HEAD', escapeshellarg($installDir)));
  90. if ($current_rev === $rev) {
  91. continue;
  92. }
  93. system(sprintf('cd %s && git fetch origin && git reset --hard %s', escapeshellarg($installDir), escapeshellarg($rev)));
  94. }
  95. if ('update' === $command || 'lock' === $command) {
  96. ob_start();
  97. system(sprintf('cd %s && git log -n 1 --format=%%H', escapeshellarg($installDir)));
  98. $newversion = trim(ob_get_clean());
  99. ob_start();
  100. system(sprintf('cd %s && git name-rev --tags --name-only %s', escapeshellarg($installDir), $newversion));
  101. // remove trailing ^0 from tags, those are the tags themselves
  102. $niceversion = preg_replace('/\^0$/', '', trim(ob_get_clean()));
  103. // undefined is returned in case no name-rev could be found
  104. if ('undefined' !== $niceversion) {
  105. $newversions[] = $name.' '.$niceversion;
  106. } else {
  107. $newversions[] = $name.' '.$newversion;
  108. }
  109. }
  110. }
  111. // update?
  112. if ('update' === $command || 'lock' === $command) {
  113. echo '> Updating deps.lock'.PHP_EOL;
  114. file_put_contents($rootDir.'/deps.lock', implode("\n", $newversions)."\n");
  115. }
  116. // php on windows can't use the shebang line from system()
  117. $interpreter = defined('PHP_WINDOWS_VERSION_BUILD') ? 'php.exe' : '';
  118. // Update the bootstrap files
  119. system(sprintf('%s %s %s', $interpreter, escapeshellarg($rootDir.'/vendor/bundles/Sensio/Bundle/DistributionBundle/Resources/bin/build_bootstrap.php'), escapeshellarg($rootDir)));
  120. // Update assets
  121. system(sprintf('%s %s assets:install %s', $interpreter, escapeshellarg($rootDir.'/app/console'), escapeshellarg($rootDir.'/web/')));
  122. // Remove the cache
  123. system(sprintf('%s %s cache:clear --no-warmup', $interpreter, escapeshellarg($rootDir.'/app/console')));