-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoboFile.php
202 lines (185 loc) · 5.72 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
<?php
/**
* This is Phlomis project's console commands configuration for Robo task runner.
*
* @see http://robo.li/
*/
class RoboFile extends \Robo\Tasks
{
private $vendorDir;
private $assetPackages;
/**
* Contruct for the class, checks and creates the dist folders
*/
public function __construct() {
// check the build folder
$buildDirs = array(
'dist/styles',
'dist/scripts',
'dist/fonts',
'dist/images'
);
$this->createPaths($buildDirs);
// get the vendorDir
$io = new Composer\IO\NullIO();
$factory = new Composer\Factory();
$composer = $factory->createComposer($io);
$this->vendorDir = rtrim($composer->getConfig()->get('vendor-dir'), '/');
// go through installed packages (taken from Composer\Command\ShowCommand.php)
$installedRepo = $composer->getRepositoryManager()->getLocalRepository();
$this->assetPackages = [];
foreach ($installedRepo->getPackages() as $package) {
if ($package->getType() == 'bower-asset-library' ) {
// store the extra information for assets
$this->assetPackages[$package->getPrettyName()] = $package;
}
}
}
private function createPaths($paths){
// iterate through paths array and create folder
foreach ($paths as $path) {
// use Symfony 2 mkdir for recursive directory creation
if (! is_dir($path)) {
$this->_mkdir($path);
}
}
}
private function getAssetPath($packageName) {
foreach ($this->assetPackages as $k => $package) {
if ( strpos($k, $packageName) !== FALSE) {
return $this->vendorDir .'/'. $k . '/';
}
}
}
private function getAssetMain($packageName) {
foreach ($this->assetPackages as $k => $package) {
if ( strpos($k, $packageName) !== FALSE) {
$extra = $package->getExtra();
$main = '';
if (isset($extra['bower-asset-main'])) $main = $extra['bower-asset-main'];
// check if single value is given, then return a string instead of an array
if (is_array($main) && count($main) == 1){
$main = $this->vendorDir.'/bower-asset/'.$packageName.'/'.$main[0];
}
return $main;
}
}
}
/**
* Search and replace path dependencies, simple replacement for wiredep
*/
private function pathDependencies() {
// replace bower_asset directories
$this->taskReplaceInFile('assets/styles/main.scss')
->from('../../bower_components')
->to('')
->run();
}
/**
* Installation steps done after composer post-install
* Copying the Sage files with rsync
*/
public function install() {
if ( file_exists($this->getAssetPath('sage')) ) {
// rsync runs with --ignore-existing so that it does not overwrite existing files
// we only exclude files which do not exist, and we do not want them
$this->taskRsync()
->fromPath($this->getAssetPath('sage'))
->toPath('./')
->recursive()
->option('ignore-existing')
->exclude('.bowerrc')
->exclude('bower.json')
->exclude('composer.lock')
->exclude('.editorconfig')
->exclude('.github')
->exclude('gulpfile.js')
->exclude('CHANGELOG.md')
->exclude('.jscsrc')
->exclude('.jshintrc')
->exclude('LICENSE.md')
->exclude('package.json')
->exclude('.travis.yml')
// ->dryRun()
// ->verbose()
// ->stats()
->run();
}
}
/**
* Main build step, included to be compatible with Sage gulp
*/
public function build() {
$this->styles();
$this->scripts();
$this->fonts();
$this->images();
}
/**
* `gulp styles` - Compiles, combines, and optimizes Bower CSS and project CSS
* By default this task will only log a warning if a precompiler error is
* raised. If the `--production` flag is set: this task will fail outright.
*/
public function styles() {
// fix path issues
$this->pathDependencies();
$this->taskScss(
[
'assets/styles/main.scss' => 'dist/styles/main.css'
]
)
->addImportPath('assets/styles')
->addImportPath('vendor/bower-asset')
->setFormatter('Leafo\ScssPhp\Formatter\Compressed')
->run();
}
/**
* `gulp scripts` - Runs JSHint then compiles, combines, and optimizes Bower JS and project JS.
*/
public function scripts() {
$this->taskMinify('assets/scripts/main.js')
->to('dist/scripts/main.js')
->run();
$this->taskMinify($this->getAssetMain('modernizr'))
->to('dist/scripts/modernizr.js')
->run();
}
/**
* `gulp fonts` - Grabs all the fonts and outputs them in a flattened directory structure
* See: https://github.com/armed/gulp-flatten
*/
public function fonts() {
$fonts = 'assets/fonts/*.{eot,svg,ttf,woff,woff2}';
$this->taskFlattenDir($fonts)
->to('dist/fonts')
->run();
}
/**
* `gulp images` - Run lossless compression on all the images.
*/
public function images() {
$this->taskImageMinify('assets/images/*')
->to('dist/images/')
->run();
// Copy .ico
$this->taskFlattenDir('assets/images/*.ico')
->to('dist/images')
->run();
}
/**
* `gulp watch` - Use BrowserSync to proxy your dev server and synchronize code changes across devices.
* Specify the hostname of your dev server at `manifest.config.devUrl`.
* When a modification is made to an asset, run the
* build step for that asset and inject the changes into the page.
* @link http://www.browsersync.io
*/
public function watch() {
$this->taskWatch()
->monitor('assets/scripts/main.js', function() {
$this->scripts();
})
->monitor('assets/styles/main.scss', function() {
$this->styles();
})->run();
}
}