Skip to content

Commit a01366b

Browse files
committed
Merge branch 'dev'
2 parents 964db90 + 94aac0c commit a01366b

17 files changed

+141
-66
lines changed

src/PatternLab/Builder.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use \PatternLab\Config;
1717
use \PatternLab\Data;
1818
use \PatternLab\Dispatcher;
19+
use \PatternLab\FileUtil;
1920
use \PatternLab\Parsers\Documentation;
2021
use \PatternLab\PatternData\Exporters\NavItemsExporter;
2122
use \PatternLab\PatternData\Exporters\PatternPartialsExporter;
@@ -110,6 +111,12 @@ protected function generateAnnotations() {
110111
*/
111112
protected function generateIndex() {
112113

114+
// bomb if missing index.html
115+
if (!file_exists(Config::getOption("publicDir")."/index.html")) {
116+
$index = Console::getHumanReadablePath(Config::getOption("publicDir")).DIRECTORY_SEPARATOR."index.html";
117+
Console::writeError("<path>".$index."</path> is missing. grab a copy from your StyleguideKit...");
118+
}
119+
113120
// set-up the dispatcher
114121
$dispatcherInstance = Dispatcher::getInstance();
115122

@@ -121,7 +128,7 @@ protected function generateIndex() {
121128

122129
// double-check that the data directory exists
123130
if (!is_dir($dataDir)) {
124-
mkdir($dataDir);
131+
FileUtil::makeDir($dataDir);
125132
}
126133

127134
$output = "";
@@ -236,7 +243,7 @@ protected function generatePatterns($options = array()) {
236243
$store = PatternData::get();
237244
foreach ($store as $patternStoreKey => $patternStoreData) {
238245

239-
if (($patternStoreData["category"] == "pattern") && (!$patternStoreData["hidden"])) {
246+
if (($patternStoreData["category"] == "pattern") && isset($patternStoreData["hidden"]) && (!$patternStoreData["hidden"])) {
240247

241248
$path = $patternStoreData["pathDash"];
242249
$pathName = (isset($patternStoreData["pseudo"])) ? $patternStoreData["pathOrig"] : $patternStoreData["pathName"];

src/PatternLab/Config.php

+14-19
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public static function init($baseDir = "", $verbose = true) {
202202

203203
// set-up the various dirs
204204
self::$options["configDir"] = self::$userConfigDir;
205+
self::$options["configPath"] = self::$userConfigPath;
205206
self::$options["coreDir"] = is_dir(self::$options["baseDir"]."_core") ? self::$options["baseDir"]."_core" : self::$options["baseDir"]."core";
206207
self::$options["exportDir"] = isset(self::$options["exportDir"]) ? self::$options["baseDir"].self::cleanDir(self::$options["exportDir"]) : self::$options["baseDir"]."exports";
207208
self::$options["publicDir"] = isset(self::$options["publicDir"]) ? self::$options["baseDir"].self::cleanDir(self::$options["publicDir"]) : self::$options["baseDir"]."public";
@@ -218,22 +219,9 @@ public static function init($baseDir = "", $verbose = true) {
218219
self::$options["styleguideKitPath"] = self::$options["baseDir"].self::cleanDir(self::getStyleguideKitPath(self::$options["styleguideKitPath"]));
219220
}
220221

221-
// double-check a few directories are real
222-
// refactor this at some point. it's verbose
223-
if (!isset(self::$options["sourceDir"])) {
224-
Console::writeError("please make sure sourceDir is set in <path>./config/config.yml</path> by adding 'sourceDir=some/path'. sorry, stopping pattern lab... :(");
225-
} else if (!is_dir(self::$options["sourceDir"])) {
226-
Console::writeError("hrm... i can't seem to find the directory with your source files. are you sure they're at <path>".Console::getHumanReadablePath(self::$options["sourceDir"])."</path>? you can fix this in <path>./config/config.yml</path> by editing sourceDir. sorry, stopping pattern lab... :(");
227-
}
228-
if (!isset(self::$options["publicDir"])) {
229-
Console::writeError("please make sure publicDir is set in <path>./config/config.yml</path> by adding 'publicDir=some/path'. sorry, stopping pattern lab... :(");
230-
} else if (!is_dir(self::$options["publicDir"])) {
231-
Console::writeError("hrm... i can't seem to find the directory where you want to write your styleguide. are you sure it's at <path>".Console::getHumanReadablePath(self::$options["sourceDir"])."</path>? you can fix this in <path>./config/config.yml</path> by editing publicDir. sorry, stopping pattern lab... :(");
232-
}
233-
if (isset(self::$options["styleguideKitPath"]) && !is_dir(self::$options["styleguideKitPath"])) {
234-
Console::writeError("hrm... i can't seem to find the directory where your styleguide files are located. are you sure it's at <path>".Console::getHumanReadablePath(self::$options["styleguideKitPath"])."</path>? you can fix this in <path>./config/config.yml</path> by editing styleguideKitPath. sorry, stopping pattern lab... :(");
235-
}
236-
222+
// double-check a few directories are real and set-up
223+
FileUtil::checkPathFromConfig(self::$options["sourceDir"], self::$userConfigPath, "sourceDir");
224+
FileUtil::checkPathFromConfig(self::$options["publicDir"], self::$userConfigPath, "publicDir");
237225

238226
// make sure styleguideExcludes is set to an array even if it's empty
239227
if (is_string(self::$options["styleGuideExcludes"])) {
@@ -341,9 +329,16 @@ public static function updateConfigOption($optionName,$optionValue, $force = fal
341329
if (is_string($optionValue) && strpos($optionValue,"<prompt>") !== false) {
342330

343331
// prompt for input using the supplied query
344-
$prompt = str_replace("</prompt>","",str_replace("<prompt>","",$optionValue));
345332
$options = "";
346-
$input = Console::promptInput($prompt,$options,false);
333+
$default = "";
334+
$prompt = str_replace("</prompt>","",str_replace("<prompt>","",$optionValue));
335+
if (strpos($prompt, "<default>") !== false) {
336+
$default = explode("<default>",$prompt);
337+
$default = explode("</default>",$default[1]);
338+
$default = $default[0];
339+
}
340+
341+
$input = Console::promptInput($prompt,$options,$default,false);
347342

348343
self::writeUpdateConfigOption($optionName,$input);
349344
Console::writeTag("ok","config option ".$optionName." updated...", false, true);
@@ -364,7 +359,7 @@ public static function updateConfigOption($optionName,$optionValue, $force = fal
364359
// prompt for input
365360
$prompt = "update the config option <desc>".$optionName." (".$currentOptionValue.")</desc> with the value <desc>".$newOptionValue."</desc>?";
366361
$options = "Y/n";
367-
$input = Console::promptInput($prompt,$options);
362+
$input = Console::promptInput($prompt,$options,"Y");
368363

369364
if ($input == "y") {
370365
self::writeUpdateConfigOption($optionName,$optionValue);

src/PatternLab/Console.php

+19-8
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ public static function writeHelpCommand($command = "") {
367367
$lengthLong = 0;
368368
foreach ($commandOptions as $option => $attributes) {
369369
$optionShort = (!empty($attributes["optionShort"][0]) && (($attributes["optionShort"][0] != "z") || ($attributes["optionShort"] != ""))) ? "|-".$attributes["optionShort"] : "";
370-
$optionList .= "[--".$attributes["optionLong"].$optionShort."] ";
370+
$optionExtra = (!empty($attributes["optionExtra"])) ? " ".$attributes["optionExtra"] : "";
371+
$optionList .= "[--".$attributes["optionLong"].$optionShort.$optionExtra."] ";
371372
$lengthLong = ($attributes["optionLongLength"] > $lengthLong) ? $attributes["optionLongLength"] : $lengthLong;
372373
}
373374

@@ -382,7 +383,7 @@ public static function writeHelpCommand($command = "") {
382383
self::writeLine("");
383384
self::writeLine("<h1>".$commandLongUC." Command Options</h1>",true,true);
384385
self::writeLine("<h2>Usage</h2>:",true,true);
385-
self::writeLine(" php ".self::$self." --".$commandLong.$commandShortInc." ".$commandExampleList.$optionList,true,true);
386+
self::writeLine(" php ".self::$self." --".$commandLong.$commandShortInc." ".$optionList,true,true);
386387

387388
// write out the available options
388389
if (count($commandOptions) > 0) {
@@ -531,12 +532,13 @@ public static function writeWarning($line,$doubleSpace = false,$doubleBreak = fa
531532
* Prompt the user for some input
532533
* @param {String} the text for the prompt
533534
* @param {String} the text for the options
535+
* @param {String} the text for the default option
534536
* @param {Boolean} if we should lowercase the input before sending it back
535537
* @param {String} the tag that should be used when drawing the content
536538
*
537539
* @return {String} trimmed input given by the user
538540
*/
539-
public static function promptInput($prompt = "", $options = "", $lowercase = true, $tag = "info") {
541+
public static function promptInput($prompt = "", $options = "", $default = "", $lowercase = true, $tag = "info") {
540542

541543
// check prompt
542544
if (empty($prompt)) {
@@ -551,11 +553,20 @@ public static function promptInput($prompt = "", $options = "", $lowercase = tru
551553
// make sure no end-of-line is added
552554
$prompt .= " <nophpeol>";
553555

554-
// open the terminal and wait for feedback
555-
$stdin = fopen("php://stdin", "r");
556-
Console::writeTag($tag,$prompt);
557-
$input = trim(fgets($stdin));
558-
fclose($stdin);
556+
// make sure we're not running in no interaction mode. if so just use the default for the input
557+
if (InstallerUtil::$isInteractive) {
558+
559+
// open the terminal and wait for feedback
560+
$stdin = fopen("php://stdin", "r");
561+
Console::writeTag($tag,$prompt);
562+
$input = trim(fgets($stdin));
563+
fclose($stdin);
564+
565+
} else {
566+
567+
$input = $default;
568+
569+
}
559570

560571
// check to see if it should be lowercased before sending back
561572
return ($lowercase) ? strtolower($input) : $input;

src/PatternLab/Console/Commands/ServerCommand.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ public function __construct() {
2626
Console::setCommand($this->command,"Start the PHP-based server","The server command will start PHP's web server for you.","s");
2727
Console::setCommandOption($this->command,"host:","Provide a custom hostname. Default value is <path>localhost</path>.","To use a custom hostname and the default port:","","<host>");
2828
Console::setCommandOption($this->command,"port:","Provide a custom port. Default value is <path>8080</path>.","To use a custom port and the default hostname:","","<port>");
29+
Console::setCommandOption($this->command,"quiet","Turn on quiet mode for the server.","To turn on quiet mode:");
2930
Console::setCommandSample($this->command,"To provide both a custom hostname and port:","--host <host> --port <port>");
30-
31+
3132
}
3233

3334
public function run() {
@@ -48,9 +49,20 @@ public function run() {
4849
$port = Console::findCommandOptionValue("port");
4950
$host = $port ? $host.":".$port : $host.":8080";
5051

52+
$null = Console::findCommandOption("quiet");
53+
$null = $null ? " >& /dev/null" : "";
54+
55+
$php = isset($_SERVER["_"]) ? $_SERVER["_"] : Config::getOption("phpBin");
56+
57+
if (!$php) {
58+
$configPath = Console::getHumanReadablePath(Config::getOption("configPath"));
59+
Console::writeError("please add the option `phpBin` with the path to PHP to <path>".$configPath."</path> before running the server...");
60+
}
61+
5162
// start-up the server with the router
5263
Console::writeInfo("server started on http://".$host." - use ctrl+c to exit...");
53-
passthru("cd ".$publicDir." && ".$_SERVER["_"]." -S ".$host." ".$coreDir."/server/router.php");
64+
65+
passthru("cd ".$publicDir." && ".$php." -S ".$host." ".$coreDir."/server/router.php".$null);
5466

5567
}
5668

src/PatternLab/Fetch.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public function fetchStarterKit($starterkit = "") {
9191
Console::writeInfo("finished downloading the starterkit...");
9292

9393
// make sure the temp dir exists before copying into it
94-
if (!is_dir($tempDirSK)) {
95-
mkdir($tempDirSK);
94+
if (!is_dir($tempDirSK)) {
95+
mkdir($tempDirSK);
9696
}
9797

9898
// extract, if the zip is supposed to be unpacked do that (e.g. stripdir)
@@ -195,7 +195,7 @@ protected function mirrorDist($sourceDir, $tempDirDist) {
195195

196196
$prompt = "a starterkit is already installed. merge the new files with it or replace it?";
197197
$options = "M/r";
198-
$input = Console::promptInput($prompt,$options);
198+
$input = Console::promptInput($prompt,$options,"M");
199199
$fsOptions = ($input == "r") ? array("delete" => true, "override" => true) : array("delete" => false, "override" => false);
200200

201201
}

src/PatternLab/FileUtil.php

+34-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,42 @@
2020
use \Symfony\Component\Filesystem\Exception\IOExceptionInterface;
2121

2222
class FileUtil {
23-
23+
24+
/**
25+
* Check that a dir from the config exists and try to build it if needed
26+
* @param {String} directory to be checked/built
27+
* @param {String} the config path
28+
* @param {String} the config option that would help them
29+
*/
30+
public static function checkPathFromConfig($path, $configPath, $configOption = "") {
31+
32+
if (!isset($path)) {
33+
Console::writeError("please make sure ".$configOption." is set in <path>".Console::getHumanReadablePath($configPath)."</path> by adding '".$configOption."=some/path'. sorry, stopping pattern lab... :(");
34+
} else if (!is_dir($path)) {
35+
Console::writeWarning("i can't seem to find the directory <path>".Console::getHumanReadablePath($path)."</path>...");
36+
self::makeDir($path);
37+
Console::writeWarning("i created <path>".Console::getHumanReadablePath($path)."</path> just in case. you can edit this in <path>".Console::getHumanReadablePath($configPath)."</path> by editing ".$configOption."...");
38+
}
39+
40+
}
41+
42+
/**
43+
* Make a directory
44+
* @param {String} directory to be made
45+
*/
46+
public static function makeDir($dir) {
47+
$fs = new Filesystem();
48+
try {
49+
$fs->mkdir($dir);
50+
} catch (IOExceptionInterface $e) {
51+
Console::writeError("an error occurred while creating your directory at <path>".$e->getPath()."</path>...");
52+
}
53+
unset($fs);
54+
}
55+
2456
/**
2557
* Copies a file from the given source path to the given public path.
26-
* THIS IS NOT FOR PATTERNS
58+
* THIS IS NOT FOR PATTERNS
2759
* @param {String} the source file
2860
* @param {String} the public file
2961
*/

src/PatternLab/InstallerUtil.php

+18-6
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414

1515
use \PatternLab\Config;
1616
use \PatternLab\Console;
17+
use \PatternLab\FileUtil;
1718
use \PatternLab\Timer;
1819
use \Symfony\Component\Filesystem\Filesystem;
1920
use \Symfony\Component\Filesystem\Exception\IOExceptionInterface;
2021
use \Symfony\Component\Finder\Finder;
2122

2223
class InstallerUtil {
2324

25+
public static $isInteractive;
26+
2427
/**
2528
* Move the component files from the package to their location in the patternlab-components dir
2629
* @param {String/Array} the items to create a fileList for
@@ -62,7 +65,13 @@ protected static function init() {
6265
// make sure the source dir is set-up
6366
$sourceDir = Config::getOption("sourceDir");
6467
if (!is_dir($sourceDir)) {
65-
mkdir($sourceDir);
68+
FileUtil::makeDir($sourceDir);
69+
}
70+
71+
// make sure the public dir is set-up
72+
$publicDir = Config::getOption("publicDir");
73+
if (!is_dir($publicDir)) {
74+
FileUtil::makeDir($publicDir);
6675
}
6776

6877
Dispatcher::init();
@@ -477,7 +486,7 @@ protected static function pathExists($packageName,$path) {
477486
// prompt for input using the supplied query
478487
$prompt = "the path <path>".$humanReadablePath."</path> already exists. merge or replace with the contents of <path>".$packageName."</path> package?";
479488
$options = "M/r";
480-
$input = Console::promptInput($prompt,$options);
489+
$input = Console::promptInput($prompt,$options,"M");
481490

482491
if ($input == "m") {
483492
Console::writeTag("ok","contents of <path>".$humanReadablePath."</path> have been merged with the package's content...", false, true);
@@ -504,7 +513,7 @@ protected static function pathExists($packageName,$path) {
504513
*/
505514
public static function postInstallCmd($installerInfo, $event) {
506515

507-
self::packagesInstall($installerInfo);
516+
self::packagesInstall($installerInfo, $event);
508517

509518
}
510519

@@ -516,7 +525,7 @@ public static function postInstallCmd($installerInfo, $event) {
516525
public static function postUpdateCmd($installerInfo, $event) {
517526

518527
if (!$installerInfo["packagesRemove"]) {
519-
self::packagesInstall($installerInfo);
528+
self::packagesInstall($installerInfo, $event);
520529
}
521530

522531
}
@@ -541,7 +550,7 @@ protected static function promptStarterKitInstall($starterKitSuggestions) {
541550

542551
$prompt = "choose an option or hit return to skip:";
543552
$options = "(ex. 1)";
544-
$input = Console::promptInput($prompt,$options);
553+
$input = Console::promptInput($prompt,$options,"1");
545554
$result = (int)$input - 1;
546555

547556
if (isset($starterKitSuggestions[$result])) {
@@ -574,7 +583,10 @@ protected static function removeDots($path) {
574583
* Handle some Pattern Lab specific tasks based on what's found in the package's composer.json file on install
575584
* @param {Array} the info culled from installing various pattern lab-related packages
576585
*/
577-
protected static function packagesInstall($installerInfo) {
586+
protected static function packagesInstall($installerInfo, $event) {
587+
588+
// mark if this is an interactive call or not
589+
self::$isInteractive = $event->getIO()->isInteractive();
578590

579591
// initialize a bunch of stuff like config and console
580592
self::init();

src/PatternLab/PatternData/Exporters/DataLinkExporter.php

+15-13
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,29 @@
1717
use \PatternLab\Timer;
1818

1919
class DataLinkExporter extends \PatternLab\PatternData\Exporter {
20-
20+
2121
public function __construct($options = array()) {
22-
22+
2323
parent::__construct($options);
24-
24+
2525
}
26-
26+
2727
public function run() {
28-
28+
2929
$store = PatternData::get();
3030
foreach ($store as $patternStoreKey => $patternStoreData) {
31-
31+
3232
if ($patternStoreData["category"] == "pattern") {
33-
34-
$value = "../../patterns/".$patternStoreData["pathDash"]."/".$patternStoreData["pathDash"].".html";
35-
Data::setOptionLink($patternStoreKey, $value);
36-
33+
34+
if (isset($patternStoreData["pathDash"])) {
35+
$value = "../../patterns/".$patternStoreData["pathDash"]."/".$patternStoreData["pathDash"].".html";
36+
Data::setOptionLink($patternStoreKey, $value);
37+
}
38+
3739
}
38-
40+
3941
}
40-
42+
4143
}
42-
44+
4345
}

0 commit comments

Comments
 (0)