public function run()
 {
     if (version_compare(phpversion(), '5.4.0', '<')) {
         Console::writeWarning("you must have PHP 5.4.0 or greater to use this feature. you are using PHP " . phpversion() . "...");
     } else {
         // set-up defaults
         $publicDir = Config::getOption("publicDir");
         $coreDir = Config::getOption("coreDir");
         $host = Console::findCommandOptionValue("host");
         $host = $host ? $host : "localhost";
         $port = Console::findCommandOptionValue("port");
         $host = $port ? $host . ":" . $port : $host . ":8080";
         $quiet = Console::findCommandOption("quiet");
         // set-up the base command
         $command = $this->pathPHP . " -S " . $host . " " . $coreDir . "/server/router.php";
         $commands = array();
         $commands[] = array("command" => $command, "cwd" => $publicDir, "timeout" => null, "idle" => 1800);
         // get the watch command info
         if (Console::findCommandOption("with-watch")) {
             $watchCommand = new WatchCommand();
             $commands[] = array("command" => $watchCommand->build() . " --no-procs", "timeout" => null, "idle" => 1800);
         }
         Console::writeInfo("server started on http://" . $host . " - use ctrl+c to exit...");
         $processSpawner = new ProcessSpawner();
         $processSpawner->spawn($commands, $quiet);
     }
 }
 public function run()
 {
     // set-up required vars
     $options = array();
     $options["moveStatic"] = Console::findCommandOption("p|patternsonly") ? false : true;
     $options["noCacheBuster"] = Console::findCommandOption("n|nocache");
     // see if the starterKit flag was passed so you know what dir to watch
     if (Console::findCommandOption("sk")) {
         // load the starterkit watcher
         $w = new Watcher();
         $w->watchStarterKit();
     } else {
         if (Console::findCommandOption("no-procs")) {
             // don't have to worry about loading processes so launch watcher
             // load the generator
             $g = new Generator();
             $g->generate($options);
             // load the watcher
             $w = new Watcher();
             $w->watch($options);
         } else {
             // a vanilla --watch command needs to have a --no-procs version built
             // so we don't get caught in while() loops. re-request the console command
             $commands = array();
             $commands[] = array("command" => $this->build() . " --no-procs", "timeout" => null, "idle" => 600);
             Console::writeInfo("spawning the watch process...");
             $process = new ProcessSpawner();
             $process->spawn($commands);
         }
     }
 }
 /**
  * Moves static files that aren't directly related to Pattern Lab
  * @param  {String}       file name to be moved
  * @param  {String}       copy for the message to be printed out
  * @param  {String}       part of the file name to be found for replacement
  * @param  {String}       the replacement
  */
 public static function moveStaticFile($fileName, $copy = "", $find = "", $replace = "")
 {
     self::moveFile($fileName, str_replace($find, $replace, $fileName));
     Util::updateChangeTime();
     if ($copy != "") {
         Console::writeInfo($fileName . " " . $copy . "...");
     }
 }
 public function run()
 {
     if (Console::findCommandOption("list")) {
         // get all of the options
         $options = Config::getOptions();
         // sort 'em alphabetically
         ksort($options);
         // find length of longest option
         $lengthLong = 0;
         foreach ($options as $optionName => $optionValue) {
             $lengthLong = strlen($optionName) > $lengthLong ? strlen($optionName) : $lengthLong;
         }
         // iterate over each option and spit it out
         foreach ($options as $optionName => $optionValue) {
             $optionValue = is_array($optionValue) ? implode(", ", $optionValue) : $optionValue;
             $optionValue = !$optionValue ? "false" : $optionValue;
             $spacer = Console::getSpacer($lengthLong, strlen($optionName));
             Console::writeLine("<info>" . $optionName . ":</info>" . $spacer . $optionValue);
         }
     } else {
         if (Console::findCommandOption("get")) {
             // figure out which optino was passed
             $searchOption = Console::findCommandOptionValue("get");
             $optionValue = Config::getOption($searchOption);
             // write it out
             if (!$optionValue) {
                 Console::writeError("the --get value you provided, <info>" . $searchOption . "</info>, does not exists in the config...");
             } else {
                 $optionValue = is_array($optionValue) ? implode(", ", $optionValue) : $optionValue;
                 $optionValue = !$optionValue ? "false" : $optionValue;
                 Console::writeInfo($searchOption . ": <ok>" . $optionValue . "</ok>");
             }
         } else {
             if (Console::findCommandOption("set")) {
                 // find the value that was passed
                 $updateOption = Console::findCommandOptionValue("set");
                 $updateOptionBits = explode("=", $updateOption);
                 if (count($updateOptionBits) == 1) {
                     Console::writeError("the --set value should look like <info>optionName=\"optionValue\"</info>. nothing was updated...");
                 }
                 // set the name and value that were passed
                 $updateName = $updateOptionBits[0];
                 $updateValue = $updateOptionBits[1][0] == "\"" || $updateOptionBits[1][0] == "'" ? substr($updateOptionBits[1], 1, strlen($updateOptionBits[1]) - 1) : $updateOptionBits[1];
                 // make sure the option being updated already exists
                 $currentValue = Config::getOption($updateName);
                 if (!$currentValue) {
                     Console::writeError("the --set option you provided, <info>" . $updateName . "</info>, does not exists in the config. nothing will be updated...");
                 } else {
                     Config::updateConfigOption($updateName, $updateValue);
                 }
             } else {
                 // no acceptable options were passed so write out the help
                 Console::writeHelpCommand($this->command);
             }
         }
     }
 }
 public function run()
 {
     if (version_compare(phpversion(), '5.4.0', '<')) {
         Console::writeWarning("you must have PHP 5.4.0 or greater to use this feature. you are using PHP " . phpversion() . "...");
     } else {
         // set-up defaults
         $publicDir = Config::getOption("publicDir");
         $coreDir = Config::getOption("coreDir");
         // start-up the server with the router
         Console::writeInfo("server started on localhost:8080. use ctrl+c to exit...");
         passthru("cd " . $publicDir . " && " . $_SERVER["_"] . " -S localhost:8080 " . $coreDir . "/server/router.php");
     }
 }
 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);
     }
 }
