/system/src/Grav/Console/Cli/InstallCommand.php

https://gitlab.com/3dplex/3d-plex-main-site · PHP · 190 lines · 129 code · 23 blank · 38 comment · 18 complexity · 446588fc073165ca8c47afcbe66bd9bf MD5 · raw file

  1. <?php
  2. <<<<<<< HEAD
  3. =======
  4. /**
  5. * @package Grav.Console
  6. *
  7. * @copyright Copyright (C) 2014 - 2016 RocketTheme, LLC. All rights reserved.
  8. * @license MIT License; see LICENSE file for details.
  9. */
  10. >>>>>>> update grav cms
  11. namespace Grav\Console\Cli;
  12. use Grav\Console\ConsoleCommand;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Yaml\Yaml;
  16. <<<<<<< HEAD
  17. /**
  18. * Class InstallCommand
  19. * @package Grav\Console\Cli
  20. */
  21. =======
  22. >>>>>>> update grav cms
  23. class InstallCommand extends ConsoleCommand
  24. {
  25. /**
  26. * @var
  27. */
  28. protected $config;
  29. /**
  30. * @var
  31. */
  32. protected $local_config;
  33. /**
  34. * @var
  35. */
  36. protected $destination;
  37. /**
  38. * @var
  39. */
  40. protected $user_path;
  41. /**
  42. *
  43. */
  44. protected function configure()
  45. {
  46. $this
  47. ->setName("install")
  48. ->addOption(
  49. 'symlink',
  50. 's',
  51. InputOption::VALUE_NONE,
  52. 'Symlink the required bits'
  53. )
  54. ->addArgument(
  55. 'destination',
  56. InputArgument::OPTIONAL,
  57. 'Where to install the required bits (default to current project)'
  58. )
  59. ->setDescription("Installs the dependencies needed by Grav. Optionally can create symbolic links")
  60. ->setHelp('The <info>install</info> command installs the dependencies needed by Grav. Optionally can create symbolic links');
  61. }
  62. /**
  63. * @return int|null|void
  64. */
  65. protected function serve()
  66. {
  67. $dependencies_file = '.dependencies';
  68. $this->destination = ($this->input->getArgument('destination')) ? $this->input->getArgument('destination') : ROOT_DIR;
  69. // fix trailing slash
  70. $this->destination = rtrim($this->destination, DS) . DS;
  71. $this->user_path = $this->destination . USER_PATH;
  72. if (false === $this->isWindows()) {
  73. $local_config_file = exec('eval echo ~/.grav/config');
  74. if (file_exists($local_config_file)) {
  75. $this->local_config = Yaml::parse($local_config_file);
  76. $this->output->writeln('Read local config from <cyan>' . $local_config_file . '</cyan>');
  77. }
  78. }
  79. // Look for dependencies file in ROOT and USER dir
  80. if (file_exists($this->user_path . $dependencies_file)) {
  81. $this->config = Yaml::parse($this->user_path . $dependencies_file);
  82. } elseif (file_exists($this->destination . $dependencies_file)) {
  83. $this->config = Yaml::parse($this->destination . $dependencies_file);
  84. } else {
  85. $this->output->writeln('<red>ERROR</red> Missing .dependencies file in <cyan>user/</cyan> folder');
  86. }
  87. // If yaml config, process
  88. if ($this->config) {
  89. if (!$this->input->getOption('symlink')) {
  90. // Updates composer first
  91. $this->output->writeln("\nInstalling vendor dependencies");
  92. $this->output->writeln($this->composerUpdate(GRAV_ROOT, 'install'));
  93. $this->gitclone();
  94. } else {
  95. $this->symlink();
  96. }
  97. } else {
  98. $this->output->writeln('<red>ERROR</red> invalid YAML in ' . $dependencies_file);
  99. }
  100. }
  101. /**
  102. * Clones from Git
  103. */
  104. private function gitclone()
  105. {
  106. $this->output->writeln('');
  107. $this->output->writeln('<green>Cloning Bits</green>');
  108. $this->output->writeln('============');
  109. $this->output->writeln('');
  110. foreach ($this->config['git'] as $repo => $data) {
  111. <<<<<<< HEAD
  112. $path = $this->destination . DS . $data['path'];
  113. if (!file_exists($path)) {
  114. exec('cd "' . $this->destination . '" && git clone -b ' . $data['branch'] . ' ' . $data['url'] . ' ' . $data['path']);
  115. $this->output->writeln('<green>SUCCESS</green> cloned <magenta>' . $data['url'] . '</magenta> -> <cyan>' . $path . '</cyan>');
  116. =======
  117. $this->destination = rtrim($this->destination, DS);
  118. $path = $this->destination . DS . $data['path'];
  119. if (!file_exists($path)) {
  120. exec('cd "' . $this->destination . '" && git clone -b ' . $data['branch'] . ' ' . $data['url'] . ' ' . $data['path'], $output, $return);
  121. if (!$return) {
  122. $this->output->writeln('<green>SUCCESS</green> cloned <magenta>' . $data['url'] . '</magenta> -> <cyan>' . $path . '</cyan>');
  123. } else {
  124. $this->output->writeln('<red>ERROR</red> cloning <magenta>' . $data['url']);
  125. }
  126. >>>>>>> update grav cms
  127. $this->output->writeln('');
  128. } else {
  129. $this->output->writeln('<red>' . $path . ' already exists, skipping...</red>');
  130. $this->output->writeln('');
  131. }
  132. }
  133. }
  134. /**
  135. * Symlinks
  136. */
  137. private function symlink()
  138. {
  139. $this->output->writeln('');
  140. $this->output->writeln('<green>Symlinking Bits</green>');
  141. $this->output->writeln('===============');
  142. $this->output->writeln('');
  143. if (!$this->local_config) {
  144. $this->output->writeln('<red>No local configuration available, aborting...</red>');
  145. $this->output->writeln('');
  146. return;
  147. }
  148. exec('cd ' . $this->destination);
  149. foreach ($this->config['links'] as $repo => $data) {
  150. $from = $this->local_config[$data['scm'] . '_repos'] . $data['src'];
  151. $to = $this->destination . $data['path'];
  152. if (file_exists($from)) {
  153. if (!file_exists($to)) {
  154. symlink($from, $to);
  155. $this->output->writeln('<green>SUCCESS</green> symlinked <magenta>' . $data['src'] . '</magenta> -> <cyan>' . $data['path'] . '</cyan>');
  156. $this->output->writeln('');
  157. } else {
  158. $this->output->writeln('<red>destination: ' . $to . ' already exists, skipping...</red>');
  159. $this->output->writeln('');
  160. }
  161. } else {
  162. $this->output->writeln('<red>source: ' . $from . ' does not exists, skipping...</red>');
  163. $this->output->writeln('');
  164. }
  165. }
  166. }
  167. }