diff --git a/packages/flutter_tools/lib/src/ios/mac.dart b/packages/flutter_tools/lib/src/ios/mac.dart index 8ba2f160d9cd4..526e497f0b62c 100644 --- a/packages/flutter_tools/lib/src/ios/mac.dart +++ b/packages/flutter_tools/lib/src/ios/mac.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'dart:async'; +import 'dart:io'; import 'package:meta/meta.dart'; import 'package:process/process.dart'; @@ -589,6 +590,11 @@ Please file an issue at: https://github.com/shorebirdtech/shorebird/issues/new if (autoUpdate != null) { yamlContent.writeln('auto_update: $autoUpdate'); } + + final String? shorebirdPublicKeyEnvVar = Platform.environment['SHOREBIRD_PUBLIC_KEY']; + if (shorebirdPublicKeyEnvVar != null) { + yamlContent.writeln('patch_public_key: $shorebirdPublicKeyEnvVar'); + } shorebirdYaml.writeAsStringSync(yamlContent.toString(), flush: true); } diff --git a/packages/shorebird_tests/test/android_test.dart b/packages/shorebird_tests/test/android_test.dart index 92d1499cd9d99..41d2a651d23e4 100644 --- a/packages/shorebird_tests/test/android_test.dart +++ b/packages/shorebird_tests/test/android_test.dart @@ -9,6 +9,7 @@ void main() { expect(projectDirectory.apkFile().existsSync(), isTrue); expect(projectDirectory.shorebirdFile.existsSync(), isTrue); + expect(projectDirectory.getGeneratedAndroidShorebirdYaml(), completes); }); group('when passing the public key through the environment variable', () { @@ -25,7 +26,7 @@ void main() { ); final generatedYaml = - await projectDirectory.getGeneratedShorebirdYaml(); + await projectDirectory.getGeneratedAndroidShorebirdYaml(); expect( generatedYaml.keys, @@ -50,7 +51,7 @@ void main() { await projectDirectory.runFlutterBuildApk(flavor: 'internal'); final generatedYaml = - await projectDirectory.getGeneratedShorebirdYaml( + await projectDirectory.getGeneratedAndroidShorebirdYaml( flavor: 'internal', ); @@ -74,7 +75,7 @@ void main() { ); final generatedYaml = - await projectDirectory.getGeneratedShorebirdYaml( + await projectDirectory.getGeneratedAndroidShorebirdYaml( flavor: 'internal', ); diff --git a/packages/shorebird_tests/test/ios_test.dart b/packages/shorebird_tests/test/ios_test.dart new file mode 100644 index 0000000000000..519623c87ede2 --- /dev/null +++ b/packages/shorebird_tests/test/ios_test.dart @@ -0,0 +1,50 @@ +import 'package:test/test.dart'; + +import 'shorebird_tests.dart'; + +void main() { + group( + 'shorebird ios projects', + () { + testWithShorebirdProject('can build', (projectDirectory) async { + await projectDirectory.runFlutterBuildIos(); + + expect(projectDirectory.iosArchiveFile().existsSync(), isTrue); + expect(projectDirectory.getGeneratedIoShorebirdYaml(), completes); + }); + + group('when passing the public key through the environment variable', () { + testWithShorebirdProject( + 'adds the public key on top of the original file', + (projectDirectory) async { + final originalYaml = projectDirectory.shorebirdYaml; + + const base64PublicKey = 'public_123'; + await projectDirectory.runFlutterBuildIos( + environment: { + 'SHOREBIRD_PUBLIC_KEY': base64PublicKey, + }, + ); + + final generatedYaml = + await projectDirectory.getGeneratedIoShorebirdYaml(); + + expect( + generatedYaml.keys, + containsAll(originalYaml.keys), + ); + + print(generatedYaml); + expect( + generatedYaml['patch_public_key'], + equals(base64PublicKey), + ); + }, + ); + }); + + // TODO(erickzanardo): Add tests for flavors. + }, + testOn: 'mac-os', + ); +} diff --git a/packages/shorebird_tests/test/shorebird_tests.dart b/packages/shorebird_tests/test/shorebird_tests.dart index dbe1b4d4c2dcc..fbc26661fa7cb 100644 --- a/packages/shorebird_tests/test/shorebird_tests.dart +++ b/packages/shorebird_tests/test/shorebird_tests.dart @@ -63,24 +63,24 @@ Future testWithShorebirdProject(String name, ), )..createSync(); - File( - path.join( - projectDirectory.path, - 'shorebird.yaml', - ), - ).writeAsStringSync(''' -app_id: 123 -'''); - try { await _createFlutterProject(projectDirectory); projectDirectory.pubspecFile.writeAsStringSync(''' ${projectDirectory.pubspecFile.readAsStringSync()} - assets: - shorebird.yaml '''); + + File( + path.join( + projectDirectory.path, + 'shorebird.yaml', + ), + ).writeAsStringSync(''' +app_id: "123" +'''); + await testFn(projectDirectory); } finally { projectDirectory.deleteSync(recursive: true); @@ -177,6 +177,22 @@ $flavors } } + Future runFlutterBuildIos({ + Map? environment, + }) async { + final result = await _runFlutterCommand( + // The projects used to test are generated on spot, to make it simpler we don't + // configure any apple accounts on it, so we skip code signing here. + ['build', 'ipa', '--no-codesign'], + workingDirectory: this, + environment: environment, + ); + + if (result.exitCode != 0) { + throw Exception('Failed to run `flutter build ios`: ${result.stderr}'); + } + } + File apkFile({String? flavor}) => File( path.join( this.path, @@ -188,7 +204,17 @@ $flavors ), ); - Future getGeneratedShorebirdYaml({String? flavor}) async { + Directory iosArchiveFile() => Directory( + path.join( + this.path, + 'build', + 'ios', + 'archive', + 'Runner.xcarchive', + ), + ); + + Future getGeneratedAndroidShorebirdYaml({String? flavor}) async { final decodedBytes = ZipDecoder().decodeBytes(apkFile(flavor: flavor).readAsBytesSync()); @@ -206,4 +232,20 @@ $flavors ).readAsStringSync(); return loadYaml(yamlString) as YamlMap; } + + Future getGeneratedIoShorebirdYaml() async { + final yamlString = File( + path.join( + iosArchiveFile().path, + 'Products', + 'Applications', + 'Runner.app', + 'Frameworks', + 'App.framework', + 'flutter_assets', + 'shorebird.yaml', + ), + ).readAsStringSync(); + return loadYaml(yamlString) as YamlMap; + } }