Skip to content

Commit 0cf6625

Browse files
committed
initial fixes to support new install process
1 parent 25c71af commit 0cf6625

File tree

1 file changed

+122
-137
lines changed

1 file changed

+122
-137
lines changed

src/PatternLab/InstallerUtil.php

Lines changed: 122 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,46 @@ protected static function buildFileList($initialList) {
4848
* Common init sequence
4949
*/
5050
protected static function init() {
51-
51+
5252
// start the timer
5353
Timer::start();
54-
54+
5555
// initialize the console to print out any issues
5656
Console::init();
57-
57+
5858
// initialize the config for the pluginDir
5959
$baseDir = __DIR__."/../../../../../";
6060
Config::init($baseDir,false);
61-
61+
62+
// make sure the source dir is set-up
63+
$sourceDir = Config::getOption("sourceDir");
64+
if (!is_dir($sourceDir)) {
65+
mkdir($sourceDir);
66+
}
67+
6268
Dispatcher::init();
6369

6470
}
71+
72+
/**
73+
* Include StarterKit in an array_filter
74+
*/
75+
public static function includeStarterKit($var) {
76+
77+
$result = ($var["type"] == "patternlab-starterkit");
78+
return $result;
79+
80+
}
81+
82+
/**
83+
* Exclude StarterKit in an array_filter
84+
*/
85+
public static function excludeStarterKit($var) {
86+
87+
$result = ($var["type"] != "patternlab-starterkit");
88+
return $result;
89+
90+
}
6591

6692
/**
6793
* Parse the component types to figure out what needs to be moved and added to the component JSON files
@@ -388,123 +414,29 @@ protected static function pathExists($packageName,$path) {
388414
return false;
389415

390416
}
391-
417+
392418
/**
393-
* Run the PL tasks when a package is installed
419+
* Run the PL tasks when Composer runs an install command
420+
* @param {Array} collected package info
394421
* @param {Object} a script event object from composer
395422
*/
396-
public static function postPackageInstall($event) {
397-
398-
// run the console and config inits
399-
self::init();
400-
401-
// run the tasks based on what's in the extra dir
402-
self::runTasks($event,"install");
403-
423+
public static function postInstallCmd($installerInfo, $event) {
424+
425+
self::runTasks($installerInfo);
426+
404427
}
405-
428+
406429
/**
407-
* Run the PL tasks when a package is updated
430+
* Run the PL tasks when Composer runs an update command
431+
* @param {Array} collected package info
408432
* @param {Object} a script event object from composer
409433
*/
410-
public static function postPackageUpdate($event) {
411-
412-
// run the console and config inits
413-
self::init();
414-
415-
self::runTasks($event,"update");
416-
434+
public static function postUpdateCmd($installerInfo, $event) {
435+
436+
self::runTasks($installerInfo);
437+
417438
}
418-
419-
/**
420-
* Ask questions after the create package is done
421-
* @param {Object} a script event object from composer
422-
*/
423-
public static function postCreateProjectCmd($event) {
424-
425-
// see if there is an extra component
426-
$extra = $event->getComposer()->getPackage()->getExtra();
427-
428-
if (isset($extra["patternlab"])) {
429-
430-
self::init();
431-
Console::writeLine("");
432-
433-
// see if we have any starterkits to suggest
434-
if (isset($extra["patternlab"]["starterKitSuggestions"]) && is_array($extra["patternlab"]["starterKitSuggestions"])) {
435-
436-
$suggestions = $extra["patternlab"]["starterKitSuggestions"];
437-
438-
// suggest starterkits
439-
Console::writeInfo("suggested starterkits that work with this edition:", false, true);
440-
foreach ($suggestions as $i => $suggestion) {
441-
442-
// write each suggestion
443-
$num = $i + 1;
444-
Console::writeLine($num.": ".$suggestion, true);
445-
446-
}
447-
448-
// prompt for input on the suggestions
449-
Console::writeLine("");
450-
$prompt = "choose an option or hit return to skip:";
451-
$options = "(ex. 1)";
452-
$input = Console::promptInput($prompt,$options);
453-
$result = (int)$input - 1;
454-
455-
if (isset($suggestions[$result])) {
456-
457-
Console::writeLine("");
458-
$f = new Fetch();
459-
$result = $f->fetchStarterKit($suggestions[$result]);
460-
461-
if ($result) {
462-
463-
Console::writeLine("");
464-
$g = new Generator();
465-
$g->generate(array("foo" => "bar"));
466-
467-
Console::writeLine("");
468-
Console::writeInfo("type <desc>php core/console --server</desc> to start the built-in server and see Pattern Lab...", false, true);
469-
470-
}
471-
472-
} else {
473-
474-
Console::writeWarning("you will need to install a StarterKit before using Pattern Lab...");
475-
476-
}
477-
478-
}
479-
480-
}
481-
482-
}
483-
484-
/**
485-
* Make sure certain things are set-up before running the installation of a package
486-
* @param {Object} a script event object from composer
487-
*/
488-
public static function preInstallCmd($event) {
489-
490-
// run the console and config inits
491-
self::init();
492-
493-
// default vars
494-
$sourceDir = Config::getOption("sourceDir");
495-
$packagesDir = Config::getOption("packagesDir");
496-
497-
// check directories
498-
if (!is_dir($sourceDir)) {
499-
mkdir($sourceDir);
500-
}
501-
502-
if (!is_dir($packagesDir)) {
503-
mkdir($packagesDir);
504-
}
505-
506-
}
507-
439+
508440
/**
509441
* Make sure pattern engines and listeners are removed on uninstall
510442
* @param {Object} a script event object from composer
@@ -518,7 +450,7 @@ public static function prePackageUninstallCmd($event) {
518450
$package = $event->getOperation()->getPackage();
519451
$type = $package->getType();
520452
$name = $package->getName();
521-
$pathBase = Config::getOption("packagesDir")."/".$name;
453+
$pathBase = $package->getTargetDir();
522454

523455
// see if the package has a listener and remove it
524456
self::scanForListener($pathBase,true);
@@ -531,6 +463,50 @@ public static function prePackageUninstallCmd($event) {
531463
// go over .json in patternlab-components/, remove references to packagename
532464

533465
}
466+
467+
/**
468+
* Prompt the user to install a starterkit
469+
* @param {Array} the starterkit suggestions
470+
*/
471+
protected static function promptStarterKitInstall($starterKitSuggestions) {
472+
473+
Console::writeLine("");
474+
475+
// suggest starterkits
476+
Console::writeInfo("suggested starterkits that work with this edition:", false, true);
477+
foreach ($starterKitSuggestions as $i => $suggestion) {
478+
$num = $i + 1;
479+
Console::writeLine($num.": ".$suggestion, true);
480+
}
481+
482+
// prompt for input on the suggestions
483+
Console::writeLine("");
484+
485+
$prompt = "choose an option or hit return to skip:";
486+
$options = "(ex. 1)";
487+
$input = Console::promptInput($prompt,$options);
488+
$result = (int)$input - 1;
489+
490+
if (isset($starterKitSuggestions[$result])) {
491+
492+
Console::writeLine("");
493+
$f = new Fetch();
494+
$result = $f->fetchStarterKit($suggestions[$result]);
495+
496+
if ($result) {
497+
498+
Console::writeLine("");
499+
$g = new Generator();
500+
$g->generate(array("foo" => "bar"));
501+
502+
Console::writeLine("");
503+
Console::writeInfo("type <desc>php core/console --server</desc> to start the built-in server and see Pattern Lab...", false, true);
504+
505+
}
506+
507+
}
508+
509+
}
534510

535511
/**
536512
* Remove dots from the path to make sure there is no file system traversal when looking for or writing files
@@ -550,30 +526,39 @@ protected static function removeDots($path) {
550526

551527
/**
552528
* Handle some Pattern Lab specific tasks based on what's found in the package's composer.json file
553-
* @param {Object} a script event object from composer
554-
* @param {String} the type of event starting the runTasks command
529+
* @param {Array} the info culled from installing various pattern lab-related packages
555530
*/
556-
protected static function runTasks($event,$type) {
557-
558-
// get package info
559-
$package = ($type == "install") ? $event->getOperation()->getPackage() : $event->getOperation()->getTargetPackage();
560-
$extra = $package->getExtra();
561-
$type = $package->getType();
562-
$name = $package->getName();
563-
$pathBase = Config::getOption("packagesDir")."/".$name;
564-
$pathDist = $pathBase."/dist/";
565-
566-
// make sure we're only evaluating pattern lab packages
567-
if (strpos($type,"patternlab-") !== false) {
568-
531+
protected static function runTasks($installerInfo) {
532+
533+
// initialize a bunch of stuff like config and console
534+
self::init();
535+
536+
// make sure user is prompted to install starterkit
537+
if (!empty($installerInfo["starterKitSuggestions"])) {
538+
self::promptStarterKitInstall($installerInfo["starterKitSuggestions"]);
539+
}
540+
541+
// reorder packages so the starterkit is first if it's being installed as a package
542+
$packages = $installerInfo["packages"];
543+
$packages = array_merge(array_filter($packages, "self::includeStarterKit"), array_filter($packages, "self::excludeStarterKit"));
544+
545+
foreach ($packages as $package) {
546+
547+
// set-up package info
548+
$extra = $package["extra"];
549+
$type = $package["type"];
550+
$name = $package["name"];
551+
$pathBase = $package["pathBase"];
552+
$pathDist = $package["pathDist"];
553+
569554
// make sure that it has the name-spaced section of data to be parsed. if it exists parse it
570-
if (isset($extra["patternlab"])) {
571-
self::parseComposerExtraList($extra["patternlab"], $name, $pathDist);
555+
if (!empty($extra)) {
556+
self::parseComposerExtraList($extra, $name, $pathDist);
572557
}
573-
558+
574559
// see if the package has a listener
575560
self::scanForListener($pathBase);
576-
561+
577562
// address other specific needs based on type
578563
if ($type == "patternlab-patternengine") {
579564
self::scanForPatternEngineRule($pathBase);
@@ -582,9 +567,9 @@ protected static function runTasks($event,$type) {
582567
} else if (($type == "patternlab-styleguidekit") && (strpos($name,"-assets-") === false)) {
583568
Config::updateConfigOption("styleguideKit",$name);
584569
}
585-
570+
586571
}
587-
572+
588573
}
589574

590575
/**
@@ -594,7 +579,7 @@ protected static function runTasks($event,$type) {
594579
protected static function scanForListener($pathPackage,$remove = false) {
595580

596581
// get listener list path
597-
$pathList = Config::getOption("packagesDir")."/listeners.json";
582+
$pathList = Config::getOption("configDir")."/listeners.json";
598583

599584
// make sure listeners.json exists. if not create it
600585
if (!file_exists($pathList)) {
@@ -637,7 +622,7 @@ protected static function scanForListener($pathPackage,$remove = false) {
637622
protected static function scanForPatternEngineRule($pathPackage,$remove = false) {
638623

639624
// get listener list path
640-
$pathList = Config::getOption("packagesDir")."/patternengines.json";
625+
$pathList = Config::getOption("configDir")."/patternengines.json";
641626

642627
// make sure patternengines.json exists. if not create it
643628
if (!file_exists($pathList)) {

0 commit comments

Comments
 (0)