Skip to content

Commit a82a5a9

Browse files
authored
Update builder logic for the Dart Debug Extension (#1717)
1 parent 3cf4382 commit a82a5a9

File tree

9 files changed

+175
-9803
lines changed

9 files changed

+175
-9803
lines changed

dwds/debug_extension/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
build/
2+
prod_build/
3+
dev_build/
4+
extension_key.txt

dwds/debug_extension/CONTRIBUTING.md

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,39 @@
11
## Building
22

3-
- With dart2js:
3+
> Note: First make the script executable: `chmod +x tool/build_extension.sh`
4+
5+
### With DDC (for development):
46

57
```
6-
dart run build_runner build web -o build -r
8+
./tool/build_extension.sh
79
```
810

9-
This will build to the `/web` directory.
11+
- The DDC-compiled extension will be located in the `/dev_build/web` directory.
1012

11-
- With DDC:
13+
### With dart2js (for release):
1214

1315
```
14-
dart run build_runner build web -o build
16+
./tool/build_extension.sh prod
1517
```
1618

17-
This will build to the `/build/web` directory.
19+
- The dart2js-compiled extension will be located in the `/prod_build` directory.
1820

1921
## Local Development
2022

21-
### Update `manifest.json`:
22-
23-
- Change the `default_icon` in `manifest.json` to `dart_dev.png` (Note: this is
24-
not strictly necessary, but will help you to distinguish your local version of
25-
the extension from the published version)
26-
- \[For Googlers\] The developer key is needed for local development and
27-
testing. Add one of the whitelisted keys to `web/manifest.json`. IMPORTANT: DO
28-
NOT COMMIT THE KEY.
23+
### \[For Googlers\] Create an `extension_key.txt` file:
2924

30-
```
31-
{
32-
"name": "Dart Debug Extension",
33-
"key": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
34-
...
35-
}
36-
```
25+
- Create a `extension_key.txt` file at the root of `/debug_extension`. Paste in
26+
the value of one of the whitelisted developer keys into this txt file.
27+
IMPORTANT: DO NOT COMMIT THE KEY. It will be copied into the `manifest.json`
28+
when you build the extension.
3729

3830
### Build and upload your local extension
3931

4032
- Build the extension following the instructions above
4133
- Visit chrome://extensions
4234
- Toggle "Developer mode" on
4335
- Click the "Load unpacked" button
44-
- Select the extension directory: `/dwds/debug_extension/web`
36+
- Select the extension directory: `dev_build/web`
4537

4638
### Debug your local extension
4739

@@ -58,14 +50,18 @@ This will build to the `/build/web` directory.
5850

5951
1. Update the version in `web/manifest.json`, `pubspec.yaml`, and in the
6052
`CHANGELOG`.
61-
1. Build dart2js: `pub run build_runner build web -o build -r`
53+
1. Follow the instructions above to build the dart2js-compiled release version
54+
of the extension.
6255

63-
> *At this point, you should manually verify that everything is working by
64-
> following the steps in [Local Development](#local-development).*
56+
> \*At this point, you should manually verify that everything is working by
57+
> following the steps in [Local Development](#local-development), except load
58+
> the extension from the `prod_build` directory. You will need to add an
59+
> extension key to the `manifest.json` file in `prod_build` to test locally.
6560
66-
3. Open a PR to submit the version and build changes.
61+
3. Open a PR to submit the version change.
6762
1. Once submitted, pull the changes down to your local branch, and create a zip
68-
of the `debug_extension/web` directory (NOT `debug_extension/build/web`).
63+
of the `prod_build` directory (NOT `dev_build/web`). **Remove the Googler
64+
extension key that was added by the builder to the `manifest.json` file.**
6965
1. Rename the zip `version_XX.XX.XX.zip` (eg, `version_1.24.0.zip`) and add it
7066
to the go/dart-debug-extension-zips folder
7167

dwds/debug_extension/build.yaml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ targets:
77
- -O4
88
- --csp
99
generate_for:
10-
- web/background.dart
10+
- web/**.dart
1111
extension|client_js_copy_builder:
1212
enabled: true
1313

@@ -17,7 +17,15 @@ builders:
1717
builder_factories:
1818
- copyBuilder
1919
build_extensions:
20-
web/background.dart.js:
21-
- web/background.js
20+
{
21+
"web/{{}}.dart.js": ["prod_build/{{}}.js"],
22+
"web/{{}}.png": ["prod_build/{{}}.png"],
23+
"web/{{}}.html": ["prod_build/{{}}.html"],
24+
"web/{{}}.css": ["prod_build/{{}}.css"],
25+
"web/manifest.json": ["prod_build/manifest.json"],
26+
"web/panel.js": ["prod_build/panel.js"],
27+
"web/detector.js": ["prod_build/detector.js"],
28+
"web/devtools.js": ["prod_build/devtools.js"],
29+
}
2230
auto_apply: none
2331
build_to: source
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
# Copyright 2022 The Chromium Authors. All rights reserved.
4+
# Use of this source code is governed by a BSD-style license that can be
5+
# found in the LICENSE file.
6+
7+
# INSTRUCTIONS:
8+
9+
# Building DDC-compiled app (for development work):
10+
# ./tool/build_extension.sh
11+
12+
# Building dart2js-compiled app (for release):
13+
# ./tool/build_extension.sh prod
14+
15+
prod="false"
16+
17+
case "$1" in
18+
prod)
19+
prod="true"
20+
shift;;
21+
esac
22+
23+
if [ $prod == true ]; then
24+
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
25+
echo "Building dart2js-compiled extension to /prod_build directory."
26+
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
27+
dart run build_runner build web --delete-conflicting-outputs --output build --release
28+
exit 1
29+
fi
30+
31+
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
32+
echo "Building DDC-compiled extension to dev_build/web directory."
33+
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
34+
dart run build_runner build web --delete-conflicting-outputs --output dev_build
35+
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
36+
"Updating the manifest.json file in dev_build/web directory."
37+
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
38+
dart tool/update_dev_manifest.dart

dwds/debug_extension/tool/copy_builder.dart

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,39 @@ import 'package:build/build.dart';
77
/// Factory for the build script.
88
Builder copyBuilder(_) => _CopyBuilder();
99

10-
/// Copies the [_backgroundJsId] file to [_backgroundJsCopyId].
1110
class _CopyBuilder extends Builder {
1211
@override
1312
Map<String, List<String>> get buildExtensions => {
14-
_backgroundJsId.path: [_backgroundJsCopyId.path]
13+
"web/{{}}.dart.js": ["prod_build/{{}}.js"],
14+
"web/{{}}.png": ["prod_build/{{}}.png"],
15+
"web/{{}}.html": ["prod_build/{{}}.html"],
16+
"web/{{}}.css": ["prod_build/{{}}.css"],
17+
"web/manifest.json": ["prod_build/manifest.json"],
18+
"web/panel.js": ["prod_build/panel.js"],
19+
"web/detector.js": ["prod_build/detector.js"],
20+
"web/devtools.js": ["prod_build/devtools.js"],
1521
};
1622

1723
@override
18-
void build(BuildStep buildStep) {
19-
if (buildStep.inputId == _backgroundJsId) {
20-
buildStep.writeAsString(
21-
_backgroundJsCopyId, buildStep.readAsString(_backgroundJsId));
24+
void build(BuildStep buildStep) async {
25+
final inputAsset = buildStep.inputId;
26+
final allowedOutputs = buildStep.allowedOutputs;
27+
28+
if (allowedOutputs.length != 1) {
2229
return;
23-
} else {
24-
throw StateError(
25-
'Unexpected input for `CopyBuilder` expected only $_backgroundJsId');
2630
}
31+
32+
final outputAsset = allowedOutputs.first;
33+
await _copyBinaryFile(buildStep,
34+
inputAsset: inputAsset, outputAsset: outputAsset);
2735
}
28-
}
2936

30-
final _backgroundJsId = AssetId('extension', 'web/background.dart.js');
31-
final _backgroundJsCopyId = AssetId('extension', 'web/background.js');
37+
Future<void> _copyBinaryFile(
38+
BuildStep buildStep, {
39+
required AssetId inputAsset,
40+
required AssetId outputAsset,
41+
}) {
42+
return buildStep.writeAsBytes(
43+
outputAsset, buildStep.readAsBytes(inputAsset));
44+
}
45+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:io';
6+
7+
/// Adds the extension key and updates the icon in the manifest.json.
8+
void main() async {
9+
final manifestJson = File('dev_build/web/manifest.json');
10+
final extensionKey = File('extension_key.txt');
11+
final keyValue =
12+
await extensionKey.exists() ? await extensionKey.readAsString() : null;
13+
_updateManifest(manifestJson, extensionKey: keyValue);
14+
}
15+
16+
Future<void> _updateManifest(File manifestJson, {String? extensionKey}) async {
17+
final lines = manifestJson.readAsLinesSync();
18+
final newLines = <String>[];
19+
for (final line in lines) {
20+
final trimmedLine = line.trimLeft();
21+
if (trimmedLine.startsWith('"name":') && extensionKey != null) {
22+
newLines.add(line);
23+
newLines.add('${line.leftPadding()}"key": "$extensionKey",');
24+
} else if (trimmedLine.startsWith('"default_icon":')) {
25+
newLines.add('${line.leftPadding()}"default_icon": "dart_dev.png"');
26+
} else {
27+
newLines.add(line);
28+
}
29+
}
30+
final content = newLines.joinWithNewLine();
31+
return manifestJson.writeAsStringSync(content);
32+
}
33+
34+
extension LeftPaddingExtension on String {
35+
String leftPadding() {
36+
String padding = '';
37+
int idx = 0;
38+
while (idx < length && this[idx] == ' ') {
39+
padding += ' ';
40+
idx++;
41+
}
42+
return padding;
43+
}
44+
}
45+
46+
extension JoinExtension on List<String> {
47+
String joinWithNewLine() {
48+
return '${join('\n')}\n';
49+
}
50+
}

0 commit comments

Comments
 (0)