Skip to content

Commit ba573a2

Browse files
committed
update RunCommand
update the compatibility with CommandLine api improve the logic of the command
1 parent 97c74f6 commit ba573a2

File tree

1 file changed

+77
-57
lines changed

1 file changed

+77
-57
lines changed

src/Commands/RunCommand.php

Lines changed: 77 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,112 +9,132 @@
99

1010
namespace DevNet\Cli\Commands;
1111

12-
use DevNet\System\Command\CommandEventArgs;
1312
use DevNet\System\Command\CommandLine;
14-
use DevNet\System\Command\CommandOption;
15-
use DevNet\System\Command\ICommandHandler;
13+
use DevNet\System\Command\Help\HelpBuilder;
14+
use DevNet\System\Command\Parsing\Parser;
1615
use DevNet\System\Runtime\LauncherProperties;
1716
use DevNet\System\Runtime\MainMethodRunner;
1817
use DevNet\System\IO\ConsoleColor;
1918
use DevNet\System\IO\Console;
2019

21-
class RunCommand extends CommandLine implements ICommandHandler
20+
class RunCommand extends CommandLine
2221
{
2322
public function __construct()
2423
{
25-
$this->setName('run');
26-
$this->setDescription('Run DevNet Application.');
27-
$this->addOption(new CommandOption('--help', '-h'));
28-
$this->addOption(new CommandOption('--project', '-p'));
29-
$this->addHandler($this);
24+
parent::__construct('run', 'Run a DevNet project');
25+
26+
$this->addOption('--project', "Path to the project file to run, by default 'project.phproj' in current directory", '-p');
3027
}
3128

32-
public function execute(object $sender, CommandEventArgs $args): void
29+
public function invoke(array $args): void
3330
{
34-
$workspace = getcwd();
35-
$mainClass = "Application\Program";
36-
$loader = LauncherProperties::getLoader();
37-
$help = $args->get('--help');
31+
$parser = new Parser();
32+
33+
foreach ($this->getarguments() as $argument) {
34+
$parser->addArgument($argument);
35+
}
36+
37+
foreach ($this->getoptions() as $option) {
38+
$parser->addOption($option);
39+
}
40+
41+
$result = $parser->parse($args);
42+
$parameters = $result->getOptions();
3843

44+
$help = $parameters['--help'] ?? null;
3945
if ($help) {
40-
$this->showHelp();
46+
$help = new HelpBuilder($this);
47+
$help->writeDescription();
48+
$help->writeHeading('Usage:');
49+
$help->writeLine(' devnet run [options] <additional arguments>');
50+
$help->writeLine();
51+
$help->writeOptions();
52+
$help->writeHeading('Additional Arguments:');
53+
$help->writeLine(' Arguments that are passed to the executed application.');
54+
$help->writeLine();
55+
$help->build()->write();
56+
return;
4157
}
4258

43-
$inputs = $args->Inputs;
44-
$project = $args->get('--project');
59+
$this->execute($parameters, $result->getUnparsedTokens());
60+
}
61+
62+
public function execute(array $parameters, array $arguments): void
63+
{
64+
$workspace = getcwd();
65+
$projectPath = getcwd() . "/project.phproj";
66+
$mainClass = "Application\Program";
67+
$loader = LauncherProperties::getLoader();
68+
$project = $parameters['--project'] ?? null;
4569

4670
if ($project) {
47-
if ($project->Value) {
48-
$workspace = $project->Value;
49-
$loader->setWorkspace($workspace);
50-
foreach ($inputs as $key => $arg) {
71+
if ($project->getValue()) {
72+
$projectPath = $project->getValue();
73+
foreach ($arguments as $key => $arg) {
5174
if ($arg == $project->Name) {
52-
unset($inputs[$key]);
53-
unset($inputs[$key + 1]);
54-
$inputs = array_values($inputs);
75+
unset($arguments[$key]);
76+
unset($arguments[$key + 1]);
77+
$arguments = array_values($arguments);
5578
break;
5679
}
5780
}
5881
}
5982
}
6083

61-
if (!file_exists($workspace . "/project.phproj")) {
84+
if (!file_exists($projectPath)) {
6285
Console::foregroundColor(ConsoleColor::Red);
63-
Console::writeline("Couldn't find a project to run in {$workspace}, Ensure if it exists, or pass the correct project path using the option --project.");
86+
Console::writeLine("Couldn't find a project to run in {$workspace}, Ensure if it exists, or pass the correct project path using the option --project.");
6487
Console::resetColor();
65-
exit;
88+
return;
6689
}
6790

68-
$projectFile = simplexml_load_file($workspace . "/project.phproj");
91+
$workspace = dirname($projectPath);
92+
$loader->setWorkspace($workspace);
6993

70-
if ($projectFile) {
71-
$namespace = $projectFile->properties->namespace;
72-
$entrypoint = $projectFile->properties->entrypoint;
73-
$packages = $projectFile->dependencies->package ?? [];
94+
$projectFile = simplexml_load_file($projectPath);
7495

75-
if ($namespace && $entrypoint) {
76-
$namespace = (string)$namespace;
77-
$entrypoint = (string)$entrypoint;
78-
$mainClass = $namespace . "\\" . $entrypoint;
79-
$loader->map($namespace, "/");
80-
}
96+
if (!$projectFile) {
97+
Console::foregroundColor(ConsoleColor::Red);
98+
Console::writeLine("Project file type not supported!");
99+
Console::resetColor();
100+
return;
101+
}
81102

82-
foreach ($packages as $package) {
83-
$include = (string)$package->attributes()->include;
84-
if (file_exists($workspace . '/' . $include)) {
85-
require $workspace . '/' . $include;
86-
}
103+
$namespace = $projectFile->properties->namespace;
104+
$entrypoint = $projectFile->properties->entrypoint;
105+
$packages = $projectFile->dependencies->package ?? [];
106+
107+
if ($namespace && $entrypoint) {
108+
$namespace = (string)$namespace;
109+
$entrypoint = (string)$entrypoint;
110+
$mainClass = $namespace . "\\" . $entrypoint;
111+
$loader->map($namespace, "/");
112+
}
113+
114+
foreach ($packages as $package) {
115+
$include = (string)$package->attributes()->include;
116+
if (file_exists($workspace . '/' . $include)) {
117+
require $workspace . '/' . $include;
87118
}
88119
}
89120

90121
$mainClass = ucwords($mainClass, "\\");
91122

92123
if (!class_exists($mainClass)) {
93124
Console::foregroundColor(ConsoleColor::Red);
94-
Console::writeline("Couldn't find the class {$mainClass} in " . $workspace);
125+
Console::writeLine("Couldn't find the class {$mainClass} in " . $workspace);
95126
Console::resetColor();
96127
exit;
97128
}
98129

99130
if (!method_exists($mainClass, 'main')) {
100131
Console::foregroundColor(ConsoleColor::Red);
101-
Console::writeline("Couldn't find the main method to run, Ensure it exists in the class {$mainClass}");
132+
Console::writeLine("Couldn't find the main method to run, Ensure it exists in the class {$mainClass}");
102133
Console::resetColor();
103134
exit;
104135
}
105136

106-
$runner = new MainMethodRunner($mainClass, $inputs);
137+
$runner = new MainMethodRunner($mainClass, $arguments);
107138
$runner->run();
108139
}
109-
110-
public function showHelp(): void
111-
{
112-
Console::writeline("Usage: devnet run [arguments] [options]");
113-
Console::writeline();
114-
Console::writeline("Options:");
115-
Console::writeline(" --help Displays help for this command.");
116-
Console::writeline(" --project Path to the project to run.");
117-
Console::writeline();
118-
exit;
119-
}
120140
}

0 commit comments

Comments
 (0)