Skip to content

Commit 2c158b5

Browse files
authored
Modifies the update_dev_manifest script (#1727)
1 parent cff143a commit 2c158b5

File tree

5 files changed

+159
-64
lines changed

5 files changed

+159
-64
lines changed

dwds/debug_extension/tool/build_extension.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
3232
echo "Building DDC-compiled extension to dev_build/web directory."
3333
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
3434
dart run build_runner build web --delete-conflicting-outputs --output dev_build
35-
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
36-
echo "Updating the manifest.json file in dev_build/web directory."
37-
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
38-
dart tool/update_dev_manifest.dart
35+
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
36+
echo "Updating files in dev_build/web directory to add dev-signifiers."
37+
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
38+
dart tool/update_dev_files.dart
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
void main() async {
8+
_updateManifestJson();
9+
_updateDevtoolsJs();
10+
}
11+
12+
/// Adds the Googler extension key, updates the extension icon, and prefixes the
13+
/// extension name with "[DEV]".
14+
Future<void> _updateManifestJson() async {
15+
final manifestJson = File('dev_build/web/manifest.json');
16+
final extensionKeyTxt = File('extension_key.txt');
17+
final extensionKey = await extensionKeyTxt.exists()
18+
? await extensionKeyTxt.readAsString()
19+
: null;
20+
_transformDevFile(manifestJson, (line) {
21+
if (_matchesKey(line: line, key: 'name')) {
22+
return [
23+
_newKeyValue(
24+
oldLine: line,
25+
newKey: 'name',
26+
newValue: '[DEV] Dart Debug Extension',
27+
),
28+
if (extensionKey != null)
29+
_newKeyValue(
30+
oldLine: line,
31+
newKey: 'key',
32+
newValue: extensionKey,
33+
),
34+
];
35+
}
36+
if (_matchesKey(line: line, key: 'default_icon')) {
37+
return [
38+
_newKeyValue(
39+
oldLine: line,
40+
newKey: 'default_icon',
41+
newValue: 'dart_dev.png',
42+
)
43+
];
44+
}
45+
if (_matchesValue(line: line, value: 'background.js')) {
46+
return [
47+
_newKeyValue(
48+
oldLine: line,
49+
newKey: null,
50+
newValue: 'background.dart.js',
51+
)
52+
];
53+
} else {
54+
return [line];
55+
}
56+
});
57+
}
58+
59+
/// Prefixes the names of the panels that are added to Chrome DevTools with
60+
/// "[DEV]".
61+
Future<void> _updateDevtoolsJs() async {
62+
final devtoolsJs = File('dev_build/web/devtools.js');
63+
return _transformDevFile(devtoolsJs, (line) {
64+
final originalDebuggerLine = "const DEBUGGER_PANEL_NAME = 'Dart Debugger';";
65+
final modifiedDebuggerLine =
66+
"const DEBUGGER_PANEL_NAME = '[DEV] Dart Debugger';";
67+
final originalInspectorLine =
68+
"const INSPECTOR_PANEL_NAME = 'Flutter Inspector';";
69+
final modifiedInspectorLine =
70+
"const INSPECTOR_PANEL_NAME = '[DEV] Flutter Inspector';";
71+
if (_matchesLine(line: line, match: originalDebuggerLine)) {
72+
return [_newLine(oldLine: line, newLine: modifiedDebuggerLine)];
73+
}
74+
if (_matchesLine(line: line, match: originalInspectorLine)) {
75+
return [_newLine(oldLine: line, newLine: modifiedInspectorLine)];
76+
}
77+
return [line];
78+
});
79+
}
80+
81+
Future<void> _transformDevFile(
82+
File devFile, List<String> Function(String) transformLine) async {
83+
final lines = devFile.readAsLinesSync();
84+
final newLines = <String>[];
85+
for (final line in lines) {
86+
newLines.addAll(transformLine(line));
87+
}
88+
final content = newLines.joinWithNewLine();
89+
return devFile.writeAsStringSync(content);
90+
}
91+
92+
bool _matchesKey({required String line, required String key}) {
93+
return line.trimLeft().startsWith('"$key":');
94+
}
95+
96+
bool _matchesValue({required String line, required String value}) {
97+
return line.trimRight().endsWith('"$value"') ||
98+
line.trimRight().endsWith('"$value",');
99+
}
100+
101+
bool _matchesLine({required String line, required String match}) {
102+
return line.trim() == match;
103+
}
104+
105+
String _newKeyValue({
106+
required String oldLine,
107+
String? newKey,
108+
String? newValue,
109+
}) {
110+
final lineStart = oldLine.leftPadding();
111+
final key = newKey != null ? '"$newKey": ' : '';
112+
final value = newValue != null ? '"$newValue"' : '';
113+
final lineEnd = oldLine.trim().endsWith(',') ? ',' : '';
114+
return '$lineStart$key$value$lineEnd';
115+
}
116+
117+
String _newLine({
118+
required String oldLine,
119+
required String newLine,
120+
}) {
121+
final lineStart = oldLine.leftPadding();
122+
return '$lineStart$newLine';
123+
}
124+
125+
extension LeftPaddingExtension on String {
126+
String leftPadding() {
127+
String padding = '';
128+
int idx = 0;
129+
while (idx < length && this[idx] == ' ') {
130+
padding += ' ';
131+
idx++;
132+
}
133+
return padding;
134+
}
135+
}
136+
137+
extension JoinExtension on List<String> {
138+
String joinWithNewLine() {
139+
return '${join('\n')}\n';
140+
}
141+
}

dwds/debug_extension/tool/update_dev_manifest.dart

Lines changed: 0 additions & 50 deletions
This file was deleted.

dwds/debug_extension/web/devtools.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
(function loadDevToolsScript() {
22
const DDR_DART_APP_ATTRIBUTE = 'data-ddr-dart-app';
3+
// Note: Changes to the DEBUGGER_PANEL_NAME and INSPECTOR_PANEL_NAME
4+
// must be reflected in `tool/update_dev_files.dart` as well.
5+
const DEBUGGER_PANEL_NAME = 'Dart Debugger';
6+
const INSPECTOR_PANEL_NAME = 'Flutter Inspector';
37

48
let debuggerCreated = false;
59
let inspectorCreated = false;
@@ -30,16 +34,16 @@
3034
if (!isDartApp) return;
3135

3236
chrome.devtools.panels.create(
33-
'Dart Debugger', '', 'debugger_panel.html'
34-
);
37+
DEBUGGER_PANEL_NAME, '', 'debugger_panel.html'
38+
);
3539
debuggerCreated = true;
3640
createInspectorPanelIfFlutterApp();
3741
});
3842
}
3943

