Skip to content

Dart Debug Extension MV3 scaffold #1732

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

Merged
merged 3 commits into from
Sep 1, 2022
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
3 changes: 3 additions & 0 deletions dwds/debug_extension_mv3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/
compiled/
extension_key.txt
26 changes: 26 additions & 0 deletions dwds/debug_extension_mv3/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
targets:
$default:
builders:
build_web_compilers|entrypoint:
options:
dart2js_args:
- -O1 # Note: Change to -04 for minified JS.
- --csp
generate_for:
- web/**.dart
mv3_extension|client_js_copy_builder:
enabled: true

builders:
client_js_copy_builder:
import: "tool/copy_builder.dart"
builder_factories:
- copyBuilder
build_extensions:
{
"web/{{}}.dart.js": ["compiled/{{}}.dart.js"],
"web/{{}}.png": ["compiled/{{}}.png"],
"web/manifest.json": ["compiled/manifest.json"],
}
auto_apply: none
build_to: source
17 changes: 17 additions & 0 deletions dwds/debug_extension_mv3/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: mv3_extension
publish_to: none
version: 1.30.0
homepage: https://github.com/dart-lang/webdev
description: >-
A Chrome extension for Dart debugging.

environment:
sdk: '>=2.12.0 <3.0.0'

dependencies:
js: ^0.6.1+1

dev_dependencies:
build: ^2.0.0
build_web_compilers: ^3.0.0
build_runner: ^2.0.6
20 changes: 20 additions & 0 deletions dwds/debug_extension_mv3/tool/build_extension.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# Copyright 2022 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# INSTRUCTIONS:

# Builds the unminifed dart2js app (see DDC issue: https://github.com/dart-lang/sdk/issues/49869):
# ./tool/build_extension.sh

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "Building dart2js-compiled extension to /compiled directory."
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
dart run build_runner build web --output build --release

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "Updating manifest.json in /compiled directory."
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
dart tool/update_dev_files.dart
40 changes: 40 additions & 0 deletions dwds/debug_extension_mv3/tool/copy_builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2019, 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.

import 'package:build/build.dart';

/// Factory for the build script.
Builder copyBuilder(_) => _CopyBuilder();

class _CopyBuilder extends Builder {
@override
Map<String, List<String>> get buildExtensions => {
"web/{{}}.dart.js": ["compiled/{{}}.dart.js"],
"web/{{}}.png": ["compiled/{{}}.png"],
"web/manifest.json": ["compiled/manifest.json"],
};

@override
void build(BuildStep buildStep) async {
final inputAsset = buildStep.inputId;
final allowedOutputs = buildStep.allowedOutputs;

if (allowedOutputs.length != 1) {
return;
}

final outputAsset = allowedOutputs.first;
await _copyBinaryFile(buildStep,
inputAsset: inputAsset, outputAsset: outputAsset);
}

Future<void> _copyBinaryFile(
BuildStep buildStep, {
required AssetId inputAsset,
required AssetId outputAsset,
}) {
return buildStep.writeAsBytes(
outputAsset, buildStep.readAsBytes(inputAsset));
}
}
78 changes: 78 additions & 0 deletions dwds/debug_extension_mv3/tool/update_dev_files.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2022, 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.

import 'dart:io';

void main() async {
_updateManifestJson();
}

/// Adds the Googler extension key.
Future<void> _updateManifestJson() async {
final manifestJson = File('compiled/manifest.json');
final extensionKeyTxt = File('extension_key.txt');
final extensionKey = await extensionKeyTxt.exists()
? await extensionKeyTxt.readAsString()
: null;
_transformDevFile(manifestJson, (line) {
if (_matchesKey(line: line, key: 'name')) {
return [
line,
if (extensionKey != null)
_newKeyValue(
oldLine: line,
newKey: 'key',
newValue: extensionKey,
),
];
} else {
return [line];
}
});
}

Future<void> _transformDevFile(
File devFile, List<String> Function(String) transformLine) async {
final lines = devFile.readAsLinesSync();
final newLines = <String>[];
for (final line in lines) {
newLines.addAll(transformLine(line));
}
final content = newLines.joinWithNewLine();
return devFile.writeAsStringSync(content);
}

bool _matchesKey({required String line, required String key}) {
return line.trimLeft().startsWith('"$key":');
}

String _newKeyValue({
required String oldLine,
String? newKey,
String? newValue,
}) {
final lineStart = oldLine.leftPadding();
final key = newKey != null ? '"$newKey": ' : '';
final value = newValue != null ? '"$newValue"' : '';
final lineEnd = oldLine.trim().endsWith(',') ? ',' : '';
return '$lineStart$key$value$lineEnd';
}

extension LeftPaddingExtension on String {
String leftPadding() {
String padding = '';
int idx = 0;
while (idx < length && this[idx] == ' ') {
padding += ' ';
idx++;
}
return padding;
}
}

extension JoinExtension on List<String> {
String joinWithNewLine() {
return '${join('\n')}\n';
}
}
19 changes: 19 additions & 0 deletions dwds/debug_extension_mv3/web/background.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2022, 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.

@JS()
library background;

import 'package:js/js.dart';

import 'chrome_api.dart';
import 'web_api.dart';

void main() {
console.log('Running Dart Debug Extension.');
// Detect clicks on the Dart Debug Extension icon.
chrome.action.onClicked.addListener(allowInterop((_) {
console.log('Detected click on the Dart Debug Extension icon.');
}));
}
34 changes: 34 additions & 0 deletions dwds/debug_extension_mv3/web/chrome_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2022, 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.

import 'package:js/js.dart';

@JS()
external Chrome get chrome;

@JS()
@anonymous
class Chrome {
external Action get action;
}

@JS()
@anonymous
class Action {
// https://developer.chrome.com/docs/extensions/reference/action/#event-onClicked
external OnClickedHandler get onClicked;
}

@JS()
@anonymous
class OnClickedHandler {
external void addListener(void Function(Tab tab) callback);
}

@JS()
@anonymous
class Tab {
external int get id;
external String get url;
}
Binary file added dwds/debug_extension_mv3/web/dart_dev.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions dwds/debug_extension_mv3/web/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "[MV3] Dart Debug Extension",
"version": "1.0",
"manifest_version": 3,
"action": {
"default_icon": "dart_dev.png"
},
"background": {
"service_worker": "background.dart.js"
}
}
15 changes: 15 additions & 0 deletions dwds/debug_extension_mv3/web/web_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2022, 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.

import 'package:js/js.dart';

@JS()
external Console get console;

@JS()
@anonymous
class Console {
external void log(String header,
[String style1, String style2, String style3]);
}