Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generator: Support the latest package dependencies #328

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 8 additions & 93 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -340,47 +340,7 @@ jobs:
- job_004
- job_005
job_010:
name: "generate_and_analyze; Dart dev; PKG: web_generator; `dart analyze --fatal-infos .`"
runs-on: ubuntu-latest
steps:
- name: Cache Pub hosted dependencies
uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a
with:
path: "~/.pub-cache/hosted"
key: "os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:web_generator;commands:analyze"
restore-keys: |
os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:web_generator
os:ubuntu-latest;pub-cache-hosted;sdk:dev
os:ubuntu-latest;pub-cache-hosted
os:ubuntu-latest
- name: Setup Dart SDK
uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94
with:
sdk: dev
- id: checkout
name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- id: web_generator_pub_upgrade
name: web_generator; dart pub upgrade
run: dart pub upgrade
if: "always() && steps.checkout.conclusion == 'success'"
working-directory: web_generator
- name: "web_generator; dart analyze --fatal-infos ."
run: dart analyze --fatal-infos .
if: "always() && steps.web_generator_pub_upgrade.conclusion == 'success'"
working-directory: web_generator
needs:
- job_001
- job_002
- job_003
- job_004
- job_005
- job_006
- job_007
- job_008
- job_009
job_011:
name: "generate_and_analyze; Dart dev; PKG: web_generator; `dart bin/update_bindings.dart`"
name: "generate_and_analyze; Dart dev; PKG: web_generator; `dart pub -C ../web get && dart bin/update_bindings.dart && dart analyze --fatal-infos ../web`"
runs-on: ubuntu-latest
steps:
- name: Cache Pub hosted dependencies
Expand All @@ -405,8 +365,8 @@ jobs:
run: dart pub upgrade
if: "always() && steps.checkout.conclusion == 'success'"
working-directory: web_generator
- name: web_generator; dart bin/update_bindings.dart
run: dart bin/update_bindings.dart
- name: "web_generator; dart pub -C ../web get && dart bin/update_bindings.dart && dart analyze --fatal-infos ../web"
run: "dart pub -C ../web get && dart bin/update_bindings.dart && dart analyze --fatal-infos ../web"
if: "always() && steps.web_generator_pub_upgrade.conclusion == 'success'"
working-directory: web_generator
needs:
Expand All @@ -419,50 +379,8 @@ jobs:
- job_007
- job_008
- job_009
job_012:
name: "generate_all_and_analyze; Dart dev; PKG: web_generator; `dart analyze --fatal-infos .`"
runs-on: ubuntu-latest
steps:
- name: Cache Pub hosted dependencies
uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a
with:
path: "~/.pub-cache/hosted"
key: "os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:web_generator;commands:analyze"
restore-keys: |
os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:web_generator
os:ubuntu-latest;pub-cache-hosted;sdk:dev
os:ubuntu-latest;pub-cache-hosted
os:ubuntu-latest
- name: Setup Dart SDK
uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94
with:
sdk: dev
- id: checkout
name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- id: web_generator_pub_upgrade
name: web_generator; dart pub upgrade
run: dart pub upgrade
if: "always() && steps.checkout.conclusion == 'success'"
working-directory: web_generator
- name: "web_generator; dart analyze --fatal-infos ."
run: dart analyze --fatal-infos .
if: "always() && steps.web_generator_pub_upgrade.conclusion == 'success'"
working-directory: web_generator
needs:
- job_001
- job_002
- job_003
- job_004
- job_005
- job_006
- job_007
- job_008
- job_009
- job_010
- job_011
job_013:
name: "generate_all_and_analyze; Dart dev; PKG: web_generator; `dart bin/update_bindings.dart --generate-all`"
job_011:
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`"
runs-on: ubuntu-latest
steps:
- name: Cache Pub hosted dependencies
Expand All @@ -487,8 +405,8 @@ jobs:
run: dart pub upgrade
if: "always() && steps.checkout.conclusion == 'success'"
working-directory: web_generator
- name: "web_generator; dart bin/update_bindings.dart --generate-all"
run: dart bin/update_bindings.dart --generate-all
- name: "web_generator; dart pub -C ../web get && dart bin/update_bindings.dart --generate-all && dart analyze --fatal-infos ../web"
run: "dart pub -C ../web get && dart bin/update_bindings.dart --generate-all && dart analyze --fatal-infos ../web"
if: "always() && steps.web_generator_pub_upgrade.conclusion == 'success'"
working-directory: web_generator
needs:
Expand All @@ -502,8 +420,7 @@ jobs:
- job_008
- job_009
- job_010
- job_011
job_014:
job_012:
name: "dart_fixes; Dart main; PKG: web; `dart fix --compare-to-golden test_fixes`"
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -545,5 +462,3 @@ jobs:
- job_009
- job_010
- job_011
- job_012
- job_013
8 changes: 4 additions & 4 deletions tool/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ for PKG in ${PKGS}; do
dart fix --compare-to-golden test_fixes || EXIT_CODE=$?
;;
command_1)
echo 'dart bin/update_bindings.dart'
dart bin/update_bindings.dart || EXIT_CODE=$?
echo 'dart pub -C ../web get && dart bin/update_bindings.dart && dart analyze --fatal-infos ../web'
dart pub -C ../web get && dart bin/update_bindings.dart && dart analyze --fatal-infos ../web || EXIT_CODE=$?
;;
command_2)
echo 'dart bin/update_bindings.dart --generate-all'
dart bin/update_bindings.dart --generate-all || EXIT_CODE=$?
echo 'dart pub -C ../web get && dart bin/update_bindings.dart --generate-all && dart analyze --fatal-infos ../web'
dart pub -C ../web get && dart bin/update_bindings.dart --generate-all && dart analyze --fatal-infos ../web || EXIT_CODE=$?
;;
format)
echo 'dart format --output=none --set-exit-if-changed .'
Expand Down
33 changes: 27 additions & 6 deletions web_generator/bin/update_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:analyzer/dart/element/type.dart';
import 'package:args/args.dart';
import 'package:io/ansi.dart' as ansi;
import 'package:io/io.dart';
import 'package:package_config/package_config.dart';
import 'package:path/path.dart' as p;

void main(List<String> arguments) async {
Expand Down Expand Up @@ -48,13 +49,16 @@ $_usage''');
await _generateJsTypeSupertypes();

if (argResult['compile'] as bool) {
final webPkgLangVersion = await _webPackageLanguageVersion(_webPackagePath);
// Compile Dart to Javascript.
await _runProc(
Platform.executable,
[
'compile',
'js',
'--enable-asserts',
'--server-mode',
'-DlanguageVersion=$webPkgLangVersion',
'dart_main.dart',
'-o',
'dart_main.js',
Expand All @@ -64,8 +68,7 @@ $_usage''');
}

// Determine the set of previously generated files.
final domDir =
Directory(Platform.script.resolve('../../web/lib/src/dom').path);
final domDir = Directory(p.join(_webPackagePath, 'lib/src/dom'));
final existingFiles =
domDir.listSync(recursive: true).whereType<File>().where((file) {
if (!file.path.endsWith('.dart')) return false;
Expand All @@ -83,7 +86,7 @@ $_usage''');
'node',
[
'main.mjs',
'--output-directory=${Platform.script.resolve('../../web/lib/src').path}',
'--output-directory=${p.join(_webPackagePath, 'lib/src')}',
if (generateAll) '--generate-all',
],
workingDirectory: _bindingsGeneratorPath,
Expand Down Expand Up @@ -128,6 +131,25 @@ $_startComment
}
}

Future<String> _webPackageLanguageVersion(String pkgPath) async {
srujzs marked this conversation as resolved.
Show resolved Hide resolved
final packageConfig = await findPackageConfig(Directory(pkgPath));
if (packageConfig == null) {
throw StateError('No package config for "$pkgPath"');
}
final package =
packageConfig.packageOf(Uri.file(p.join(pkgPath, 'pubspec.yaml')));
if (package == null) {
throw StateError('No package at "$pkgPath"');
}
final languageVersion = package.languageVersion;
if (languageVersion == null) {
throw StateError('No language version "$pkgPath"');
}
return '$languageVersion.0';
}

final _webPackagePath = Platform.script.resolve('../../web').path;

String _packageLockVersion(String package) {
final packageLockData = jsonDecode(
File(p.join(_bindingsGeneratorPath, 'package-lock.json'))
Expand Down Expand Up @@ -174,9 +196,8 @@ Future<void> _runProc(
// used by the translator to handle IDL types.
Future<void> _generateJsTypeSupertypes() async {
// Use a file that uses `dart:js_interop` for analysis.
final contextCollection = AnalysisContextCollection(includedPaths: [
p.fromUri(Platform.script.resolve('../../web/lib/src/dom.dart'))
]);
final contextCollection = AnalysisContextCollection(
includedPaths: [p.join(_webPackagePath, 'lib/src/dom.dart')]);
final dartJsInterop = (await contextCollection.contexts.single.currentSession
.getLibraryByUri('dart:js_interop') as LibraryElementResult)
.element;
Expand Down
24 changes: 17 additions & 7 deletions web_generator/lib/src/dart_main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:js_interop';
import 'package:args/args.dart';
import 'package:code_builder/code_builder.dart' as code;
import 'package:dart_style/dart_style.dart';
import 'package:pub_semver/pub_semver.dart';

import 'filesystem_api.dart';
import 'generate_bindings.dart';
Expand All @@ -19,15 +20,24 @@ import 'util.dart';
// probably involve parsing the TC39 spec.

void main(List<String> args) async {
var languageVersionString = const String.fromEnvironment('languageVersion');
if (languageVersionString.isEmpty) {
languageVersionString = DartFormatter.latestLanguageVersion.toString();
}
final ArgResults argResult;
argResult = _parser.parse(args);
await _generateAndWriteBindings(
outputDirectory: argResult['output-directory'] as String,
generateAll: argResult['generate-all'] as bool);
outputDirectory: argResult['output-directory'] as String,
generateAll: argResult['generate-all'] as bool,
languageVersion: Version.parse(languageVersionString),
);
}

Future<void> _generateAndWriteBindings(
{required String outputDirectory, required bool generateAll}) async {
Future<void> _generateAndWriteBindings({
required String outputDirectory,
required bool generateAll,
required Version languageVersion,
}) async {
const librarySubDir = 'dom';

ensureDirectoryExists('$outputDirectory/$librarySubDir');
Expand All @@ -38,20 +48,20 @@ Future<void> _generateAndWriteBindings(
final libraryPath = entry.key;
final library = entry.value;

final contents = _emitLibrary(library).toJS;
final contents = _emitLibrary(library, languageVersion).toJS;
fs.writeFileSync('$outputDirectory/$libraryPath'.toJS, contents);
}
}

String _emitLibrary(code.Library library) {
String _emitLibrary(code.Library library, Version languageVersion) {
final emitter = code.DartEmitter(
allocator: code.Allocator(),
orderDirectives: true,
useNullSafetySyntax: true,
);

final source = library.accept(emitter);
return DartFormatter(languageVersion: DartFormatter.latestLanguageVersion)
return DartFormatter(languageVersion: languageVersion)
.format(source.toString());
}

Expand Down
12 changes: 8 additions & 4 deletions web_generator/mono_pkg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ stages:
- test: --test-randomize-ordering-seed=random -p chrome
- test: --test-randomize-ordering-seed=random -p chrome -c dart2wasm
- generate_and_analyze:
- command: dart bin/update_bindings.dart
- analyze: --fatal-infos .
- command:
- dart pub -C ../web get
- dart bin/update_bindings.dart
- dart analyze --fatal-infos ../web
kevmoo marked this conversation as resolved.
Show resolved Hide resolved
- generate_all_and_analyze:
- command: dart bin/update_bindings.dart --generate-all
- analyze: --fatal-infos .
- command:
- dart pub -C ../web get
- dart bin/update_bindings.dart --generate-all
- dart analyze --fatal-infos ../web
8 changes: 5 additions & 3 deletions web_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ description: Generator scripts for package:web.
repository: https://github.com/dart-lang/web

environment:
sdk: ^3.6.0-0
sdk: ^3.6.0

dependencies:
analyzer: ^6.3.0
analyzer: ^7.0.0
args: ^2.4.0
build_runner: ^2.4.6
build_web_compilers: ^4.0.8
code_builder: ^4.10.0
collection: ^1.18.0
dart_flutter_team_lints: ^3.0.0
dart_style: ^2.3.7
dart_style: ^3.0.0
io: ^1.0.4
package_config: ^2.1.1
path: ^1.8.3
pub_semver: ^2.1.5
test: ^1.24.4
Loading