4044
function createInspectorPanelIfFlutterApp() {
4145
const checkFlutterAppInterval = setInterval(function () {
42-
if (inspectorCreated|| checkFlutterCount++ > 10) {
46+
if (inspectorCreated || checkFlutterCount++ > 10) {
4347
clearInterval(checkFlutterAppInterval);
4448
return;
4549
}
@@ -51,8 +55,8 @@
5155
function (isFlutterWeb) {
5256
if (isFlutterWeb) {
5357
chrome.devtools.panels.create(
54-
'Flutter Inspector', '', 'inspector_panel.html'
55-
);
58+
INSPECTOR_PANEL_NAME, '', 'inspector_panel.html'
59+
);
5660
inspectorCreated = true;
5761
}
5862
}

dwds/debug_extension/web/manifest.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"minimum_chrome_version": "10.0",
55
"devtools_page": "devtools.html",
66
"externally_connectable": {
7-
"ids": [
8-
"nbkbficgbembimioedhceniahniffgpl"
9-
]
7+
"ids": [
8+
"nbkbficgbembimioedhceniahniffgpl"
9+
]
1010
},
1111
"manifest_version": 2,
1212
"browser_action": {
@@ -33,6 +33,6 @@
3333
"detector.js"
3434
],
3535
"run_at": "document_end"
36-
}
36+
}
3737
]
38-
}
38+
}

0 commit comments

Comments
 (0)