|
9 | 9 |
|
10 | 10 | namespace DevNet\Cli\Commands; |
11 | 11 |
|
12 | | -use DevNet\System\Command\CommandEventArgs; |
13 | 12 | 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; |
16 | 15 | use DevNet\System\Runtime\LauncherProperties; |
17 | 16 | use DevNet\System\Runtime\MainMethodRunner; |
18 | 17 | use DevNet\System\IO\ConsoleColor; |
19 | 18 | use DevNet\System\IO\Console; |
20 | 19 |
|
21 | | -class RunCommand extends CommandLine implements ICommandHandler |
| 20 | +class RunCommand extends CommandLine |
22 | 21 | { |
23 | 22 | public function __construct() |
24 | 23 | { |
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'); |
30 | 27 | } |
31 | 28 |
|
32 | | - public function execute(object $sender, CommandEventArgs $args): void |
| 29 | + public function invoke(array $args): void |
33 | 30 | { |
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(); |
38 | 43 |
|
| 44 | + $help = $parameters['--help'] ?? null; |
39 | 45 | 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; |
41 | 57 | } |
42 | 58 |
|
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; |
45 | 69 |
|
46 | 70 | 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) { |
51 | 74 | 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); |
55 | 78 | break; |
56 | 79 | } |
57 | 80 | } |
58 | 81 | } |
59 | 82 | } |
60 | 83 |
|
61 | | - if (!file_exists($workspace . "/project.phproj")) { |
| 84 | + if (!file_exists($projectPath)) { |
62 | 85 | 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."); |
64 | 87 | Console::resetColor(); |
65 | | - exit; |
| 88 | + return; |
66 | 89 | } |
67 | 90 |
|
68 | | - $projectFile = simplexml_load_file($workspace . "/project.phproj"); |
| 91 | + $workspace = dirname($projectPath); |
| 92 | + $loader->setWorkspace($workspace); |
69 | 93 |
|
70 | | - if ($projectFile) { |
71 | | - $namespace = $projectFile->properties->namespace; |
72 | | - $entrypoint = $projectFile->properties->entrypoint; |
73 | | - $packages = $projectFile->dependencies->package ?? []; |
| 94 | + $projectFile = simplexml_load_file($projectPath); |
74 | 95 |
|
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 | + } |
81 | 102 |
|
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; |
87 | 118 | } |
88 | 119 | } |
89 | 120 |
|
90 | 121 | $mainClass = ucwords($mainClass, "\\"); |
91 | 122 |
|
92 | 123 | if (!class_exists($mainClass)) { |
93 | 124 | 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); |
95 | 126 | Console::resetColor(); |
96 | 127 | exit; |
97 | 128 | } |
98 | 129 |
|
99 | 130 | if (!method_exists($mainClass, 'main')) { |
100 | 131 | 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}"); |
102 | 133 | Console::resetColor(); |
103 | 134 | exit; |
104 | 135 | } |
105 | 136 |
|
106 | | - $runner = new MainMethodRunner($mainClass, $inputs); |
| 137 | + $runner = new MainMethodRunner($mainClass, $arguments); |
107 | 138 | $runner->run(); |
108 | 139 | } |
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 | | - } |
120 | 140 | } |
0 commit comments