|
6 | 6 | * Copyright (c) 2013-2014 Dave Olsen, http://dmolsen.com
|
7 | 7 | * Licensed under the MIT license
|
8 | 8 | *
|
9 |
| - * Usage: |
10 |
| - * |
11 |
| - * php builder.php -g |
12 |
| - * Iterates over the 'source' directories & files and generates the entire site a single time. |
13 |
| - * It also cleans the 'public' directory. |
14 |
| - * |
15 |
| - * php builder/builder.php -gc |
16 |
| - * In addition to the -g flag features it will also generate CSS for each pattern. Resource instensive. |
17 |
| - * |
18 |
| - * php builder.php -w |
19 |
| - * Generates the site like the -g flag and then watches for changes in the 'source' directories & |
20 |
| - * files. Will re-generate files if they've changed. |
21 |
| - * |
22 |
| - * php builder.php -wr |
23 |
| - * In addition to the -w flag features it will also automatically start the auto-reload server. |
24 |
| - * |
25 |
| - * php builder.php -v |
26 |
| - * Prints out the current version of Pattern Lab. |
27 |
| - * |
28 | 9 | */
|
29 | 10 |
|
| 11 | +/******************************* |
| 12 | + * General Set-up |
| 13 | + *******************************/ |
| 14 | + |
30 | 15 | // check to see if json_decode exists. might be disabled in installs of PHP 5.5
|
31 | 16 | if (!function_exists("json_decode")) {
|
32 | 17 | print "Please check that your version of PHP includes the JSON extension. It's required for Pattern Lab to run. Aborting.\n";
|
|
43 | 28 | $loader->setNamespaceSeparator("_");
|
44 | 29 | $loader->register();
|
45 | 30 |
|
46 |
| -// make sure this script is being accessed from the command line |
47 |
| -if (php_sapi_name() != 'cli') { |
48 |
| - print "The builder script can only be run from the command line.\n"; |
49 |
| - exit; |
50 |
| -} |
51 | 31 |
|
52 |
| -// grab the arguments from the command line |
53 |
| -$args = getopt("gwcrv"); |
| 32 | +/******************************* |
| 33 | + * Console Set-up |
| 34 | + *******************************/ |
54 | 35 |
|
55 |
| -// load Pattern Lab's config, if first time set-up move files appropriately too |
56 |
| -$co = new PatternLab\Configurer; |
57 |
| -$config = $co->getConfig(); |
| 36 | +$console = new PatternLab\Console; |
58 | 37 |
|
59 |
| -// show the version of Pattern Lab |
60 |
| -if (isset($args["v"])) { |
61 |
| - print "You're running v".$config["v"]." of the PHP version of Pattern Lab.\n"; |
62 |
| - exit; |
63 |
| -} |
| 38 | +// set-up the generate command and options |
| 39 | +$console->setCommand("g","generate","Generate Pattern Lab","The generate command generates an entire site a single time. By default it removes old content in public/, compiles the patterns and moves content from source/ into public/"); |
| 40 | +$console->setCommandOption("g","p","patternsonly","Generate only the patterns. Does NOT clean public/.","To generate only the patterns:"); |
| 41 | +$console->setCommandOption("g","n","nocache","Set the cacheBuster value to 0.","To turn off the cacheBuster:"); |
| 42 | +$console->setCommandOption("g","c","enablecss","Generate CSS for each pattern. Resource intensive.","To run and generate the CSS for each pattern:"); |
64 | 43 |
|
65 |
| -// generate the pattern lab site if appropriate |
66 |
| -if (isset($args["g"]) || isset($args["w"])) { |
67 |
| - |
68 |
| - $g = new PatternLab\Generator($config); |
69 |
| - $c = false; |
| 44 | +// set-up an alias for the generate command |
| 45 | +$console->setCommand("b","build","Alias for the generate command","Alias for the generate command. Please refer to it's help for full options."); |
| 46 | + |
| 47 | +// set-up the watch command and options |
| 48 | +$console->setCommand("w","watch","Watch for changes and regenerate","The watch command builds Pattern Lab, watches for changes in source/ and regenerates Pattern Lab when there are any."); |
| 49 | +$console->setCommandOption("w","p","patternsonly","Watches only the patterns. Does NOT clean public/.","To watch and generate only the patterns:"); |
| 50 | +$console->setCommandOption("w","n","nocache","Set the cacheBuster value to 0.","To turn off the cacheBuster:"); |
| 51 | +$console->setCommandOption("w","r","autoreload","Turn on the auto-reload service.","To turn on auto-reload:"); |
| 52 | + |
| 53 | +// set-up the version command |
| 54 | +$console->setCommand("v","version","Print the version number","The version command prints out the current version of Pattern Lab."); |
| 55 | + |
| 56 | + |
| 57 | +/******************************* |
| 58 | + * Figure out what to run |
| 59 | + *******************************/ |
| 60 | + |
| 61 | +// get what was passed on the command line |
| 62 | +$console->getArguments(); |
| 63 | + |
| 64 | +if ($console->findCommand("h|help") && ($command = $console->getCommand())) { |
70 | 65 |
|
71 |
| - // check to see if CSS for patterns should be parsed & outputted |
72 |
| - if (isset($args["c"]) && !isset($args["w"])) { |
73 |
| - $c = true; |
74 |
| - } |
| 66 | + // write the usage & help for a specific command |
| 67 | + $console->writeHelpCommand($command); |
75 | 68 |
|
76 |
| - $g->generate($c); |
| 69 | +} else if ($command = $console->getCommand()) { |
77 | 70 |
|
78 |
| - // have some fun |
79 |
| - if (!isset($args["w"])) { |
80 |
| - $g->printSaying(); |
81 |
| - } |
| 71 | + // run commands |
82 | 72 |
|
83 |
| -} |
84 |
| - |
85 |
| -// watch the source directory and regenerate any changed files |
86 |
| -if (isset($args["w"])) { |
| 73 | + // load Pattern Lab's config, if first time set-up move files appropriately too |
| 74 | + $configurer = new PatternLab\Configurer; |
| 75 | + $config = $configurer->getConfig(); |
87 | 76 |
|
88 |
| - $w = new PatternLab\Watcher($config); |
89 |
| - $a = false; |
| 77 | + // set-up required vars |
| 78 | + $enableCSS = ($console->findCommandOption("c|enablecss")) ? true : false; |
| 79 | + $moveStatic = ($console->findCommandOption("p|patternsonly")) ? false : true; |
| 80 | + $noCacheBuster = ($console->findCommandOption("n|nocache")) ? true : false; |
| 81 | + $autoReload = ($console->findCommandOption("r|autoreload")) ? true : false; |
90 | 82 |
|
91 |
| - if (isset($args["r"])) { |
92 |
| - $a = true; |
| 83 | + if (($command == "g") || ($command == "b")) { |
| 84 | + |
| 85 | + // load the generator |
| 86 | + $g = new PatternLab\Generator($config); |
| 87 | + $g->generate($enableCSS,$moveStatic,$noCacheBuster); |
| 88 | + $g->printSaying(); |
| 89 | + |
| 90 | + } else if ($command == "w") { |
| 91 | + |
| 92 | + // CSS feature should't be used with watch |
| 93 | + $enableCSS = false; |
| 94 | + |
| 95 | + // load the generator |
| 96 | + $g = new PatternLab\Generator($config); |
| 97 | + $g->generate($enableCSS,$moveStatic,$noCacheBuster); |
| 98 | + |
| 99 | + // load the watcher |
| 100 | + $w = new PatternLab\Watcher($config); |
| 101 | + $w->watch($autoReload,$moveStatic,$noCacheBuster); |
| 102 | + |
| 103 | + } else if ($command == "v") { |
| 104 | + |
| 105 | + // write out the version number |
| 106 | + print "You're running v".$config["v"]." of the PHP version of Pattern Lab.\n"; |
| 107 | + exit; |
| 108 | + |
93 | 109 | }
|
94 | 110 |
|
95 |
| - $w->watch($a); |
96 |
| - |
97 |
| -} |
98 |
| - |
99 |
| -// when in doubt write out the usage |
100 |
| -if (!isset($args["g"]) && !isset($args["w"]) && !isset($args["v"])) { |
| 111 | +} else { |
101 | 112 |
|
102 |
| - print "\n"; |
103 |
| - print "Usage:\n\n"; |
104 |
| - print " php ".$_SERVER["PHP_SELF"]." -g\n"; |
105 |
| - print " Iterates over the 'source' directories & files and generates the entire site a single time.\n"; |
106 |
| - print " It also cleans the 'public' directory.\n\n"; |
107 |
| - print " php ".$_SERVER["PHP_SELF"]." -gc\n"; |
108 |
| - print " In addition to the -g flag features it will also generate CSS for each pattern. Resource instensive.\n\n"; |
109 |
| - print " php ".$_SERVER["PHP_SELF"]." -w\n"; |
110 |
| - print " Generates the site like the -g flag and then watches for changes in the 'source' directories &\n"; |
111 |
| - print " files. Will re-generate files if they've changed.\n\n"; |
112 |
| - print " php ".$_SERVER["PHP_SELF"]." -wr\n"; |
113 |
| - print " In addition to the -w flag features it will also automatically start the auto-reload server.\n\n"; |
114 |
| - print " php ".$_SERVER["PHP_SELF"]." -v\n"; |
115 |
| - print " Prints out the current version of Pattern Lab.\n\n"; |
| 113 | + // write the generic help |
| 114 | + $console->writeHelp(); |
116 | 115 |
|
117 | 116 | }
|
0 commit comments