Skip to content

Modifies the update_dev_manifest script #1727

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 4 commits into from
Aug 23, 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
8 changes: 4 additions & 4 deletions dwds/debug_extension/tool/build_extension.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
141 changes: 141 additions & 0 deletions dwds/debug_extension/tool/update_dev_files.dart
Original file line number Diff line number Diff line change
@@ -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<void> _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<void> _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<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":');
}

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> {
String joinWithNewLine() {
return '${join('\n')}\n';
}
}
50 changes: 0 additions & 50 deletions dwds/debug_extension/tool/update_dev_manifest.dart

This file was deleted.

14 changes: 9 additions & 5 deletions dwds/debug_extension/web/devtools.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -30,16 +34,16 @@
if (!isDartApp) return;

chrome.devtools.panels.create(
'Dart Debugger', '', 'debugger_panel.html'
);
DEBUGGER_PANEL_NAME, '', 'debugger_panel.html'
);
debuggerCreated = true;
createInspectorPanelIfFlutterApp();
});
}

function createInspectorPanelIfFlutterApp() {
const checkFlutterAppInterval = setInterval(function () {
if (inspectorCreated|| checkFlutterCount++ > 10) {
if (inspectorCreated || checkFlutterCount++ > 10) {
clearInterval(checkFlutterAppInterval);
return;
}
Expand All @@ -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;
}
}
Expand Down
10 changes: 5 additions & 5 deletions dwds/debug_extension/web/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"minimum_chrome_version": "10.0",
"devtools_page": "devtools.html",
"externally_connectable": {
"ids": [
"nbkbficgbembimioedhceniahniffgpl"
]
"ids": [
"nbkbficgbembimioedhceniahniffgpl"
]
},
"manifest_version": 2,
"browser_action": {
Expand All @@ -33,6 +33,6 @@
"detector.js"
],
"run_at": "document_end"
}
}
]
}
}