Skip to content

Commit ed79d41

Browse files
committed
adding --suggestions flag to starterkit command
1 parent 144d6da commit ed79d41

File tree

1 file changed

+80
-7
lines changed

1 file changed

+80
-7
lines changed

src/PatternLab/Console/Commands/StarterKitCommand.php

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use \PatternLab\Console;
1515
use \PatternLab\Console\Command;
1616
use \PatternLab\Fetch;
17+
use \PatternLab\InstallerUtil;
18+
use \PatternLab\JSON;
1719
use \PatternLab\Timer;
1820

1921
class StarterKitCommand extends Command {
@@ -27,32 +29,103 @@ public function __construct() {
2729
Console::setCommand($this->command,"Initialize or fetch a specific StarterKit","The StarterKit command downloads StarterKits.","k");
2830
Console::setCommandOption($this->command,"init","Initialize with a blank StarterKit based on the active PatternEngine.","To initialize your project with a base StarterKit:","i");
2931
Console::setCommandOption($this->command,"install:","Fetch a specific StarterKit from GitHub.","To fetch a StarterKit from GitHub:","j:","<starterkit-name>");
32+
Console::setCommandOption($this->command,"suggestions","Show suggested StarterKits for this Edition. Offer install prompt.","To show suggested StarterKits for this Edition:");
3033

3134
}
3235

3336
public function run() {
3437

3538
// find the value given to the command
36-
$init = Console::findCommandOption("i|init");
37-
$starterkit = Console::findCommandOptionValue("f|install");
39+
$init = Console::findCommandOption("i|init");
40+
$starterkit = Console::findCommandOptionValue("f|install");
41+
$suggestions = Console::findCommandOption("suggestions");
3842

43+
if ($suggestions) {
44+
45+
$this->starterKitSuggestions();
46+
47+
} else if ($init || $starterkit) {
48+
49+
$this->starterKitInstall($starterkit, $init);
50+
51+
} else {
52+
53+
Console::writeHelpCommand($this->command);
54+
55+
}
56+
57+
}
58+
59+
protected function starterKitInstall($starterkit, $init) {
60+
61+
// set-up the base starterkit
3962
if ($init) {
4063
$patternEngine = Config::getOption("patternExtension");
4164
$starterkit = "pattern-lab/starterkit-".$patternEngine."-base";
4265
}
4366

44-
if ($starterkit) {
67+
// download the starterkit
68+
$f = new Fetch();
69+
$f->fetchStarterKit($starterkit);
70+
71+
}
72+
73+
protected function starterKitSuggestions() {
74+
75+
Console::writeLine("");
76+
77+
$composerPath = Config::getOption("baseDir")."/composer.json";
78+
if (file_exists($composerPath)) {
79+
80+
$json = file_get_contents($composerPath);
81+
$data = json_decode($json,true);
82+
if ($jsonErrorMessage = JSON::hasError()) {
83+
JSON::lastErrorMsg(Console::getHumanReadablePath($oldStyleAnnotationsPath),$jsonErrorMessage,$data);
84+
}
4585

46-
// download the starterkit
47-
$f = new Fetch();
48-
$f->fetchStarterKit($starterkit);
86+
if (isset($data["extra"]) && isset($data["extra"]["patternlab"]) && isset($data["extra"]["patternlab"]["starterKitSuggestions"])) {
87+
88+
$starterKitSuggestions = $data["extra"]["patternlab"]["starterKitSuggestions"];
89+
90+
Console::writeInfo("suggested starterkits that work with this edition:", false, true);
91+
foreach ($starterKitSuggestions as $i => $suggestion) {
92+
$num = $i + 1;
93+
Console::writeLine($num.": ".$suggestion, true);
94+
}
95+
96+
// hack around installer util feature in Console::promptInput
97+
InstallerUtil::$isInteractive = true;
98+
99+
// prompt for input on the suggestions
100+
Console::writeLine("");
101+
102+
$prompt = "choose an option or hit return to cancel:";
103+
$options = "(ex. 1)";
104+
$input = Console::promptInput($prompt,$options,"1");
105+
$result = (int)$input - 1;
106+
107+
if (isset($starterKitSuggestions[$result])) {
108+
109+
Console::writeLine("");
110+
$f = new Fetch();
111+
$result = $f->fetchStarterKit($starterKitSuggestions[$result]);
112+
113+
}
114+
115+
} else {
116+
117+
Console::writeWarning("this edition has no starterkits to suggested...", false, true);
118+
119+
}
49120

50121
} else {
51122

52-
Console::writeHelpCommand($this->command);
123+
Console::writeError("can't find composer.json to get suggestions...", false, true);
53124

54125
}
55126

127+
128+
56129
}
57130

58131
}

0 commit comments

Comments
 (0)