forked from arian/amd-packager-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packager-cli.php
executable file
·108 lines (88 loc) · 2.54 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
#!/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;
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"
. " --output The file the output should be written to\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"
. "\n";
exit;
}
function warn($message){
$std_err = fopen('php://stderr', 'w');
fwrite($std_err, $message);
fclose($std_err);
}
$requires = array();
for ($i = 0, $l = count($args); $i < $l; $i++){
$arg = $args[$i];
switch ($arg){
case '-h': case '--help': help(); break;
case '--options':
$options_file = $args[++$i];
break;
case '--output':
$output_file = $args[++$i];
break;
case '--graph':
$graph_file = $args[++$i];
$method = 'graph';
break;
case '--modules': case '--list':
$method = 'modules';
break;
case '--dependencies':
$method = 'dependencies';
break;
default:
$requires[] = $arg;
break;
}
}
if (empty($requires)) help();
$packager = new Packager;
$options = $options_file ? (include $options_file) : array();
$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');
$builder = $packager->req($requires);
if ($method == 'output' || $method == 'modules'){
warn("\nLoaded Modules:\n " . implode("\n ", $builder->modules()) . "\n\n");
}
if ($method == 'output'){
$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");
}