This repository was archived by the owner on Aug 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCodegen.hack
294 lines (266 loc) · 8.4 KB
/
Codegen.hack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
namespace Facebook\HackRouter;
use type Facebook\HackCodegen\{
CodegenGeneratedFrom,
HackCodegenConfig,
HackCodegenFactory,
IHackCodegenConfig,
};
use type Facebook\DefinitionFinder\{BaseParser, TreeParser};
use type Facebook\HackRouter\PrivateImpl\{ClassFacts, ControllerFacts};
final class Codegen {
const type TUriBuilderOutput = shape(
'file' => string,
?'namespace' => string,
'class' => shape(
'name' => string,
),
?'trait' => shape(
'name' => string,
'method' => string,
),
);
const type TUriBuilderCodegenConfig = shape(
?'baseClass' => classname<UriBuilderCodegenBase<UriBuilderBase>>,
?'parameterCodegenBuilder' => RequestParameterCodegenBuilder,
?'returnSpec' => shape(
'type' => string,
'getter' => string,
),
'output' => (function(
classname<IncludeInUriMap>,
): ?self::TUriBuilderOutput),
);
const type TRequestParametersOutput = shape(
'file' => string,
?'namespace' => string,
'class' => shape(
'name' => string,
),
'trait' => shape(
'name' => string,
),
);
const type TRequestParametersCodegenConfig = shape(
?'getParameters' => RequestParametersCodegenBuilder::TGetParameters,
?'baseClass' =>
classname<RequestParametersCodegenBase<RequestParametersBase>>,
?'parameterCodegenBuilder' => RequestParameterCodegenBuilder,
'trait' => shape(
'methodName' => string,
'getRawParametersCode' => string,
?'requireExtends' => ImmSet<classname<mixed>>,
?'requireImplements' => ImmSet<classname<mixed>>,
),
'output' => (function(
classname<IncludeInUriMap>,
): ?self::TRequestParametersOutput),
);
const type TRouterCodegenConfig = shape(
'file' => string,
?'namespace' => string,
'class' => string,
'abstract' => bool,
?'cliLookup' => shape(
'class' => string,
'file' => string,
),
);
const type TCodegenConfig = shape(
?'hackCodegenConfig' => IHackCodegenConfig,
?'controllerBase' => classname<IncludeInUriMap>,
?'generatedFrom' => CodegenGeneratedFrom,
?'router' => self::TRouterCodegenConfig,
?'uriBuilders' => self::TUriBuilderCodegenConfig,
?'requestParameters' => self::TRequestParametersCodegenConfig,
?'discardChanges' => bool,
);
/**
* @deprecated, use forTreeAsync() instead.
*/
public static function forTree(
string $source_root,
self::TCodegenConfig $config,
): Codegen {
// leaving for now as it's a public API
/* HHAST_IGNORE_ERROR[DontUseAsioJoin] Kept for backward compatibility. */
return \HH\Asio\join(static::forTreeAsync($source_root, $config));
}
public static async function forTreeAsync(
string $source_root,
self::TCodegenConfig $config,
): Awaitable<Codegen> {
return new self(await TreeParser::fromPathAsync($source_root), $config);
}
<<__Memoize>>
private function getGeneratedFrom(): CodegenGeneratedFrom {
return $this->config['generatedFrom'] ??
$this->getHackCodegenFactory()->codegenGeneratedFromScript();
}
public function build(): void {
$this->buildRouter();
$this->buildUriBuilders();
$this->buildRequestParameters();
}
private ControllerFacts<IncludeInUriMap> $controllerFacts;
private function getControllerBase(): classname<IncludeInUriMap> {
return $this->config['controllerBase'] ?? IncludeInUriMap::class;
}
<<__Memoize>>
private function getHackCodegenConfig(): IHackCodegenConfig {
return $this->config['hackCodegenConfig'] ?? new HackCodegenConfig();
}
<<__Memoize>>
private function getHackCodegenFactory(): HackCodegenFactory {
return new HackCodegenFactory($this->getHackCodegenConfig());
}
private function __construct(
BaseParser $parser,
private self::TCodegenConfig $config,
) {
$this->controllerFacts = (
new ControllerFacts($this->getControllerBase(), new ClassFacts($parser))
);
}
private function buildRouter(): void {
$config = Shapes::idx($this->config, 'router');
if ($config === null) {
return;
}
$uri_map = (new UriMapBuilder($this->controllerFacts))->getUriMap();
(
new RouterCodegenBuilder(
$this->getHackCodegenConfig(),
$this->getControllerBase(),
$uri_map,
)
)
->setCreateAbstractClass($config['abstract'])
->setGeneratedFrom($this->getGeneratedFrom())
->setDiscardChanges($this->config['discardChanges'] ?? false)
->renderToFile(
$config['file'],
Shapes::idx($config, 'namespace'),
$config['class'],
);
$cli_config = $config['cliLookup'] ?? null;
if ($cli_config === null) {
return;
}
(new RouterCLILookupCodegenBuilder($this->getHackCodegenConfig()))
->setGeneratedFrom($this->getGeneratedFrom())
->setDiscardChanges($this->config['discardChanges'] ?? false)
->renderToFile(
$cli_config['file'],
Shapes::idx($config, 'namespace'),
$config['class'],
$cli_config['class'],
);
}
private function buildUriBuilders(): void {
$config = Shapes::idx($this->config, 'uriBuilders');
if ($config === null) {
return;
}
$base = $config['baseClass'] ?? UriBuilderCodegen::class;
$param_builder = $config['parameterCodegenBuilder'] ??
new RequestParameterCodegenBuilder($this->getHackCodegenConfig());
$get_output = $config['output'];
$return_spec = $config['returnSpec'] ??
shape(
'getter' => 'getPath',
'type' => 'string',
);
$builder = (
new UriBuilderCodegenBuilder(
$this->getHackCodegenConfig(),
$base,
$param_builder,
$return_spec['getter'],
$return_spec['type'],
)
)
->setGeneratedFrom($this->getGeneratedFrom())
->setDiscardChanges(
Shapes::idx($this->config ?? shape(), 'discardChanges', false),
);
$controllers = $this->controllerFacts->getControllers()->keys();
foreach ($controllers as $controller) {
$output = $get_output($controller);
if ($output === null) {
continue;
}
$builder->renderToFile(
$output['file'],
shape(
'controller' => $controller,
'namespace' => Shapes::idx($output, 'namespace'),
'class' => $output['class'],
'trait' => $output['trait'] ?? null,
),
);
}
}
private function buildRequestParameters(): void {
$config = Shapes::idx($this->config, 'requestParameters');
if ($config === null) {
return;
}
$base = $config['baseClass'] ?? RequestParametersCodegen::class;
$param_builder = $config['parameterCodegenBuilder'] ??
new RequestParameterCodegenBuilder($this->getHackCodegenConfig());
$get_output = $config['output'];
$getParameters = $config['getParameters'] ??
(classname<HasUriPattern> $class) ==> {
return $class::getUriPattern()->getParameters()
->map($param ==> shape('spec' => $param, 'optional' => false));
};
$builder = (
new RequestParametersCodegenBuilder(
$this->getHackCodegenConfig(),
$getParameters,
$config['trait']['getRawParametersCode'],
$base,
$param_builder,
)
)
->setDiscardChanges(
Shapes::idx($this->config ?? shape(), 'discardChanges', false),
)
->setGeneratedFrom($this->getGeneratedFrom());
foreach ($config['trait']['requireExtends'] ?? vec[] as $what) {
$builder->traitRequireExtends($what);
}
foreach ($config['trait']['requireImplements'] ?? vec[] as $what) {
$builder->traitRequireImplements($what);
}
$controllers = $this->controllerFacts->getControllers()->keys();
foreach ($controllers as $controller) {
$output = $get_output($controller);
if ($output === null) {
continue;
}
$builder->renderToFile(
$output['file'],
shape(
'controller' => $controller,
'namespace' => Shapes::idx($output, 'namespace'),
'class' => shape(
'name' => $output['class']['name'],
),
'trait' => shape(
'name' => $output['trait']['name'],
'method' => $config['trait']['methodName'],
),
),
);
}
}
}