|
9 | 9 |
|
10 | 10 | namespace DevNet\Cli\Commands; |
11 | 11 |
|
12 | | -use DevNet\System\Command\CommandArgument; |
| 12 | +use DevNet\Cli\Templating\TemplateProvider; |
| 13 | +use DevNet\Cli\Templating\TemplateRegistry; |
13 | 14 | use DevNet\System\Command\CommandEventArgs; |
14 | 15 | use DevNet\System\Command\CommandLine; |
15 | | -use DevNet\System\Command\CommandOption; |
16 | | -use DevNet\System\Command\ICommandHandler; |
17 | 16 | use DevNet\System\IO\ConsoleColor; |
18 | 17 | use DevNet\System\IO\Console; |
19 | 18 |
|
20 | | -class NewCommand extends CommandLine implements ICommandHandler |
| 19 | +class NewCommand extends CommandLine |
21 | 20 | { |
| 21 | + private TemplateRegistry $registry; |
| 22 | + |
22 | 23 | public function __construct() |
23 | 24 | { |
24 | | - $this->setName('new'); |
25 | | - $this->setDescription('Create a new project.'); |
26 | | - $this->addArgument(new CommandArgument('template')); |
27 | | - $this->addOption(new CommandOption('--help', '-h')); |
28 | | - $this->addOption(new CommandOption('--project', '-p')); |
29 | | - $this->addHandler($this); |
30 | | - } |
| 25 | + parent::__construct('new', 'Create a new DevNet project'); |
31 | 26 |
|
32 | | - public function execute(object $sender, CommandEventArgs $args): void |
33 | | - { |
34 | | - if ($args->Residual) { |
35 | | - Console::foregroundColor(ConsoleColor::Red); |
36 | | - Console::writeline("The specified argument or option is not valid, try '--help' option for usage information."); |
37 | | - Console::resetColor(); |
38 | | - exit; |
39 | | - } |
| 27 | + $this->addArgument('template', 'The template project want to create'); |
| 28 | + $this->addOption('--output', 'Location to place the generated project output', '-o'); |
| 29 | + $this->setHandler($this); |
40 | 30 |
|
41 | | - $basePath = null; |
42 | | - $template = $args->get('template'); |
43 | | - $help = $args->get('--help'); |
44 | | - $project = $args->get('--project'); |
| 31 | + $this->registry = TemplateRegistry::getSingleton(); |
| 32 | + $this->registry->set('console', new TemplateProvider('console', 'Create a console application', __DIR__ . '/../../template')); |
45 | 33 |
|
46 | | - if ($help) { |
47 | | - $this->showHelp(); |
48 | | - } |
| 34 | + $this->setHelp(function ($builder) { |
| 35 | + $builder->useDefaults(); |
| 36 | + $builder->writeHeading('Templates:'); |
| 37 | + |
| 38 | + $rows = []; |
| 39 | + foreach ($this->registry as $provider) { |
| 40 | + $rows[$provider->getName()] = $provider->getDescription(); |
| 41 | + } |
| 42 | + |
| 43 | + $builder->writeRows($rows); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + public function __invoke(object $sender, CommandEventArgs $args): void |
| 48 | + { |
| 49 | + $path = null; |
| 50 | + $template = $args->getParameter('template'); |
| 51 | + $output = $args->getParameter('--output'); |
49 | 52 |
|
50 | | - if (!$template || !$template->Value) { |
| 53 | + if (!$template || !$template->getValue()) { |
51 | 54 | Console::foreGroundColor(ConsoleColor::Red); |
52 | | - Console::writeline("Template argument is missing!"); |
| 55 | + Console::writeLine("Template argument is missing!"); |
53 | 56 | Console::resetColor(); |
54 | | - exit; |
| 57 | + return; |
55 | 58 | } |
56 | 59 |
|
57 | | - if ($project) { |
58 | | - if (!$project->Value) { |
| 60 | + if ($output) { |
| 61 | + if (!$output->getValue()) { |
59 | 62 | Console::foreGroundColor(ConsoleColor::Red); |
60 | | - Console::writeline('Project argument is missing!'); |
| 63 | + Console::writeLine('Directory argument is missing!'); |
61 | 64 | Console::resetColor(); |
62 | | - exit; |
| 65 | + return; |
63 | 66 | } |
64 | | - $basePath = $project->Value; |
| 67 | + $path = $output->getValue(); |
65 | 68 | } |
66 | 69 |
|
67 | | - $destination = implode("/", [getcwd(), $basePath]); |
68 | | - $templateName = $template->Value ?? ''; |
| 70 | + $destination = implode("/", [getcwd(), $path]); |
| 71 | + $templateName = $template->getValue() ?? ''; |
69 | 72 | $templateName = strtolower($templateName); |
| 73 | + $provider = $this->registry->get($templateName); |
70 | 74 |
|
71 | | - $rootDir = dirname(__DIR__, 3); |
72 | | - $source = $rootDir . '/templates/' . $templateName; |
73 | | - $result = false; |
74 | | - |
75 | | - if ($templateName == 'console' || $templateName == 'web' || $templateName == 'mvc') { |
76 | | - $source .= '-template'; |
77 | | - } |
78 | | - if (!is_dir($source)) { |
| 75 | + if (!$provider || !is_dir($provider->getSourcePath())) { |
79 | 76 | Console::foregroundColor(ConsoleColor::Red); |
80 | | - Console::writeline("The template {$templateName} does not exist!"); |
| 77 | + Console::writeLine("The template {$templateName} does not exist!"); |
81 | 78 | Console::resetColor(); |
82 | | - exit; |
| 79 | + return; |
83 | 80 | } |
84 | 81 |
|
85 | | - $result = self::createProject($source, $destination); |
| 82 | + $result = self::createProject($provider->getSourcePath(), $destination); |
86 | 83 |
|
87 | 84 | if ($result) { |
88 | 85 | Console::foregroundColor(ConsoleColor::Green); |
89 | | - Console::writeline("The {$templateName} project was created successfully."); |
| 86 | + Console::writeLine("The template {$templateName} project was created successfully."); |
90 | 87 | Console::resetColor(); |
91 | 88 | } else { |
92 | 89 | Console::foregroundColor(ConsoleColor::Red); |
93 | | - Console::writeline("Somthing whent wrong! faild to create {$templateName} template."); |
| 90 | + Console::writeLine("Somthing whent wrong! faild to create {$templateName} template."); |
94 | 91 | Console::resetColor(); |
95 | 92 | } |
96 | | - |
97 | | - exit; |
98 | 93 | } |
99 | 94 |
|
100 | 95 | public static function createProject(string $src, string $dst): bool |
@@ -122,57 +117,4 @@ public static function createProject(string $src, string $dst): bool |
122 | 117 |
|
123 | 118 | return true; |
124 | 119 | } |
125 | | - |
126 | | - public function showHelp() |
127 | | - { |
128 | | - Console::writeline("Usage: devnet new [template] [options]"); |
129 | | - Console::writeline(); |
130 | | - Console::writeline("Options:"); |
131 | | - Console::writeline(" --help Displays help for this command."); |
132 | | - Console::writeline(" --project Location of where to place the template project."); |
133 | | - Console::writeline(); |
134 | | - Console::writeline("templates:"); |
135 | | - |
136 | | - $root = dirname(__DIR__, 3); |
137 | | - $list = []; |
138 | | - |
139 | | - if (is_dir($root . '/templates')) { |
140 | | - $list = scandir($root . '/templates'); |
141 | | - } |
142 | | - |
143 | | - // remove current and back directory references (. , ..) |
144 | | - array_shift($list); |
145 | | - array_shift($list); |
146 | | - |
147 | | - $maxLenth = 0; |
148 | | - $metadata = []; |
149 | | - foreach ($list as $name) { |
150 | | - if (file_exists($root . '/templates/' . $name . '/composer.json')) { |
151 | | - $json = file_get_contents($root . '/templates/' . $name . '/composer.json'); |
152 | | - $project = json_decode($json); |
153 | | - |
154 | | - if ($name == 'console-template' || $name == 'web-template' || $name == 'mvc-template') { |
155 | | - $name = strstr($name, '-', true); |
156 | | - } |
157 | | - |
158 | | - $lenth = strlen($name); |
159 | | - if ($lenth > $maxLenth) { |
160 | | - $maxLenth = $lenth; |
161 | | - } |
162 | | - |
163 | | - $metadata[] = ['name' => $name, 'description' => $project->description]; |
164 | | - } |
165 | | - } |
166 | | - |
167 | | - //print template description with auto-alignment |
168 | | - foreach ($metadata as $template) { |
169 | | - $lenth = strlen($template['name']); |
170 | | - $steps = $maxLenth - $lenth + 4; |
171 | | - $space = str_repeat(" ", $steps); |
172 | | - Console::writeline(" {$template['name']}{$space}{$template['description']}"); |
173 | | - } |
174 | | - |
175 | | - Console::writeline(); |
176 | | - exit; |
177 | | - } |
178 | 120 | } |
0 commit comments