diff --git a/tool/plugin/lib/build_spec.dart b/tool/plugin/lib/build_spec.dart index e5fa1c4b96..0b6abfb6ae 100644 --- a/tool/plugin/lib/build_spec.dart +++ b/tool/plugin/lib/build_spec.dart @@ -1,8 +1,6 @@ // Copyright 2017 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. -// @dart = 2.12 - import 'dart:async'; import 'dart:io'; diff --git a/tool/plugin/lib/edit.dart b/tool/plugin/lib/edit.dart index b652b66a15..0c1bfccdbb 100644 --- a/tool/plugin/lib/edit.dart +++ b/tool/plugin/lib/edit.dart @@ -14,122 +14,102 @@ Set appliedEditCommands = {}; void checkAndClearAppliedEditCommands() { if (appliedEditCommands.length != editCommands.length) { - var commands = {}; + final commands = {}; commands.addAll(editCommands); commands.removeAll(appliedEditCommands); separator("UNUSED EditCommand"); - for (var cmd in commands) { + for (final cmd in commands) { log(cmd.toString()); } } appliedEditCommands.clear(); } +const String _actionThreadEditContent = ''' + + public @NotNull ActionUpdateThread getActionUpdateThread() { + return ActionUpdateThread.BGT; + } +'''; + List editCommands = [ // When using LATEST-EAP-SNAPSHOT, also set baseVersion to LATEST-EAP-SNAPSHOT in the build spec. - Subst( + EditCommand( path: 'build.gradle.kts', - initial: 'version.set(ideVersion)', - replacement: 'version.set("LATEST-EAP-SNAPSHOT")', - version: '2023.1', + initials: ['version.set(ideVersion)'], + replacements: ['version.set("LATEST-EAP-SNAPSHOT")'], + versions: ['2023.1'], ), - Subst( + EditCommand( path: 'flutter-idea/build.gradle.kts', - initial: 'version.set(ideVersion)', - replacement: 'version.set("LATEST-EAP-SNAPSHOT")', - version: '2023.1', + initials: ['version.set(ideVersion)'], + replacements: ['version.set("LATEST-EAP-SNAPSHOT")'], + versions: ['2023.1'], ), - MultiSubst( + EditCommand( path: 'flutter-studio/src/io/flutter/android/AndroidModuleLibraryManager.java', initials: ['//import ', 'extends ProjectImpl '], replacements: ['import ', 'extends ProjectExImpl '], versions: ['2022.1', '2022.2', '2031.1'], ), - Subst( + EditCommand( path: 'flutter-idea/src/io/flutter/vmService/frame/DartVmServiceValue.java', - initial: '@NotNull', - replacement: '', + initials: ['@NotNull'], + replacements: [''], versions: ['2022.1', '2022.2'], ), - Subst( + EditCommand( path: 'flutter-idea/src/io/flutter/analytics/FlutterAnalysisServerListener.java', - initial: '@NotNull', - replacement: '', + initials: ['@NotNull'], + replacements: [''], versions: ['2022.1', '2022.2'], ), - Subst( + EditCommand( path: 'resources/META-INF/plugin_template.xml', - initial: '', - replacement: '', + initials: [''], + replacements: [''], versions: ['2022.1', '2022.2'], ), - Subst( + EditCommand( path: 'flutter-idea/src/io/flutter/actions/AttachDebuggerAction.java', - initial: ''' - public @NotNull ActionUpdateThread getActionUpdateThread() { - return ActionUpdateThread.BGT; - } -''', - replacement: '', - version: '2022.1', + initials: [_actionThreadEditContent], + replacements: [''], + versions: ['2022.1'], ), - Subst( + EditCommand( path: 'flutter-idea/src/io/flutter/actions/DeviceSelectorAction.java', - initial: ''' - public @NotNull ActionUpdateThread getActionUpdateThread() { - return ActionUpdateThread.BGT; - } -''', - replacement: '', - version: '2022.1', + initials: [_actionThreadEditContent], + replacements: [''], + versions: ['2022.1'], ), - Subst( + EditCommand( path: - 'flutter-idea/src/io/flutter/actions/DeviceSelectorRefresherAction.java', - initial: ''' - public @NotNull ActionUpdateThread getActionUpdateThread() { - return ActionUpdateThread.BGT; - } -''', - replacement: '', - version: '2022.1', + 'flutter-idea/src/io/flutter/actions/DeviceSelectorRefresherAction.java', + initials: [_actionThreadEditContent], + replacements: [''], + versions: ['2022.1'], ), - Subst( + EditCommand( path: 'flutter-idea/src/io/flutter/actions/FlutterRetargetAppAction.java', - initial: ''' - public @NotNull ActionUpdateThread getActionUpdateThread() { - return ActionUpdateThread.BGT; - } -''', - replacement: '', - version: '2022.1', + initials: [_actionThreadEditContent], + replacements: [''], + versions: ['2022.1'], ), ]; -// Used to test checkAndClearAppliedEditCommands() -class Unused extends EditCommand { - @override - String get path => 'unused'; - - @override - String? convert(BuildSpec spec) { - return null; - } -} - /// Apply all the editCommands applicable to a given BuildSpec. Future applyEdits(BuildSpec spec, Function compileFn) async { // Handle skipped files. for (String file in spec.filesToSkip) { - var entity = + final entity = FileSystemEntity.isFileSync(file) ? File(file) : Directory(file); if (entity.existsSync()) { await entity.rename('$file~'); log('renamed $file'); if (entity is File) { - var stubFile = File('${file}_stub'); + final stubFile = File('${file}_stub'); if (stubFile.existsSync()) { await stubFile.copy(file); log('copied ${file}_stub'); @@ -138,10 +118,10 @@ Future applyEdits(BuildSpec spec, Function compileFn) async { } } - var edited = {}; + final edited = {}; try { - for (var edit in editCommands) { - var source = edit.convert(spec); + for (final edit in editCommands) { + final source = edit.convert(spec); if (source != null) { edited[edit] = source; appliedEditCommands.add(edit); @@ -157,9 +137,9 @@ Future applyEdits(BuildSpec spec, Function compileFn) async { }); // Restore skipped files. - for (var file in spec.filesToSkip) { - var name = '$file~'; - var entity = + for (final file in spec.filesToSkip) { + final name = '$file~'; + final entity = FileSystemEntity.isFileSync(name) ? File(name) : Directory(name); if (entity.existsSync()) { await entity.rename(file); @@ -169,101 +149,61 @@ Future applyEdits(BuildSpec spec, Function compileFn) async { } /// Make some changes to a source file prior to compiling for a specific IDE. -abstract class EditCommand { - String? convert(BuildSpec spec); - - void restore(String source) { - var processedFile = File(path); - processedFile.writeAsStringSync(source); - } +class EditCommand { + EditCommand({ + required this.path, + required List initials, + required List replacements, + this.versions = const [], + }) : assert(initials.length == replacements.length), + assert(initials.isNotEmpty), + assert(versions.isNotEmpty), + initials = initials.map(_platformAdaptiveString).toList(), + replacements = replacements.map(_platformAdaptiveString).toList(); - String get path; -} + /// The target file path. + final String path; -/// Single substitution in a file for one or more IDE versions. -class Subst extends EditCommand { - @override - String path; - String initial; - String replacement; + final List initials; + final List replacements; /// The list of platform versions to perform the substitution for. - List versions; - - Subst( - {this.versions = const [''], - required this.initial, - required this.replacement, - required this.path, - String? version}) { - if (version != null) { - // Disallow both version and versions keywords. - versions = [version]; - } - } + final List versions; - @override String? convert(BuildSpec spec) { if (versionMatches(spec)) { - var processedFile = File(path); - var source = processedFile.readAsStringSync(); - var original = source; - source = source.replaceAll(initial, replacement); + final processedFile = File(path); + String source = processedFile.readAsStringSync(); + final original = source; + for (int i = 0; i < initials.length; i++) { + source = source.replaceAll(initials[i], replacements[i]); + } processedFile.writeAsStringSync(source); - log('edited $path'); return original; } else { return null; } } - @override - String toString() => "Subst(path: $path, versions: $versions)"; - bool versionMatches(BuildSpec spec) { return versions.any((v) => spec.version.startsWith(v)); } -} - -// TODO Unify Subst and MultiSubst, and find a better name. -class MultiSubst extends EditCommand { - @override - String path; - List initials; - List replacements; - List versions; - - MultiSubst({ - required this.versions, - required this.initials, - required this.replacements, - required this.path, - }) { - assert(initials.length == replacements.length); - assert(versions.isNotEmpty); - assert(initials.isNotEmpty); - } - @override - String? convert(BuildSpec spec) { - if (versionMatches(spec)) { - var processedFile = File(path); - var source = processedFile.readAsStringSync(); - var original = source; - for (var i = 0; i < initials.length; i++) { - source = source.replaceAll(initials[i], replacements[i]); - } - processedFile.writeAsStringSync(source); - return original; - } else { - return null; - } + void restore(String source) { + final processedFile = File(path); + processedFile.writeAsStringSync(source); } @override - String toString() => "Subst(path: $path, versions: $versions)"; + String toString() => "EditCommand(path: $path, versions: $versions)"; +} - bool versionMatches(BuildSpec spec) { - return versions.any((v) => spec.version.startsWith(v)); +String _platformAdaptiveString(String value) { + if (value.isEmpty) { + return value; + } else if (Platform.isWindows) { + return value.replaceAll('\n', '\r\n'); + } else { + return value.replaceAll('\r\n', '\n'); } } diff --git a/tool/plugin/lib/plugin.dart b/tool/plugin/lib/plugin.dart index 0f6d0afc25..4d304355e2 100644 --- a/tool/plugin/lib/plugin.dart +++ b/tool/plugin/lib/plugin.dart @@ -55,15 +55,6 @@ List createBuildSpecs(ProductCommand command) { return specs; } -Future deleteBuildContents() async { - final dir = Directory(p.join(rootPath, 'build')); - if (!dir.existsSync()) throw 'No build directory found'; - var args = []; - args.add('-rf'); - args.add(p.join(rootPath, 'build', '*')); - return await exec('rm', args); -} - List findJars(String path) { final dir = Directory(path); return dir @@ -163,16 +154,6 @@ Future jar(String directory, String outFile) async { return await exec('jar', args, cwd: directory); } -Future moveToArtifacts(ProductCommand cmd, BuildSpec spec) async { - final dir = Directory(p.join(rootPath, 'artifacts')); - if (!dir.existsSync()) throw 'No artifacts directory found'; - var file = pluginRegistryIds[spec.pluginId]; - var args = []; - args.add(p.join(rootPath, 'build', file)); - args.add(cmd.releasesFilePath(spec)); - return await exec('mv', args); -} - Future performReleaseChecks(ProductCommand cmd) async { // git must have a release_NN branch where NN is the value of --release // git must have no uncommitted changes @@ -267,13 +248,6 @@ String substituteTemplateVariables(String line, BuildSpec spec) { return line; } -Future zip(String directory, String outFile) async { - var dest = p.absolute(outFile); - createDir(p.dirname(dest)); - var args = ['-r', dest, p.basename(directory)]; - return await exec('zip', args, cwd: p.dirname(directory)); -} - void _copyFile(File file, Directory to, {String filename = ''}) { if (!file.existsSync()) { throw "${file.path} does not exist"; @@ -731,6 +705,7 @@ abstract class ProductCommand extends Command { Future run() async { await _initGlobals(); await _initSpecs(); + _handleSymlinksOnWindows(); try { return await doit(); } catch (ex, stack) { @@ -764,6 +739,26 @@ abstract class ProductCommand extends Command { } return specs.length; } + + void _handleSymlinksOnWindows() { + if (!Platform.isWindows) { + return; + } + const List symlinks = [ + r'flutter-idea\resources', + r'flutter-studio\resources', + ]; + final currentPath = Directory.current.path; + for (final entityPath in symlinks) { + final path = '$currentPath\\$entityPath'; + if (FileSystemEntity.isLinkSync(path)) { + continue; + } + final content = File(path).readAsStringSync(); + File(path).deleteSync(); + Link(path).createSync(content); + } + } } /// A crude rename utility. The IntelliJ feature does not work on the case diff --git a/tool/plugin/lib/util.dart b/tool/plugin/lib/util.dart index 3ac4ff0ec8..513d202208 100644 --- a/tool/plugin/lib/util.dart +++ b/tool/plugin/lib/util.dart @@ -1,8 +1,6 @@ // Copyright 2017 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. -// @dart = 2.12 - import 'dart:async'; import 'dart:convert'; import 'dart:io'; diff --git a/tool/plugin/test/plugin_test.dart b/tool/plugin/test/plugin_test.dart index c5083ab779..7b3d01be35 100644 --- a/tool/plugin/test/plugin_test.dart +++ b/tool/plugin/test/plugin_test.dart @@ -1,8 +1,6 @@ // Copyright 2017 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. -// @dart = 2.12 - import 'dart:async'; import 'dart:io';