Skip to content

Commit 52f05e9

Browse files
committed
improve 'new' command to use installed templates instead of integrated templates
1 parent 4e58a5e commit 52f05e9

28 files changed

+47
-309
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
}
1111
],
1212
"require": {
13-
"devnet/system": "1.1.*"
13+
"devnet/system": "1.1.*",
14+
"devnet/console": "1.1.*"
1415
},
1516
"bin": [
1617
"bin/devnet"

src/Commands/NewCommand.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,15 @@ public function __construct()
2929
$this->addOption('--output', 'Location to place the generated project output', '-o');
3030

3131
$this->registry = TemplateRegistry::getSingleton();
32-
$this->registry->set('console', new TemplateProvider('console', 'Console application.', __DIR__ . '/../../templates/console'));
33-
$this->registry->set('web', new TemplateProvider('web', 'DevNet web application.', __DIR__ . '/../../templates/web'));
32+
$this->registry->set('console', new TemplateProvider(dirname(__DIR__, 3) . '/console'));
33+
34+
if (is_file(dirname(__DIR__, 3) . '/web/composer.json')) {
35+
$this->registry->set('web', new TemplateProvider(dirname(__DIR__, 3) . '/web'));
36+
}
37+
38+
if (is_file(dirname(__DIR__, 3) . '/mvc/composer.json')) {
39+
$this->registry->set('mvc', new TemplateProvider(dirname(__DIR__, 3) . '/mvc'));
40+
}
3441

3542
$this->setHelp(function ($builder) {
3643
$builder->useDefaults();
@@ -72,7 +79,7 @@ public function onExecute(object $sender, CommandEventArgs $args): void
7279
$templateName = strtolower($templateName);
7380
$provider = $this->registry->get($templateName);
7481

75-
if (!$provider || !is_dir($provider->getSourcePath())) {
82+
if (!$provider || !is_dir($provider->getPath())) {
7683
Console::$ForegroundColor = ConsoleColor::Red;
7784
Console::writeLine("The template {$templateName} does not exist!");
7885
Console::resetColor();
@@ -92,20 +99,20 @@ public function onExecute(object $sender, CommandEventArgs $args): void
9299
}
93100
}
94101

95-
public static function createProject(string $src, string $dst): bool
102+
public static function createProject(string $source, string $destination): bool
96103
{
97104
try {
98-
$dir = opendir($src);
99-
if (!is_dir($dst)) {
100-
mkdir($dst, 0777, true);
105+
$dir = opendir($source);
106+
if (!is_dir($destination)) {
107+
mkdir($destination, 0777, true);
101108
}
102109

103110
while ($file = readdir($dir)) {
104111
if ($file !== '.' && $file !== '..' && $file !== '.git') {
105-
if (is_dir($src . '/' . $file)) {
106-
self::createProject($src . '/' . $file, $dst . '/' . $file);
112+
if (is_dir($source . '/' . $file)) {
113+
self::createProject($source . '/' . $file, $destination . '/' . $file);
107114
} else {
108-
copy($src . '/' . $file, $dst . '/' . $file);
115+
copy($source . '/' . $file, $destination . '/' . $file);
109116
}
110117
}
111118
}

src/Plugin/ITemplateProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ public function getDescription(): string;
2424
/**
2525
* Get the template source path
2626
*/
27-
public function getSourcePath(): string;
27+
public function getPath(): string;
2828
}

src/Plugin/TemplateProvider.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,33 @@ class TemplateProvider implements ITemplateProvider
1313
{
1414
protected string $name;
1515
protected string $description;
16-
protected string $sourcePath;
16+
protected string $path;
1717

18-
public function __construct(string $name, string $description, string $sourcePath)
18+
public function __construct(string $sourcePath)
1919
{
20-
$this->name = $name;
21-
$this->description = $description;
22-
$this->sourcePath = $sourcePath;
20+
$this->path = $sourcePath;
21+
if (!is_file($sourcePath . "/composer.json")) {
22+
throw new \Exception("Cannot find 'composer.json' in the template path : " . $sourcePath);
23+
}
24+
25+
$content = file_get_contents($sourcePath . "/composer.json");
26+
$package = json_decode($content);
27+
28+
if (!isset($package->name)) {
29+
throw new \Exception("The template package name is mission in : " . $sourcePath . "/composer.json");
30+
}
31+
32+
$segments = explode('/', $package->name);
33+
if (count($segments) != 2) {
34+
throw new \Exception("The template package name has an invalid format in : " . $sourcePath . "/composer.json");
35+
}
36+
37+
if (!isset($package->description)) {
38+
throw new \Exception("The template package description is mission in : " > $sourcePath . "/composer.json");
39+
}
40+
41+
$this->name = $segments[1];
42+
$this->description = $package->description;
2343
}
2444

2545
/**
@@ -41,8 +61,8 @@ public function getDescription(): string
4161
/**
4262
* Get the template source path
4363
*/
44-
public function getSourcePath(): string
64+
public function getPath(): string
4565
{
46-
return $this->sourcePath;
66+
return $this->path;
4767
}
4868
}

templates/console/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

templates/console/.vscode/launch.json

Lines changed: 0 additions & 21 deletions
This file was deleted.

templates/console/README.md

Lines changed: 0 additions & 29 deletions
This file was deleted.

templates/console/bin/apphost

Lines changed: 0 additions & 34 deletions
This file was deleted.

templates/console/bin/run

Lines changed: 0 additions & 4 deletions
This file was deleted.

templates/console/bin/run.bat

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)