Beispiel #7
0
 /**
  * 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);
                     }
                 }
             }
         }
     }
 }
 public function watchStarterKit()
 {
     // default vars
     $starterKitPath = $this->starterKitPathPrompt();
     $sourceDir = Config::getOption("sourceDir");
     $fs = new Filesystem();
     $c = false;
     // track that one loop through the pattern file listing has completed
     $o = new \stdClass();
     // create an object to hold the properties
     $cp = new \stdClass();
     // create an object to hold a clone of $o
     $o->patterns = new \stdClass();
     Console::writeLine("watching your starterkit for changes...");
     // run forever
     while (true) {
         // clone the patterns so they can be checked in case something gets deleted
         $cp = clone $o->patterns;
         $objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($starterKitPath), \RecursiveIteratorIterator::SELF_FIRST);
         // make sure dots are skipped
         $objects->setFlags(\FilesystemIterator::SKIP_DOTS);
         foreach ($objects as $name => $object) {
             // clean-up the file name and make sure it's not one of the pattern lab files or to be ignored
             $fileName = str_replace($starterKitPath . DIRECTORY_SEPARATOR, "", $name);
             // check to see if it's a new directory
             if ($object->isDir() && !isset($o->{$fileName}) && !is_dir($starterKitPath . "/" . $fileName)) {
                 mkdir($sourceDir . "/" . $fileName);
                 $o->{$fileName} = "dir created";
                 // placeholder
                 Console::writeLine($fileName . "/ directory was created...");
             }
             // check to see if it's a new file or a file that has changed
             if (file_exists($name)) {
                 $mt = $object->getMTime();
                 if ($object->isFile() && !isset($o->{$fileName}) && !file_exists($sourceDir . DIRECTORY_SEPARATOR . $fileName)) {
                     $o->{$fileName} = $mt;
                     $fs->copy($starterKitPath . DIRECTORY_SEPARATOR . $fileName, $sourceDir . DIRECTORY_SEPARATOR . $fileName);
                     Console::writeInfo($fileName . " added...");
                 } else {
                     if ($object->isFile() && isset($o->{$fileName}) && $o->{$fileName} != $mt) {
                         $o->{$fileName} = $mt;
                         $fs->copy($starterKitPath . DIRECTORY_SEPARATOR . $fileName, $sourceDir . DIRECTORY_SEPARATOR . $fileName);
                         Console::writeInfo($fileName . " changed...");
                     } else {
                         if (!isset($o->fileName)) {
                             $o->{$fileName} = $mt;
                         }
                     }
                 }
             } else {
                 unset($o->{$fileName});
             }
         }
         $c = true;
         // taking out the garbage. basically killing mustache after each run.
         if (gc_enabled()) {
             gc_collect_cycles();
         }
         // pause for .05 seconds to give the CPU a rest
         usleep(50000);
     }
 }
 public function run()
 {
     Console::writeInfo("you're running <desc>v" . Config::getOption("v") . "</desc> of the PHP version of Pattern Lab...");
 }
 /**
  * Set the given option to the given value
  */
 protected function setOption()
 {
     // find the value that was passed
     $updateOption = Console::findCommandOptionValue("set");
     $updateOptionBits = explode("=", $updateOption);
     if (count($updateOptionBits) == 1) {
         Console::writeError("the --set value should look like <info>optionName=\"optionValue\"</info>. nothing was updated...");
     }
     // set the name and value that were passed
     $updateName = $updateOptionBits[0];
     $updateValue = $updateOptionBits[1][0] == "\"" || $updateOptionBits[1][0] == "'" ? substr($updateOptionBits[1], 1, strlen($updateOptionBits[1]) - 1) : $updateOptionBits[1];
     // make sure the option being updated already exists
     $currentValue = Config::getOption($updateName);
     if (!$currentValue) {
         Console::writeError("the --set option you provided, <info>" . $updateName . "</info>, does not exists in the config. nothing will be updated...");
     } else {
         Config::updateConfigOption($updateName, $updateValue);
         Console::writeInfo("config option updated...");
     }
 }
 /**
  * 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]);
     }
 }
Beispiel #14
0
 /**
  * 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);
                     }
                 }
             }
         }
     }
 }