-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoboFile.php
403 lines (367 loc) · 13.4 KB
/
RoboFile.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
use Cake\Event\EventDispatcherTrait;
use Elephfront\RoboCssMinify\Task\Loader\LoadCssMinifyTasksTrait;
use Elephfront\RoboImportJs\Task\Loader\LoadImportJavascriptTasksTrait;
use Elephfront\RoboJsMinify\Task\Loader\LoadJsMinifyTasksTrait;
use Elephfront\RoboLiveReload\Task\Loader\LoadLiveReloadTaskTrait;
use Elephfront\RoboSass\Task\Loader\LoadSassTaskTrait;
use Robo\Tasks;
/**
* This is the Robo commands file.
*/
class RoboFile extends Tasks
{
use EventDispatcherTrait {
dispatchEvent as protected;
eventManager as protected;
}
use LoadCssMinifyTasksTrait;
use LoadImportJavascriptTasksTrait;
use LoadJsMinifyTasksTrait;
use LoadSassTaskTrait;
use LoadLiveReloadTaskTrait;
/**
* Filepath of the user configuration
*
* @var string
*/
const ELEPHFRONT_CONFIG = 'elephfront-config.php';
/**
* Filepath of the bootstrap (useful to bind events listener for instance)
*
* @var string
*/
const ELEPHFRONT_BOOTSTRAP = 'elephfront-bootstrap.php';
/**
* Configuration array.
*
* @var array
*/
protected $config = [];
/**
* RoboFile constructor.
*
* Will load the basic configuration and the merge it with the user configuration (if any).
*/
public function __construct()
{
$this->config = $this->loadDefaultConfig();
if (is_file(self::ELEPHFRONT_BOOTSTRAP)) {
include self::ELEPHFRONT_BOOTSTRAP;
}
// We store it in a variable so the included file has access to the existing configuration if needed
$config = $this->config;
if (is_file(self::ELEPHFRONT_CONFIG)) {
$userConfig = include self::ELEPHFRONT_CONFIG;
$this->config = $this->merge($this->config, $userConfig);
}
}
/**
* Load the default configuration.
*
* @return array
*/
protected function loadDefaultConfig()
{
$source = 'src' . DIRECTORY_SEPARATOR;
$build = 'build' . DIRECTORY_SEPARATOR;
return [
'paths' => [
'source' => $source,
'build' => $build
],
'sources' => [
'css' => $source . 'assets' . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR,
'js' => $source . 'assets' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR,
'directories' => [
'pages' => $source . 'pages' . DIRECTORY_SEPARATOR,
'system' => $source . 'system' . DIRECTORY_SEPARATOR
]
],
'compile' => [
'css' => [
$source . 'assets' . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'main.scss' => $build . 'assets' . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'main.min.css'
],
'js' => [
$source . 'assets' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . 'main.js' => $build . 'assets' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . 'main.min.js'
],
'directories' => [
$source . 'pages' . DIRECTORY_SEPARATOR => $build . 'pages' . DIRECTORY_SEPARATOR,
$source . 'system' . DIRECTORY_SEPARATOR => $build . 'system' . DIRECTORY_SEPARATOR
]
],
'router' => $build . 'system' . DIRECTORY_SEPARATOR . 'router.php'
];
}
/**
* Serve task
* - will build the entire project
* - will start the PHP Built-in server to serve the pages
* - will open the browser with the location of the locale PHP built-in server
* - will start the WebSocket server to enable the live-reload
* - will start watching for changes in the source CSS, JS and pages files
*
* @param array $opts Array of options. Two options are supported :
* - *host* : the name of the host the server should respond to
* - *port* : the port binded to the host
*
* @return void
*/
public function serve($opts = ['host' => 'localhost', 'port' => 9876])
{
$this->build();
$this->taskLiveReload()->run();
$this->startServer($opts['host'], $opts['port']);
$this->startBrowser($opts['host'], $opts['port']);
$this->startWatch();
}
/**
* Start the built-in PHP Server in the background.
* Once started, the URL "http://$host:$port" will respond to the server.
*
* @param string $host The name of the host the server should respond to
* @param int $port The port binded to the host
* @return void
*/
protected function startServer($host = 'localhost', $port = 9876)
{
$this->taskServer($port)
->background()
->host($host)
->arg($this->config['router'])
->dir($this->config['paths']['build'])
->run();
}
/**
* Opens the browser at the host and port location for the `startServer()` task.
*
* @param string $host The name of the host the server should respond to
* @param int $port The port binded to the host
* @return void
*/
protected function startBrowser($host = 'localhost', $port = 9876)
{
$url = sprintf('http://%s:%d', $host, $port);
$this->taskOpenBrowser($url)
->run();
}
/**
* Starts the watch for the CSS and JS folders. Any changes in those folders will trigger the assets compilation
* tasks, as well as send a message to the WebSocket server.
*
* @return void
*/
protected function startWatch()
{
$this->taskWatch()
->monitor($this->config['sources']['css'], function() {
$this->compileScss(true);
$this->taskLiveReload()->sendReloadMessage();
})
->monitor($this->config['sources']['js'], function() {
$this->compileJs();
$this->taskLiveReload()->sendReloadMessage();
})
->monitor($this->config['sources']['directories'], function() {
$this->copyDirectories();
$this->taskLiveReload()->sendReloadMessage();
})
->run();
}
/**
* Build the entire `build` directory from the `src` directory
*
* @return void
*/
public function build()
{
if (is_dir($this->config['paths']['build'])) {
$this->taskCleanDir([$this->config['paths']['build']])->run();
}
$this->copyDirectories();
$this->compileAssets();
}
/**
* Copy the pages sub-directory to the build directory
*
* @return void
*/
public function copyDirectories()
{
$directoriesSourceMap = $this->config['compile']['directories'];
foreach ($directoriesSourceMap as $source => $build) {
$this->prepareBuildDir(dirname($build));
}
$this
->taskCopyDir($directoriesSourceMap)
->run();
}
/**
* Compile all of the assets source to a minified build.
*
* Will execute the following commands :
*
* - compileCss
* - compileJs
*
* @return void
*/
public function compileAssets()
{
$this->compileScss();
$this->compileJs();
}
/**
* Executes the compilation of CSS assets
*
* @param bool $disableEvents Whether events should be dispatched or not. Default to false.
* @return void
*/
public function compileScss($disableEvents = false)
{
$cssSourceMap = $this->config['compile']['css'];
if ($disableEvents !== true) {
$event = new \Cake\Event\Event('Elephfront.Scss.beforeCompile', $this, [
'sourceMap' => $cssSourceMap,
'config' => $this->config
]);
$this->eventManager()->dispatch($event);
}
$collection = $this->collectionBuilder();
$collection
->taskSass()
->setDestinationsMap($cssSourceMap)
->taskCssMinify()
->run();
if ($disableEvents !== true) {
$event = new \Cake\Event\Event('Elephfront.Scss.afterCompile', $this, [
'sourceMap' => $cssSourceMap,
'config' => $this->config
]);
$this->eventManager()->dispatch($event);
}
}
/**
* Executes the compilation of JS assets
*
* @param bool $disableEvents Whether events should be dispatched or not. Default to false.
* @return void
*/
public function compileJs($disableEvents = false)
{
$jsSourceMap = $this->config['compile']['js'];
if ($disableEvents !== true) {
$event = new \Cake\Event\Event('Elephfront.Js.beforeCompile', $this, [
'sourceMap' => $jsSourceMap,
'config' => $this->config
]);
$this->eventManager()->dispatch($event);
}
$collection = $this->collectionBuilder();
$collection
->taskImportJavascript()
->setDestinationsMap($jsSourceMap)
->disableWriteFile()
->taskJsMinify()
->run();
if ($disableEvents !== true) {
$event = new \Cake\Event\Event('Elephfront.Js.afterCompile', $this, [
'sourceMap' => $jsSourceMap,
'config' => $this->config
]);
$this->eventManager()->dispatch($event);
}
}
/**
* Will prepare the build directory. If no target is specified, the entire build directory will be wiped.
* If the build preparation should be done for a specific target (e.g. only for building CSS files), the parameter
* target should be set as a string, being the name of the target (e.g. 'css').
*
* @param bool|string $target False if no specific target, string of the build target.
* @return void
*/
protected function prepareBuildDir($target = false)
{
if (is_dir($target)) {
return;
}
mkdir($target, 0755, true);
}
/**
* This function can be thought of as a hybrid between PHP's `array_merge` and `array_merge_recursive`.
*
* The difference between this method and the built-in ones, is that if an array key contains another array, then
* Hash::merge() will behave in a recursive fashion (unlike `array_merge`). But it will not act recursively for
* keys that contain scalar values (unlike `array_merge_recursive`).
*
* Note: This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays.
*
* @param array $data Array to be merged
* @param mixed $merge Array to merge with. The argument and all trailing arguments will be array cast when merged
* @return array Merged array
*
* Taken from the CakePHP framework
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @since 2.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
protected function merge(array $data, $merge)
{
$args = array_slice(func_get_args(), 1);
$return = $data;
foreach ($args as &$curArg) {
$stack[] = [(array)$curArg, &$return];
}
unset($curArg);
$this->_merge($stack, $return);
return $return;
}
/**
* Merge helper function to reduce duplicated code between merge() and expand().
*
* @param array $stack The stack of operations to work with.
* @param array $return The return value to operate on.
* @return void
*
* Taken from the CakePHP framework
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @since 2.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
protected function _merge($stack, &$return)
{
while (!empty($stack)) {
foreach ($stack as $curKey => &$curMerge) {
foreach ($curMerge[0] as $key => &$val) {
$isArray = is_array($curMerge[1]);
if ($isArray && !empty($curMerge[1][$key]) && (array)$curMerge[1][$key] === $curMerge[1][$key] && (array)$val === $val) {
// Recurse into the current merge data as it is an array.
$stack[] = [&$val, &$curMerge[1][$key]];
} elseif ((int)$key === $key && $isArray && isset($curMerge[1][$key])) {
$curMerge[1][] = $val;
} else {
$curMerge[1][$key] = $val;
}
}
unset($stack[$curKey]);
}
unset($curMerge);
}
}
}