-
Notifications
You must be signed in to change notification settings - Fork 9
/
pipe-compile
executable file
·99 lines (76 loc) · 2.22 KB
/
pipe-compile
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
#!/usr/bin/env php
<?php
function includeIfExists($file)
{
if (file_exists($file)) {
return $file;
}
}
if ((!$loader = includeIfExists(__DIR__.'/../autoload.php')) && (!$loader = includeIfExists(__DIR__.'/../../../autoload.php'))) {
die('You must set up the project dependencies, run the following commands:'.PHP_EOL.
'curl -s http://getcomposer.org/installer | php'.PHP_EOL.
'php composer.phar install'.PHP_EOL);
}
require_once $loader;
use Ulrichsg\Getopt,
Pipe\Environment,
Pipe\Config;
$opts = new Getopt(array(
array("c", "config", Getopt::REQUIRED_ARGUMENT),
array('p', 'path', Getopt::REQUIRED_ARGUMENT),
array(null, 'debug', Getopt::NO_ARGUMENT),
array('C', 'compress', Getopt::NO_ARGUMENT),
array(null, 'js-compressor', Getopt::REQUIRED_ARGUMENT),
array(null, 'css-compressor', Getopt::REQUIRED_ARGUMENT),
));
function displayUsage()
{
return <<<EOT
Usage: pipe-compile [-c|--config <config>] [-p|--path <path>] [--js-compressor <compressor>] <file> [> <output_file>]
EOT;
}
try {
$opts->parse();
} catch (\InvalidArgumentException $e) {
fwrite(STDERR, displayUsage());
exit(255);
}
$files = $opts->getOperands();
if (count($files) < 1) {
fwrite(STDERR, displayUsage());
exit(255);
}
$paths = array_filter(explode(':', $opts->getOption("path")));
$configFile = $opts->getOption("config") ?: "pipe_config.yml";
if (file_exists($configFile)) {
$config = Config::fromYaml($configFile);
} else {
$config = new Config();
}
if ($cssCompressor = $opts->getOption("css-compressor")) {
$config->cssCompressor = $cssCompressor;
}
if ($jsCompressor = $opts->getOption("js-compressor")) {
$config->jsCompressor = $jsCompressor;
}
if ($opts->getOption("debug")) {
$config->debug = true;
}
$env = $config->createEnvironment();
$env->appendPath(getcwd());
foreach ($paths as $path) {
$env->appendPath($path);
}
$out = '';
foreach ($files as $file) {
$asset = $env->find($file, array("bundled" => true));
if (!$asset) {
fwrite(STDERR, "pipe-compile: Asset '$file' not found.\n");
exit(1);
}
$out .= $asset->getBody() . "\n";
}
if ($opts->getOption('compress')) {
$out = gzencode($out, 9);
}
echo $out;