diff --git a/dwds/debug_extension/tool/build_extension.sh b/dwds/debug_extension/tool/build_extension.sh index e93a93f93..7afbba74c 100755 --- a/dwds/debug_extension/tool/build_extension.sh +++ b/dwds/debug_extension/tool/build_extension.sh @@ -32,7 +32,7 @@ echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" echo "Building DDC-compiled extension to dev_build/web directory." echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" dart run build_runner build web --delete-conflicting-outputs --output dev_build -echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" -echo "Updating the manifest.json file in dev_build/web directory." -echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" -dart tool/update_dev_manifest.dart +echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" +echo "Updating files in dev_build/web directory to add dev-signifiers." +echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" +dart tool/update_dev_files.dart diff --git a/dwds/debug_extension/tool/update_dev_files.dart b/dwds/debug_extension/tool/update_dev_files.dart new file mode 100644 index 000000000..24ec970be --- /dev/null +++ b/dwds/debug_extension/tool/update_dev_files.dart @@ -0,0 +1,141 @@ +// 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(); + _updateDevtoolsJs(); +} + +/// Adds the Googler extension key, updates the extension icon, and prefixes the +/// extension name with "[DEV]". +Future _updateManifestJson() async { + final manifestJson = File('dev_build/web/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 [ + _newKeyValue( + oldLine: line, + newKey: 'name', + newValue: '[DEV] Dart Debug Extension', + ), + if (extensionKey != null) + _newKeyValue( + oldLine: line, + newKey: 'key', + newValue: extensionKey, + ), + ]; + } + if (_matchesKey(line: line, key: 'default_icon')) { + return [ + _newKeyValue( + oldLine: line, + newKey: 'default_icon', + newValue: 'dart_dev.png', + ) + ]; + } + if (_matchesValue(line: line, value: 'background.js')) { + return [ + _newKeyValue( + oldLine: line, + newKey: null, + newValue: 'background.dart.js', + ) + ]; + } else { + return [line]; + } + }); +} + +/// Prefixes the names of the panels that are added to Chrome DevTools with +/// "[DEV]". +Future _updateDevtoolsJs() async { + final devtoolsJs = File('dev_build/web/devtools.js'); + return _transformDevFile(devtoolsJs, (line) { + final originalDebuggerLine = "const DEBUGGER_PANEL_NAME = 'Dart Debugger';"; + final modifiedDebuggerLine = + "const DEBUGGER_PANEL_NAME = '[DEV] Dart Debugger';"; + final originalInspectorLine = + "const INSPECTOR_PANEL_NAME = 'Flutter Inspector';"; + final modifiedInspectorLine = + "const INSPECTOR_PANEL_NAME = '[DEV] Flutter Inspector';"; + if (_matchesLine(line: line, match: originalDebuggerLine)) { + return [_newLine(oldLine: line, newLine: modifiedDebuggerLine)]; + } + if (_matchesLine(line: line, match: originalInspectorLine)) { + return [_newLine(oldLine: line, newLine: modifiedInspectorLine)]; + } + return [line]; + }); +} + +Future _transformDevFile( + File devFile, List Function(String) transformLine) async { + final lines = devFile.readAsLinesSync(); + final newLines = []; + 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":'); +} + +bool _matchesValue({required String line, required String value}) { + return line.trimRight().endsWith('"$value"') || + line.trimRight().endsWith('"$value",'); +} + +bool _matchesLine({required String line, required String match}) { + return line.trim() == match; +} + +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'; +} + +String _newLine({ + required String oldLine, + required String newLine, +}) { + final lineStart = oldLine.leftPadding(); + return '$lineStart$newLine'; +} + +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 joinWithNewLine() { + return '${join('\n')}\n'; + } +} diff --git a/dwds/debug_extension/tool/update_dev_manifest.dart b/dwds/debug_extension/tool/update_dev_manifest.dart deleted file mode 100644 index 104c62d5b..000000000 --- a/dwds/debug_extension/tool/update_dev_manifest.dart +++ /dev/null @@ -1,50 +0,0 @@ -// 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('dev_build/web/manifest.json'); - final extensionKey = File('extension_key.txt'); - final keyValue = - await extensionKey.exists() ? await extensionKey.readAsString() : null; - _updateManifest(manifestJson, extensionKey: keyValue); -} - -Future _updateManifest(File manifestJson, {String? extensionKey}) async { - final lines = manifestJson.readAsLinesSync(); - final newLines = []; - 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 joinWithNewLine() { - return '${join('\n')}\n'; - } -} diff --git a/dwds/debug_extension/web/devtools.js b/dwds/debug_extension/web/devtools.js index 97c18a39f..4f2189a3a 100644 --- a/dwds/debug_extension/web/devtools.js +++ b/dwds/debug_extension/web/devtools.js @@ -1,5 +1,9 @@ (function loadDevToolsScript() { const DDR_DART_APP_ATTRIBUTE = 'data-ddr-dart-app'; + // Note: Changes to the DEBUGGER_PANEL_NAME and INSPECTOR_PANEL_NAME + // must be reflected in `tool/update_dev_files.dart` as well. + const DEBUGGER_PANEL_NAME = 'Dart Debugger'; + const INSPECTOR_PANEL_NAME = 'Flutter Inspector'; let debuggerCreated = false; let inspectorCreated = false; @@ -30,8 +34,8 @@ if (!isDartApp) return; chrome.devtools.panels.create( - 'Dart Debugger', '', 'debugger_panel.html' - ); + DEBUGGER_PANEL_NAME, '', 'debugger_panel.html' + ); debuggerCreated = true; createInspectorPanelIfFlutterApp(); }); @@ -39,7 +43,7 @@ function createInspectorPanelIfFlutterApp() { const checkFlutterAppInterval = setInterval(function () { - if (inspectorCreated|| checkFlutterCount++ > 10) { + if (inspectorCreated || checkFlutterCount++ > 10) { clearInterval(checkFlutterAppInterval); return; } @@ -51,8 +55,8 @@ function (isFlutterWeb) { if (isFlutterWeb) { chrome.devtools.panels.create( - 'Flutter Inspector', '', 'inspector_panel.html' - ); + INSPECTOR_PANEL_NAME, '', 'inspector_panel.html' + ); inspectorCreated = true; } } diff --git a/dwds/debug_extension/web/manifest.json b/dwds/debug_extension/web/manifest.json index 50b9fc02e..9c83bd302 100644 --- a/dwds/debug_extension/web/manifest.json +++ b/dwds/debug_extension/web/manifest.json @@ -4,9 +4,9 @@ "minimum_chrome_version": "10.0", "devtools_page": "devtools.html", "externally_connectable": { - "ids": [ - "nbkbficgbembimioedhceniahniffgpl" - ] + "ids": [ + "nbkbficgbembimioedhceniahniffgpl" + ] }, "manifest_version": 2, "browser_action": { @@ -33,6 +33,6 @@ "detector.js" ], "run_at": "document_end" - } + } ] -} +} \ No newline at end of file