Skip to content

Commit f57d3de

Browse files
committed
adding support for composer's 'no interaction' mode
1 parent 9e3affb commit f57d3de

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

src/PatternLab/Config.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,16 @@ public static function updateConfigOption($optionName,$optionValue, $force = fal
329329
if (is_string($optionValue) && strpos($optionValue,"<prompt>") !== false) {
330330

331331
// prompt for input using the supplied query
332-
$prompt = str_replace("</prompt>","",str_replace("<prompt>","",$optionValue));
333332
$options = "";
334-
$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);
335342

336343
self::writeUpdateConfigOption($optionName,$input);
337344
Console::writeTag("ok","config option ".$optionName." updated...", false, true);
@@ -352,7 +359,7 @@ public static function updateConfigOption($optionName,$optionValue, $force = fal
352359
// prompt for input
353360
$prompt = "update the config option <desc>".$optionName." (".$currentOptionValue.")</desc> with the value <desc>".$newOptionValue."</desc>?";
354361
$options = "Y/n";
355-
$input = Console::promptInput($prompt,$options);
362+
$input = Console::promptInput($prompt,$options,"Y");
356363

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

src/PatternLab/Console.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -532,12 +532,13 @@ public static function writeWarning($line,$doubleSpace = false,$doubleBreak = fa
532532
* Prompt the user for some input
533533
* @param {String} the text for the prompt
534534
* @param {String} the text for the options
535+
* @param {String} the text for the default option
535536
* @param {Boolean} if we should lowercase the input before sending it back
536537
* @param {String} the tag that should be used when drawing the content
537538
*
538539
* @return {String} trimmed input given by the user
539540
*/
540-
public static function promptInput($prompt = "", $options = "", $lowercase = true, $tag = "info") {
541+
public static function promptInput($prompt = "", $options = "", $default = "", $lowercase = true, $tag = "info") {
541542

542543
// check prompt
543544
if (empty($prompt)) {
@@ -552,11 +553,18 @@ public static function promptInput($prompt = "", $options = "", $lowercase = tru
552553
// make sure no end-of-line is added
553554
$prompt .= " <nophpeol>";
554555

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

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

src/PatternLab/Fetch.php

Lines changed: 3 additions & 3 deletions
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/InstallerUtil.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ protected static function pathExists($packageName,$path) {
484484
// prompt for input using the supplied query
485485
$prompt = "the path <path>".$humanReadablePath."</path> already exists. merge or replace with the contents of <path>".$packageName."</path> package?";
486486
$options = "M/r";
487-
$input = Console::promptInput($prompt,$options);
487+
$input = Console::promptInput($prompt,$options,"M");
488488

489489
if ($input == "m") {
490490
Console::writeTag("ok","contents of <path>".$humanReadablePath."</path> have been merged with the package's content...", false, true);
@@ -548,7 +548,7 @@ protected static function promptStarterKitInstall($starterKitSuggestions) {
548548

549549
$prompt = "choose an option or hit return to skip:";
550550
$options = "(ex. 1)";
551-
$input = Console::promptInput($prompt,$options);
551+
$input = Console::promptInput($prompt,$options,"1");
552552
$result = (int)$input - 1;
553553

554554
if (isset($starterKitSuggestions[$result])) {

0 commit comments

Comments
 (0)