public function run() { // find the value given to the command $package = Console::findCommandOptionValue("p|package"); if ($package) { // if <prompt> was passed ask the user for the package name if ($package == "prompt") { $prompt = "what is the name of the package you want to fetch?"; $options = "(ex. pattern-lab/plugin-kss)"; $package = Console::promptInput($prompt, $options); } // make sure it looks like a valid package if (strpos($package, "/") === false) { Console::writeError("that wasn't a valid package name. it should look like <info>pattern-lab/plugin-kss</info>..."); } // run composer via fetch $f = new Fetch(); $f->fetchPackage($package); } else { Console::writeHelpCommand($this->command); } }
protected function starterKitSuggestions() { Console::writeLine(""); $composerPath = Config::getOption("baseDir") . "/composer.json"; if (file_exists($composerPath)) { $json = file_get_contents($composerPath); $data = json_decode($json, true); if ($jsonErrorMessage = JSON::hasError()) { JSON::lastErrorMsg(Console::getHumanReadablePath($oldStyleAnnotationsPath), $jsonErrorMessage, $data); } if (isset($data["extra"]) && isset($data["extra"]["patternlab"]) && isset($data["extra"]["patternlab"]["starterKitSuggestions"])) { $starterKitSuggestions = $data["extra"]["patternlab"]["starterKitSuggestions"]; Console::writeInfo("suggested starterkits that work with this edition:", false, true); foreach ($starterKitSuggestions as $i => $suggestion) { $num = $i + 1; Console::writeLine($num . ": " . $suggestion, true); } // hack around installer util feature in Console::promptInput InstallerUtil::$isInteractive = true; // prompt for input on the suggestions Console::writeLine(""); $prompt = "choose an option or hit return to cancel:"; $options = "(ex. 1)"; $input = Console::promptInput($prompt, $options, "1"); $result = (int) $input - 1; if (isset($starterKitSuggestions[$result])) { Console::writeLine(""); $f = new Fetch(); $result = $f->fetchStarterKit($starterKitSuggestions[$result]); } } else { Console::writeWarning("this edition has no starterkits to suggested...", false, true); } } else { Console::writeError("can't find composer.json to get suggestions...", false, true); } }
/** * Force mirror the dist/ folder to source/ * @param {String} path to the source directory * @param {String} path to the temp dist directory */ protected function mirrorDist($sourceDir, $tempDirDist) { // set default vars $fsOptions = array(); $emptyDir = true; $validFiles = array("README", ".gitkeep", ".DS_Store", "styleguide"); // see if the source directory is empty if (is_dir($sourceDir)) { $objects = new \DirectoryIterator($sourceDir); foreach ($objects as $object) { if (!$object->isDot() && !in_array($object->getFilename(), $validFiles)) { $emptyDir = false; break; } } } // if source directory isn't empty ask if it's ok to nuke what's there if (!$emptyDir) { $prompt = "a starterkit is already installed. merge the new files with it or replace it?"; $options = "M/r"; $input = Console::promptInput($prompt, $options); $fsOptions = $input == "r" ? array("delete" => true, "override" => true) : array("delete" => false, "override" => false); } // mirror dist to source Console::writeInfo("installing the starterkit files..."); $fs = new Filesystem(); $fs->mirror($tempDirDist, $sourceDir, null); Console::writeInfo("starterkit files have been installed..."); }
/** * Update a single config option based on a change in composer.json * @param {String} the name of the option to be changed * @param {String} the new value of the option to be changed */ public static function updateConfigOption($optionName, $optionValue) { if (is_string($optionValue) && strpos($optionValue, "<prompt>") !== false) { // prompt for input using the supplied query $prompt = str_replace("</prompt>", "", str_replace("<prompt>", "", $optionValue)); $options = ""; $input = Console::promptInput($prompt, $options, false); self::writeUpdateConfigOption($optionName, $input); Console::writeTag("ok", "config option " . $optionName . " updated...", false, true); } else { if (!isset(self::$options[$optionName]) || self::$options["overrideConfig"] == "a") { // if the option isn't set or the config is always to override update the config self::writeUpdateConfigOption($optionName, $optionValue); } else { if (self::$options["overrideConfig"] == "q") { // standardize the values for comparison $currentOptionValue = is_array(self::$options[$optionName]) ? implode(", ", self::$options[$optionName]) : self::$options[$optionName]; $newOptionValue = is_array($optionValue) ? implode(", ", $optionValue) : $optionValue; if ($currentOptionValue != $newOptionValue) { // prompt for input $prompt = "update the config option <desc>" . $optionName . " (" . $currentOptionValue . ")</desc> with the value <desc>" . $newOptionValue . "</desc>?"; $options = "Y/n"; $input = Console::promptInput($prompt, $options); if ($input == "y") { self::writeUpdateConfigOption($optionName, $optionValue); Console::writeInfo("config option " . $optionName . " updated...", false, true); } else { Console::writeWarning("config option <desc>" . $optionName . "</desc> not updated...", false, true); } } } } } }
protected function starterKitPathPrompt() { // need to figure this out long-term InstallerUtil::$isInteractive = true; $input = Console::promptInput("Tell me the path to the starterkit you want to watch.", "e.g. vendor/pattern-lab/starterkit-mustache-demo/dist", "baz", false); // set-up the full starterkit path $starterKitPath = Config::getOption("baseDir") . $input; if (!is_dir($starterKitPath)) { Console::writeWarning("that doesn't seem to be a real directory. let's try again..."); $starterKitPath = $this->starterKitPathPrompt(); } return $starterKitPath; }
/** * Ask questions after the create package is done * @param {Object} a script event object from composer */ public static function postCreateProjectCmd($event) { // see if there is an extra component $extra = $event->getComposer()->getPackage()->getExtra(); if (isset($extra["patternlab"])) { self::init(); Console::writeLine(""); // see if we have any starterkits to suggest if (isset($extra["patternlab"]["starterKitSuggestions"]) && is_array($extra["patternlab"]["starterKitSuggestions"])) { $suggestions = $extra["patternlab"]["starterKitSuggestions"]; // suggest starterkits Console::writeInfo("suggested starterkits that work with this edition:", false, true); foreach ($suggestions as $i => $suggestion) { // write each suggestion $num = $i + 1; Console::writeLine($num . ": " . $suggestion, true); } // prompt for input on the suggestions Console::writeLine(""); $prompt = "choose an option or hit return to skip:"; $options = "(ex. 1)"; $input = Console::promptInput($prompt, $options); $result = (int) $input - 1; if (isset($suggestions[$result])) { Console::writeLine(""); $f = new Fetch(); $result = $f->fetchStarterKit($suggestions[$result]); if ($result) { Console::writeLine(""); $g = new Generator(); $g->generate(array("foo" => "bar")); Console::writeLine(""); Console::writeInfo("type <desc>php core/console --server</desc> to start the built-in server and see Pattern Lab...", false, true); } } else { Console::writeWarning("you will need to install a StarterKit before using Pattern Lab..."); } } } }
/** * Prompt the user to install a starterkit * @param {Array} the starterkit suggestions */ protected static function promptStarterKitInstall($starterKitSuggestions) { Console::writeLine(""); // suggest starterkits Console::writeInfo("suggested starterkits that work with this edition:", false, true); foreach ($starterKitSuggestions as $i => $suggestion) { $num = $i + 1; Console::writeLine($num . ": " . $suggestion, true); } // prompt for input on the suggestions Console::writeLine(""); $prompt = "choose an option or hit return to skip:"; $options = "(ex. 1)"; $input = Console::promptInput($prompt, $options, "1"); $result = (int) $input - 1; if (isset($starterKitSuggestions[$result])) { Console::writeLine(""); $f = new Fetch(); $result = $f->fetchStarterKit($starterKitSuggestions[$result]); } }
/** * Update a single config option based on a change in composer.json * @param {String} the name of the option to be changed * @param {String} the new value of the option to be changed * @param {Boolean} whether to force the update of the option */ public static function updateConfigOption($optionName, $optionValue, $force = false) { if (is_string($optionValue) && strpos($optionValue, "<prompt>") !== false) { // prompt for input using the supplied query $options = ""; $default = ""; $prompt = str_replace("</prompt>", "", str_replace("<prompt>", "", $optionValue)); if (strpos($prompt, "<default>") !== false) { $default = explode("<default>", $prompt); $default = explode("</default>", $default[1]); $default = $default[0]; } $input = Console::promptInput($prompt, $options, $default, false); self::writeUpdateConfigOption($optionName, $input); Console::writeTag("ok", "config option " . $optionName . " updated...", false, true); } else { if (!isset(self::$options[$optionName]) || self::$options["overrideConfig"] == "a" || $force) { // if the option isn't set or the config is always to override update the config self::writeUpdateConfigOption($optionName, $optionValue); } else { if (self::$options["overrideConfig"] == "q") { // standardize the values for comparison $currentOption = self::getOption($optionName); $currentOptionValue = $currentOption; $newOptionValue = $optionValue; $optionNameOutput = $optionName; // dive into plugins to do a proper comparison to if ($optionName == "plugins") { // replace the data in anticipation of it being used $optionValue = array_replace_recursive($currentOption, $newOptionValue); // get the key of the plugin that is being added/updated reset($newOptionValue); $newOptionKey = key($newOptionValue); if (!array_key_exists($newOptionKey, $currentOptionValue)) { // if the key doesn't exist just write out the new config and move on self::writeUpdateConfigOption($optionName, $optionValue); return; } else { // see if the existing configs for the plugin exists. if so just return with no changes if ($newOptionValue[$newOptionKey] == $currentOptionValue[$newOptionKey]) { return; } else { $optionNameOutput = $optionName . "." . $newOptionKey; } } } if ($currentOptionValue != $newOptionValue) { // prompt for input if (is_array($currentOptionValue)) { $prompt = "update the config option <desc>" . $optionNameOutput . "</desc> with the value from the package install?"; } else { $prompt = "update the config option <desc>" . $optionNameOutput . " (" . $currentOptionValue . ")</desc> with the value <desc>" . $newOptionValue . "</desc>?"; } $options = "Y/n"; $input = Console::promptInput($prompt, $options, "Y"); if ($input == "y") { // update the config option self::writeUpdateConfigOption($optionName, $optionValue); Console::writeInfo("config option " . $optionNameOutput . " updated...", false, true); } else { Console::writeWarning("config option <desc>" . $optionNameOutput . "</desc> not updated...", false, true); } } } } } }