Skip to content

Commit f6c661a

Browse files
committed
Add Job creation
1 parent 79a93ae commit f6c661a

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Devtools for GimliDuck Framework
2828
|controller <controller_name> |Create a new controller |
2929
|logic <logic_name> |Create a new logic file |
3030
|model <model_name> |Create a new model |
31+
|job <job_name> |Create a new Job |
3132
|version |Prints the version information for gimli |
3233
```
3334

src/App/Actions/Create_Init_Action.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ public function execute(Request $Request): Request_Response {
8585
];
8686
}
8787

88+
$job_defaults = select("Would you like to set job file defaults?", ["Yes", "No"]);
89+
if ($job_defaults === "Yes") {
90+
$devtools_config["jobs"] = [
91+
'default_save_path' => text(label: 'What is the default save path for job files?', default: 'App/Jobs'),
92+
'namespace' => text(label: 'What is the default namespace for job files?', default: 'App\Jobs'),
93+
'extends' => text(label: 'What is the default class to extend for job files?'),
94+
];
95+
}
96+
8897
if ($force_overwrite) {
8998
$continue = confirm("Would you like to continue? Doing so will overwrite your existing configuration file.");
9099
if (!$continue) {

src/App/Actions/Create_Job_Action.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace GimliDev\Actions;
5+
6+
use Clyde\Actions\Action_Base;
7+
use Clyde\Request\Request;
8+
use Clyde\Request\Request_Response;
9+
use GimliDev\Builders\File_Builder;
10+
11+
use function GimliDev\load_config;
12+
use function Laravel\Prompts\confirm;
13+
use function Laravel\Prompts\text;
14+
15+
class Create_Job_Action extends Action_Base {
16+
17+
const RESPONSE_NAMESPACE = "Gimli\Http\Response";
18+
19+
protected array $job_config = [];
20+
21+
/**
22+
* Execute the action
23+
*
24+
* @param Request $Request
25+
* @return Request_Response
26+
*/
27+
public function execute(Request $Request): Request_Response {
28+
29+
$config = load_config();
30+
31+
$this->job_config = $config['jobs'] ?? [
32+
'default_save_path' => 'App/Jobs',
33+
'namespace' => 'App\\Jobs',
34+
'extends' => null
35+
];
36+
37+
return $this->buildSingleAction($Request);
38+
39+
}
40+
41+
/**
42+
* Format the path for the job
43+
*
44+
* @param Request $Request
45+
* @return array
46+
*/
47+
protected function formatPath(Request $Request): array {
48+
$job_name = $Request->getArgument('job_name');
49+
$path = $Request->getArgument('save_path') ?? '';
50+
51+
if (empty($path)) {
52+
$path = text(label: 'Where would you like to save the job?', default: $this->job_config['default_save_path']);
53+
}
54+
55+
$path = $path[0] !== '/' ? '/' . $path : $path;
56+
$path = $path[strlen($path) - 1] !== '/' ? $path . '/' : $path;
57+
$job_path = ROOT . $path . $job_name . '.php';
58+
59+
return [$job_name, $job_path];
60+
}
61+
62+
/**
63+
* Build a Job
64+
*
65+
* @param Request $Request
66+
* @return Request_Response
67+
*/
68+
protected function buildSingleAction(Request $Request): Request_Response {
69+
[$job_name, $job_path] = $this->formatPath($Request);
70+
71+
$namespace = $Request->getArgument('namespace') ?? '';
72+
73+
if (empty($namespace)) {
74+
$namespace = text(label: 'What is the namespace for the job?', default: $this->job_config['namespace']);
75+
}
76+
77+
if (file_exists($job_path)) {
78+
$this->Printer->error('A job with that name already exists.');
79+
exit(1);
80+
}
81+
82+
$extends = confirm('Would you like to extend a base job or class?');
83+
$extends_path = '';
84+
if ($extends) {
85+
$extends_path = text(label: 'What is the namespace of the class you would like to extend?', placeholder: $this->job_config['extends']);
86+
}
87+
88+
$File_Builder = new File_Builder($job_name, $namespace, $extends_path);
89+
$File_Builder->addUseStatements([self::RESPONSE_NAMESPACE]);
90+
$File_Builder->addMethods([
91+
[
92+
'name' => '__invoke',
93+
'return' => self::RESPONSE_NAMESPACE,
94+
'return_name' => 'Response',
95+
'params' => [
96+
[
97+
'name' => 'Response',
98+
'type' => self::RESPONSE_NAMESPACE,
99+
'type_short' => 'Response',
100+
'comment' => 'The Response object'
101+
],
102+
[
103+
'name' => 'subcommand',
104+
'type' => 'string',
105+
'type_short' => 'string',
106+
'comment' => 'Subcommand'
107+
],
108+
[
109+
'name' => 'options',
110+
'type' => 'array',
111+
'type_short' => 'array',
112+
'comment' => 'Array of options and their values'
113+
],
114+
[
115+
'name' => 'flags',
116+
'type' => 'array',
117+
'type_short' => 'array',
118+
'comment' => 'Array of passed flags'
119+
]
120+
]
121+
]
122+
]);
123+
124+
$job = $File_Builder->getClass();
125+
126+
file_put_contents($job_path, $job);
127+
128+
$this->Printer->success("Job created at $job_path");
129+
130+
return new Request_Response(true);
131+
}
132+
}

src/gimli

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use Clyde\Args\Arg_Option;
1515
use Clyde\Commands\Command;
1616
use Clyde\Tools\Emoji;
1717
use GimliDev\Actions\Create_Controller_Action;
18+
use GimliDev\Actions\Create_Job_Action;
1819
use GimliDev\Actions\Create_Logic_Action;
1920
use GimliDev\Actions\Create_Model_Action;
2021

@@ -147,6 +148,28 @@ Application::create('gimli')
147148
->save()
148149
)
149150

151+
// Create a Job file
152+
->command(
153+
Command::create('job <job_name>')
154+
->about('Create a new Job')
155+
->action(Create_Job_Action::class)
156+
->arg(
157+
Arg_Option::create('namespace')
158+
->help('Set the namespace for the Job, remember to double escape \\')
159+
->shortName('n')
160+
->longName('namespace')
161+
->save()
162+
)
163+
->arg(
164+
Arg_Option::create('save_path')
165+
->help('Set the save path for the Job')
166+
->shortName('p')
167+
->longName('save_path')
168+
->save()
169+
)
170+
->save()
171+
)
172+
150173
// TODO:
151174

152175
// Create a new view

0 commit comments

Comments
 (0)