Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.

Commit 66e8799

Browse files
committed
added a new class to handle common stuff with the console
1 parent facce7e commit 66e8799

File tree

1 file changed

+271
-0
lines changed

1 file changed

+271
-0
lines changed

core/lib/PatternLab/Console.php

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
<?php
2+
3+
/*!
4+
* Pattern Lab Console Class - v0.7.8
5+
*
6+
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
7+
* Licensed under the MIT license
8+
*
9+
* Handles the set-up of the console commands, options, and documentation
10+
* Heavily influenced by the symfony/console output format
11+
*
12+
*/
13+
14+
namespace PatternLab;
15+
16+
class Console {
17+
18+
private $optionsShort = "h";
19+
private $optionsLong = array("help");
20+
private $options = array();
21+
private $commands = array();
22+
private $self;
23+
24+
/**
25+
* Set-up a default var
26+
*/
27+
public function __construct() {
28+
$this->self = $_SERVER["PHP_SELF"];
29+
}
30+
31+
/**
32+
* Get the arguments that have been passed to the script via the commmand line
33+
*/
34+
public function getArguments() {
35+
if (php_sapi_name() != 'cli') {
36+
print "The builder script can only be run from the command line.\n";
37+
exit;
38+
}
39+
$this->options = getopt($this->optionsShort,$this->optionsLong);
40+
}
41+
42+
/**
43+
* See if a particular command was passed to the script via the command line. Can either be the short or long version
44+
* @param {String} list of arguments to check
45+
*
46+
* @return {Boolean} if the command has been passed to the script via the command line
47+
*/
48+
public function findCommand($args) {
49+
$args = explode("|",$args);
50+
foreach ($args as $arg) {
51+
if (isset($this->options[$arg])) {
52+
return true;
53+
}
54+
}
55+
return false;
56+
}
57+
58+
/**
59+
* Return the command that was given in the command line arguments
60+
*
61+
* @return {String} the command. passes false if no command was found
62+
*/
63+
public function getCommand() {
64+
foreach ($this->commands as $command => $attributes) {
65+
if (isset($this->options[$command]) || isset($this->options[$attributes["commandLong"]])) {
66+
return $command;
67+
}
68+
}
69+
return false;
70+
}
71+
72+
/**
73+
* Set-up the command so it can be used from the command line
74+
* @param {String} the single character version of the command
75+
* @param {String} the long version of the command
76+
* @param {String} the description to be used in the "available commands" section of writeHelp()
77+
* @param {String} the description to be used in the "help" section of writeHelpCommand()
78+
*/
79+
public function setCommand($short,$long,$desc,$help) {
80+
$this->optionsShort .= $short;
81+
$this->optionsLong[] = $long;
82+
$this->commands[$short] = array("commandShort" => $short, "commandLong" => $long, "commandLongLength" => strlen($long), "commandDesc" => $desc, "commandHelp" => $help, "commandOptions" => array());
83+
}
84+
85+
/**
86+
* See if a particular option was passed to the script via the command line. Can either be the short or long version
87+
* @param {String} list of arguments to check
88+
*
89+
* @return {Boolean} if the command has been passed to the script via the command line
90+
*/
91+
public function findCommandOption($args) {
92+
$args = explode("|",$args);
93+
foreach ($args as $arg) {
94+
if (isset($this->options[$arg])) {
95+
return true;
96+
}
97+
}
98+
return false;
99+
}
100+
101+
/**
102+
* Set-up an option for a given command so it can be used from the command line
103+
* @param {String} the single character of the command that this option is related to
104+
* @param {String} the single character version of the option
105+
* @param {String} the long version of the option
106+
* @param {String} the description to be used in the "available options" section of writeHelpCommand()
107+
* @param {String} the sample to be used in the "sample" section of writeHelpCommand()
108+
*/
109+
public function setCommandOption($command,$short,$long,$desc,$sample) {
110+
if (strpos($this->optionsShort,$short) === false) {
111+
$this->optionsShort .= $short;
112+
}
113+
if (!in_array($long,$this->optionsLong)) {
114+
$this->optionsLong[] = $long;
115+
}
116+
$this->commands[$command]["commandOptions"][$short] = array("optionShort" => $short, "optionLong" => $long, "optionLongLength" => strlen($long), "optionDesc" => $desc, "optionSample" => $sample);
117+
}
118+
119+
/**
120+
* Write out the generic help
121+
*/
122+
public function writeHelp() {
123+
124+
/*
125+
126+
The generic help follows this format:
127+
128+
Pattern Lab Console Options
129+
130+
Usage:
131+
php core/console command [options]
132+
133+
Available commands:
134+
--build (-b) Build Pattern Lab
135+
--watch (-w) Build Pattern Lab and watch for changes and rebuild as necessary
136+
--version (-v) Display the version number
137+
--help (-h) Display this help message.
138+
139+
*/
140+
141+
// find length of longest command
142+
$lengthLong = 0;
143+
foreach ($this->commands as $command => $attributes) {
144+
$lengthLong = ($attributes["commandLongLength"] > $lengthLong) ? $attributes["commandLongLength"] : $lengthLong;
145+
}
146+
147+
// write out the generic usage info
148+
$this->writeLine("Pattern Lab Console Options",true);
149+
$this->writeLine("Usage:");
150+
$this->writeLine(" php ".$this->self." command [options]",true);
151+
$this->writeLine("Available commands:");
152+
153+
// write out the commands
154+
foreach ($this->commands as $command => $attributes) {
155+
$spacer = $this->getSpacer($lengthLong,$attributes["commandLongLength"]);
156+
$this->writeLine(" --".$attributes["commandLong"].$spacer."(-".$attributes["commandShort"].") ".$attributes["commandDesc"]);
157+
}
158+
159+
$this->writeLine("");
160+
161+
}
162+
163+
/**
164+
* Write out the command-specific help
165+
* @param {String} the single character of the command that this option is related to
166+
*/
167+
public function writeHelpCommand($command = "") {
168+
169+
/*
170+
171+
The command help follows this format:
172+
173+
Build Command Options
174+
175+
Usage:
176+
php core/console --build [--patternsonly|-p] [--nocache|-n] [--enablecss|-c]
177+
178+
Available options:
179+
--patternsonly (-p) Build only the patterns. Does NOT clean public/.
180+
--nocache (-n) Set the cacheBuster value to 0.
181+
--enablecss (-c) Generate CSS for each pattern. Resource intensive.
182+
--help (-h) Display this help message.
183+
184+
Help:
185+
The build command builds an entire site a single time. It compiles the patterns and moves content from source/ into public/
186+
187+
Samples:
188+
189+
To run and generate the CSS for each pattern:
190+
php core/console build -c
191+
192+
To build only the patterns and not move other files from source/ to public/
193+
php core/console build -p
194+
195+
To turn off the cacheBuster
196+
php core/console build -n
197+
*/
198+
199+
// if given an empty command or the command doesn't exist in the lists give the generic help
200+
if (empty($command)) {
201+
$this->writeHelp();
202+
return;
203+
}
204+
205+
$commandShort = $this->commands[$command]["commandShort"];
206+
$commandLong = $this->commands[$command]["commandLong"];
207+
$commandHelp = $this->commands[$command]["commandHelp"];
208+
$commandOptions = $this->commands[$command]["commandOptions"];
209+
210+
$commandLongUC = ucfirst($commandLong);
211+
212+
// write out the option list and get the longest item
213+
$optionList = "";
214+
$lengthLong = 0;
215+
foreach ($commandOptions as $option => $attributes) {
216+
$optionList .= "[--".$attributes["optionLong"]."|-".$attributes["optionShort"]."] ";
217+
$lengthLong = ($attributes["optionLongLength"] > $lengthLong) ? $attributes["optionLongLength"] : $lengthLong;
218+
}
219+
220+
// write out the generic usage info
221+
$this->writeLine($commandLongUC." Command Options",true);
222+
$this->writeLine("Usage:");
223+
$this->writeLine(" php ".$this->self." --".$commandLong." ".$optionList,true);
224+
$this->writeLine("Available options:");
225+
226+
// write out the options
227+
foreach ($commandOptions as $option => $attributes) {
228+
$spacer = $this->getSpacer($lengthLong,$attributes["optionLongLength"]);
229+
$this->writeLine(" --".$attributes["optionLong"].$spacer."(-".$attributes["optionShort"].") ".$attributes["optionDesc"]);
230+
}
231+
232+
$this->writeLine("");
233+
$this->writeLine("Help:");
234+
$this->writeLine(" ".$commandHelp,true);
235+
$this->writeLine(" Samples:",true);
236+
237+
// write out the samples
238+
foreach ($commandOptions as $option => $attributes) {
239+
$this->writeLine(" ".$attributes["optionSample"]);
240+
$this->writeLine(" php ".$this->self." --".$commandLong." --".$attributes["optionLong"]);
241+
$this->writeLine(" php ".$this->self." -".$commandShort." -".$attributes["optionShort"],true);
242+
}
243+
244+
}
245+
246+
/**
247+
* Write out a line of the help
248+
* @param {Boolean} handle double-break
249+
*/
250+
protected function writeLine($line,$doubleBreak = false) {
251+
$break = ($doubleBreak) ? "\n\n" : "\n";
252+
print $line.$break;
253+
}
254+
255+
/**
256+
* Make sure the space is properly set between long command options and short command options
257+
* @param {Integer} the longest length of the command's options
258+
* @param {Integer} the character length of the given option
259+
*/
260+
protected function getSpacer($lengthLong,$itemLongLength) {
261+
$i = 0;
262+
$spacer = " ";
263+
$spacerLength = $lengthLong - $itemLongLength;
264+
while ($i < $spacerLength) {
265+
$spacer .= " ";
266+
$i++;
267+
}
268+
return $spacer;
269+
}
270+
271+
}

0 commit comments

Comments
 (0)