-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathStarterKitCommand.php
More file actions
131 lines (85 loc) · 3.62 KB
/
StarterKitCommand.php
File metadata and controls
131 lines (85 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/*!
* Console StarterKit Command Class
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
*/
namespace PatternLab\Console\Commands;
use \PatternLab\Config;
use \PatternLab\Console;
use \PatternLab\Console\Command;
use \PatternLab\Fetch;
use \PatternLab\InstallerUtil;
use \PatternLab\JSON;
use \PatternLab\Timer;
class StarterKitCommand extends Command {
public function __construct() {
parent::__construct();
$this->command = "starterkit";
Console::setCommand($this->command,"Initialize or fetch a specific StarterKit","The StarterKit command downloads StarterKits.","k");
Console::setCommandOption($this->command,"init","Initialize with a blank StarterKit based on the active PatternEngine.","To initialize your project with a base StarterKit:","i");
Console::setCommandOption($this->command,"install:","Fetch a specific StarterKit from GitHub.","To fetch a StarterKit from GitHub:","j:","<starterkit-name>");
Console::setCommandOption($this->command,"suggestions","Show suggested StarterKits for this Edition. Offer install prompt.","To show suggested StarterKits for this Edition:");
}
public function run() {
// find the value given to the command
$init = Console::findCommandOption("i|init");
$starterkit = Console::findCommandOptionValue("f|install");
$suggestions = Console::findCommandOption("suggestions");
if ($suggestions) {
$this->starterKitSuggestions();
} else if ($init || $starterkit) {
$this->starterKitInstall($starterkit, $init);
} else {
Console::writeHelpCommand($this->command);
}
}
protected function starterKitInstall($starterkit, $init) {
// set-up the base starterkit
if ($init) {
$patternEngine = Config::getOption("patternExtension");
$starterkit = "pattern-lab/starterkit-".$patternEngine."-base";
}
// download the starterkit
$f = new Fetch();
$f->fetchStarterKit($starterkit);
}
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);
}
}
}