forked from markstory/asset_compress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssetCompressShell.php
208 lines (188 loc) · 5.19 KB
/
AssetCompressShell.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
<?php
App::uses('AppShell', 'Console/Command');
App::uses('AssetConfig', 'AssetCompress.Lib');
App::uses('AssetCompiler', 'AssetCompress.Lib');
App::uses('AssetCache', 'AssetCompress.Lib');
App::uses('Folder', 'Utility');
/**
* Asset Compress Shell
*
* Assists in clearing and creating the build files this plugin makes.
*
*/
class AssetCompressShell extends AppShell {
public $tasks = array('AssetCompress.AssetBuild');
/**
* Create the configuration object used in other classes.
*
*/
public function startup() {
parent::startup();
AssetConfig::clearAllCachedKeys();
$this->_Config = AssetConfig::buildFromIniFile($this->params['config']);
$this->AssetBuild->setThemes($this->_findThemes());
$this->out();
}
/**
* Builds all the files defined in the build file.
*
* @return void
*/
public function build() {
$this->out('Building files defined in the ini file');
$this->hr();
$this->build_ini();
$this->out();
$this->out('Building files in views');
$this->hr();
$this->build_dynamic();
}
public function build_ini() {
$this->AssetBuild->setConfig($this->_Config);
$this->AssetBuild->buildIni();
}
public function build_dynamic() {
$this->AssetBuild->setConfig($this->_Config);
$viewpaths = App::path('View');
$this->AssetBuild->buildDynamic($viewpaths);
}
/**
* Clears the build directories for both CSS and JS
*
* @return void
*/
public function clear() {
$this->clear_build_ts();
$this->out('Clearing Javascript build files:');
$this->hr();
$this->_clearBuilds('js');
$this->out('');
$this->out('Clearing CSS build files:');
$this->hr();
$this->_clearBuilds('css');
$this->out();
$this->out('<success>Complete</success>');
}
/**
* Clears out all the cache keys associated with asset_compress.
*
* Note: method really does nothing here because keys are cleared in startup.
* This method exists for times when you just want to clear the cache keys
* associated with asset_compress
*/
public function clear_cache() {
$this->out('Clearing all cache keys:');
$this->hr();
}
/**
* Clears the build timestamp. Try to clear it out even if they do not have ts file enabled in
* the INI.
*
* build timestamp file is only created when build() is run from this shell
*/
public function clear_build_ts() {
$this->out('Clearing build timestamp.');
$this->out();
AssetConfig::clearBuildTimeStamp();
}
/**
* clear the builds for a specific extension.
*
* @return void
*/
protected function _clearBuilds($ext) {
$themes = $this->_findThemes();
$targets = $this->_Config->targets($ext);
if (empty($targets)) {
$this->err('No ' . $ext . ' build files defined, skipping');
return;
}
$this->_clearPath(TMP, $themes, $targets);
$path = $this->_Config->cachePath($ext);
if (!file_exists($path)) {
$this->err('Build directory ' . $path . ' for ' . $ext . ' does not exist.');
return;
}
$this->_clearPath($path, $themes, $targets);
}
/**
* Clear a path of build targets.
*
* @param string $path The path to clear.
* @param array $themes The themes to clear.
* @param array $targets The build targets to clear.
* @return void
*/
protected function _clearPath($path, $themes, $targets) {
$dir = new DirectoryIterator($path);
foreach ($dir as $file) {
$name = $base = $file->getFilename();
if (in_array($name, array('.', '..'))) {
continue;
}
// timestampped files.
if (preg_match('/^(.*)\.v\d+(\.[a-z]+)$/', $name, $matches)) {
$base = $matches[1] . $matches[2];
}
// themed files
foreach ($themes as $theme) {
if (strpos($base, $theme) === 0) {
list($themePrefix, $base) = explode('-', $base);
}
}
if (in_array($base, $targets)) {
$this->out(' - Deleting ' . $path . $name);
unlink($path . $name);
continue;
}
}
}
/**
* Find all the themes in an application.
* This is used to generate theme asset builds.
*
* @return array Array of theme names.
*/
protected function _findThemes() {
$viewpaths = App::path('View');
$themes = array();
foreach ($viewpaths as $path) {
if (is_dir($path . 'Themed')) {
$Folder = new Folder($path . 'Themed');
list($dirs, $files) = $Folder->read(false, true);
$themes = array_merge($themes, $dirs);
}
}
return $themes;
}
/**
* get the option parser.
*
* @return void
*/
public function getOptionParser() {
$parser = parent::getOptionParser();
return $parser->description(array(
'Asset Compress Shell',
'',
'Builds and clears assets defined in your asset_compress.ini',
'file and in your view files.'
))->addSubcommand('clear', array(
'help' => 'Clears all builds defined in the ini file.'
))->addSubcommand('build', array(
'help' => 'Generate all builds defined in the ini and view files.'
))->addSubcommand('build_ini', array(
'help' => 'Generate only build files defined in the ini file.'
))->addSubcommand('build_dynamic', array(
'help' => 'Build build files defined in view files.'
))->addOption('config', array(
'help' => 'Choose the config file to use.',
'short' => 'c',
'default' => APP . 'Config' . DS . 'asset_compress.ini'
))->addOption('force', array(
'help' => 'Force assets to rebuild. Ignores timestamp rules.',
'short' => 'f',
'boolean' => true
));
}
}