From a1902364099c7f7cd442cff13df119a9565b49d1 Mon Sep 17 00:00:00 2001 From: Bryan Oltman Date: Fri, 17 Jan 2025 13:36:54 -0500 Subject: [PATCH] feat: support flavors on macOS (#2780) --- .../lib/src/artifact_manager.dart | 4 +- .../lib/src/commands/patch/macos_patcher.dart | 4 +- .../src/commands/release/macos_releaser.dart | 6 +- .../test/src/artifact_manager_test.dart | 27 ++++++++ .../commands/patch/macos_patcher_test.dart | 65 +++++++++++++++++++ .../commands/release/macos_releaser_test.dart | 31 +++++++++ 6 files changed, 130 insertions(+), 7 deletions(-) diff --git a/packages/shorebird_cli/lib/src/artifact_manager.dart b/packages/shorebird_cli/lib/src/artifact_manager.dart index 5b2e8a01..0672106e 100644 --- a/packages/shorebird_cli/lib/src/artifact_manager.dart +++ b/packages/shorebird_cli/lib/src/artifact_manager.dart @@ -265,7 +265,7 @@ class ArtifactManager { } /// The directory containing the compiled macOS .app file, if it exists. - Directory? getMacOSAppDirectory() { + Directory? getMacOSAppDirectory({String? flavor}) { final projectRoot = shorebirdEnv.getShorebirdProjectRoot()!; final appDirectory = Directory( @@ -275,7 +275,7 @@ class ArtifactManager { 'macos', 'Build', 'Products', - 'Release', + flavor != null ? 'Release-$flavor' : 'Release', ), ); diff --git a/packages/shorebird_cli/lib/src/commands/patch/macos_patcher.dart b/packages/shorebird_cli/lib/src/commands/patch/macos_patcher.dart index 98b80661..018c03f0 100644 --- a/packages/shorebird_cli/lib/src/commands/patch/macos_patcher.dart +++ b/packages/shorebird_cli/lib/src/commands/patch/macos_patcher.dart @@ -232,7 +232,7 @@ For more information see: ${supportedFlutterVersionsUrl.toLink()}''', throw ProcessExit(ExitCode.software.code); } - final appPath = artifactManager.getMacOSAppDirectory()!.path; + final appPath = artifactManager.getMacOSAppDirectory(flavor: flavor)!.path; final tempDir = await Directory.systemTemp.createTemp(); final zippedApp = File(p.join(tempDir.path, '${p.basename(appPath)}.zip')); await ditto.archive( @@ -258,7 +258,7 @@ For more information see: ${supportedFlutterVersionsUrl.toLink()}''', } // Verify that we have built a patch .app - if (artifactManager.getMacOSAppDirectory()?.path == null) { + if (artifactManager.getMacOSAppDirectory(flavor: flavor)?.path == null) { logger.err('Unable to find .app directory'); throw ProcessExit(ExitCode.software.code); } diff --git a/packages/shorebird_cli/lib/src/commands/release/macos_releaser.dart b/packages/shorebird_cli/lib/src/commands/release/macos_releaser.dart index 1bff39f3..0230e79f 100644 --- a/packages/shorebird_cli/lib/src/commands/release/macos_releaser.dart +++ b/packages/shorebird_cli/lib/src/commands/release/macos_releaser.dart @@ -125,7 +125,7 @@ For more information see: ${supportedFlutterVersionsUrl.toLink()}''', throw ProcessExit(ExitCode.software.code); } - final appDirectory = artifactManager.getMacOSAppDirectory(); + final appDirectory = artifactManager.getMacOSAppDirectory(flavor: flavor); if (appDirectory == null) { logger.err('Unable to find .app directory'); throw ProcessExit(ExitCode.software.code); @@ -161,7 +161,7 @@ For more information see: ${supportedFlutterVersionsUrl.toLink()}''', required Release release, required String appId, }) async { - final appDirectory = artifactManager.getMacOSAppDirectory(); + final appDirectory = artifactManager.getMacOSAppDirectory(flavor: flavor); if (appDirectory == null) { logger.err('Unable to find .app directory'); throw ProcessExit(ExitCode.software.code); @@ -204,6 +204,6 @@ For more information see: ${supportedFlutterVersionsUrl.toLink()}''', @override String get postReleaseInstructions => ''' -macOS app created at ${artifactManager.getMacOSAppDirectory()!.path}. +macOS app created at ${artifactManager.getMacOSAppDirectory(flavor: flavor)!.path}. '''; } diff --git a/packages/shorebird_cli/test/src/artifact_manager_test.dart b/packages/shorebird_cli/test/src/artifact_manager_test.dart index ef0b4ccd..99e6e407 100644 --- a/packages/shorebird_cli/test/src/artifact_manager_test.dart +++ b/packages/shorebird_cli/test/src/artifact_manager_test.dart @@ -763,6 +763,33 @@ void main() { ); }); }); + + group('when a flavor is provided', () { + const flavor = 'my-flavor'; + late Directory appDirectory; + + setUp(() { + appDirectory = Directory( + p.join( + projectRoot.path, + 'build', + 'macos', + 'Build', + 'Products', + 'Release-$flavor', + 'my.app', + ), + )..createSync(recursive: true); + }); + + test('includes flavor in lookup path', () async { + final result = runWithOverrides( + () => artifactManager.getMacOSAppDirectory(flavor: flavor), + ); + + expect(result!.path, equals(appDirectory.path)); + }); + }); }); group('getIosAppDirectory', () { diff --git a/packages/shorebird_cli/test/src/commands/patch/macos_patcher_test.dart b/packages/shorebird_cli/test/src/commands/patch/macos_patcher_test.dart index ac6bf0aa..52d28891 100644 --- a/packages/shorebird_cli/test/src/commands/patch/macos_patcher_test.dart +++ b/packages/shorebird_cli/test/src/commands/patch/macos_patcher_test.dart @@ -698,6 +698,39 @@ For more information see: ${supportedFlutterVersionsUrl.toLink()}''', ); }); + group('when flavor is provided', () { + const flavor = 'my-flavor'; + + setUp(() { + patcher = MacosPatcher( + argParser: argParser, + argResults: argResults, + flavor: flavor, + target: null, + ); + + when( + () => artifactManager.getMacOSAppDirectory(flavor: flavor), + ).thenReturn(appDirectory); + }); + + test('builds with flavor', () async { + await runWithOverrides(patcher.buildPatchArtifact); + verify( + () => artifactBuilder.buildMacos( + codesign: any(named: 'codesign'), + args: any(named: 'args'), + flavor: flavor, + target: any(named: 'target'), + buildProgress: any(named: 'buildProgress'), + ), + ).called(1); + verify( + () => artifactManager.getMacOSAppDirectory(flavor: flavor), + ).called(1); + }); + }); + group('when --split-debug-info is provided', () { final tempDir = Directory.systemTemp.createTempSync(); final splitDebugInfoPath = p.join(tempDir.path, 'symbols'); @@ -1295,6 +1328,38 @@ For more information see: ${supportedFlutterVersionsUrl.toLink()}''', ); }); + group('when flavor is provided', () { + const flavor = 'my-flavor'; + + setUp(() { + patcher = MacosPatcher( + argParser: argParser, + argResults: argResults, + flavor: flavor, + target: null, + ); + + when( + () => artifactManager.getMacOSAppDirectory(flavor: flavor), + ).thenReturn(appDirectory); + }); + + test('finds app directory corresponding to flavor', () async { + await runWithOverrides( + () => patcher.createPatchArtifacts( + appId: appId, + releaseId: releaseId, + releaseArtifact: releaseArtifactFile, + supplementArtifact: supplementArtifactFile, + ), + ); + + verify( + () => artifactManager.getMacOSAppDirectory(flavor: flavor), + ).called(1); + }); + }); + group('when class table link info is not present', () { setUp(() { when( diff --git a/packages/shorebird_cli/test/src/commands/release/macos_releaser_test.dart b/packages/shorebird_cli/test/src/commands/release/macos_releaser_test.dart index ea2a4e14..47fb45b8 100644 --- a/packages/shorebird_cli/test/src/commands/release/macos_releaser_test.dart +++ b/packages/shorebird_cli/test/src/commands/release/macos_releaser_test.dart @@ -295,6 +295,37 @@ To change the version of this release, change your app's version in your pubspec ).thenAnswer((_) async => flutterVersionAndRevision); }); + group('when flavor is provided', () { + const flavor = 'myFlavor'; + + setUp(() { + releaser = MacosReleaser( + argResults: argResults, + flavor: flavor, + target: null, + ); + + when( + () => artifactManager.getMacOSAppDirectory(flavor: flavor), + ).thenReturn(appDirectory); + }); + + test('forwards flavor to artifact builder', () async { + await runWithOverrides(releaser.buildReleaseArtifacts); + + verify( + () => artifactBuilder.buildMacos( + flavor: flavor, + args: any(named: 'args'), + buildProgress: any(named: 'buildProgress'), + ), + ).called(1); + verify( + () => artifactManager.getMacOSAppDirectory(flavor: flavor), + ).called(1); + }); + }); + group('when not codesigning', () { setUp(() { when(() => argResults['codesign']).thenReturn(false);