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