diff --git a/README.md b/README.md index 0080ea0b..f56e61d9 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,6 @@ void main() { ## Web IDL version - + Based on [`@webref/idl 3.39.1`](https://www.npmjs.com/package/@webref/idl/v/3.39.1). - + diff --git a/lib/src/dom/badging.dart b/lib/src/dom/badging.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/badging.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/beacon.dart b/lib/src/dom/beacon.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/beacon.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/compat.dart b/lib/src/dom/compat.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/compat.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/csp_embedded_enforcement.dart b/lib/src/dom/csp_embedded_enforcement.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/csp_embedded_enforcement.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/css_images_4.dart b/lib/src/dom/css_images_4.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/css_images_4.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/css_shadow_parts.dart b/lib/src/dom/css_shadow_parts.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/css_shadow_parts.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/device_memory.dart b/lib/src/dom/device_memory.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/device_memory.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/html_media_capture.dart b/lib/src/dom/html_media_capture.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/html_media_capture.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/input_events.dart b/lib/src/dom/input_events.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/input_events.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/permissions_request.dart b/lib/src/dom/permissions_request.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/permissions_request.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/permissions_revoke.dart b/lib/src/dom/permissions_revoke.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/permissions_revoke.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/pointerlock.dart b/lib/src/dom/pointerlock.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/pointerlock.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/prefer_current_tab.dart b/lib/src/dom/prefer_current_tab.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/prefer_current_tab.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/prerendering_revamped.dart b/lib/src/dom/prerendering_revamped.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/prerendering_revamped.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/private_click_measurement.dart b/lib/src/dom/private_click_measurement.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/private_click_measurement.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/savedata.dart b/lib/src/dom/savedata.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/savedata.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/storage_access.dart b/lib/src/dom/storage_access.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/storage_access.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/wai_aria.dart b/lib/src/dom/wai_aria.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/wai_aria.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/wasm_web_api.dart b/lib/src/dom/wasm_web_api.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/wasm_web_api.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/webdriver.dart b/lib/src/dom/webdriver.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/webdriver.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/webrtc_svc.dart b/lib/src/dom/webrtc_svc.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/webrtc_svc.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/src/dom/webxr_gamepads_module.dart b/lib/src/dom/webxr_gamepads_module.dart deleted file mode 100644 index 6c60fd15..00000000 --- a/lib/src/dom/webxr_gamepads_module.dart +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. -// -// Generated from Web IDL definitions. diff --git a/lib/web.dart b/lib/web.dart index 109be681..c621e9f5 100644 --- a/lib/web.dart +++ b/lib/web.dart @@ -12,13 +12,10 @@ export 'src/dom/audio_session.dart'; export 'src/dom/autoplay_detection.dart'; export 'src/dom/background_fetch.dart'; export 'src/dom/background_sync.dart'; -export 'src/dom/badging.dart'; export 'src/dom/battery_status.dart'; -export 'src/dom/beacon.dart'; export 'src/dom/capture_handle_identity.dart'; export 'src/dom/captured_mouse_events.dart'; export 'src/dom/clipboard_apis.dart'; -export 'src/dom/compat.dart'; export 'src/dom/compression.dart'; export 'src/dom/compute_pressure.dart'; export 'src/dom/console.dart'; @@ -28,7 +25,6 @@ export 'src/dom/cookie_store.dart'; export 'src/dom/crash_reporting.dart'; export 'src/dom/credential_management.dart'; export 'src/dom/csp.dart'; -export 'src/dom/csp_embedded_enforcement.dart'; export 'src/dom/csp_next.dart'; export 'src/dom/css_anchor_position.dart'; export 'src/dom/css_animation_worklet.dart'; @@ -44,7 +40,6 @@ export 'src/dom/css_counter_styles.dart'; export 'src/dom/css_font_loading.dart'; export 'src/dom/css_fonts.dart'; export 'src/dom/css_highlight_api.dart'; -export 'src/dom/css_images_4.dart'; export 'src/dom/css_layout_api.dart'; export 'src/dom/css_masking.dart'; export 'src/dom/css_nav.dart'; @@ -53,7 +48,6 @@ export 'src/dom/css_parser_api.dart'; export 'src/dom/css_properties_values_api.dart'; export 'src/dom/css_pseudo.dart'; export 'src/dom/css_regions.dart'; -export 'src/dom/css_shadow_parts.dart'; export 'src/dom/css_transitions.dart'; export 'src/dom/css_transitions_2.dart'; export 'src/dom/css_typed_om.dart'; @@ -64,7 +58,6 @@ export 'src/dom/cssom_view.dart'; export 'src/dom/custom_state_pseudo_class.dart'; export 'src/dom/datacue.dart'; export 'src/dom/deprecation_reporting.dart'; -export 'src/dom/device_memory.dart'; export 'src/dom/device_posture.dart'; export 'src/dom/digital_goods.dart'; export 'src/dom/document_picture_in_picture.dart'; @@ -111,14 +104,12 @@ export 'src/dom/get_installed_related_apps.dart'; export 'src/dom/gyroscope.dart'; export 'src/dom/hr_time.dart'; export 'src/dom/html.dart'; -export 'src/dom/html_media_capture.dart'; export 'src/dom/idle_detection.dart'; export 'src/dom/image_capture.dart'; export 'src/dom/image_resource.dart'; export 'src/dom/indexeddb.dart'; export 'src/dom/ink_enhancement.dart'; export 'src/dom/input_device_capabilities.dart'; -export 'src/dom/input_events.dart'; export 'src/dom/intersection_observer.dart'; export 'src/dom/intervention_reporting.dart'; export 'src/dom/is_input_pending.dart'; @@ -171,16 +162,10 @@ export 'src/dom/performance_timeline.dart'; export 'src/dom/periodic_background_sync.dart'; export 'src/dom/permissions.dart'; export 'src/dom/permissions_policy.dart'; -export 'src/dom/permissions_request.dart'; -export 'src/dom/permissions_revoke.dart'; export 'src/dom/picture_in_picture.dart'; export 'src/dom/pointerevents.dart'; -export 'src/dom/pointerlock.dart'; export 'src/dom/portals.dart'; -export 'src/dom/prefer_current_tab.dart'; -export 'src/dom/prerendering_revamped.dart'; export 'src/dom/presentation_api.dart'; -export 'src/dom/private_click_measurement.dart'; export 'src/dom/private_network_access.dart'; export 'src/dom/proximity.dart'; export 'src/dom/push_api.dart'; @@ -194,7 +179,6 @@ export 'src/dom/requeststorageaccessfor.dart'; export 'src/dom/resize_observer.dart'; export 'src/dom/resource_timing.dart'; export 'src/dom/sanitizer_api.dart'; -export 'src/dom/savedata.dart'; export 'src/dom/scheduling_apis.dart'; export 'src/dom/screen_capture.dart'; export 'src/dom/screen_orientation.dart'; @@ -210,7 +194,6 @@ export 'src/dom/shape_detection_api.dart'; export 'src/dom/shared_storage.dart'; export 'src/dom/speech_api.dart'; export 'src/dom/storage.dart'; -export 'src/dom/storage_access.dart'; export 'src/dom/storage_buckets.dart'; export 'src/dom/streams.dart'; export 'src/dom/svg.dart'; @@ -229,9 +212,7 @@ export 'src/dom/user_timing.dart'; export 'src/dom/vibration.dart'; export 'src/dom/video_rvfc.dart'; export 'src/dom/virtual_keyboard.dart'; -export 'src/dom/wai_aria.dart'; export 'src/dom/wasm_js_api.dart'; -export 'src/dom/wasm_web_api.dart'; export 'src/dom/web_animations.dart'; export 'src/dom/web_animations_2.dart'; export 'src/dom/web_app_launch.dart'; @@ -252,7 +233,6 @@ export 'src/dom/webcodecs_opus_codec_registration.dart'; export 'src/dom/webcodecs_vp9_codec_registration.dart'; export 'src/dom/webcrypto_secure_curves.dart'; export 'src/dom/webcryptoapi.dart'; -export 'src/dom/webdriver.dart'; export 'src/dom/webgl1.dart'; export 'src/dom/webgl2.dart'; export 'src/dom/webgl_blend_equation_advanced_coherent.dart'; @@ -284,7 +264,6 @@ export 'src/dom/webrtc_ice.dart'; export 'src/dom/webrtc_identity.dart'; export 'src/dom/webrtc_priority.dart'; export 'src/dom/webrtc_stats.dart'; -export 'src/dom/webrtc_svc.dart'; export 'src/dom/websockets.dart'; export 'src/dom/webtransport.dart'; export 'src/dom/webusb.dart'; @@ -293,7 +272,6 @@ export 'src/dom/webxr.dart'; export 'src/dom/webxr_ar_module.dart'; export 'src/dom/webxr_depth_sensing.dart'; export 'src/dom/webxr_dom_overlays.dart'; -export 'src/dom/webxr_gamepads_module.dart'; export 'src/dom/webxr_hand_input.dart'; export 'src/dom/webxr_hit_test.dart'; export 'src/dom/webxr_lighting_estimation.dart'; diff --git a/tool/bindings_generator/dart_main.dart b/tool/bindings_generator/dart_main.dart index 9ec2a87b..fd01214b 100644 --- a/tool/bindings_generator/dart_main.dart +++ b/tool/bindings_generator/dart_main.dart @@ -23,14 +23,26 @@ void main(List args) async { Future _generateAndWriteBindings(String dir) async { const librarySubDir = 'src/dom'; + ensureDirectoryExists('$dir/$librarySubDir'); + final bindings = await generateBindings(packageRoot, librarySubDir); for (var entry in bindings.entries) { - final formattedContents = _runDartFormat(entry.value).toJS; - fs.writeFileSync('$dir/${entry.key}'.toJS, formattedContents); + final libraryPath = entry.key; + final library = entry.value; + + final contents = _emitLibrary(library).toJS; + fs.writeFileSync('$dir/$libraryPath'.toJS, contents); } } -String _runDartFormat(code.Library library) => DartFormatter().format( - '''${library.accept(code.DartEmitter(allocator: code.Allocator(), orderDirectives: true, useNullSafetySyntax: true))}''', - ); +String _emitLibrary(code.Library library) { + final emitter = code.DartEmitter( + allocator: code.Allocator(), + orderDirectives: true, + useNullSafetySyntax: true, + ); + + final source = library.accept(emitter); + return DartFormatter().format(source.toString()); +} diff --git a/tool/bindings_generator/filesystem_api.dart b/tool/bindings_generator/filesystem_api.dart index b63d8d31..0afd1a4a 100644 --- a/tool/bindings_generator/filesystem_api.dart +++ b/tool/bindings_generator/filesystem_api.dart @@ -14,6 +14,13 @@ class JSMkdirOptions { external factory JSMkdirOptions({JSBoolean? recursive}); } +@JS() +@anonymous +@staticInterop +class JSReadFileOptions { + external factory JSReadFileOptions({JSString? encoding}); +} + @JS() @staticInterop class FileSystem {} @@ -21,10 +28,13 @@ class FileSystem {} // TODO(joshualitt): Replace `void` with `JSVoid` extension FileSystemExtension on FileSystem { external JSBoolean existsSync(JSString path); + @JS('mkdirSync') external void mkdirSyncOneArg(JSString path); + @JS('mkdirSync') external void mkdirSyncTwoArg(JSString path, JSMkdirOptions options); + void mkdirSync(JSString path, [JSMkdirOptions? options]) { if (options == null) { return mkdirSyncOneArg(path); @@ -33,5 +43,7 @@ extension FileSystemExtension on FileSystem { } } + external JSAny readFileSync(JSString path, [JSReadFileOptions options]); + external void writeFileSync(JSString path, JSString contents); } diff --git a/tool/bindings_generator/generate_bindings.dart b/tool/bindings_generator/generate_bindings.dart index 6efea3b2..aaf8e2d1 100644 --- a/tool/bindings_generator/generate_bindings.dart +++ b/tool/bindings_generator/generate_bindings.dart @@ -47,11 +47,12 @@ Future> _generateCSSStyleDeclarations() async { Future generateBindings( String packageRoot, String librarySubDir) async { final cssStyleDeclarations = await _generateCSSStyleDeclarations(); - final translator = Translator(librarySubDir, cssStyleDeclarations); + final translator = + Translator(packageRoot, librarySubDir, cssStyleDeclarations); final array = objectEntries(await idl.parseAll().toDart as JSObject); for (var i = 0; i < array.length; i++) { final entry = array[i] as JSArray; - final shortname = (entry[0] as JSString).toDart.kebabToSnake; + final shortname = (entry[0] as JSString).toDart; final ast = entry[1] as JSArray; translator.collect(shortname, ast); } diff --git a/tool/bindings_generator/js_type_supertypes.dart b/tool/bindings_generator/js_type_supertypes.dart index 3d19aa49..56fd79b0 100644 --- a/tool/bindings_generator/js_type_supertypes.dart +++ b/tool/bindings_generator/js_type_supertypes.dart @@ -2,7 +2,7 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// Updated by /tool/update_bindings.dart. Do not modify by hand. +// Updated by tool/update_bindings.dart. Do not modify by hand. const Map jsTypeSupertypes = { 'JSAny': null, diff --git a/tool/bindings_generator/package-lock.json b/tool/bindings_generator/package-lock.json index f242a031..79b8f339 100644 --- a/tool/bindings_generator/package-lock.json +++ b/tool/bindings_generator/package-lock.json @@ -1,16 +1,17 @@ { - "name": "Dart web bindings generator", + "name": "dart_web_bindings_generator", "version": "0.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "Dart web bindings generator", + "name": "dart_web_bindings_generator", "version": "0.0.1", "license": "BSD 3", - "devDependencies": { + "dependencies": { "@webref/css": "^6.5.6", "@webref/idl": "^3.23.0", + "web-specs": "^2.74.1", "webidl2": "^24.2.2" } }, @@ -18,7 +19,6 @@ "version": "6.8.1", "resolved": "https://registry.npmjs.org/@webref/css/-/css-6.8.1.tgz", "integrity": "sha512-+GjGSdigPjTm82suc5Pr0x8L7OIGCe0WWFbMWh8P9goQcPDNzfaDKSPpzqgfmiP4pQlOQbSSohMlAGv8rKjwsA==", - "dev": true, "peerDependencies": { "css-tree": "^2.3.1" } @@ -27,7 +27,6 @@ "version": "3.39.1", "resolved": "https://registry.npmjs.org/@webref/idl/-/idl-3.39.1.tgz", "integrity": "sha512-dKPhh8b6LOL4p8NQjLwSovu5db4P7Fxkl7t6QnuG6e8EdztmD2jJYFV/d6nDaK/WBNFSZxssqJTT2EfJLVcphw==", - "dev": true, "peerDependencies": { "webidl2": "^24.4.1" } @@ -36,7 +35,6 @@ "version": "2.3.1", "resolved": "https://registry.npmjs.org/css-tree/-/css-tree-2.3.1.tgz", "integrity": "sha512-6Fv1DV/TYw//QF5IzQdqsNDjx/wc8TrMBZsqjL9eW01tWb7R7k/mq+/VXfJCl7SoD5emsJop9cOByJZfs8hYIw==", - "dev": true, "peer": true, "dependencies": { "mdn-data": "2.0.30", @@ -50,24 +48,26 @@ "version": "2.0.30", "resolved": "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.30.tgz", "integrity": "sha512-GaqWWShW4kv/G9IEucWScBx9G1/vsFZZJUO+tD26M8J8z3Kw5RDQjaoZe03YAClgeS/SWPOcb4nkFBTEi5DUEA==", - "dev": true, "peer": true }, "node_modules/source-map-js": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.0.2.tgz", "integrity": "sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==", - "dev": true, "peer": true, "engines": { "node": ">=0.10.0" } }, + "node_modules/web-specs": { + "version": "2.74.1", + "resolved": "https://registry.npmjs.org/web-specs/-/web-specs-2.74.1.tgz", + "integrity": "sha512-OrXix5LVFhnJ1uvj00jy1vAOkUNzoVWKF7IqkTgqTI9CmYIdLxpknSvE/iqOOHUVNX90izUUj8lJVNjCdnWCuA==" + }, "node_modules/webidl2": { "version": "24.4.1", "resolved": "https://registry.npmjs.org/webidl2/-/webidl2-24.4.1.tgz", "integrity": "sha512-cPToqvZlxTAlaMucZyU28XtFLJz3XPdTdIWK/r3IaP1jfkjqne3OTniJS8DZqfzee1aBUQn80d6s2vYert50kg==", - "dev": true, "engines": { "node": ">= 14" } diff --git a/tool/bindings_generator/package.json b/tool/bindings_generator/package.json index bdf1a7d5..3c2cbec9 100644 --- a/tool/bindings_generator/package.json +++ b/tool/bindings_generator/package.json @@ -1,5 +1,5 @@ { - "name": "Dart web bindings generator", + "name": "dart_web_bindings_generator", "version": "0.0.1", "description": "", "scripts": { @@ -8,9 +8,10 @@ "keywords": [], "author": "Dart project authors", "license": "BSD 3", - "devDependencies": { - "@webref/idl": "^3.23.0", + "dependencies": { "@webref/css": "^6.5.6", + "@webref/idl": "^3.23.0", + "web-specs": "^2.74.1", "webidl2": "^24.2.2" } } diff --git a/tool/bindings_generator/translator.dart b/tool/bindings_generator/translator.dart index 8c17388b..1432a735 100644 --- a/tool/bindings_generator/translator.dart +++ b/tool/bindings_generator/translator.dart @@ -2,12 +2,14 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'dart:convert'; import 'dart:js_interop'; import 'package:code_builder/code_builder.dart' as code; import 'package:path/path.dart' as p; import 'banned_names.dart'; +import 'filesystem_api.dart'; import 'singletons.dart'; import 'type_aliases.dart'; import 'type_union.dart'; @@ -418,20 +420,26 @@ class _MemberName { } class Translator { + final String packageRoot; + final String _librarySubDir; + final List _cssStyleDeclarations; + final _libraries = {}; final _typeToDeclaration = {}; final _typeToLibrary = {}; final _interfacelikes = {}; final _includes = []; - final String _librarySubDir; + late String _currentlyTranslatingUrl; - final List _cssStyleDeclarations; + late WebSpecs webSpecs; /// Singleton so that various helper methods can access info about the AST. static Translator? instance; - Translator(this._librarySubDir, this._cssStyleDeclarations) { + Translator( + this.packageRoot, this._librarySubDir, this._cssStyleDeclarations) { instance = this; + webSpecs = WebSpecs.read(); } /// Set or update partial interfaces so we can have a unified interface @@ -472,11 +480,17 @@ class Translator { } } - void collect(String name, JSArray ast) { - final libraryPath = '$_librarySubDir/$name.dart'; + void collect(String shortName, JSArray ast) { + final libraryPath = '$_librarySubDir/${shortName.kebabToSnake}.dart'; assert(!_libraries.containsKey(libraryPath)); + + // TODO: Use the info from the spec to skip generation of some libraries. + // ignore: unused_local_variable + final spec = webSpecs.specFor(shortName)!; + final library = _Library(this, '$packageRoot/$libraryPath'); _libraries[libraryPath] = library; + for (var i = 0; i < ast.length; i++) { library.add(ast[i] as idl.Node); } @@ -834,14 +848,91 @@ class Translator { TranslationResult translate() { // Create a root import that exports all of the other libraries. final dartLibraries = {}; - dartLibraries['web.dart'] = generateRootImport(_libraries.keys); // Translate each IDL library into a Dart library. for (var entry in _libraries.entries) { _currentlyTranslatingUrl = entry.value.url; - dartLibraries[entry.key] = _library(entry.value); + + final dartLibrary = _library(entry.value); + if (dartLibrary.body.isEmpty && dartLibrary.directives.isEmpty) { + print(' not generating empty library: ${entry.value.url}'); + } else { + dartLibraries[entry.key] = dartLibrary; + } } + dartLibraries['web.dart'] = generateRootImport(dartLibraries.keys); + return dartLibraries; } } + +class WebSpecs { + static WebSpecs read() { + final path = p.join('node_modules', 'web-specs', 'index.json'); + final content = (fs.readFileSync( + path.toJS, + JSReadFileOptions(encoding: 'utf8'.toJS), + ) as JSString) + .toDart; + return WebSpecs( + (jsonDecode(content) as List) + .map((json) => WebSpec(json as Map)) + .toList(), + ); + } + + final List specs; + + WebSpecs(this.specs); + + WebSpec? specFor(String shortName) { + for (final spec in specs) { + if (spec.shortname == shortName) { + return spec; + } + } + + for (final spec in specs) { + if (spec.seriesShortname == shortName) { + return spec; + } + } + + return null; + } +} + +class WebSpec { + final Map json; + + WebSpec(this.json); + + String get url => json['url'] as String; + + String get shortname => json['shortname'] as String; + + String? get seriesShortname { + if (!json.containsKey('series')) return null; + return (json['series'] as Map)['shortname'] as String?; + } + + String get standing => json['standing'] as String; + + List get categories { + if (json.containsKey('categories')) { + return (json['categories'] as List).cast(); + } else { + return const []; + } + } + + String? get releaseStatus { + if (!json.containsKey('release')) return null; + return (json['release'] as Map)['status'] as String?; + } + + @override + String toString() => + '$shortname $url $standing [${categories.join(',')}] $releaseStatus'; +} diff --git a/tool/update_bindings.dart b/tool/update_bindings.dart index d4bffc3e..caaf97d2 100644 --- a/tool/update_bindings.dart +++ b/tool/update_bindings.dart @@ -37,9 +37,10 @@ $_usage'''); // Run `npm install` or `npm upgrade` as needed. if (argResult['update'] as bool) { - await _runProc('npm', ['update'], _bindingsGeneratorPath); + await _runProc('npm', ['update'], workingDirectory: _bindingsGeneratorPath); } else { - await _runProc('npm', ['install'], _bindingsGeneratorPath); + await _runProc('npm', ['install'], + workingDirectory: _bindingsGeneratorPath); } // Compute JS type supertypes for union calculation in translator. @@ -57,24 +58,38 @@ $_usage'''); '-o', 'dart_main.js', ], - _bindingsGeneratorPath, + workingDirectory: _bindingsGeneratorPath, ); } - // Delete all previously generated files. + // Determine the set of previously generated files. final domDir = Directory(p.join('lib', 'src', 'dom')); - for (final file in domDir.listSync(recursive: true).whereType()) { - if (!file.path.endsWith('.dart')) continue; + final existingFiles = + domDir.listSync(recursive: true).whereType().where((file) { + if (!file.path.endsWith('.dart')) return false; final contents = file.readAsStringSync(); - if (contents.contains('Generated from Web IDL definitions')) { + return contents.contains('Generated from Web IDL definitions'); + }).toList(); + final timeStamps = { + for (final file in existingFiles) file.path: file.lastModifiedSync(), + }; + + // Run app with `node`. + await _runProc( + 'node', + ['main.mjs', '../../lib'], + workingDirectory: _bindingsGeneratorPath, + ); + + // Delete previously generated files that have not been updated. + for (final file in existingFiles) { + final stamp = timeStamps[file.path]; + if (stamp == file.lastModifiedSync()) { file.deleteSync(); } } - // Run app with `node`. - await _runProc('node', ['main.mjs', '../../lib'], _bindingsGeneratorPath); - // Update readme. final readmeFile = File( p.normalize(p.join(_bindingsGeneratorPath, '..', '..', 'README.md')), @@ -127,7 +142,7 @@ final _bindingsGeneratorPath = const _webRefIdl = '@webref/idl'; -const _thisScript = '/tool/update_bindings.dart'; +const _thisScript = 'tool/update_bindings.dart'; const _startComment = ''; @@ -136,9 +151,9 @@ const _endComment = Future _runProc( String executable, - List arguments, - String workingDirectory, -) async { + List arguments, { + required String workingDirectory, +}) async { print(ansi.styleBold.wrap(['*', executable, ...arguments].join(' '))); final proc = await Process.start( executable, @@ -213,7 +228,7 @@ Future _generateJsTypeSupertypes() async { await _runProc( Platform.executable, ['format', jsTypeSupertypesPath], - _bindingsGeneratorPath, + workingDirectory: _bindingsGeneratorPath, ); }