Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shorebird_cli): recognize flavors in Flutter modules #569

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/shorebird_cli/lib/src/shorebird_flavor_mixin.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:collection/collection.dart';
import 'package:path/path.dart' as p;
import 'package:platform/platform.dart';
Expand All @@ -12,13 +14,24 @@ mixin ShorebirdFlavorMixin on ShorebirdJavaMixin {
String appRoot, {
Platform platform = const LocalPlatform(),
}) async {
// Flutter apps have android files in root/android
// Flutter modules have android files in root/.android
final androidRoot = [
Directory(p.join(appRoot, 'android')),
Directory(p.join(appRoot, '.android')),
].firstWhereOrNull((dir) => dir.existsSync());

if (androidRoot == null) {
return {};
}

final executable = platform.isWindows ? 'gradlew.bat' : 'gradlew';
final javaHome = getJavaHome(platform);
final result = await process.run(
p.join(appRoot, 'android', executable),
p.join(androidRoot.path, executable),
['app:tasks', '--all', '--console=auto'],
runInShell: true,
workingDirectory: p.join(appRoot, 'android'),
workingDirectory: androidRoot.path,
environment: {
if (javaHome != null) 'JAVA_HOME': javaHome,
},
Expand Down
48 changes: 42 additions & 6 deletions packages/shorebird_cli/test/src/commands/init_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ environment:
late Progress progress;
late InitCommand command;

Directory setUpAppTempDir() {
final tempDir = Directory.systemTemp.createTempSync();
Directory(p.join(tempDir.path, 'android')).createSync(recursive: true);
return tempDir;
}

Directory setUpModuleTempDir() {
final tempDir = Directory.systemTemp.createTempSync();
Directory(p.join(tempDir.path, '.android')).createSync(recursive: true);
return tempDir;
}

setUp(() {
httpClient = _MockHttpClient();
argResults = _MockArgResults();
Expand Down Expand Up @@ -108,7 +120,7 @@ environment:
when(() => platform.isLinux).thenReturn(true);
when(() => platform.isMacOS).thenReturn(false);
when(() => platform.isWindows).thenReturn(false);
final tempDir = Directory.systemTemp.createTempSync();
final tempDir = setUpAppTempDir();
const javaHome = 'test_java_home';
when(() => platform.environment).thenReturn({'JAVA_HOME': javaHome});
await expectLater(
Expand All @@ -125,12 +137,13 @@ environment:
),
).called(1);
});

test('uses correct executable on windows', () async {
final platform = _MockPlatform();
when(() => platform.isWindows).thenReturn(true);
when(() => platform.isMacOS).thenReturn(false);
when(() => platform.isLinux).thenReturn(false);
final tempDir = Directory.systemTemp.createTempSync();
final tempDir = setUpAppTempDir();
when(() => platform.environment).thenReturn({
'PROGRAMFILES': tempDir.path,
'PROGRAMFILES(X86)': tempDir.path,
Expand Down Expand Up @@ -163,7 +176,7 @@ environment:
when(() => platform.isWindows).thenReturn(false);
when(() => platform.isMacOS).thenReturn(true);
when(() => platform.isLinux).thenReturn(false);
final tempDir = Directory.systemTemp.createTempSync();
final tempDir = setUpAppTempDir();
when(() => platform.environment).thenReturn({'HOME': tempDir.path});
final androidStudioDir = Directory(
p.join(
Expand Down Expand Up @@ -199,7 +212,7 @@ environment:
when(() => platform.isWindows).thenReturn(false);
when(() => platform.isMacOS).thenReturn(false);
when(() => platform.isLinux).thenReturn(true);
final tempDir = Directory.systemTemp.createTempSync();
final tempDir = setUpAppTempDir();
when(() => platform.environment).thenReturn({'HOME': tempDir.path});
final androidStudioDir = Directory(
p.join(tempDir.path, '.AndroidStudio'),
Expand All @@ -223,6 +236,29 @@ environment:
),
).called(1);
});

test('extracts flavors from module directory structure', () async {
final platform = _MockPlatform();
when(() => platform.isLinux).thenReturn(true);
when(() => platform.isMacOS).thenReturn(false);
when(() => platform.isWindows).thenReturn(false);
final tempDir = setUpModuleTempDir();
const javaHome = 'test_java_home';
when(() => platform.environment).thenReturn({'JAVA_HOME': javaHome});
await expectLater(
command.extractProductFlavors(tempDir.path, platform: platform),
completes,
);
verify(
() => process.run(
p.join(tempDir.path, '.android', 'gradlew'),
['app:tasks', '--all', '--console=auto'],
runInShell: true,
workingDirectory: p.join(tempDir.path, '.android'),
environment: {'JAVA_HOME': javaHome},
),
).called(1);
});
});

test('returns no user error when not logged in', () async {
Expand Down Expand Up @@ -314,7 +350,7 @@ If you want to reinitialize Shorebird, please run "shorebird init --force".''',
when(() => result.exitCode).thenReturn(1);
when(() => result.stdout).thenReturn('error');
when(() => result.stderr).thenReturn('oops');
final tempDir = Directory.systemTemp.createTempSync();
final tempDir = setUpAppTempDir();
File(
p.join(tempDir.path, 'pubspec.yaml'),
).writeAsStringSync(pubspecYamlContent);
Expand Down Expand Up @@ -389,7 +425,7 @@ If you want to reinitialize Shorebird, please run "shorebird init --force".''',
p.join('test', 'fixtures', 'gradle_app_tasks.txt'),
).readAsStringSync(),
);
final tempDir = Directory.systemTemp.createTempSync();
final tempDir = setUpAppTempDir();
File(
p.join(tempDir.path, 'pubspec.yaml'),
).writeAsStringSync(pubspecYamlContent);
Expand Down