Skip to content

Commit c15ff68

Browse files
authored
[macOS] Refactor macOS build/codesize analysis (flutter#137164)
Extracts out _shouldWriteCodeSizeAnalysis and _writeCodeSizeAnalysis from the main buildMacOS function. No tets changes sine this is a simple restructuring for readability and does not change any tool logic.
1 parent 8f6c1e3 commit c15ff68

File tree

1 file changed

+51
-40
lines changed

1 file changed

+51
-40
lines changed

packages/flutter_tools/lib/src/macos/build_macos.dart

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -140,46 +140,57 @@ Future<void> buildMacOS({
140140
if (result != 0) {
141141
throwToolExit('Build process failed');
142142
}
143-
if (buildInfo.codeSizeDirectory != null && sizeAnalyzer != null) {
144-
final String arch = DarwinArch.x86_64.name;
145-
final File aotSnapshot = globals.fs.directory(buildInfo.codeSizeDirectory)
146-
.childFile('snapshot.$arch.json');
147-
final File precompilerTrace = globals.fs.directory(buildInfo.codeSizeDirectory)
148-
.childFile('trace.$arch.json');
149-
150-
// This analysis is only supported for release builds.
151-
// Attempt to guess the correct .app by picking the first one.
152-
final Directory candidateDirectory = globals.fs.directory(
153-
globals.fs.path.join(getMacOSBuildDirectory(), 'Build', 'Products', 'Release'),
154-
);
155-
final Directory appDirectory = candidateDirectory.listSync()
156-
.whereType<Directory>()
157-
.firstWhere((Directory directory) {
158-
return globals.fs.path.extension(directory.path) == '.app';
159-
});
160-
final Map<String, Object?> output = await sizeAnalyzer.analyzeAotSnapshot(
161-
aotSnapshot: aotSnapshot,
162-
precompilerTrace: precompilerTrace,
163-
outputDirectory: appDirectory,
164-
type: 'macos',
165-
excludePath: 'Versions', // Avoid double counting caused by symlinks
166-
);
167-
final File outputFile = globals.fsUtils.getUniqueFile(
168-
globals.fs
169-
.directory(globals.fsUtils.homeDirPath)
170-
.childDirectory('.flutter-devtools'), 'macos-code-size-analysis', 'json',
171-
)..writeAsStringSync(jsonEncode(output));
172-
// This message is used as a sentinel in analyze_apk_size_test.dart
173-
globals.printStatus(
174-
'A summary of your macOS bundle analysis can be found at: ${outputFile.path}',
175-
);
143+
await _writeCodeSizeAnalysis(buildInfo, sizeAnalyzer);
144+
globals.flutterUsage.sendTiming('build', 'xcode-macos', Duration(milliseconds: sw.elapsedMilliseconds));
145+
}
176146

177-
// DevTools expects a file path relative to the .flutter-devtools/ dir.
178-
final String relativeAppSizePath = outputFile.path.split('.flutter-devtools/').last.trim();
179-
globals.printStatus(
180-
'\nTo analyze your app size in Dart DevTools, run the following command:\n'
181-
'dart devtools --appSizeBase=$relativeAppSizePath'
182-
);
147+
/// Performs a size analysis of the AOT snapshot and writes to an analysis file, if configured.
148+
///
149+
/// Size analysis will be run for release builds where the --analyze-size
150+
/// option has been specified. By default, size analysis JSON output is written
151+
/// to ~/.flutter-devtools/macos-code-size-analysis_NN.json.
152+
Future<void> _writeCodeSizeAnalysis(BuildInfo buildInfo, SizeAnalyzer? sizeAnalyzer) async {
153+
// Bail out if the size analysis option was not specified.
154+
if (buildInfo.codeSizeDirectory == null || sizeAnalyzer == null) {
155+
return;
183156
}
184-
globals.flutterUsage.sendTiming('build', 'xcode-macos', Duration(milliseconds: sw.elapsedMilliseconds));
157+
final String arch = DarwinArch.x86_64.name;
158+
final File aotSnapshot = globals.fs.directory(buildInfo.codeSizeDirectory)
159+
.childFile('snapshot.$arch.json');
160+
final File precompilerTrace = globals.fs.directory(buildInfo.codeSizeDirectory)
161+
.childFile('trace.$arch.json');
162+
163+
// This analysis is only supported for release builds.
164+
// Attempt to guess the correct .app by picking the first one.
165+
final Directory candidateDirectory = globals.fs.directory(
166+
globals.fs.path.join(getMacOSBuildDirectory(), 'Build', 'Products', 'Release'),
167+
);
168+
final Directory appDirectory = candidateDirectory.listSync()
169+
.whereType<Directory>()
170+
.firstWhere((Directory directory) {
171+
return globals.fs.path.extension(directory.path) == '.app';
172+
});
173+
final Map<String, Object?> output = await sizeAnalyzer.analyzeAotSnapshot(
174+
aotSnapshot: aotSnapshot,
175+
precompilerTrace: precompilerTrace,
176+
outputDirectory: appDirectory,
177+
type: 'macos',
178+
excludePath: 'Versions', // Avoid double counting caused by symlinks
179+
);
180+
final File outputFile = globals.fsUtils.getUniqueFile(
181+
globals.fs
182+
.directory(globals.fsUtils.homeDirPath)
183+
.childDirectory('.flutter-devtools'), 'macos-code-size-analysis', 'json',
184+
)..writeAsStringSync(jsonEncode(output));
185+
// This message is used as a sentinel in analyze_apk_size_test.dart
186+
globals.printStatus(
187+
'A summary of your macOS bundle analysis can be found at: ${outputFile.path}',
188+
);
189+
190+
// DevTools expects a file path relative to the .flutter-devtools/ dir.
191+
final String relativeAppSizePath = outputFile.path.split('.flutter-devtools/').last.trim();
192+
globals.printStatus(
193+
'\nTo analyze your app size in Dart DevTools, run the following command:\n'
194+
'dart devtools --appSizeBase=$relativeAppSizePath'
195+
);
185196
}

0 commit comments

Comments
 (0)