Skip to content

Commit

Permalink
Use lib in package directly during flutter test
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Apr 11, 2024
1 parent d3a95a9 commit 11c6893
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 117 deletions.
15 changes: 5 additions & 10 deletions packages/realm_dart/lib/src/cli/common/target_os_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,19 @@ enum TargetOsType {
ios,
linux,
macos,
windows,
windows;

bool get isDesktop => [TargetOsType.linux, TargetOsType.macos, TargetOsType.windows].contains(this);
}

// Cannot use Dart 2.17 enhanced enums, due to an issue with build_cli :-/
enum Flavor {
flutter,
dart,
dart;
}

extension FlavorEx on Flavor {
String get packageName {
switch (this) {
case Flavor.dart:
return 'realm_dart';
case Flavor.flutter:
return 'realm';
}
}
String get packageName => switch (this) { Flavor.dart => 'realm_dart', Flavor.flutter => 'realm' };
}

extension StringEx on String {
Expand Down
72 changes: 34 additions & 38 deletions packages/realm_dart/lib/src/cli/install/install_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,34 @@ class InstallCommand extends Command<void> {

late Options options;

bool get debug => options.debug;
late Pubspec pubspec;
late final debug = options.debug;
late final flavor = options.flavor!;
late final force = options.force; // not used!?!
late final targetOsType = options.targetOsType!;

InstallCommand() {
populateOptionsParser(argParser);
pubspec = parsePubspec(File('pubspec.yaml'));
final defaultFlavor = pubspec.dependencies['flutter'] == null ? Flavor.dart : Flavor.flutter; // default depends on project type
populateOptionsParser(
argParser,
targetOsTypeDefaultOverride: Platform.operatingSystem.asTargetOsType,
flavorDefaultOverride: defaultFlavor,
);
}

Directory getBinaryPath(Directory realmPackagePath, {required bool isFlutter}) {
if (isFlutter) {
final root = realmPackagePath.path;
return Directory(switch (options.targetOsType) {
TargetOsType.android => path.join(root, 'android', 'src', 'main', 'cpp', 'lib'),
TargetOsType.ios => path.join(root, 'ios'),
TargetOsType.macos => path.join(root, 'macos'),
TargetOsType.linux => path.join(root, 'linux', 'binary', 'linux'),
TargetOsType.windows => path.join(root, 'windows', 'binary', 'windows'),
_ => throw Exception('Unsupported target OS type for Flutter: ${options.targetOsType}')
});
}
// TODO: Should binaries not go into package also for Dart?
return Directory(path.join(Directory.current.absolute.path, 'binary', options.targetOsType!.name));
Directory getBinaryPath(Directory realmPackagePath, Flavor flavor) {
final root = realmPackagePath.path;
return switch (flavor) {
Flavor.dart => Directory(path.join(root, 'binary', targetOsType.name)),
Flavor.flutter => Directory(switch (targetOsType) {
TargetOsType.android => path.join(root, 'android', 'src', 'main', 'cpp', 'lib'),
TargetOsType.ios => path.join(root, 'ios'),
TargetOsType.macos => path.join(root, 'macos'),
TargetOsType.linux => path.join(root, 'linux', 'binary', 'linux'),
TargetOsType.windows => path.join(root, 'windows', 'binary', 'windows'),
}),
};
}

Future<bool> shouldSkipDownload(String binariesPath, String expectedVersion) async {
Expand Down Expand Up @@ -105,31 +113,29 @@ class InstallCommand extends Command<void> {
if (packageConfig == null) {
abort('Run `dart pub get`');
}
final package = packageConfig.packages.where((p) => p.name == name).singleOrNull;
final package = packageConfig[name];
if (package == null) {
abort('$name package not found in dependencies. Add $name package to your pubspec.yaml');
}
return Directory.fromUri(package.root);
}

Future<Pubspec> parsePubspec(File file) async {
Pubspec parsePubspec(File file) {
try {
return Pubspec.parse(await file.readAsString(), sourceUrl: file.uri);
} on Exception catch (e) {
throw Exception('Error parsing package pubspec at ${file.parent}. Error $e');
return Pubspec.parse(file.readAsStringSync(), sourceUrl: file.uri);
} catch (e) {
abort('Error parsing package pubspec at ${file.parent}. Error $e');
}
}

@override
FutureOr<void> run() async {
final pubspec = await parsePubspec(File('pubspec.yaml'));
final flavor = pubspec.dependencies['flutter'] == null ? Flavor.dart : Flavor.flutter;

options = parseOptionsResult(argResults!);
validateOptions(flavor);

final flavorName = flavor.packageName;
final realmDependency = pubspec.dependencyOverrides[flavorName] ?? pubspec.dependencies[flavorName];
print(pubspec.dependencyOverrides.values.join('\n'));
print(realmDependency);
if (realmDependency is PathDependency) {
print('Path dependency for $flavorName found. Skipping install of native lib (assuming local development)');
return;
Expand All @@ -140,26 +146,16 @@ class InstallCommand extends Command<void> {
}

final realmPackagePath = await getPackagePath(flavorName);
final realmPubspec = await parsePubspec(File(path.join(realmPackagePath.path, "pubspec.yaml")));
final realmPubspec = parsePubspec(File(path.join(realmPackagePath.path, "pubspec.yaml")));

final binaryPath = getBinaryPath(realmPackagePath, isFlutter: flavor == Flavor.flutter);
final binaryPath = getBinaryPath(realmPackagePath, flavor);
print(binaryPath);
final archiveName = '${options.targetOsType!.name}.tar.gz';
final archiveName = '${targetOsType.name}.tar.gz';
await downloadAndExtractBinaries(binaryPath, realmPubspec.version!, archiveName);

print('Realm install command finished.');
}

void validateOptions(Flavor flavor) {
final targetOs = flavor == Flavor.dart ? getTargetOS() : options.targetOsType;
if (targetOs == null) {
abort('Target OS not specified');
}
options.targetOsType = targetOs;
}

TargetOsType getTargetOS() => Platform.operatingSystem.asTargetOsType ?? (throw UnsupportedError('Unsupported platform ${Platform.operatingSystem}'));

Never abort(String error) {
print(error);
print(usage);
Expand Down
10 changes: 8 additions & 2 deletions packages/realm_dart/lib/src/cli/install/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ part 'options.g.dart';

@CliOptions()
class Options {
@CliOption(help: 'The target OS to install binaries for.', abbr: 't')
@CliOption(help: 'The flavor to install binaries for.', abbr: 'f', provideDefaultToOverride: true)
Flavor? flavor;

@CliOption(help: 'The target OS to install binaries for.', abbr: 't', provideDefaultToOverride: true)
TargetOsType? targetOsType;

// use to debug install command
Expand All @@ -23,6 +26,9 @@ class Options {

String get usage => _$parserForOptions.usage;

ArgParser populateOptionsParser(ArgParser p) => _$populateOptionsParser(p);
ArgParser populateOptionsParser(ArgParser parser, {
TargetOsType? targetOsTypeDefaultOverride,
Flavor? flavorDefaultOverride,
}) => _$populateOptionsParser(parser, targetOsTypeDefaultOverride: targetOsTypeDefaultOverride, flavorDefaultOverride: flavorDefaultOverride);

Options parseOptionsResult(ArgResults results) => _$parseOptionsResult(results);
57 changes: 39 additions & 18 deletions packages/realm_dart/lib/src/cli/install/options.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 11c6893

Please sign in to comment.