Skip to content

Commit 0aabeb4

Browse files
committed
fix: update macos shorebird.yaml before signing (#73)
* fix: update macos shorebird.yaml before signing * fix test * tests * Move shorebird.yaml processing to assets.dart to update the file on copy * Remove unnecessary update of shorebird.yaml in ios/mac.dart * Handle empty string flavor
1 parent 25bb75e commit 0aabeb4

File tree

8 files changed

+57
-84
lines changed

8 files changed

+57
-84
lines changed

packages/flutter_tools/gradle/src/main/groovy/flutter.groovy

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,43 +1387,6 @@ class FlutterPlugin implements Plugin<Project> {
13871387
return
13881388
}
13891389
Task copyFlutterAssetsTask = addFlutterDeps(variant)
1390-
copyFlutterAssetsTask.doLast {
1391-
// TODO(eseidel): This is currently duplicating logic
1392-
// inside shorebird_yaml.dart in the flutter tool. We should
1393-
// just call `flutter build shorebird-yaml` or something
1394-
// instead, but I don't know how to call `flutter build`
1395-
// from here yet.
1396-
def yaml = new Yaml()
1397-
def outputDir = copyFlutterAssetsTask.destinationDir
1398-
1399-
// Read the shorebird.yaml file into a map.
1400-
def shorebirdYamlFile = new File("${outputDir}/flutter_assets/shorebird.yaml")
1401-
def shorebirdYamlData = yaml.load(shorebirdYamlFile.text)
1402-
1403-
// Update the app_id to the correct flavor if one was provided.
1404-
if (variant.flavorName != null && !variant.flavorName.isEmpty()) {
1405-
def shorebirdFlavor = shorebirdYamlData.flavors[variant.flavorName]
1406-
if (shorebirdFlavor == null) {
1407-
throw new GradleException("Flavor '${variant.flavorName}' not found in shorebird.yaml")
1408-
}
1409-
shorebirdYamlData['app_id'] = shorebirdFlavor
1410-
}
1411-
1412-
// Remove any flavors. This is a no-op if the flavors key doesn't exist.
1413-
shorebirdYamlData.remove('flavors')
1414-
1415-
// Add a public key if one was provided via an env var.
1416-
// Ideally we'd have a way to pass the key as a parameter, but
1417-
// an env var was the easiest way to get this working.
1418-
def shorebirdPublicKeyEnvVar = System.getenv('SHOREBIRD_PUBLIC_KEY')
1419-
if (shorebirdPublicKeyEnvVar != null && !shorebirdPublicKeyEnvVar.isEmpty()) {
1420-
shorebirdYamlData['patch_public_key'] = shorebirdPublicKeyEnvVar
1421-
}
1422-
1423-
// Write the updated map back to the shorebird.yaml file.
1424-
def updatedYamlString = yaml.dumpAsMap(shorebirdYamlData)
1425-
shorebirdYamlFile.write(updatedYamlString)
1426-
}
14271390
def variantOutput = variant.outputs.first()
14281391
def processResources = variantOutput.hasProperty(propProcessResourcesProvider) ?
14291392
variantOutput.processResourcesProvider.get() : variantOutput.processResources

packages/flutter_tools/lib/src/build_system/targets/assets.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import '../../convert.dart';
1414
import '../../dart/package_map.dart';
1515
import '../../devfs.dart';
1616
import '../../flutter_manifest.dart';
17+
import '../../globals.dart' as globals show platform;
18+
import '../../shorebird/shorebird_yaml.dart';
1719
import '../build_system.dart';
1820
import '../depfile.dart';
1921
import '../exceptions.dart';
@@ -179,6 +181,19 @@ Future<Depfile> copyAssets(
179181
}
180182
if (doCopy) {
181183
await (content.file as File).copy(file.path);
184+
if (file.basename == 'shorebird.yaml') {
185+
try {
186+
updateShorebirdYaml(
187+
environment.defines[kFlavor],
188+
file.path,
189+
environment: globals.platform.environment,
190+
);
191+
} on Exception catch (error) {
192+
throw Exception(
193+
'Failed to generate shorebird configuration. Error: $error',
194+
);
195+
}
196+
}
182197
}
183198
} else {
184199
await file.writeAsBytes(await entry.value.content.contentsAsBytes());

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import '../migrations/xcode_thin_binary_build_phase_input_paths_migration.dart';
3232
import '../plugins.dart';
3333
import '../project.dart';
3434
import '../reporting/reporting.dart';
35-
import '../shorebird/shorebird_yaml.dart';
3635
import 'application_package.dart';
3736
import 'code_signing.dart';
3837
import 'migrations/host_app_info_plist_migration.dart';
@@ -549,13 +548,6 @@ Future<XcodeBuildResult> buildXcodeProject({
549548
}
550549
}
551550

552-
try {
553-
updateShorebirdYaml(buildInfo, app.shorebirdYamlPath, environment: globals.platform.environment);
554-
} on Exception catch (error) {
555-
globals.printError('[shorebird] failed to generate shorebird configuration.\n$error');
556-
return XcodeBuildResult(success: false);
557-
}
558-
559551
return XcodeBuildResult(
560552
success: true,
561553
output: outputDir,

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import '../migrations/xcode_project_object_version_migration.dart';
2222
import '../migrations/xcode_script_build_phase_migration.dart';
2323
import '../migrations/xcode_thin_binary_build_phase_input_paths_migration.dart';
2424
import '../project.dart';
25-
import '../shorebird/shorebird_yaml.dart';
2625
import 'application_package.dart';
2726
import 'cocoapod_utils.dart';
2827
import 'migrations/flutter_application_migration.dart';
@@ -222,32 +221,6 @@ Future<void> buildMacOS({
222221
}
223222
final String? applicationBundle = MacOSApp.fromMacOSProject(flutterProject.macos).applicationBundle(buildInfo);
224223
if (applicationBundle != null) {
225-
final String shorebirdYamlPath = globals.fs.path.join(
226-
applicationBundle,
227-
'Contents',
228-
'Frameworks',
229-
'App.framework',
230-
'Resources',
231-
'flutter_assets',
232-
'shorebird.yaml',
233-
);
234-
if (globals.fs.isFileSync(shorebirdYamlPath)) {
235-
try {
236-
updateShorebirdYaml(
237-
buildInfo,
238-
shorebirdYamlPath,
239-
environment: globals.platform.environment,
240-
);
241-
} on Exception catch (error) {
242-
globals.printError(
243-
'[shorebird] failed to generate shorebird configuration.\n$error',
244-
);
245-
throw Exception(
246-
'Failed to generate shorebird configuration. Error: $error',
247-
);
248-
}
249-
}
250-
251224
final Directory outputDirectory = globals.fs.directory(applicationBundle);
252225
// This output directory is the .app folder itself.
253226
final int? directorySize = globals.os.getDirectorySize(outputDirectory);

packages/flutter_tools/lib/src/shorebird/shorebird_yaml.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ import 'package:yaml/yaml.dart';
66
import 'package:yaml_edit/yaml_edit.dart';
77

88
import '../base/file_system.dart';
9-
import '../build_info.dart';
109
import '../globals.dart' as globals;
1110

12-
void updateShorebirdYaml(BuildInfo buildInfo, String shorebirdYamlPath, {required Map<String, String> environment}) {
11+
void updateShorebirdYaml(String? flavor, String shorebirdYamlPath, {required Map<String, String> environment}) {
1312
final File shorebirdYaml = globals.fs.file(shorebirdYamlPath);
1413
if (!shorebirdYaml.existsSync()) {
1514
throw Exception('shorebird.yaml not found at $shorebirdYamlPath');
1615
}
1716
final YamlDocument input = loadYamlDocument(shorebirdYaml.readAsStringSync());
1817
final YamlMap yamlMap = input.contents as YamlMap;
19-
final Map<String, dynamic> compiled = compileShorebirdYaml(yamlMap, flavor: buildInfo.flavor, environment: environment);
18+
final Map<String, dynamic> compiled = compileShorebirdYaml(yamlMap, flavor: flavor, environment: environment);
2019
// Currently we write out over the same yaml file, we should fix this to
2120
// write to a new .json file instead and avoid naming confusion between the
2221
// input and compiled files.
@@ -26,7 +25,7 @@ void updateShorebirdYaml(BuildInfo buildInfo, String shorebirdYamlPath, {require
2625
}
2726

2827
String appIdForFlavor(YamlMap yamlMap, {required String? flavor}) {
29-
if (flavor == null) {
28+
if (flavor == null || flavor.isEmpty) {
3029
final String? defaultAppId = yamlMap['app_id'] as String?;
3130
if (defaultAppId == null || defaultAppId.isEmpty) {
3231
throw Exception('Cannot find "app_id" in shorebird.yaml');

packages/flutter_tools/lib/src/windows/build_windows.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Future<void> buildWindows(
137137

138138
if (shorebirdYamlFile.existsSync()) {
139139
try {
140-
updateShorebirdYaml(buildInfo, shorebirdYamlFile.path, environment: globals.platform.environment);
140+
updateShorebirdYaml(buildInfo.flavor, shorebirdYamlFile.path, environment: globals.platform.environment);
141141
} on Exception catch (error) {
142142
globals.printError('[shorebird] failed to generate shorebird configuration.\n$error');
143143
throw Exception('Failed to generate shorebird configuration');

packages/flutter_tools/test/general.shard/build_system/targets/macos_test.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,43 @@ void main() {
564564
ProcessManager: () => processManager,
565565
});
566566

567+
testUsingContext('ReleaseMacOSBundleFlutterAssets updates shorebird.yaml if present', () async {
568+
environment.defines[kBuildMode] = 'release';
569+
environment.defines[kXcodeAction] = 'install';
570+
environment.defines[kFlavor] = 'internal';
571+
572+
fileSystem.file('bin/cache/artifacts/engine/darwin-x64/vm_isolate_snapshot.bin')
573+
.createSync(recursive: true);
574+
fileSystem.file('bin/cache/artifacts/engine/darwin-x64/isolate_snapshot.bin')
575+
.createSync(recursive: true);
576+
fileSystem.file(fileSystem.path.join(environment.buildDir.path, 'App.framework', 'App'))
577+
.createSync(recursive: true);
578+
final String shorebirdYamlPath = fileSystem.path.join(
579+
environment.buildDir.path,
580+
'App.framework','Versions',
581+
'A',
582+
'Resources',
583+
'flutter_assets',
584+
'shorebird.yaml',
585+
);
586+
fileSystem.file(fileSystem.path.join(environment.buildDir.path, 'App.framework', 'App'))
587+
..createSync(recursive: true)
588+
..writeAsStringSync('''
589+
# Some other text that should be removed
590+
app_id: base-app-id
591+
flavors:
592+
internal: internal-app-id
593+
stable: stable-app-id
594+
''');
595+
596+
await const ReleaseMacOSBundleFlutterAssets().build(environment);
597+
598+
expect(fileSystem.file(shorebirdYamlPath).readAsStringSync(), 'app_id: internal-app-id');
599+
}, overrides: <Type, Generator>{
600+
FileSystem: () => fileSystem,
601+
ProcessManager: () => processManager,
602+
});
603+
567604
testUsingContext('DebugMacOSFramework creates expected binary with arm64 only arch', () async {
568605
environment.defines[kDarwinArchs] = 'arm64';
569606
processManager.addCommand(

packages/flutter_tools/test/general.shard/shorebird/shorebird_yaml_test.dart

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import 'dart:io';
66

7-
import 'package:flutter_tools/src/build_info.dart';
87
import 'package:flutter_tools/src/shorebird/shorebird_yaml.dart';
98
import 'package:test/test.dart';
109
import 'package:yaml/yaml.dart';
@@ -86,12 +85,7 @@ base_url: https://example.com
8685
final File tempFile = File('${tempDir.path}/shorebird.yaml');
8786
tempFile.writeAsStringSync(yamlContents);
8887
updateShorebirdYaml(
89-
const BuildInfo(
90-
BuildMode.release,
91-
'foo',
92-
treeShakeIcons: false,
93-
packageConfigPath: '',
94-
),
88+
'foo',
9589
tempFile.path,
9690
environment: <String, String>{'SHOREBIRD_PUBLIC_KEY': '4-a'},
9791
);

0 commit comments

Comments
 (0)