-
Notifications
You must be signed in to change notification settings - Fork 85
Update builder logic for the Dart Debug Extension #1717
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
Changes from 16 commits
ffa56a9
fffc803
ca2667a
87d0b86
59da8d7
7012062
4db90cf
229220b
6a38612
6663f6b
a8f112d
6bc0ddc
e9f2cd6
b9ccc5b
33c7bb2
6479625
ba9404d
99f65ae
d5f4151
a146c37
973bc9c
af42b98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
build/ | ||
extension_key.txt |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/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: | ||
|
||
# Building DDC-compiled app (for development work): | ||
# ./tool/build_extension.sh | ||
|
||
# Building dart2js-compiled app (for release): | ||
# ./tool/build_extension.sh prod | ||
|
||
prod="false" | ||
|
||
case "$1" in | ||
prod) | ||
prod="true" | ||
shift;; | ||
esac | ||
|
||
# Clean existing build directory to avoid merge conflicts. | ||
dart run build_runner clean | ||
|
||
if [ $prod == true ]; then | ||
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" | ||
echo "Building dart2js-compiled extension to build/web_prod directory." | ||
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" | ||
dart run build_runner build web -o build -r | ||
exit 1 | ||
fi | ||
|
||
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" | ||
echo "Building DDC-compiled extension to build/web directory." | ||
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" | ||
dart run build_runner build web -o build | ||
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" | ||
echo "Updating the manifest.json file in build/web directory." | ||
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" | ||
dart tool/update_dev_manifest.dart |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,25 +7,39 @@ import 'package:build/build.dart'; | |||||
/// Factory for the build script. | ||||||
Builder copyBuilder(_) => _CopyBuilder(); | ||||||
|
||||||
/// Copies the [_backgroundJsId] file to [_backgroundJsCopyId]. | ||||||
class _CopyBuilder extends Builder { | ||||||
@override | ||||||
Map<String, List<String>> get buildExtensions => { | ||||||
_backgroundJsId.path: [_backgroundJsCopyId.path] | ||||||
"web/{{}}.dart.js": ["build/web_prod/{{}}.js"], | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do I understand correctly that you are outputting into Do you get a mix of "to source" written output, and then the merged output all in the same directory? Would it work to change this builder to be to-cache instead of to-source, and make the directory move into a subdirectory of
Suggested change
I think that should result in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah thank you! Updated so now:
|
||||||
"web/{{}}.png": ["build/web_prod/{{}}.png"], | ||||||
"web/{{}}.html": ["build/web_prod/{{}}.html"], | ||||||
"web/{{}}.css": ["build/web_prod/{{}}.css"], | ||||||
"web/manifest.json": ["build/web_prod/manifest.json"], | ||||||
"web/panel.js": ["build/web_prod/panel.js"], | ||||||
"web/detector.js": ["build/web_prod/detector.js"], | ||||||
"web/devtools.js": ["build/web_prod/devtools.js"], | ||||||
}; | ||||||
|
||||||
@override | ||||||
void build(BuildStep buildStep) { | ||||||
if (buildStep.inputId == _backgroundJsId) { | ||||||
buildStep.writeAsString( | ||||||
_backgroundJsCopyId, buildStep.readAsString(_backgroundJsId)); | ||||||
void build(BuildStep buildStep) async { | ||||||
final inputAsset = buildStep.inputId; | ||||||
final allowedOutputs = buildStep.allowedOutputs; | ||||||
|
||||||
if (allowedOutputs.length != 1) { | ||||||
return; | ||||||
} else { | ||||||
throw StateError( | ||||||
'Unexpected input for `CopyBuilder` expected only $_backgroundJsId'); | ||||||
} | ||||||
|
||||||
final outputAsset = allowedOutputs.first; | ||||||
await _copyBinaryFile(buildStep, | ||||||
inputAsset: inputAsset, outputAsset: outputAsset); | ||||||
} | ||||||
} | ||||||
|
||||||
final _backgroundJsId = AssetId('extension', 'web/background.dart.js'); | ||||||
final _backgroundJsCopyId = AssetId('extension', 'web/background.js'); | ||||||
Future<void> _copyBinaryFile( | ||||||
BuildStep buildStep, { | ||||||
required AssetId inputAsset, | ||||||
required AssetId outputAsset, | ||||||
}) { | ||||||
return buildStep.writeAsBytes( | ||||||
outputAsset, buildStep.readAsBytes(inputAsset)); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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'; | ||
|
||
/// Adds the extension key and updates the icon in the manifest.json. | ||
void main() async { | ||
final manifestJson = File('build/web/manifest.json'); | ||
final extensionKey = File('extension_key.txt'); | ||
final keyValue = | ||
await extensionKey.exists() ? await extensionKey.readAsString() : null; | ||
_updateManifest(manifestJson, extensionKey: keyValue); | ||
} | ||
|
||
Future<void> _updateManifest(File manifestJson, {String? extensionKey}) async { | ||
final lines = manifestJson.readAsLinesSync(); | ||
final newLines = <String>[]; | ||
for (final line in lines) { | ||
final trimmedLine = line.trimLeft(); | ||
if (trimmedLine.startsWith('"name":') && extensionKey != null) { | ||
newLines.add(line); | ||
newLines.add('${line.leftPadding()}"key": "$extensionKey",'); | ||
} else if (trimmedLine.startsWith('"default_icon":')) { | ||
newLines.add('${line.leftPadding()}"default_icon": "dart_dev.png"'); | ||
} else { | ||
newLines.add(line); | ||
} | ||
} | ||
final content = newLines.joinWithNewLine(); | ||
return manifestJson.writeAsStringSync(content); | ||
} | ||
|
||
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'; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How often did you find this was necessary? We don't expect a clean to be necessary for every build in typical workflows.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, solved the problem with
--delete-conflicting-outputs
flag. Was trying to prevent the prompt about conflicts from showing up when running the continuous integration tests