-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathBuilder.php
More file actions
474 lines (374 loc) · 17.4 KB
/
Builder.php
File metadata and controls
474 lines (374 loc) · 17.4 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?php
/*!
* Builder Class
*
* Copyright (c) 2013-2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Holds most of the "generate" functions used in the the Generator and Watcher class
*
*/
namespace PatternLab;
use \PatternLab\Annotations;
use \PatternLab\Config;
use \PatternLab\Data;
use \PatternLab\Dispatcher;
use \PatternLab\FileUtil;
use \PatternLab\Parsers\Documentation;
use \PatternLab\PatternData\Exporters\NavItemsExporter;
use \PatternLab\PatternData\Exporters\PatternPartialsExporter;
use \PatternLab\PatternData\Exporters\PatternPathDestsExporter;
use \PatternLab\PatternData\Exporters\PatternPathSrcExporter;
use \PatternLab\PatternData\Exporters\ViewAllPathsExporter;
use \PatternLab\PatternEngine;
use \PatternLab\Template;
use \PatternLab\Timer;
use \Symfony\Component\Finder\Finder;
class Builder {
/**
* When initializing the Builder class make sure the template helper is set-up
*/
public function __construct() {
// set-up the pattern engine
PatternEngine::init();
// set-up the various attributes for rendering templates
Template::init();
}
/**
* Generates the annotations js file
*/
protected function generateAnnotations() {
// set-up the dispatcher
$dispatcherInstance = Dispatcher::getInstance();
// note the start of the operation
$dispatcherInstance->dispatch("builder.generateAnnotationsStart");
// default var
$publicDir = Config::getOption("publicDir");
// encode the content so it can be written out
$json = json_encode(Annotations::get());
// make sure annotations/ exists
if (!is_dir($publicDir."/annotations")) {
mkdir($publicDir."/annotations");
}
// write out the new annotations.js file
file_put_contents($publicDir."/annotations/annotations.js","var comments = ".$json.";");
// note the end of the operation
$dispatcherInstance->dispatch("builder.generateAnnotationsEnd");
}
/**
* Generates the data that powers the index page
*/
protected function generateIndex() {
/**
* Handle missing index.html. Solves https://github.com/drupal-pattern-lab/patternlab-php-core/issues/14
* Could also be used to re-add missing styleguidekit assets with a few edits?
*
* 1. @TODO: Figure out a better way to future-proof path resolution for styleguidekit `dist` folder
* 2. Recusirively copy files from styleguidekit to publicDir via https://stackoverflow.com/a/7775949
* 3. Make sure we only try to create new directories if they don't already exist
* 4. Only copy files if they are missing (vs changed, etc)
*/
if (!file_exists(Config::getOption("publicDir")."/index.html")) {
$index = Console::getHumanReadablePath(Config::getOption("publicDir")).DIRECTORY_SEPARATOR."index.html";
Console::writeWarning($index . " is missing. No biggie. Grabbing a fresh copy from your StyleguideKit...");
$baseDir = Config::getOption("baseDir") . '/vendor';
$finder = new Finder();
// Locate the current theme's styleguidekit assets via the patternlab-styleguidekit `type` in composer.json
$finder->files()->name("composer.json")->in($baseDir)->contains('patternlab-styleguidekit')->sortByName();
foreach ($finder as $file) {
$src = dirname($file->getRealPath()) . DIRECTORY_SEPARATOR . 'dist'; /* [1] */
$dest= Config::getOption("publicDir");
if (is_dir($src)){
if(!is_dir($dest)) {
mkdir($dest, 0755);
}
foreach ( /* [2] */
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($src, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item
) {
if ($item->isDir()) {
if(!is_dir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName())) { /* [3] */
mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
}
} else {
if(!file_exists($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName())) { /* [4] */
copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
}
}
}
}
}
}
// set-up the dispatcher
$dispatcherInstance = Dispatcher::getInstance();
// note the start of the operation
$dispatcherInstance->dispatch("builder.generateIndexStart");
// default var
$dataDir = Config::getOption("publicDir")."/styleguide/data";
// double-check that the data directory exists
if (!is_dir($dataDir)) {
FileUtil::makeDir($dataDir);
}
$output = "";
// load and write out the config options
$config = array();
$exposedOptions = Config::getOption("exposedOptions");
foreach ($exposedOptions as $exposedOption) {
$config[$exposedOption] = Config::getOption($exposedOption);
}
$output .= "var config = ".json_encode($config).";\n";
// load the ish Controls
$ishControls = array();
$controlsToHide = array();
$ishControlsHide = Config::getOption("ishControlsHide");
if ($ishControlsHide) {
foreach ($ishControlsHide as $controlToHide) {
$controlsToHide[$controlToHide] = "true";
}
}
$ishControls["ishControlsHide"] = $controlsToHide;
$output .= "var ishControls = ".json_encode($ishControls).";\n";
// load and write out the items for the navigation
$niExporter = new NavItemsExporter();
$navItems = $niExporter->run();
$output .= "var navItems = ".json_encode($navItems).";\n";
// load and write out the items for the pattern paths
$patternPaths = array();
$ppdExporter = new PatternPathDestsExporter();
$patternPaths = $ppdExporter->run();
$output .= "var patternPaths = ".json_encode($patternPaths).";\n";
// load and write out the items for the view all paths
$viewAllPaths = array();
$vapExporter = new ViewAllPathsExporter();
$viewAllPaths = $vapExporter->run($navItems);
$output .= "var viewAllPaths = ".json_encode($viewAllPaths).";\n";
// gather plugin package information
$packagesInfo = array();
$componentDir = Config::getOption("componentDir");
if (!is_dir($componentDir)) {
mkdir($componentDir);
}
$componentPackagesDir = $componentDir."/packages";
if (!is_dir($componentDir."/packages")) {
mkdir($componentDir."/packages");
}
$finder = new Finder();
$finder->files()->name("*.json")->in($componentPackagesDir);
$finder->sortByName();
foreach ($finder as $file) {
$filename = $file->getFilename();
if ($filename[0] != "_") {
$javascriptPaths = array();
$packageInfo = json_decode(file_get_contents($file->getPathname()),true);
foreach ($packageInfo["templates"] as $templateKey => $templatePath) {
$templatePathFull = $componentDir."/".$packageInfo["name"]."/".$templatePath;
$packageInfo["templates"][$templateKey] = (file_exists($templatePathFull)) ? file_get_contents($templatePathFull) : "";
}
foreach ($packageInfo["javascripts"] as $key => $javascriptPath) {
$javascriptPaths[] = "patternlab-components/".$packageInfo["name"]."/".$javascriptPath;
}
$packageInfo["javascripts"] = $javascriptPaths;
$packagesInfo[] = $packageInfo;
}
}
$output .= "var plugins = ".json_encode($packagesInfo).";";
// write out the data
file_put_contents($dataDir."/patternlab-data.js",$output);
// Structuring all the same data that went into `patternlab-data.js` and putting it into `patternlab-data.json` too
$allPlData = array(
'config' => $config,
'ishControls' => $ishControls,
'navItems' => $navItems,
'patternPaths' => $patternPaths,
'viewAllPaths' => $viewAllPaths,
'plugins' => $packagesInfo,
);
file_put_contents($dataDir."/patternlab-data.json", json_encode($allPlData));
// note the end of the operation
$dispatcherInstance->dispatch("builder.generateIndexEnd");
}
/**
* Generates all of the patterns and puts them in the public directory
* @param {Array} various options that might affect the export. primarily the location.
*/
protected function generatePatterns($options = array()) {
// set-up the dispatcher
$dispatcherInstance = Dispatcher::getInstance();
// note the beginning of the operation
$dispatcherInstance->dispatch("builder.generatePatternsStart");
// set-up common vars
$exportFiles = (isset($options["exportFiles"]) && $options["exportFiles"]);
$exportDir = Config::getOption("exportDir");
$patternPublicDir = !$exportFiles ? Config::getOption("patternPublicDir") : Config::getOption("patternExportDir");
$patternSourceDir = Config::getOption("patternSourceDir");
$patternExtension = Config::getOption("patternExtension");
$suffixRendered = Config::getOption("outputFileSuffixes.rendered");
$suffixRaw = Config::getOption("outputFileSuffixes.rawTemplate");
$suffixMarkupOnly = Config::getOption("outputFileSuffixes.markupOnly");
// make sure the export dir exists
if ($exportFiles && !is_dir($exportDir)) {
mkdir($exportDir);
}
// make sure patterns exists
if (!is_dir($patternPublicDir)) {
mkdir($patternPublicDir);
}
// loop over the pattern data store to render the individual patterns
$store = PatternData::get();
foreach ($store as $patternStoreKey => $patternStoreData) {
if (($patternStoreData["category"] == "pattern") && isset($patternStoreData["hidden"]) && (!$patternStoreData["hidden"])) {
$path = $patternStoreData["pathDash"];
$pathName = (isset($patternStoreData["pseudo"])) ? $patternStoreData["pathOrig"] : $patternStoreData["pathName"];
// modify the pattern mark-up
$markup = $patternStoreData["code"];
$markupFull = $patternStoreData["header"].$markup.$patternStoreData["footer"];
$markupEngine = file_get_contents($patternSourceDir."/".$pathName.".".$patternExtension);
// if the pattern directory doesn't exist create it
if (!is_dir($patternPublicDir."/".$path)) {
mkdir($patternPublicDir."/".$path);
}
// write out the various pattern files
file_put_contents($patternPublicDir."/".$path."/".$path.$suffixRendered.".html",$markupFull);
if (!$exportFiles) {
file_put_contents($patternPublicDir."/".$path."/".$path.$suffixMarkupOnly.".html",$markup);
file_put_contents($patternPublicDir."/".$path."/".$path.$suffixRaw.".".$patternExtension,$markupEngine);
}
}
}
// note the end of the operation
$dispatcherInstance->dispatch("builder.generatePatternsEnd");
}
/**
* Generates the style guide view
*/
protected function generateStyleguide() {
// set-up the dispatcher
$dispatcherInstance = Dispatcher::getInstance();
// note the beginning of the operation
$dispatcherInstance->dispatch("builder.generateStyleguideStart");
// default var
$publicDir = Config::getOption("publicDir");
// load the pattern loader
$ppdExporter = new PatternPathSrcExporter();
$patternPathSrc = $ppdExporter->run();
$options = array();
$options["patternPaths"] = $patternPathSrc;
$patternEngineBasePath = PatternEngine::getInstance()->getBasePath();
$patternLoaderClass = $patternEngineBasePath."\Loaders\PatternLoader";
$patternLoader = new $patternLoaderClass($options);
// check directories i need
if (!is_dir($publicDir."/styleguide/")) {
mkdir($publicDir."/styleguide/");
}
if (!is_dir($publicDir."/styleguide/html/")) {
mkdir($publicDir."/styleguide/html/");
}
// grab the partials into a data object for the style guide
$ppExporter = new PatternPartialsExporter();
$partials = $ppExporter->run();
// add the pattern data so it can be exported
$patternData = array();
// add the pattern lab specific mark-up
$filesystemLoader = Template::getFilesystemLoader();
$stringLoader = Template::getStringLoader();
$globalData = Data::get();
$globalData["patternLabHead"] = $stringLoader->render(array("string" => Template::getHTMLHead(), "data" => array("cacheBuster" => $partials["cacheBuster"])));
$globalData["patternLabFoot"] = $stringLoader->render(array("string" => Template::getHTMLFoot(), "data" => array("cacheBuster" => $partials["cacheBuster"], "patternData" => json_encode($patternData))));
$globalData["viewall"] = true;
$header = $patternLoader->render(array("pattern" => Template::getPatternHead(), "data" => $globalData));
$code = $filesystemLoader->render(array("template" => "viewall", "data" => $partials));
$footer = $patternLoader->render(array("pattern" => Template::getPatternFoot(), "data" => $globalData));
$styleGuidePage = $header.$code.$footer;
file_put_contents($publicDir."/styleguide/html/styleguide.html",$styleGuidePage);
// note the end of the operation
$dispatcherInstance->dispatch("builder.generateStyleguideEnd");
}
/**
* Generates the view all pages
*/
protected function generateViewAllPages() {
// set-up the dispatcher
$dispatcherInstance = Dispatcher::getInstance();
// note the beginning of the operation
$dispatcherInstance->dispatch("builder.generateViewAllPagesStart");
// default vars
$patternPublicDir = Config::getOption("patternPublicDir");
$htmlHead = Template::getHTMLHead();
$htmlFoot = Template::getHTMLFoot();
$patternHead = Template::getPatternHead();
$patternFoot = Template::getPatternFoot();
$filesystemLoader = Template::getFilesystemLoader();
$stringLoader = Template::getStringLoader();
$globalData = Data::get();
// load the pattern loader
$ppdExporter = new PatternPathSrcExporter();
$patternPathSrc = $ppdExporter->run();
$options = array();
$options["patternPaths"] = $patternPathSrc;
$patternEngineBasePath = PatternEngine::getInstance()->getBasePath();
$patternLoaderClass = $patternEngineBasePath."\Loaders\PatternLoader";
$patternLoader = new $patternLoaderClass($options);
// make sure view all is set
$globalData["viewall"] = true;
// make sure the pattern dir exists
if (!is_dir($patternPublicDir)) {
mkdir($patternPublicDir);
}
// add view all to each list
$store = PatternData::get();
foreach ($store as $patternStoreKey => $patternStoreData) {
if ($patternStoreData["category"] == "patternSubtype") {
// grab the partials into a data object for the style guide
$ppExporter = new PatternPartialsExporter();
$partials = $ppExporter->run($patternStoreData["type"],$patternStoreData["name"]);
if (!empty($partials["partials"])) {
// add the pattern data so it can be exported
$patternData = array();
$patternData["patternPartial"] = "viewall-".$patternStoreData["typeDash"]."-".$patternStoreData["nameDash"];
$globalData["patternLabHead"] = $stringLoader->render(array("string" => Template::getHTMLHead(), "data" => array("cacheBuster" => $partials["cacheBuster"])));
$globalData["patternLabFoot"] = $stringLoader->render(array("string" => Template::getHTMLFoot(), "data" => array("cacheBuster" => $partials["cacheBuster"], "patternData" => json_encode($patternData))));
// render the parts and join them
$header = $patternLoader->render(array("pattern" => $patternHead, "data" => $globalData));
$code = $filesystemLoader->render(array("template" => "viewall", "data" => $partials));
$footer = $patternLoader->render(array("pattern" => $patternFoot, "data" => $globalData));
$viewAllPage = $header.$code.$footer;
// if the pattern directory doesn't exist create it
$patternPath = $patternStoreData["pathDash"];
if (!is_dir($patternPublicDir."/".$patternPath)) {
mkdir($patternPublicDir."/".$patternPath);
file_put_contents($patternPublicDir."/".$patternPath."/index.html",$viewAllPage);
} else {
file_put_contents($patternPublicDir."/".$patternPath."/index.html",$viewAllPage);
}
}
} else if (($patternStoreData["category"] == "patternType") && PatternData::hasPatternSubtype($patternStoreData["nameDash"])) {
// grab the partials into a data object for the style guide
$ppExporter = new PatternPartialsExporter();
$partials = $ppExporter->run($patternStoreData["name"]);
if (!empty($partials["partials"])) {
// add the pattern data so it can be exported
$patternData = array();
$patternData["patternPartial"] = "viewall-".$patternStoreData["nameDash"]."-all";
// add the pattern lab specific mark-up
$globalData["patternLabHead"] = $stringLoader->render(array("string" => $htmlHead, "data" => array("cacheBuster" => $partials["cacheBuster"])));
$globalData["patternLabFoot"] = $stringLoader->render(array("string" => $htmlFoot, "data" => array("cacheBuster" => $partials["cacheBuster"], "patternData" => json_encode($patternData))));
// render the parts and join them
$header = $patternLoader->render(array("pattern" => $patternHead, "data" => $globalData));
$code = $filesystemLoader->render(array("template" => "viewall", "data" => $partials));
$footer = $patternLoader->render(array("pattern" => $patternFoot, "data" => $globalData));
$viewAllPage = $header.$code.$footer;
// if the pattern directory doesn't exist create it
$patternPath = $patternStoreData["pathDash"];
if (!is_dir($patternPublicDir."/".$patternPath)) {
mkdir($patternPublicDir."/".$patternPath);
file_put_contents($patternPublicDir."/".$patternPath."/index.html",$viewAllPage);
} else {
file_put_contents($patternPublicDir."/".$patternPath."/index.html",$viewAllPage);
}
}
}
}
// note the end of the operation
$dispatcherInstance->dispatch("builder.generateViewAllPagesEnd");
}
}