Skip to content

Commit 84fdabe

Browse files
committed
update RunCommand
use launcher to run the application
1 parent 9278591 commit 84fdabe

File tree

1 file changed

+23
-38
lines changed

1 file changed

+23
-38
lines changed

src/Commands/RunCommand.php

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
use DevNet\System\Command\CommandLine;
1313
use DevNet\System\Command\Help\HelpBuilder;
1414
use DevNet\System\Command\Parsing\Parser;
15-
use DevNet\System\Runtime\LauncherProperties;
16-
use DevNet\System\Runtime\MainMethodRunner;
1715
use DevNet\System\IO\ConsoleColor;
1816
use DevNet\System\IO\Console;
17+
use DevNet\System\Runtime\Launcher;
1918

2019
class RunCommand extends CommandLine
2120
{
@@ -61,10 +60,9 @@ public function invoke(array $args): void
6160

6261
public function execute(array $parameters, array $arguments): void
6362
{
64-
$workspace = getcwd();
65-
$projectPath = getcwd() . "/project.phproj";
63+
$projectRoot = getcwd();
64+
$projectPath = $projectRoot . "/project.proj";
6665
$mainClass = "Application\Program";
67-
$loader = LauncherProperties::getLoader();
6866
$project = $parameters['--project'] ?? null;
6967

7068
if ($project) {
@@ -83,14 +81,11 @@ public function execute(array $parameters, array $arguments): void
8381

8482
if (!file_exists($projectPath)) {
8583
Console::foregroundColor(ConsoleColor::Red);
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.");
84+
Console::writeLine("Couldn't find a project to run in {$projectRoot}, Ensure if it exists, or pass the correct project path using the option --project.");
8785
Console::resetColor();
8886
return;
8987
}
9088

91-
$workspace = dirname($projectPath);
92-
$loader->setWorkspace($workspace);
93-
9489
$projectFile = simplexml_load_file($projectPath);
9590

9691
if (!$projectFile) {
@@ -100,41 +95,31 @@ public function execute(array $parameters, array $arguments): void
10095
return;
10196
}
10297

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-
}
98+
$projectRoot = dirname($projectPath);
99+
$mainClass = $projectFile->Properties->EntryPoint;
100+
$packages = $projectFile->Dependencies->Package ?? [];
113101

114102
foreach ($packages as $package) {
115103
$include = (string)$package->attributes()->include;
116-
if (file_exists($workspace . '/' . $include)) {
117-
require $workspace . '/' . $include;
104+
if (file_exists($projectRoot . '/' . $include)) {
105+
require $projectRoot . ' /' . $include;
118106
}
119107
}
120108

121-
$mainClass = ucwords($mainClass, "\\");
122-
123-
if (!class_exists($mainClass)) {
124-
Console::foregroundColor(ConsoleColor::Red);
125-
Console::writeLine("Couldn't find the class {$mainClass} in " . $workspace);
126-
Console::resetColor();
127-
exit;
109+
$launcher = new Launcher($projectRoot);
110+
$result = $launcher->launch($mainClass, $arguments);
111+
112+
switch ($result) {
113+
case 1:
114+
Console::foregroundColor(ConsoleColor::Red);
115+
Console::writeLine("Couldn't find the main class {$mainClass} in " . $projectRoot);
116+
Console::resetColor();
117+
break;
118+
case 2:
119+
Console::foregroundColor(ConsoleColor::Red);
120+
Console::writeLine("Couldn't find the main method to run in the class {$mainClass}");
121+
Console::resetColor();
122+
break;
128123
}
129-
130-
if (!method_exists($mainClass, 'main')) {
131-
Console::foregroundColor(ConsoleColor::Red);
132-
Console::writeLine("Couldn't find the main method to run, Ensure it exists in the class {$mainClass}");
133-
Console::resetColor();
134-
exit;
135-
}
136-
137-
$runner = new MainMethodRunner($mainClass, $arguments);
138-
$runner->run();
139124
}
140125
}

0 commit comments

Comments
 (0)