PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/lib/spark/spark_cli.php

http://github.com/ci-bonfire/Bonfire
PHP | 269 lines | 205 code | 40 blank | 24 comment | 33 complexity | 188806cc76a8465ae1b03dc72c8a9cb7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. require_once dirname(__FILE__) . '/spark_utils.php';
  3. require_once dirname(__FILE__) . '/spark_exception.php';
  4. require_once dirname(__FILE__) . '/spark_source.php';
  5. define('SPARK_VERSION', '0.0.9');
  6. ! defined('SPARK_PATH') AND define('SPARK_PATH', './sparks');
  7. class Spark_CLI {
  8. private static $commands = array(
  9. 'help' => 'help',
  10. 'install' => 'install',
  11. 'list' => 'lister',
  12. 'reinstall' => 'reinstall',
  13. 'remove' => 'remove',
  14. 'search' => 'search',
  15. 'sources' => 'sources',
  16. 'upgrade-system' => 'upgrade_system',
  17. 'version' => 'version',
  18. '' => 'index' // default action
  19. );
  20. function __construct($spark_sources)
  21. {
  22. $this->spark_sources = $spark_sources;
  23. }
  24. function execute($command, $args = array())
  25. {
  26. if (!array_key_exists($command, self::$commands))
  27. {
  28. $this->failtown("Unknown action: $command");
  29. return;
  30. }
  31. try
  32. {
  33. $method = self::$commands[$command];
  34. $this->$method($args);
  35. }
  36. catch (Exception $ex)
  37. {
  38. return $this->failtown($ex->getMessage());
  39. }
  40. }
  41. private function index($args)
  42. {
  43. Spark_utils::line('Spark (v' . SPARK_VERSION . ')');
  44. Spark_utils::line('For help: `php tools/spark help`');
  45. }
  46. private function upgrade_system() {
  47. $tool_dir = dirname(__FILE__) . '/../../';
  48. $tool_dir = realpath($tool_dir);
  49. // Get version data
  50. $source = $this->spark_sources[0];
  51. if (!$source) throw new Spark_exception('No sources listed - unsure how to upgrade');
  52. if (!$source->outdated()) // We have an acceptable version
  53. {
  54. Spark_utils::warning('Spark manager is already up to date');
  55. return;
  56. }
  57. // Build a spark and download it
  58. $data = null;
  59. $data->name = 'Spark Manager';
  60. $data->archive_url = $source->version_data->spark_manager_download_url;
  61. $zip_spark = new Zip_spark($data);
  62. $zip_spark->retrieve();
  63. // Download the new version
  64. // Remove the lib directory and the spark
  65. unlink($tool_dir . '/spark');
  66. Spark_utils::remove_full_directory($tool_dir . '/lib');
  67. // Link up the new version
  68. Spark_utils::full_move($zip_spark->temp_path . '/lib', $tool_dir . '/lib');
  69. @rename($zip_spark->temp_path . '/spark', $tool_dir . '/spark');
  70. @`chmod u+x {$tool_dir}/spark`;
  71. // Tell the user the story of what just happened
  72. Spark_utils::notice('Spark manager has been upgraded to ' . $source->version . '!');
  73. }
  74. // list the installed sparks
  75. private function lister()
  76. {
  77. if (!is_dir(SPARK_PATH)) return; // no directory yet
  78. foreach(scandir(SPARK_PATH) as $item)
  79. {
  80. if (!is_dir(SPARK_PATH . "/$item") || $item[0] == '.') continue;
  81. foreach (scandir(SPARK_PATH . "/$item") as $ver)
  82. {
  83. if (!is_dir(SPARK_PATH . "/$item/$ver") || $ver[0] == '.') continue;
  84. Spark_utils::line("$item ($ver)");
  85. }
  86. }
  87. }
  88. private function version()
  89. {
  90. Spark_utils::line(SPARK_VERSION);
  91. }
  92. private function help()
  93. {
  94. Spark_utils::line('install # Install a spark');
  95. Spark_utils::line('reinstall # Reinstall a spark');
  96. Spark_utils::line('remove # Remove a spark');
  97. Spark_utils::line('list # List installed sparks');
  98. Spark_utils::line('search # Search for a spark');
  99. Spark_utils::line('sources # Display the spark source URL(s)');
  100. Spark_utils::line('upgrade-system # Update Sparks Manager to latest version (does not upgrade any of your installed sparks)');
  101. Spark_utils::line('version # Display the installed spark version');
  102. Spark_utils::line('help # Print This message');
  103. }
  104. private function search($args)
  105. {
  106. $term = implode($args, ' ');
  107. foreach($this->spark_sources as $source)
  108. {
  109. $results = $source->search($term);
  110. foreach ($results as $result)
  111. {
  112. $result_line = "\033[33m$result->name\033[0m - $result->summary";
  113. // only show the source information if there are multiple sources
  114. if (count($this->spark_sources) > 1) $result_line .= " (source: $source->url)";
  115. Spark_utils::line($result_line);
  116. }
  117. }
  118. }
  119. private function sources()
  120. {
  121. foreach($this->spark_sources as $source)
  122. {
  123. Spark_utils::line($source->get_url());
  124. }
  125. }
  126. private function failtown($error_message)
  127. {
  128. Spark_utils::error('Uh-oh!');
  129. Spark_utils::error($error_message);
  130. }
  131. private function remove($args)
  132. {
  133. list($flats, $flags) = $this->prep_args($args);
  134. if (count($flats) != 1)
  135. {
  136. return $this->failtown('Which spark do you want to remove?');
  137. }
  138. $spark_name = $flats[0];
  139. $version = array_key_exists('v', $flags) ? $flags['v'] : null;
  140. // figure out what to remove and make sure its isntalled
  141. $dir_to_remove = SPARK_PATH . ($version == null ? "/$spark_name" : "/$spark_name/$version");
  142. if (!file_exists($dir_to_remove))
  143. {
  144. return Spark_utils::warning('Looks like that spark isn\'t installed');
  145. }
  146. if ($version == null && !array_key_exists('f', $flags))
  147. {
  148. throw new Spark_exception("Please specify a version (spark remove -v1.0.0 foo) or remove all with -f");
  149. }
  150. Spark_utils::notice("Removing $spark_name (" . ($version ? $version : 'ALL') . ") from $dir_to_remove");
  151. if (Spark_utils::remove_full_directory($dir_to_remove, true))
  152. {
  153. Spark_utils::notice('Spark removed successfully!');
  154. }
  155. else
  156. {
  157. Spark_utils::warning('Looks like that spark isn\'t installed');
  158. }
  159. // attempt to clean up - will not remove unless empty
  160. @rmdir(SPARK_PATH . "/$spark_name");
  161. }
  162. private function install($args)
  163. {
  164. list($flats, $flags) = $this->prep_args($args);
  165. if (count($flats) != 1)
  166. {
  167. return $this->failtown('format: `spark install -v1.0.0 name`');
  168. }
  169. $spark_name = $flats[0];
  170. $version = array_key_exists('v', $flags) ? $flags['v'] : 'HEAD';
  171. // retrieve the spark details
  172. foreach ($this->spark_sources as $source)
  173. {
  174. Spark_utils::notice("Retrieving spark detail from " . $source->get_url());
  175. $spark = $source->get_spark_detail($spark_name, $version);
  176. if ($spark != null) break;
  177. }
  178. // did we find the details?
  179. if ($spark == null)
  180. {
  181. throw new Spark_exception("Unable to find spark: $spark_name ($version) in any sources");
  182. }
  183. // verify the spark, and put out warnings if needed
  184. $spark->verify();
  185. // retrieve the spark
  186. Spark_utils::notice("From Downtown! Retrieving spark from " . $spark->location_detail());
  187. $spark->retrieve();
  188. // Install it
  189. $spark->install();
  190. Spark_utils::notice('Spark installed to ' . $spark->installed_path() . ' - You\'re on fire!');
  191. }
  192. private function reinstall($args)
  193. {
  194. list($flats, $flags) = $this->prep_args($args);
  195. if (count($flats) != 1)
  196. {
  197. return $this->failtown('format: `spark reinstall -v1.0.0 name`');
  198. }
  199. $spark_name = $flats[0];
  200. $version = array_key_exists('v', $flags) ? $flags['v'] : null;
  201. if ($version == null && !array_key_exists('f', $flags))
  202. {
  203. throw new Spark_exception("Please specify a version to reinstall, or use -f to remove all versions and install latest.");
  204. }
  205. $this->remove($args);
  206. $this->install($args);
  207. }
  208. /**
  209. * Prepares the command line arguments for use.
  210. *
  211. * Usage:
  212. * list($flats, $flags) = $this->prep_args($args);
  213. *
  214. * @param array the arguments array
  215. * @return array the flats and flags
  216. */
  217. private function prep_args($args)
  218. {
  219. $flats = array();
  220. $flags = array();
  221. foreach($args as $arg)
  222. {
  223. preg_match('/^(\-?[a-zA-Z])([^\s]*)$/', $arg, $matches);
  224. if (count($matches) != 3) continue;
  225. $matches[0][0] == '-' ? $flags[$matches[1][1]] = $matches[2] : $flats[] = $matches[0];
  226. }
  227. return array($flats, $flags);
  228. }
  229. }