|
| 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 | +} |
0 commit comments