forked from kamicane/packager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
packager.php
355 lines (270 loc) · 10.5 KB
/
packager.php
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
require dirname(__FILE__) . "/helpers/yaml.php";
require dirname(__FILE__) . "/helpers/array.php";
Class Packager {
public static function warn($message){
$std_err = fopen('php://stderr', 'w');
fwrite($std_err, $message);
fclose($std_err);
}
private $packages = array();
private $manifests = array();
private $root = null;
public function __construct($package_paths){
foreach ((array)$package_paths as $package_path) $this->parse_manifest($package_path);
}
private function parse_manifest($path){
$pathinfo = pathinfo($path);
if (is_dir($path)){
$package_path = $pathinfo['dirname'] . '/' . $pathinfo['basename'] . '/';
if (file_exists($package_path . 'package.yml')){
$manifest_path = $package_path . 'package.yml';
$manifest_format = 'yaml';
} else if (file_exists($package_path . 'package.yaml')){
$manifest_path = $package_path . 'package.yaml';
$manifest_format = 'yaml';
} else if (file_exists($package_path . 'package.json')){
$manifest_path = $package_path . 'package.json';
$manifest_format = 'json';
}
} else if (file_exists($path)){
$package_path = $pathinfo['dirname'] . '/';
$manifest_path = $package_path . $pathinfo['basename'];
$manifest_format = $pathinfo['extension'];
}
if ($manifest_format == 'json') $manifest = json_decode(file_get_contents($manifest_path), true);
else if ($manifest_format == 'yaml' || $manifest_format == 'yml') $manifest = YAML::decode_file($manifest_path);
if (empty($manifest)) throw new Exception("manifest not found in $package_path, or unable to parse manifest.");
$package_name = $manifest['name'];
if ($this->root == null) $this->root = $package_name;
if (array_has($this->manifests, $package_name)) return;
$manifest['path'] = $package_path;
$manifest['manifest'] = $manifest_path;
$this->manifests[$package_name] = $manifest;
foreach ($manifest['sources'] as $i => $path){
$path = $package_path . $path;
// this is where we "hook" for possible other replacers.
$source = $this->replace_build($package_path, file_get_contents($path));
$descriptor = array();
// get contents of first comment
preg_match('/\/\*\s*^---(.*?)^\.\.\.\s*\*\//ms', $source, $matches);
if (!empty($matches)) $descriptor = YAML::decode($matches[0]);
// populate / convert to array requires and provides
$requires = (array)(!empty($descriptor['requires']) ? $descriptor['requires'] : array());
$provides = (array)(!empty($descriptor['provides']) ? $descriptor['provides'] : array());
$file_name = !empty($descriptor['name']) ? $descriptor['name'] : basename($path, '.js');
// "normalization" for requires. Fills up the default package name from requires, if not present.
foreach ($requires as $i => $require)
$requires[$i] = implode('/', $this->parse_name($package_name, $require));
$license = array_get($descriptor, 'license');
$this->packages[$package_name][$file_name] = array_merge($descriptor, array(
'package' => $package_name,
'requires' => $requires,
'provides' => $provides,
'source' => $source,
'path' => $path,
'package/name' => $package_name . '/' . $file_name,
'license' => empty($license) ? array_get($manifest, 'license') : $license
));
}
}
public function add_package($package_path){
$this->parse_manifest($package_path);
}
public function remove_package($package_name){
unset($this->packages[$package_name]);
unset($this->manifests[$package_name]);
}
// # private UTILITIES
private function parse_name($default, $name){
$exploded = explode('/', $name);
$length = count($exploded);
if ($length == 1) return array($default, $exploded[0]);
if (empty($exploded[0])) return array($default, $exploded[1]);
$package = explode(':', $exploded[0]);
//if (!empty($package[1])) $version = $package[1];
return array($package[0], $exploded[1]);
}
private function replace_build($package_path, $file){
$ref = @file_get_contents($package_path . '.git/HEAD');
if (empty($ref)) return $file;
preg_match("@ref: ([\w\./-]+)@", $ref, $matches);
if (!empty($matches)) $ref = @file_get_contents($package_path . ".git/" . $matches[1]);
if (empty($ref)) return $file;
preg_match("@([\w\./-]+)@", $ref, $matches);
return str_replace("%build%", $matches[1], $file);
}
// # private HASHES
private function component_to_hash($name){
$pair = $this->parse_name($this->root, $name);
$package = array_get($this->packages, $pair[0]);
if (!empty($package)){
$component = $pair[1];
foreach ($package as $file => $data){
foreach ($data['provides'] as $c){
if ($c == $component) return $data;
}
}
}
return null;
}
private function file_to_hash($name){
$pair = $this->parse_name($this->root, $name);
$package = array_get($this->packages, $pair[0]);
if (!empty($package)){
$file_name = $pair[1];
foreach ($package as $file => $data){
if ($file == $file_name) return $data;
}
}
return null;
}
public function file_exists($name){
return $this->file_to_hash($name) ? true : false;
}
public function component_exists($name){
return $this->component_to_hash($name) ? true : false;
}
public function package_exists($name){
return array_contains($this->get_packages(), $name);
}
public function validate($more_files = array(), $more_components = array(), $more_packages = array()){
foreach ($this->packages as $name => $files){
foreach ($files as $file){
$file_requires = $file['requires'];
foreach ($file_requires as $component){
if (!$this->component_exists($component)){
self::warn("WARNING: The component $component, required in the file " . $file['package/name'] . ", has not been provided.\n");
}
}
}
}
foreach ($more_files as $file){
if (!$this->file_exists($file)) self::warn("WARNING: The required file $file could not be found.\n");
}
foreach ($more_components as $component){
if (!$this->component_exists($component)) self::warn("WARNING: The required component $component could not be found.\n");
}
foreach ($more_packages as $package){
if (!$this->package_exists($package)) self::warn("WARNING: The required package $package could not be found.\n");
}
}
// # public BUILD
public function build($files = array(), $components = array(), $packages = array(), $blocks = array()){
if (!empty($components)){
$more = $this->components_to_files($components);
foreach ($more as $file) array_include($files, $file);
}
foreach ($packages as $package){
$more = $this->get_all_files($package);
foreach ($more as $file) array_include($files, $file);
}
$files = $this->complete_files($files);
if (empty($files)) return '';
$included_sources = array();
foreach ($files as $file) $included_sources[] = $this->get_file_source($file);
$source = implode($included_sources, "\n\n");
foreach ($blocks as $block){
$source = preg_replace_callback("%(/[/*])\s*<$block>(.*?)</$block>(?:\s*\*/)?%s", array($this, "block_replacement"), $source);
}
return $source . "\n";
}
private function block_replacement($matches){
return (strpos($matches[2], ($matches[1] == "//") ? "\n" : "*/") === false) ? $matches[2] : "";
}
public function build_from_files($files){
return $this->build($files);
}
public function build_from_components($components){
return $this->build(array(), $components);
}
public function write_from_files($file_name, $files = null){
$full = $this->build_from_files($files);
file_put_contents($file_name, $full);
}
public function write_from_components($file_name, $components = null){
$full = $this->build_from_components($components);
file_put_contents($file_name, $full);
}
// # public FILES
public function get_all_files($of_package = null){
$files = array();
foreach ($this->packages as $name => $package){
if ($of_package == null || $of_package == $name) foreach ($package as $file){
$files[] = $file['package/name'];
}
}
return $files;
}
public function get_file_dependancies($file){
$hash = $this->file_to_hash($file);
if (empty($hash)) return array();
return $this->complete_files($this->components_to_files($hash['requires']));
}
public function complete_file($file){
$files = $this->get_file_dependancies($file);
$hash = $this->file_to_hash($file);
if (empty($hash)) return array();
array_include($files, $hash['package/name']);
return $files;
}
public function complete_files($files){
$ordered_files = array();
foreach ($files as $file){
$all_files = $this->complete_file($file);
foreach ($all_files as $one_file) array_include($ordered_files, $one_file);
}
return $ordered_files;
}
// # public COMPONENTS
public function component_to_file($component){
return array_get($this->component_to_hash($component), 'package/name');
}
public function components_to_files($components){
$files = array();
foreach ($components as $component){
$file_name = $this->component_to_file($component);
if (!empty($file_name) && !in_array($file_name, $files)) $files[] = $file_name;
}
return $files;
}
// # dynamic getter for PACKAGE properties and FILE properties
public function __call($method, $arguments){
if (strpos($method, 'get_file_') === 0){
$file = array_get($arguments, 0);
if (empty($file)) return null;
$key = substr($method, 9);
$hash = $this->file_to_hash($file);
return array_get($hash, $key);
}
if (strpos($method, 'get_package_') === 0){
$key = substr($method, 12);
$package = array_get($arguments, 0);
$package = array_get($this->manifests, (empty($package)) ? $this->root : $package);
return array_get($package, $key);
}
return null;
}
public function get_packages(){
return array_keys($this->packages);
}
// authors normalization
public function get_package_authors($package = null){
if (empty($package)) $package = $this->root;
$package = array_get($this->manifests, $package);
if (empty($package)) return array();
return $this->normalize_authors(array_get($package, 'authors'), array_get($package, 'author'));
}
public function get_file_authors($file){
$hash = $this->file_to_hash($file);
if (empty($hash)) return array();
return $this->normalize_authors(array_get($hash, 'authors'), array_get($hash, 'author'), $this->get_package_authors());
}
private function normalize_authors($authors = null, $author = null, $default = null){
$use = empty($authors) ? $author : $authors;
if (empty($use) && !empty($default)) return $default;
if (is_array($use)) return $use;
if (empty($use)) return array();
return array($use);
}
}