Skip to content

Commit 3ec5bcb

Browse files
committed
feat(flutter_tool): update mac.dart to generate shorebird config (#15)
1 parent 41466d5 commit 3ec5bcb

File tree

1 file changed

+55
-0
lines changed
  • packages/flutter_tools/lib/src/ios

1 file changed

+55
-0
lines changed

packages/flutter_tools/lib/src/ios/mac.dart

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:async';
66

77
import 'package:meta/meta.dart';
88
import 'package:process/process.dart';
9+
import 'package:yaml/yaml.dart';
910

1011
import '../artifacts.dart';
1112
import '../base/file_system.dart';
@@ -496,6 +497,14 @@ Future<XcodeBuildResult> buildXcodeProject({
496497
globals.printError('Archive succeeded but the expected xcarchive at $outputDir not found');
497498
}
498499
}
500+
501+
try {
502+
updateShorebirdYaml(buildInfo, app.archiveBundleOutputPath);
503+
} on Exception catch (error) {
504+
globals.printError('[shorebird] failed to generate shorebird configuration.\n$error');
505+
return XcodeBuildResult(success: false);
506+
}
507+
499508
return XcodeBuildResult(
500509
success: true,
501510
output: outputDir,
@@ -510,6 +519,52 @@ Future<XcodeBuildResult> buildXcodeProject({
510519
}
511520
}
512521

522+
void updateShorebirdYaml(BuildInfo buildInfo, String xcarchivePath) {
523+
final File shorebirdYaml = globals.fs.file(
524+
globals.fs.path.join(
525+
xcarchivePath,
526+
'Products',
527+
'Applications',
528+
'Runner.app',
529+
'Frameworks',
530+
'App.framework',
531+
'flutter_assets',
532+
'shorebird.yaml',
533+
),
534+
);
535+
if (!shorebirdYaml.existsSync()) {
536+
throw Exception('shorebird.yaml not found.');
537+
}
538+
final YamlDocument yaml = loadYamlDocument(shorebirdYaml.readAsStringSync());
539+
final YamlMap yamlMap = yaml.contents as YamlMap;
540+
final String? flavor = buildInfo.flavor;
541+
String appId = '';
542+
if (flavor == null) {
543+
final String? defaultAppId = yamlMap['app_id'] as String?;
544+
if (defaultAppId == null || defaultAppId.isEmpty) {
545+
throw Exception('Cannot find "app_id" in shorebird.yaml');
546+
}
547+
appId = defaultAppId;
548+
} else {
549+
final YamlMap? yamlFlavors = yamlMap['flavors'] as YamlMap?;
550+
if (yamlFlavors == null) {
551+
throw Exception('Cannot find "flavors" in shorebird.yaml.');
552+
}
553+
final String? flavorAppId = yamlFlavors[flavor] as String?;
554+
if (flavorAppId == null || flavorAppId.isEmpty) {
555+
throw Exception('Cannot find "app_id" for $flavor in shorebird.yaml');
556+
}
557+
appId = flavorAppId;
558+
}
559+
final StringBuffer yamlContent = StringBuffer();
560+
final String? baseUrl = yamlMap['base_url'] as String?;
561+
yamlContent.writeln('app_id: $appId');
562+
if (baseUrl != null) {
563+
yamlContent.writeln('base_url: $baseUrl');
564+
}
565+
shorebirdYaml.writeAsStringSync(yamlContent.toString(), flush: true);
566+
}
567+
513568
/// Extended attributes applied by Finder can cause code signing errors. Remove them.
514569
/// https://developer.apple.com/library/archive/qa/qa1940/_index.html
515570
Future<void> removeFinderExtendedAttributes(FileSystemEntity projectDirectory, ProcessUtils processUtils, Logger logger) async {

0 commit comments

Comments
 (0)