-
Notifications
You must be signed in to change notification settings - Fork 2
/
packager-cli.php
executable file
·199 lines (173 loc) · 4.69 KB
/
packager-cli.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
#!/usr/bin/env php
<?php
include_once dirname(__FILE__) . '/lib/Packager.php';
$args = array_slice($argv, 1);
$options_file = null;
$method = 'output';
$output_file = null;
$graph_file = null;
$watch = false;
$group = false;
function help(){
echo "\npackager-cli.php [options] <modules>\n\n"
. "Options:\n"
. " -h --help Show this help\n"
. " --options Specify another options file (defaults to options.php)\n"
. " --has [features] Features for the has() api. Example: `--has feature=0 ie=1`\n"
. " --require [modules] Require these modules\n"
. " --exclude [modules] Exclude these modues\n"
. " -o --output The file the output should be written to\n"
. " --group Group ouput by package\n"
. " --modules --list List the modules\n"
. " --dependencies List the dependencies map\n"
. " --graph Create a structural dependency graph\n"
. " and write it to this file\n"
. " --watch Watches the required modules\n"
. "\n";
exit;
}
function warn($message){
$std_err = fopen('php://stderr', 'w');
fwrite($std_err, $message);
fclose($std_err);
}
$has = array();
$_has = false;
$requires = array();
$excludes = array();
$_exclude = false;
for ($i = 0, $l = count($args); $i < $l; $i++){
$arg = $args[$i];
if (substr($arg, 0, 1) == '-'){
$_has = false;
$_exclude = false;
}
switch ($arg){
case '-h': case '--help': help(); break;
case '--options':
$options_file = $args[++$i];
break;
case '--output': case '-o':
$output_file = $args[++$i];
break;
case '--group':
$group = true;
break;
case '--has':
$_has = true;
break;
case '--require':
break;
case '--exclude':
$_exclude = true;
break;
case '--graph':
$graph_file = $args[++$i];
$method = 'graph';
break;
case '--modules': case '--list':
$method = 'modules';
break;
case '--dependencies':
$method = 'dependencies';
break;
case '--watch':
$watch = true;
break;
default:
if ($_has){
$feature = explode('=', $arg);
$has[$feature[0]] = isset($feature[1])
? ($feature[1] == 'false' || $feature[1] == '0'
? false : true)
: true;
} elseif ($_exclude){
$excludes[] = $arg;
} else {
$requires[] = $arg;
}
break;
}
}
if (empty($requires)) help();
$options = $options_file ? (include $options_file) : array();
if (!empty($options['has'])) $has = array_merge($options['has'], $has);
function packager(){
global $requires, $options;
$packager = new Packager;
$packager->setBaseUrl(
isset($options['baseurl']) ? $options['baseurl'] : getcwd()
);
if (isset($options['paths'])) foreach ($options['paths'] as $alias => $path){
$packager->addAlias($alias, $path);
}
if (!empty($options['loader'])) array_unshift($requires, dirname(__FILE__) . '/loader.js');
return $packager;
}
function build($builder){
global $method, $has, $excludes, $requires, $output_file, $graph_file, $group;
if (!empty($excludes)){
$builder->reduce($requires, $excludes);
}
if ($method == 'output' || $method == 'modules'){
warn("\nLoaded Modules:\n " . implode("\n ", $builder->modules()) . "\n\n");
}
$builder->addHas($has);
if ($method == 'output'){
if ($group && $output_file){
$output = $builder->outputByPackage();
foreach ($output as $_package => $_content){
$filename = $_package . '.js';
if (Path::extname($_package) == '.js') {
// not an AMD module
$filename = Path::filename($_package);
}
file_put_contents(Path::resolve($output_file, $filename) , $_content);
}
} else {
$output = $builder->output();
if ($output_file) file_put_contents ($output_file, $output);
else echo $output;
}
} elseif ($method == 'dependencies'){
$modules = $builder->dependencies();
$str = '';
foreach ($modules as $id => $deps){
$str .= "\n " . $id;
foreach ($deps as $dep){
$str .= "\n - " . $dep;
}
}
$str .= "\n\n";
warn($str);
}
if ($graph_file){
include_once dirname(__FILE__) . '/lib/Graph.php';
$graph = new Packager_Graph($builder);
$graph->output($graph_file);
warn("The dependency graph has been written to '" . $graph_file . "'\n");
}
}
$builder = packager()->req($requires);
if ($watch){
$files = $builder->files();
$times = array();
while (true){
foreach ($files as $file){
$time = filemtime($file);
if (empty($times[$file])){
$times[$file] = $time;
} elseif ($time != $times[$file]){
$times[$file] = $time;
warn("\nThe file '" . $file . "' has changed\n");
$builder = packager()->req($requires);
$files = $builder->files();
build($builder);
break;
}
}
sleep(1);
}
} else {
build($builder);
}