From cbc92b9ba3ff67fe85734a28dc29038c77a04626 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Tue, 1 Aug 2023 11:19:55 -0500 Subject: [PATCH] feat(flutter_tool): update mac.dart to generate shorebird config --- packages/flutter_tools/lib/src/ios/mac.dart | 55 +++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/packages/flutter_tools/lib/src/ios/mac.dart b/packages/flutter_tools/lib/src/ios/mac.dart index 29278ccf1044b..6d6ac6b3b93e4 100644 --- a/packages/flutter_tools/lib/src/ios/mac.dart +++ b/packages/flutter_tools/lib/src/ios/mac.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'package:meta/meta.dart'; import 'package:process/process.dart'; +import 'package:yaml/yaml.dart'; import '../artifacts.dart'; import '../base/file_system.dart'; @@ -477,6 +478,14 @@ Future buildXcodeProject({ globals.printError('Archive succeeded but the expected xcarchive at $outputDir not found'); } } + + try { + updateShorebirdYaml(buildInfo, app.archiveBundleOutputPath); + } on Exception catch (error) { + globals.printError('[shorebird] failed to generate shorebird configuration.\n$error'); + return XcodeBuildResult(success: false); + } + return XcodeBuildResult( success: true, output: outputDir, @@ -491,6 +500,52 @@ Future buildXcodeProject({ } } +void updateShorebirdYaml(BuildInfo buildInfo, String xcarchivePath) { + final File shorebirdYaml = globals.fs.file( + globals.fs.path.join( + xcarchivePath, + 'Products', + 'Applications', + 'Runner.app', + 'Frameworks', + 'App.framework', + 'flutter_assets', + 'shorebird.yaml', + ), + ); + if (!shorebirdYaml.existsSync()) { + throw Exception('shorebird.yaml not found.'); + } + final YamlDocument yaml = loadYamlDocument(shorebirdYaml.readAsStringSync()); + final YamlMap yamlMap = yaml.contents as YamlMap; + final String? flavor = buildInfo.flavor; + String appId = ''; + if (flavor == null) { + final String? defaultAppId = yamlMap['app_id'] as String?; + if (defaultAppId == null || defaultAppId.isEmpty) { + throw Exception('Cannot find "app_id" in shorebird.yaml'); + } + appId = defaultAppId; + } else { + final YamlMap? yamlFlavors = yamlMap['flavors'] as YamlMap?; + if (yamlFlavors == null) { + throw Exception('Cannot find "flavors" in shorebird.yaml.'); + } + final String? flavorAppId = yamlFlavors[flavor] as String?; + if (flavorAppId == null || flavorAppId.isEmpty) { + throw Exception('Cannot find "app_id" for $flavor in shorebird.yaml'); + } + appId = flavorAppId; + } + final StringBuffer yamlContent = StringBuffer(); + final String? baseUrl = yamlMap['base_url'] as String?; + yamlContent.writeln('app_id: $appId'); + if (baseUrl != null) { + yamlContent.writeln('base_url: $baseUrl'); + } + shorebirdYaml.writeAsStringSync(yamlContent.toString(), flush: true); +} + /// Extended attributes applied by Finder can cause code signing errors. Remove them. /// https://developer.apple.com/library/archive/qa/qa1940/_index.html @visibleForTesting