Skip to content

Commit

Permalink
Refactor build.dart into separate project.
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Mar 14, 2024
1 parent 57e8c24 commit 520b4ad
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 25 deletions.
5 changes: 3 additions & 2 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ scripts:
dart pub global activate dependency_validator &&
dart pub global activate ffigen &&
dart pub global activate melos &&
dart pub global activate pana
dart pub global activate pana &&
dart pub global activate --source path $MELOS_ROOT_PATH/packages/realm_dart/dev/
build:
run: >-
Expand All @@ -40,7 +41,7 @@ scripts:
melos run build:dart
build:native:
exec: dart run tool/build.dart native
exec: dev build
packageFilters:
dirExists: src # by convention

Expand Down
3 changes: 3 additions & 0 deletions packages/realm_dart/dev/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version.
9 changes: 9 additions & 0 deletions packages/realm_dart/dev/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# dev
A cli helper tool for realm_dart development

## Install
Either run `melos setup` or change to the directory of this README, then:
```shell
dart pub global activate --source path .
```
As for all dart tools ensure that `~/.pub_cache/bin` is in your path
1 change: 1 addition & 0 deletions packages/realm_dart/dev/analysis_options.yaml
6 changes: 6 additions & 0 deletions packages/realm_dart/dev/bin/dev.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

import 'package:dev/dev.dart' as dev;

void main(List<String> arguments) => dev.main(arguments);
22 changes: 22 additions & 0 deletions packages/realm_dart/dev/lib/dev.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

import 'dart:io' as io;

import 'package:args/command_runner.dart';

import 'src/build.dart';

Future<void> main(List<String> arguments) async {
final runner = CommandRunner<int>('dev', 'Helper tool for building realm_dart')
..addCommand(BuildNativeCommand())
..addCommand(PossibleTargets())
..argParser.addFlag('verbose', abbr: 'v', help: 'Print verbose output', defaultsTo: false);
try {
final exitCode = await runner.run(arguments);
io.exit(exitCode ?? 0);
} on UsageException catch (error) {
logger.err('$error');
io.exit(64); // Exit code 64 indicates a usage error.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:collection/collection.dart';
import 'package:mason_logger/mason_logger.dart';
import 'package:async/async.dart';

extension<T extends Enum> on Iterable<T> {
extension IterableX<T extends Enum> on Iterable<T> {
T? firstEqualIgnoreCase(String? value) => value == null ? null : where((e) => equalsIgnoreAsciiCase(e.name, value)).firstOrNull;
Iterable<String> get names => map((e) => e.name);
}
Expand Down Expand Up @@ -109,13 +109,13 @@ enum BuildMode {
static BuildMode? from(String? name) => BuildMode.values.firstEqualIgnoreCase(name);
}

abstract class _BaseCommand extends Command<int> {
abstract class BaseCommand extends Command<int> {
@override
final String name;
@override
final String description;

_BaseCommand(this.name, this.description);
BaseCommand(this.name, this.description);

late final verbose = globalResults!['verbose'] as bool; // don't access before run

Expand All @@ -124,10 +124,10 @@ abstract class _BaseCommand extends Command<int> {
Iterable<Target> get possibleTargets => Target.values.where((t) => t.os == os || t.os == OS.android || (os == OS.macOS && t.os == OS.iOS));
}

class _BuildNativeCommand extends _BaseCommand {
_BuildNativeCommand()
class BuildNativeCommand extends BaseCommand {
BuildNativeCommand()
: super(
'native',
'build',
'Build native assets for realm_dart',
) {
argParser
Expand Down Expand Up @@ -179,6 +179,7 @@ class _BuildNativeCommand extends _BaseCommand {
}
}
final exitCode = await p.exitCode;
assert(verbose ^ (progress != null)); // verbose <=> progress == null
if (exitCode != 0) {
progress?.fail(message);
logger.err('Error: "$command" exited with code $exitCode');
Expand Down Expand Up @@ -255,8 +256,8 @@ class _BuildNativeCommand extends _BaseCommand {
}
}

class _PossibleTargets extends _BaseCommand {
_PossibleTargets()
class PossibleTargets extends BaseCommand {
PossibleTargets()
: super(
'targets',
'List possible targets for building native assets',
Expand All @@ -281,17 +282,3 @@ class _PossibleTargets extends _BaseCommand {
}

final logger = Logger(progressOptions: ProgressOptions(trailing: ''));

Future<void> main(List<String> arguments) async {
final runner = CommandRunner<int>('build', 'Helper tool for building realm_dart')
..addCommand(_BuildNativeCommand())
..addCommand(_PossibleTargets())
..argParser.addFlag('verbose', abbr: 'v', help: 'Print verbose output', defaultsTo: false);
try {
final exitCode = await runner.run(arguments);
io.exit(exitCode ?? 0);
} on UsageException catch (error) {
logger.err('$error');
io.exit(64); // Exit code 64 indicates a usage error.
}
}
21 changes: 21 additions & 0 deletions packages/realm_dart/dev/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: dev
description: A sample command-line application.
version: 1.0.0

publish_to: none

environment:
sdk: ^3.3.1

dependencies:
args: ^2.4.2
async: ^2.11.0
collection: ^1.18.0
mason_logger: ^0.2.12

dev_dependencies:
lints: ^3.0.0
test: ^1.24.0

executables:
dev:
1 change: 0 additions & 1 deletion packages/realm_dart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@ dev_dependencies:
ffigen: ^11.0.0
json_serializable: ^6.3.1
lints: ^3.0.0
mason_logger: ^0.2.12
test: ^1.14.3
timezone: ^0.9.0

0 comments on commit 520b4ad

Please sign in to comment.