Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 7d89292

Browse files
authored
don't codegen top-level code (#17)
1 parent 329ed4b commit 7d89292

2 files changed

Lines changed: 42 additions & 27 deletions

File tree

src/RouterCLILookupCodegenBuilder.hack

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,20 @@ final class RouterCLILookupCodegenBuilder {
7272
->setShebangLine('#!/usr/bin/env hhvm')
7373
->setFileType(CodegenFileType::HACK_PARTIAL)
7474
->setGeneratedFrom($this->generatedFrom)
75-
->setPseudoMainHeader($this->getInitCode())
7675
->addClass($this->getCodegenClass($router_classname, $utility_classname))
77-
->setPseudoMainFooterf(
78-
'(new %s())->main($argv);',
79-
$utility_classname,
76+
->addFunction(
77+
$this->cg->codegenFunction('hack_router_cli_lookup_generated_main')
78+
->addEmptyUserAttribute('__EntryPoint')
79+
->setReturnType('void')
80+
->setBodyf(
81+
"%s\n".
82+
'$argv = '.
83+
'\\Facebook\\TypeAssert\\matches<KeyedContainer<int, string>>('.
84+
"\\HH\\global_get('argv'));\n".
85+
"(new %s())->main(\$argv);\n",
86+
$this->getInitCode(),
87+
$utility_classname,
88+
),
8089
);
8190

8291
if ($namespace !== null) {
@@ -165,8 +174,7 @@ final class RouterCLILookupCodegenBuilder {
165174
'/../',
166175
};
167176
$autoloader_files = ImmSet {
168-
'hh_autoload.php',
169-
'autoload.php',
177+
'autoload.hack',
170178
};
171179
$full_autoloader_files = Set { };
172180
foreach ($autoloader_files as $file) {
@@ -201,6 +209,7 @@ final class RouterCLILookupCodegenBuilder {
201209
->addLine('exit(1);')
202210
->endIfBlock()
203211
->addLine('require_once($autoloader);')
212+
->addLine('\\Facebook\\AutoloadMap\\initialize();')
204213
->endManualSection()
205214
->getCode();
206215
}

tests/examples/codegen/lookup-path.php

100755100644
Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,37 @@
77
* To re-generate this file run vendor/hhvm/hacktest/bin/hacktest
88
*
99
*
10-
* @partially-generated SignedSource<<584ccd4103e1cb5ddaa5cdc80ba52017>>
10+
* @partially-generated SignedSource<<c6a69f51450330c9524afac2290c7b3d>>
1111
*/
1212
namespace Facebook\HackRouter\CodeGen\Tests\Generated;
13-
/* BEGIN MANUAL SECTION init */
14-
$autoloader = null;
15-
$autoloader_candidates = ImmSet {
16-
__DIR__.'/vendor/hh_autoload.php',
17-
__DIR__.'/../vendor/hh_autoload.php',
18-
__DIR__.'/../../vendor/hh_autoload.php',
19-
__DIR__.'/../../../vendor/hh_autoload.php',
20-
};
21-
foreach ($autoloader_candidates as $candidate) {
22-
if (\file_exists($candidate)) {
23-
$autoloader = $candidate;
24-
break;
13+
14+
<<__EntryPoint>>
15+
function hack_router_cli_lookup_generated_main(): void {
16+
/* BEGIN MANUAL SECTION init */
17+
$autoloader = null;
18+
$autoloader_candidates = ImmSet {
19+
__DIR__.'/vendor/autoload.hack',
20+
__DIR__.'/../vendor/autoload.hack',
21+
__DIR__.'/../../vendor/autoload.hack',
22+
__DIR__.'/../../../vendor/autoload.hack',
23+
};
24+
foreach ($autoloader_candidates as $candidate) {
25+
if (\file_exists($candidate)) {
26+
$autoloader = $candidate;
27+
break;
28+
}
2529
}
30+
if ($autoloader === null) {
31+
\fwrite(\STDERR, "Can't find autoloader.\n");
32+
exit(1);
33+
}
34+
require_once($autoloader);
35+
\Facebook\AutoloadMap\initialize();
36+
/* END MANUAL SECTION */
37+
38+
$argv = \Facebook\TypeAssert\matches<KeyedContainer<int, string>>(\HH\global_get('argv'));
39+
(new MySiteRouterCLILookup())->main($argv);
2640
}
27-
if ($autoloader === null) {
28-
\fwrite(\STDERR, "Can't find autoloader.\n");
29-
exit(1);
30-
}
31-
require_once($autoloader);
32-
/* END MANUAL SECTION */
3341

3442
final class MySiteRouterCLILookup {
3543

@@ -84,5 +92,3 @@ public function main(KeyedContainer<int, string> $argv): void {
8492
}
8593
}
8694
}
87-
88-
(new MySiteRouterCLILookup())->main($argv);

0 commit comments

Comments
 (0)