|
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 -gp |
19 |
| - * Generates only the patterns a site. Does NOT clean public/ when generating the site. |
20 |
| - * |
21 |
| - * php builder.php -w |
22 |
| - * Generates the site like the -g flag and then watches for changes in the 'source' directories & |
23 |
| - * files. Will re-generate files if they've changed. |
24 |
| - * |
25 |
| - * php builder.php -wr |
26 |
| - * In addition to the -w flag features it will also automatically start the auto-reload server. |
27 |
| - * |
28 |
| - * php builder.php -wp |
29 |
| - * Similar to the -w flag but it only generates and then watches the patterns. Does NOT clean public/ when generating the site. |
30 |
| - * |
31 |
| - * php builder.php -v |
32 |
| - * Prints out the current version of Pattern Lab. |
33 |
| - * |
34 | 9 | */
|
35 | 10 |
|
| 11 | +/******************************* |
| 12 | + * General Set-up |
| 13 | + *******************************/ |
| 14 | + |
36 | 15 | // check to see if json_decode exists. might be disabled in installs of PHP 5.5
|
37 | 16 | if (!function_exists("json_decode")) {
|
38 | 17 | print "Please check that your version of PHP includes the JSON extension. It's required for Pattern Lab to run. Aborting.\n";
|
|
49 | 28 | $loader->setNamespaceSeparator("_");
|
50 | 29 | $loader->register();
|
51 | 30 |
|
52 |
| -// make sure this script is being accessed from the command line |
53 |
| -if (php_sapi_name() != 'cli') { |
54 |
| - print "The builder script can only be run from the command line.\n"; |
55 |
| - exit; |
56 |
| -} |
57 | 31 |
|
58 |
| -// grab the arguments from the command line |
59 |
| -$args = getopt("gwcrvpn"); |
| 32 | +/******************************* |
| 33 | + * Console Set-up |
| 34 | + *******************************/ |
60 | 35 |
|
61 |
| -// load Pattern Lab's config, if first time set-up move files appropriately too |
62 |
| -$co = new PatternLab\Configurer; |
63 |
| -$config = $co->getConfig(); |
| 36 | +$console = new PatternLab\Console; |
64 | 37 |
|
65 |
| -// show the version of Pattern Lab |
66 |
| -if (isset($args["v"])) { |
67 |
| - print "You're running v".$config["v"]." of the PHP version of Pattern Lab.\n"; |
68 |
| - exit; |
69 |
| -} |
| 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:"); |
70 | 43 |
|
71 |
| -// generate the pattern lab site if appropriate |
72 |
| -if (isset($args["g"]) || isset($args["w"])) { |
| 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","r","autoreload","Turn on the auto-reload service.","To turn on auto-reload:"); |
| 51 | + |
| 52 | +// set-up the version command |
| 53 | +$console->setCommand("v","version","Print the version number","The version command prints out the current version of Pattern Lab."); |
| 54 | + |
| 55 | + |
| 56 | +/******************************* |
| 57 | + * Figure out what to run |
| 58 | + *******************************/ |
| 59 | + |
| 60 | +// get what was passed on the command line |
| 61 | +$console->getArguments(); |
| 62 | + |
| 63 | +if ($console->findCommand("h|help") && ($command = $console->getCommand())) { |
73 | 64 |
|
74 |
| - $g = new PatternLab\Generator($config); |
| 65 | + // write the usage & help for a specific command |
| 66 | + $console->writeHelpCommand($command); |
75 | 67 |
|
76 |
| - // set some default values |
77 |
| - $enableCSS = false; |
78 |
| - $moveStatic = true; |
79 |
| - $noCacheBuster = false; |
| 68 | +} else if ($command = $console->getCommand()) { |
80 | 69 |
|
81 |
| - // check to see if CSS for patterns should be parsed & outputted |
82 |
| - if (isset($args["c"]) && !isset($args["w"])) { |
83 |
| - $enableCSS = true; |
84 |
| - } |
| 70 | + // run commands |
85 | 71 |
|
86 |
| - // check to see if we should just generate the patterns |
87 |
| - if (isset($args["p"])) { |
88 |
| - $moveStatic = false; |
89 |
| - } |
90 |
| - |
91 |
| - // check to see if we should turn off the cachebuster value |
92 |
| - if (isset($args["n"])) { |
93 |
| - $noCacheBuster = true; |
94 |
| - } |
| 72 | + // load Pattern Lab's config, if first time set-up move files appropriately too |
| 73 | + $configurer = new PatternLab\Configurer; |
| 74 | + $config = $configurer->getConfig(); |
95 | 75 |
|
96 |
| - $g->generate($enableCSS,$moveStatic,$noCacheBuster); |
| 76 | + // set-up required vars |
| 77 | + $enableCSS = ($console->findCommandOption("c|enablecss")) ? true : false; |
| 78 | + $moveStatic = ($console->findCommandOption("p|patternsonly")) ? false : true; |
| 79 | + $noCacheBuster = ($console->findCommandOption("n|nocache")) ? true : false; |
| 80 | + $autoReload = ($console->findCommandOption("r|autoreload")) ? true : false; |
97 | 81 |
|
98 |
| - // have some fun |
99 |
| - if (!isset($args["w"])) { |
| 82 | + if (($command == "g") || ($command == "b")) { |
| 83 | + |
| 84 | + // load the generator |
| 85 | + $g = new PatternLab\Generator($config); |
| 86 | + $g->generate($enableCSS,$moveStatic,$noCacheBuster); |
100 | 87 | $g->printSaying();
|
| 88 | + |
| 89 | + } else if ($command == "w") { |
| 90 | + |
| 91 | + // CSS feature should't be used with watch |
| 92 | + $enableCSS = false; |
| 93 | + |
| 94 | + // load the generator |
| 95 | + $g = new PatternLab\Generator($config); |
| 96 | + $g->generate($enableCSS,$moveStatic,$noCacheBuster); |
| 97 | + |
| 98 | + // load the watcher |
| 99 | + $w = new PatternLab\Watcher($config); |
| 100 | + $w->watch($autoReload,$moveStatic); |
| 101 | + |
| 102 | + } else if ($command == "v") { |
| 103 | + |
| 104 | + // write out the version number |
| 105 | + print "You're running v".$config["v"]." of the PHP version of Pattern Lab.\n"; |
| 106 | + exit; |
| 107 | + |
101 | 108 | }
|
102 | 109 |
|
103 |
| -} |
104 |
| - |
105 |
| -// watch the source directory and regenerate any changed files |
106 |
| -if (isset($args["w"])) { |
107 |
| - |
108 |
| - $w = new PatternLab\Watcher($config); |
109 |
| - |
110 |
| - // set some default values |
111 |
| - $reload = false; |
112 |
| - |
113 |
| - if (isset($args["r"])) { |
114 |
| - $reload = true; |
115 |
| - } |
116 |
| - |
117 |
| - $w->watch($reload,$moveStatic); |
118 |
| - |
119 |
| -} |
120 |
| - |
121 |
| -// when in doubt write out the usage |
122 |
| -if (!isset($args["g"]) && !isset($args["w"]) && !isset($args["v"])) { |
| 110 | +} else { |
123 | 111 |
|
124 |
| - print "\n"; |
125 |
| - print "Usage:\n\n"; |
126 |
| - print " php ".$_SERVER["PHP_SELF"]." -g\n"; |
127 |
| - print " Iterates over the 'source' directories & files and generates the entire site a single time.\n"; |
128 |
| - print " It also cleans the 'public' directory.\n\n"; |
129 |
| - print " php ".$_SERVER["PHP_SELF"]." -gc\n"; |
130 |
| - print " In addition to the -g flag features it will also generate CSS for each pattern. Resource intensive.\n\n"; |
131 |
| - print " php ".$_SERVER["PHP_SELF"]." -gp\n"; |
132 |
| - print " Generates only the patterns a site. Does NOT clean public/ when generating the site.\n\n"; |
133 |
| - print " php ".$_SERVER["PHP_SELF"]." -w\n"; |
134 |
| - print " Generates the site like the -g flag and then watches for changes in the 'source' directories &\n"; |
135 |
| - print " files. Will re-generate files if they've changed.\n\n"; |
136 |
| - print " php ".$_SERVER["PHP_SELF"]." -wr\n"; |
137 |
| - print " In addition to the -w flag features it will also automatically start the auto-reload server.\n\n"; |
138 |
| - print " php ".$_SERVER["PHP_SELF"]." -wp\n"; |
139 |
| - print " Similar to the -w flag but it only generates and then watches the patterns. Does NOT clean public/ when generating the site.\n\n"; |
140 |
| - print " php ".$_SERVER["PHP_SELF"]." -v\n"; |
141 |
| - print " Prints out the current version of Pattern Lab.\n\n"; |
| 112 | + // write the generic help |
| 113 | + $console->writeHelp(); |
142 | 114 |
|
143 | 115 | }
|
0 commit comments