From 0b65330199334e87a38477c3f377f648d0370b71 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Tue, 13 Apr 2021 14:09:04 -0700 Subject: [PATCH] Add an option to specify the output for code size intermediate files (#80310) --- .../lib/src/runner/flutter_command.dart | 14 ++++++++++- .../integration.shard/analyze_size_test.dart | 24 +++++++++++++++++++ packages/flutter_tools/test/src/common.dart | 2 +- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart index b274c92519..32d341da32 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart @@ -115,6 +115,7 @@ class FlutterOptions { static const String kDeviceUser = 'device-user'; static const String kDeviceTimeout = 'device-timeout'; static const String kAnalyzeSize = 'analyze-size'; + static const String kCodeSizeDirectory = 'code-size-directory'; static const String kNullAssertions = 'null-assertions'; static const String kAndroidGradleDaemon = 'android-gradle-daemon'; static const String kDeferredComponents = 'deferred-components'; @@ -831,8 +832,16 @@ abstract class FlutterCommand extends Command { 'This flag is only supported on "--release" builds. When building for Android, a single ' 'ABI must be specified at a time with the "--target-platform" flag. When building for iOS, ' 'only the symbols from the arm64 architecture are used to analyze code size.\n' + 'By default, the intermediate output files will be placed in a transient directory in the ' + 'build directory. This can be overriden with the "--${FlutterOptions.kCodeSizeDirectory}" option.\n' 'This flag cannot be combined with "--${FlutterOptions.kSplitDebugInfoOption}".' ); + + argParser.addOption( + FlutterOptions.kCodeSizeDirectory, + help: 'The location to write code size analysis files. If this is not specified, files ' + 'are written to a temporary directory under the build directory.' + ); } /// Compute the [BuildInfo] for the current flutter command. @@ -877,10 +886,13 @@ abstract class FlutterCommand extends Command { String codeSizeDirectory; if (argParser.options.containsKey(FlutterOptions.kAnalyzeSize) && boolArg(FlutterOptions.kAnalyzeSize)) { - final Directory directory = globals.fsUtils.getUniqueDirectory( + Directory directory = globals.fsUtils.getUniqueDirectory( globals.fs.directory(getBuildDirectory()), 'flutter_size', ); + if (argParser.options.containsKey(FlutterOptions.kCodeSizeDirectory) && stringArg(FlutterOptions.kCodeSizeDirectory) != null) { + directory = globals.fs.directory(stringArg(FlutterOptions.kCodeSizeDirectory)); + } directory.createSync(recursive: true); codeSizeDirectory = directory.path; } diff --git a/packages/flutter_tools/test/integration.shard/analyze_size_test.dart b/packages/flutter_tools/test/integration.shard/analyze_size_test.dart index b57c5ec2fe..6bb4999d56 100644 --- a/packages/flutter_tools/test/integration.shard/analyze_size_test.dart +++ b/packages/flutter_tools/test/integration.shard/analyze_size_test.dart @@ -4,6 +4,7 @@ // @dart = 2.8 +import 'package:file/file.dart'; import 'package:file_testing/file_testing.dart'; import 'package:flutter_tools/src/base/io.dart'; @@ -117,4 +118,27 @@ void main() { expect(result.exitCode, 1); }); + + testWithoutContext('--analyze-size allows overriding the directory for code size files', () async { + final String flutterBin = fileSystem.path.join(getFlutterRoot(), 'bin', 'flutter'); + final Directory tempDir = fileSystem.systemTempDirectory.createTempSync('size_test'); + + final ProcessResult result = await processManager.run([ + flutterBin, + ...getLocalEngineArguments(), + 'build', + 'apk', + '--analyze-size', + '--code-size-directory=${tempDir.path}', + '--target-platform=android-arm64', + '--release', + ], workingDirectory: fileSystem.path.join(getFlutterRoot(), 'examples', 'hello_world')); + + expect(result.exitCode, 0); + expect(tempDir.existsSync(), true); + expect(tempDir.childFile('snapshot.arm64-v8a.json').existsSync(), true); + expect(tempDir.childFile('trace.arm64-v8a.json').existsSync(), true); + + tempDir.deleteSync(recursive: true); + }); } diff --git a/packages/flutter_tools/test/src/common.dart b/packages/flutter_tools/test/src/common.dart index 46bf41f06b..908457cb00 100644 --- a/packages/flutter_tools/test/src/common.dart +++ b/packages/flutter_tools/test/src/common.dart @@ -164,7 +164,7 @@ void test(String description, FutureOr Function() body, { /// Executes a test body in zone that does not allow context-based injection. /// -/// For classes which have been refactored to excluded context-based injection +/// For classes which have been refactored to exclude context-based injection /// or globals like [fs] or [platform], prefer using this test method as it /// will prevent accidentally including these context getters in future code /// changes.