Skip to content

Commit 4d24eb5

Browse files
[web_generator] Setting up web_generator for Dart JS Interop Gen (#368)
This PR is for setting up the web_generator to begin adding support for generating Dart JS interop interface code from TypeScript Declaration Code.
1 parent f1becf0 commit 4d24eb5

29 files changed

+433
-156
lines changed

.github/workflows/dart.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Created with package:mono_repo v6.6.2
1+
# Created with package:mono_repo v6.6.3
22
name: Dart CI
33
on:
44
push:
@@ -36,7 +36,7 @@ jobs:
3636
name: Checkout repository
3737
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
3838
- name: mono_repo self validate
39-
run: dart pub global activate mono_repo 6.6.2
39+
run: dart pub global activate mono_repo 6.6.3
4040
- name: mono_repo self validate
4141
run: dart pub global run mono_repo generate --validate
4242
job_002:
@@ -340,7 +340,7 @@ jobs:
340340
- job_004
341341
- job_005
342342
job_010:
343-
name: "generate_and_analyze; Dart dev; PKG: web_generator; `dart pub -C ../web get && dart bin/update_bindings.dart && dart analyze --fatal-infos ../web`"
343+
name: "generate_and_analyze; Dart dev; PKG: web_generator; `dart pub -C ../web get && dart bin/update_idl_bindings.dart && dart analyze --fatal-infos ../web`"
344344
runs-on: ubuntu-latest
345345
steps:
346346
- name: Cache Pub hosted dependencies
@@ -365,8 +365,8 @@ jobs:
365365
run: dart pub upgrade
366366
if: "always() && steps.checkout.conclusion == 'success'"
367367
working-directory: web_generator
368-
- name: "web_generator; dart pub -C ../web get && dart bin/update_bindings.dart && dart analyze --fatal-infos ../web"
369-
run: "dart pub -C ../web get && dart bin/update_bindings.dart && dart analyze --fatal-infos ../web"
368+
- name: "web_generator; dart pub -C ../web get && dart bin/update_idl_bindings.dart && dart analyze --fatal-infos ../web"
369+
run: "dart pub -C ../web get && dart bin/update_idl_bindings.dart && dart analyze --fatal-infos ../web"
370370
if: "always() && steps.web_generator_pub_upgrade.conclusion == 'success'"
371371
working-directory: web_generator
372372
needs:
@@ -380,7 +380,7 @@ jobs:
380380
- job_008
381381
- job_009
382382
job_011:
383-
name: "generate_all_and_analyze; Dart dev; PKG: web_generator; `dart pub -C ../web get && dart bin/update_bindings.dart --generate-all && dart analyze --fatal-infos ../web`"
383+
name: "generate_all_and_analyze; Dart dev; PKG: web_generator; `dart pub -C ../web get && dart bin/update_idl_bindings.dart --generate-all && dart analyze --fatal-infos ../web`"
384384
runs-on: ubuntu-latest
385385
steps:
386386
- name: Cache Pub hosted dependencies
@@ -405,8 +405,8 @@ jobs:
405405
run: dart pub upgrade
406406
if: "always() && steps.checkout.conclusion == 'success'"
407407
working-directory: web_generator
408-
- name: "web_generator; dart pub -C ../web get && dart bin/update_bindings.dart --generate-all && dart analyze --fatal-infos ../web"
409-
run: "dart pub -C ../web get && dart bin/update_bindings.dart --generate-all && dart analyze --fatal-infos ../web"
408+
- name: "web_generator; dart pub -C ../web get && dart bin/update_idl_bindings.dart --generate-all && dart analyze --fatal-infos ../web"
409+
run: "dart pub -C ../web get && dart bin/update_idl_bindings.dart --generate-all && dart analyze --fatal-infos ../web"
410410
if: "always() && steps.web_generator_pub_upgrade.conclusion == 'success'"
411411
working-directory: web_generator
412412
needs:

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ pubspec.lock
44

55
web_generator/lib/src/*.js
66
web_generator/lib/src/*.js.*
7-
web_generator/lib/src/node_modules/
7+
web_generator/lib/src/node_modules/

tool/ci.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
# Created with package:mono_repo v6.6.2
2+
# Created with package:mono_repo v6.6.3
33

44
# Support built in commands on windows out of the box.
55

@@ -72,12 +72,12 @@ for PKG in ${PKGS}; do
7272
dart fix --compare-to-golden test_fixes || EXIT_CODE=$?
7373
;;
7474
command_1)
75-
echo 'dart pub -C ../web get && dart bin/update_bindings.dart && dart analyze --fatal-infos ../web'
76-
dart pub -C ../web get && dart bin/update_bindings.dart && dart analyze --fatal-infos ../web || EXIT_CODE=$?
75+
echo 'dart pub -C ../web get && dart bin/update_idl_bindings.dart && dart analyze --fatal-infos ../web'
76+
dart pub -C ../web get && dart bin/update_idl_bindings.dart && dart analyze --fatal-infos ../web || EXIT_CODE=$?
7777
;;
7878
command_2)
79-
echo 'dart pub -C ../web get && dart bin/update_bindings.dart --generate-all && dart analyze --fatal-infos ../web'
80-
dart pub -C ../web get && dart bin/update_bindings.dart --generate-all && dart analyze --fatal-infos ../web || EXIT_CODE=$?
79+
echo 'dart pub -C ../web get && dart bin/update_idl_bindings.dart --generate-all && dart analyze --fatal-infos ../web'
80+
dart pub -C ../web get && dart bin/update_idl_bindings.dart --generate-all && dart analyze --fatal-infos ../web || EXIT_CODE=$?
8181
;;
8282
format)
8383
echo 'dart format --output=none --set-exit-if-changed .'

web_generator/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ run on Node.
1212
To regenerate `web` bindings from the current IDL versions, run:
1313

1414
```shell
15-
dart bin/update_bindings.dart
15+
dart bin/update_idl_bindings.dart
1616
```
1717

1818
## Update to the latest Web IDL versions and regenerate
1919

2020
To re-generate the package from newer IDL versions, you can either run:
2121

2222
```shell
23-
dart bin/update_bindings.dart --update
23+
dart bin/update_idl_bindings.dart --update
2424
```
2525

2626
or, manually edit `lib/src/package.json` to use specific IDL versions, and
27-
re-run `update_bindings.dart`.
27+
re-run `update_idl_bindings.dart`.
2828

2929
### Updating the dartdoc info from MDN
3030

@@ -38,7 +38,7 @@ dart bin/scrape_mdn.dart
3838

3939
That will collect the MDN documentation into `third_party/mdn/mdn.json`; changes
4040
to that file should be committed to git. You'll need to run
41-
`update_bindings.dart` to produce Dart code using the updated documentation.
41+
`update_idl_bindings.dart` to produce Dart code using the updated documentation.
4242

4343
## Generation conventions
4444

@@ -80,7 +80,7 @@ definitions:
8080
To ignore the compatibility data and emit all members, run:
8181

8282
```shell
83-
dart bin/update_bindings.dart --generate-all
83+
dart bin/update_idl_bindings.dart --generate-all
8484
```
8585

8686
This is useful if you want to avoid having to write bindings manually for some
@@ -90,10 +90,10 @@ experimental and non-standard APIs.
9090

9191
Based on:
9292

93-
<!-- START updated by bin/update_bindings.dart. Do not modify by hand -->
93+
<!-- START updated by bin/update_idl_bindings.dart. Do not modify by hand -->
9494
| Item | Version |
9595
| --- | --: |
9696
| `@webref/css` | [6.20.3](https://www.npmjs.com/package/@webref/css/v/6.20.3) |
9797
| `@webref/elements` | [2.4.0](https://www.npmjs.com/package/@webref/elements/v/2.4.0) |
9898
| `@webref/idl` | [3.60.1](https://www.npmjs.com/package/@webref/idl/v/3.60.1) |
99-
<!-- END updated by bin/update_bindings.dart. Do not modify by hand -->
99+
<!-- END updated by bin/update_idl_bindings.dart. Do not modify by hand -->
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import 'dart:io';
2+
3+
import 'package:args/args.dart';
4+
import 'package:io/ansi.dart' as ansi;
5+
import 'package:io/io.dart';
6+
import 'package:path/path.dart' as p;
7+
import 'package:web_generator/src/cli.dart';
8+
9+
void main(List<String> arguments) async {
10+
final ArgResults argResult;
11+
12+
try {
13+
argResult = _parser.parse(arguments);
14+
} on FormatException catch (e) {
15+
print('''
16+
${ansi.lightRed.wrap(e.message)}
17+
18+
$_usage''');
19+
exitCode = ExitCode.usage.code;
20+
return;
21+
}
22+
23+
if (argResult['help'] as bool) {
24+
print(_usage);
25+
return;
26+
}
27+
28+
if (argResult.rest.isEmpty) {
29+
print('''
30+
${ansi.lightRed.wrap('At least one argument is needed')}
31+
32+
$_usage''');
33+
exitCode = ExitCode.usage.code;
34+
return;
35+
}
36+
37+
assert(p.fromUri(Platform.script).endsWith(_thisScript.toFilePath()));
38+
39+
// Run `npm install` or `npm update` as needed.
40+
final update = argResult['update'] as bool;
41+
await runProc(
42+
'npm',
43+
[update ? 'update' : 'install'],
44+
workingDirectory: bindingsGeneratorPath,
45+
);
46+
47+
final contextFile = await createJsTypeSupertypeContext();
48+
49+
// Compute JS type supertypes for union calculation in translator.
50+
await generateJsTypeSupertypes(contextFile.path);
51+
52+
if (argResult['compile'] as bool) {
53+
// Compile Dart to Javascript.
54+
await compileDartMain();
55+
}
56+
57+
final inputFile = argResult.rest.first;
58+
final outputFile = argResult['output'] as String? ??
59+
p.join(p.current, inputFile.replaceAll('.d.ts', '.dart'));
60+
final configFile =
61+
argResult['config'] as String? ?? p.join(p.current, 'webgen.yaml');
62+
final relativeOutputPath =
63+
p.relative(outputFile, from: bindingsGeneratorPath);
64+
// Run app with `node`.
65+
await runProc(
66+
'node',
67+
[
68+
'main.mjs',
69+
'--declaration',
70+
'--input=${p.relative(inputFile, from: bindingsGeneratorPath)}',
71+
'--output=$relativeOutputPath',
72+
'--config=$configFile'
73+
],
74+
workingDirectory: bindingsGeneratorPath,
75+
);
76+
77+
await contextFile.delete();
78+
return;
79+
}
80+
81+
final _thisScript = Uri.parse('bin/gen_interop_bindings.dart');
82+
83+
final _usage = '''
84+
${ansi.styleBold.wrap('Dart Interop Gen')}:
85+
$_thisScript dts <.d.ts file> [options]
86+
87+
Usage:
88+
${_parser.usage}''';
89+
90+
final _parser = ArgParser()
91+
..addFlag('help', negatable: false, help: 'Show help information')
92+
..addFlag('update', abbr: 'u', help: 'Update npm dependencies')
93+
..addFlag('compile', defaultsTo: true)
94+
..addOption('output',
95+
abbr: 'o', help: 'The output path to generate the Dart interface code')
96+
..addOption('config',
97+
hide: true,
98+
abbr: 'c',
99+
help: 'The configuration file to use for this tool (NOTE: Unimplemented)')
100+
..addFlag('help', negatable: false);

0 commit comments

Comments
 (0)