Skip to content

Commit 60e8f0c

Browse files
committed
update NewCommand
update the compatibility with CommandLine api improve the logic of the command remove show help method and use HelpBuilder instead
1 parent 2c3fe2d commit 60e8f0c

File tree

1 file changed

+46
-104
lines changed

1 file changed

+46
-104
lines changed

src/Commands/NewCommand.php

Lines changed: 46 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -9,92 +9,87 @@
99

1010
namespace DevNet\Cli\Commands;
1111

12-
use DevNet\System\Command\CommandArgument;
12+
use DevNet\Cli\Templating\TemplateProvider;
13+
use DevNet\Cli\Templating\TemplateRegistry;
1314
use DevNet\System\Command\CommandEventArgs;
1415
use DevNet\System\Command\CommandLine;
15-
use DevNet\System\Command\CommandOption;
16-
use DevNet\System\Command\ICommandHandler;
1716
use DevNet\System\IO\ConsoleColor;
1817
use DevNet\System\IO\Console;
1918

20-
class NewCommand extends CommandLine implements ICommandHandler
19+
class NewCommand extends CommandLine
2120
{
21+
private TemplateRegistry $registry;
22+
2223
public function __construct()
2324
{
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');
3126

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);
4030

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'));
4533

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');
4952

50-
if (!$template || !$template->Value) {
53+
if (!$template || !$template->getValue()) {
5154
Console::foreGroundColor(ConsoleColor::Red);
52-
Console::writeline("Template argument is missing!");
55+
Console::writeLine("Template argument is missing!");
5356
Console::resetColor();
54-
exit;
57+
return;
5558
}
5659

57-
if ($project) {
58-
if (!$project->Value) {
60+
if ($output) {
61+
if (!$output->getValue()) {
5962
Console::foreGroundColor(ConsoleColor::Red);
60-
Console::writeline('Project argument is missing!');
63+
Console::writeLine('Directory argument is missing!');
6164
Console::resetColor();
62-
exit;
65+
return;
6366
}
64-
$basePath = $project->Value;
67+
$path = $output->getValue();
6568
}
6669

67-
$destination = implode("/", [getcwd(), $basePath]);
68-
$templateName = $template->Value ?? '';
70+
$destination = implode("/", [getcwd(), $path]);
71+
$templateName = $template->getValue() ?? '';
6972
$templateName = strtolower($templateName);
73+
$provider = $this->registry->get($templateName);
7074

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())) {
7976
Console::foregroundColor(ConsoleColor::Red);
80-
Console::writeline("The template {$templateName} does not exist!");
77+
Console::writeLine("The template {$templateName} does not exist!");
8178
Console::resetColor();
82-
exit;
79+
return;
8380
}
8481

85-
$result = self::createProject($source, $destination);
82+
$result = self::createProject($provider->getSourcePath(), $destination);
8683

8784
if ($result) {
8885
Console::foregroundColor(ConsoleColor::Green);
89-
Console::writeline("The {$templateName} project was created successfully.");
86+
Console::writeLine("The template {$templateName} project was created successfully.");
9087
Console::resetColor();
9188
} else {
9289
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.");
9491
Console::resetColor();
9592
}
96-
97-
exit;
9893
}
9994

10095
public static function createProject(string $src, string $dst): bool
@@ -122,57 +117,4 @@ public static function createProject(string $src, string $dst): bool
122117

123118
return true;
124119
}
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-
}
178120
}

0 commit comments

Comments
 (0)