forked from pattern-lab/patternlab-php-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFetch.php
More file actions
289 lines (229 loc) · 8.32 KB
/
Copy pathFetch.php
File metadata and controls
289 lines (229 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/*!
* Fetch Class
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Copy a package from GitHub and put it in it's appropriate location
*
*/
namespace PatternLab;
use \Alchemy\Zippy\Zippy;
use \PatternLab\Config;
use \PatternLab\Console;
use \PatternLab\Zippy\UnpackFileStrategy;
use \Symfony\Component\Filesystem\Filesystem;
use \Symfony\Component\Filesystem\Exception\IOExceptionInterface;
class Fetch {
public $rules = array();
/**
* Set-up a default var
*/
public function __construct() {
if (!is_dir(Config::$options["sourceDir"])) {
mkdir(Config::$options["sourceDir"]);
}
if (!is_dir(Config::$options["pluginDir"])) {
mkdir(Config::$options["pluginDir"]);
}
}
/**
* Fetch a package from GitHub
* @param {String} the command option to provide the rule for
* @param {String} the path to the package to be downloaded
*
* @return {String} the modified file contents
*/
public function fetch($commandOption = "", $package = "") {
$this->loadRules();
if (empty($commandOption)) {
$package = "";
$name = "";
// iterate over the rules and see if the current file matches one, if so run the rule
foreach ($this->rules as $rule) {
if ($package = Console::findCommandOptionValue($rule->shortCommand."|".$rule->longCommand)) {
$name = $rule->name;
$unpack = $rule->unpack;
$writeDir = $rule->writeDir;
break;
}
}
} else {
foreach ($this->rules as $rule) {
if ($rule->shortCommand == $commandOption) {
$name = $rule->name;
$unpack = $rule->unpack;
$writeDir = $rule->writeDir;
}
}
}
if (empty($name)) {
print_r(Console::$options);
print "fetch rule not found...\n";
exit;
}
// see if the user passed anythign useful
if (empty($package)) {
print "please provide a path for the ".$name." before trying to fetch it...\n";
exit;
}
// figure out the options for the GH path
list($org,$repo,$tag) = $this->getPackageInfo($package);
//get the path to the GH repo and validate it
$tarballUrl = "https://github.com/".$org."/".$repo."/archive/".$tag.".tar.gz";
print "downloading the ".$name."...\n";
// try to download the given package
if (!$package = @file_get_contents($tarballUrl)) {
$error = error_get_last();
print $name." wasn't downloaded because:\n\n ".$error["message"]."\n";
exit;
}
// write the package to the temp directory
$tempFile = tempnam(sys_get_temp_dir(), "pl-sk-archive.tar.gz");
file_put_contents($tempFile, $package);
print "installing the ".$name."...\n";
// see if the source directory is empty
$emptyDir = true;
$checkDir = (!$unpack) ? $writeDir."/".$repo."-".$tag : $writeDir;
if (is_dir($checkDir)) {
$objects = new \DirectoryIterator($checkDir);
foreach ($objects as $object) {
if (!$object->isDot() && ($object->getFilename() != "README") && ($object->getFilename() != ".DS_Store")) {
$emptyDir = false;
}
}
}
// if source directory isn't empty ask if it's ok to nuke what's there
if (!$emptyDir) {
$stdin = fopen("php://stdin", "r");
print($name." already installed. delete it to install or update the ".$name."? Y/n\n");
$answer = strtolower(trim(fgets($stdin)));
fclose($stdin);
if ($answer == "y") {
print "deleting the ".$name." and re-installing it...\n";
$objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($checkDir), \RecursiveIteratorIterator::CHILD_FIRST);
// make sure dots are skipped
$objects->setFlags(\FilesystemIterator::SKIP_DOTS);
foreach($objects as $filename => $object) {
if ($object->isDir()) {
rmdir($filename);
} else if ($object->isFile()) {
unlink($filename);
}
}
} else {
print "aborting install of the ".$name."...\n";
unlink($tempFile);
exit;
}
}
// extract, if the zip is supposed to be unpacked do that (e.g. stripdir)
$zippy = Zippy::load();
if ($unpack) {
$zippy->addStrategy(new UnpackFileStrategy());
}
$zipAdapter = $zippy->getAdapterFor('tar.gz');
$archiveZip = $zipAdapter->open($tempFile);
$archiveZip = $archiveZip->extract($writeDir);
// remove the temp file
unlink($tempFile);
// run composer against any composer.json file in the package
if (file_exists($checkDir."/composer.json")) {
// load composer
$composerConfig = json_decode(file_get_contents($checkDir."/composer.json"),true);
if ($jsonErrorMessage = JSON::hasError()) {
JSON::lastErrorMsg($filepath,$jsonErrorMessage,$listItems);
print "stopping install of ".$name."...";
exit;
}
// see if we should generate the vendor directory
if (!isset($composerConfig["extra"]) || (!isset($composerConfig["extra"]["runComposer"]) || ($composerConfig["extra"]["runComposer"])) {
print "running composer...\n";
$composerPath = __DIR__."/../../bin/composer.phar";
passthru("cd ".$checkDir." && php ".$composerPath." install");
}
// see if there are assets to be moved
if (isset($composerConfig["extra"]) && isset($composerConfig["extra"]["assets"])) {
if (isset($composerConfig["extra"]["assets"]["publicDir"])) {
$this->parseFileList($checkDir,Config::$options["publicDir"],$composerConfig["extra"]["assets"]["publicDir"]);
}
if (isset($composerConfig["extra"]["assets"]["sourceDir"])) {
$this->parseFileList($checkDir,Config::$options["sourceDir"],$composerConfig["extra"]["assets"]["sourceDir"]);
}
}
// see if we need to prompt the user to modify the config
if (isset($composerConfig["extra"]) && (isset($composerConfig["extra"]["configUpdate"]) && $composerConfig["extra"]["configUpdate"]) && (isset($composerConfig["extra"]["configOption"]) && $composerConfig["extra"]["configOption"]) && (isset($composerConfig["extra"]["configValue"]) && $composerConfig["extra"]["configValue"])) {
$stdin = fopen("php://stdin", "r");
print("make this package the default ".$name."? Y/n\n");
$answer = strtolower(trim(fgets($stdin)));
fclose($stdin);
if ($answer == "y") {
Config::update($composerConfig["extra"]["configOptions"],$composerConfig["extra"]["configValue"]);
print "config updated...\n";
} else {
print "config not updated...\n";
}
}
}
// move any assets to source/ (requirejs?)
print $name." installation complete...\n";
}
/**
* Break up the package path
* @param {String} path of the GitHub repo
*
* @return {Array} the parts of the package path
*/
protected function getPackageInfo($package) {
$org = "";
$repo = "";
$tag = "master";
if (strpos($package, "#") !== false) {
list($package,$tag) = explode("#",$package);
}
if (strpos($package, "/") !== false) {
list($org,$repo) = explode("/",$package);
} else {
print "please provide a real path to a package...\n";
exit;
}
return array($org,$repo,$tag);
}
/**
* Load all of the rules related to Fetch
*/
public function loadRules() {
foreach (glob(__DIR__."/Fetch/Rules/*.php") as $filename) {
$rule = str_replace(".php","",str_replace(__DIR__."/Fetch/Rules/","",$filename));
if ($rule[0] != "_") {
$ruleClass = "\PatternLab\Fetch\Rules\\".$rule;
$this->rules[] = new $ruleClass();
}
}
}
/**
* Move the files from the package to their location in the public dir or source dir
* @param {String} the base directory for the source of the files
* @param {String} the base directory for the destintation of the files (publicDir or sourceDir)
* @param {Array} the list of files to be moved
*/
protected function parseFileList($sourceBase,$destinationBase,$fileList) {
$fs = new Filesystem();
foreach ($fileList as $source => $destination) {
if ($source[strlen($source)-1] == "*") {
$source = rtrim($source,"/*");
$destination = rtrim($destination,"/*");
$fs->mirror($sourceBase.$source,$destintationBase.$destination);
} else {
$pathInfo = explode("/",$destination);
$file = array_pop($pathInfo);
$destinationDir = implode("/",$pathInfo);
if (!$fs->exists($destinationBase.$destinationDir)) {
$fs->mkdir($destinationBase.$destinationDir);
}
$fs->copy($sourceBase.$source,$destinationBase.$destination,true);
}
}
}
}