Skip to content

Commit

Permalink
Use PartBuilder instead of SharedPartBuilder (#1310)
Browse files Browse the repository at this point in the history
* Use PartBuilder instead of SharedPartBuilder

This will allow other builders to consume output of the realm generator.
Generated files are now suffixed with `.realm.dart` instead of `.g.dart`.

* Fix generator tests

* Rude fix

* Force unicode glyphs in error messages

Instead of reading it from the console. Github windows actions are weird
in this respect. The logs will alway be rendered with unicode anyway.

* Fix ordering of async info tests

* Fix multi-file generate

* Update CHANGELOG

* Update workflows

* Fix generator tests after rebase

* Remember delete flag

* Explain mocks

* Elaborate CHANGELOG

* Rip out dotnet, etc. to make room for emulator run

* Bring us into the not so distant past - using api level 31, and NDK 25.2.9519653
  • Loading branch information
nielsenko authored Feb 19, 2024
1 parent 46e44af commit ca669a2
Show file tree
Hide file tree
Showing 163 changed files with 655 additions and 389 deletions.
28 changes: 17 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ jobs:
- name: Install dependencies
run: dart pub get

# Hack to free up space on the runner to ensure we have enough diskspace to run the emulator
- name: Remove unnecessary files (dotnet, etc.)
run: |
sudo rm -rf /usr/share/dotnet
# TODO: Move CI run tests on Android Emulator into device farm https://github.com/realm/realm-dart/issues/691
- name: Setup Android Emulator cache
uses: actions/cache@v4
Expand All @@ -414,7 +419,7 @@ jobs:
path: |
~/.android/avd/*
~/.android/adb*
key: avd-29_x64
key: avd-33-x86_64

- name: Create Android Emulator and generate snapshot for caching
if: ${{ steps.avd-cache.outputs.cache-hit != 'true' }}
Expand All @@ -423,9 +428,9 @@ jobs:
force-avd-creation: false
disable-animations: true
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
api-level: 29
api-level: 31
arch: x86_64
ndk: 21.0.6113669
ndk: 25.2.9519653
cmake: 3.10.2.4988404
script: echo "Generated Emulator snapshot for caching."

Expand All @@ -435,9 +440,9 @@ jobs:
force-avd-creation: false
disable-animations: true
emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
api-level: 29
api-level: 31
arch: x86_64
ndk: 21.0.6113669
ndk: 25.2.9519653
cmake: 3.10.2.4988404
script: cd packages/realm/tests && flutter test integration_test/all_tests.dart --dart-define=BAAS_BAASAAS_API_KEY=$BAAS_BAASAAS_API_KEY --dart-define=BAAS_DIFFERENTIATOR=$BAAS_DIFFERENTIATOR --file-reporter=json:test-results.json --suppress-analytics

Expand Down Expand Up @@ -469,6 +474,10 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu, macos, windows]
defaults:
run:
shell: bash
working-directory: packages/realm_generator

runs-on: ${{ matrix.os }}-latest
name: Generator Tests
Expand All @@ -484,9 +493,9 @@ jobs:
with:
channel: 'stable'

- name: Delete generated files
run: find . -name "*.g.dart" -delete
shell: bash
- name: Delete generated files in realm_dart
run: find . -name "*.realm.dart" -delete
working-directory: packages/realm_dart

- name: Run generator in realm_dart
run: |
Expand Down Expand Up @@ -523,13 +532,11 @@ jobs:
git --no-pager diff -w
exit 1
fi
shell: bash
- name: Run generator tests
run: |
dart pub get
dart test -r expanded --coverage ./coverage/ --test-randomize-ordering-seed random
working-directory: packages/realm_generator
- name: Generate generator coverage report
if: matrix.os == 'ubuntu'
Expand All @@ -541,7 +548,6 @@ jobs:
--lcov \
--packages .dart_tool/package_config.json \
--report-on lib
working-directory: packages/realm_generator
- name: Publish Generator Coverage
if: matrix.os == 'ubuntu'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dart-desktop-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
--lcov \
--packages .dart_tool/package_config.json \
--report-on lib,common
lcov --remove ./coverage/lcov.info '*.g.dart' '*/lib/src/cli/*' '*/lib/src/native/realm_bindings.dart' -o coverage/pruned-lcov.info
lcov --remove ./coverage/lcov.info '*.realm.dart' '*/lib/src/cli/*' '*/lib/src/native/realm_bindings.dart' -o coverage/pruned-lcov.info
- name: Publish realm_dart coverage
if: inputs.runner == 'ubuntu-latest'
Expand Down
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
## vNext (TBD)

### Breaking changes
* The generated parts are now named `.realm.dart` instead of `.g.dart`. This is because the builder is now a `PartBuilder`, instead of a `SharedPartBuilder`. To migrate to this version you need to update all the part declarations to match, ie. `part 'x.g.dart` becomes `part x.realm.dart` and rerun the generator.

This makes it easier to combine builders. Here is an example of combining with `dart_mappable`:
```dart
import 'package:dart_mappable/dart_mappable.dart';
import 'package:realm_dart/realm.dart';
part 'part_builder.realm.dart';
part 'part_builder.mapper.dart';
@MappableClass()
@RealmModel()
class $Stuff with $StuffMappable {
@MappableField()
late int id;
@override
String toString() => 'Stuff{id: $id}';
}
final realm = Realm(Configuration.local([Stuff.schema]));
void main(List<String> arguments) {
final s = realm.write(() => realm.add(Stuff(1), update: true));
print(s.toJson()); // <-- realm object as json
Realm.shutdown();
}
```

### Enhancements
* None

Expand Down
21 changes: 10 additions & 11 deletions packages/realm/build.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
targets:
$default:
sources:
exclude:
- '**/ephemeral/**'

builders:
build_cli:
generate_for:
- lib/src/cli/**/**.dart
json_serializable:
generate_for:
- lib/src/cli/metrics/flutter_info.dart
- lib/src/cli/metrics/metrics.dart
realm_generator|realm_object_builder:
realm_generator:
generate_for:
include:
- test/**.dart
- example/**.dart

builders:
realm_generator:
import: "package:realm_generator/realm_generator.dart"
builder_factories: ["generateRealmObjects"]
build_extensions: { ".dart": ["realm_objects.g.part"] }
build_extensions: { ".dart": [".realm.dart"] }
auto_apply: dependents
build_to: cache
applies_builders: ["source_gen|combining_builder"]
build_to: source
5 changes: 0 additions & 5 deletions packages/realm/example/build.yaml

This file was deleted.

3 changes: 1 addition & 2 deletions packages/realm/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
////////////////////////////////////////////////////////////////////////////////
// ignore_for_file: avoid_print

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:realm/realm.dart';

part 'main.g.dart';
part 'main.realm.dart';

@RealmModel()
class _Car {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/realm/tests/build.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
targets:
$default:
builders:
realm_generator|realm_object_builder:
realm_generator:
enabled: true
7 changes: 3 additions & 4 deletions packages/realm_dart/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ targets:
generate_for:
- lib/src/cli/metrics/flutter_info.dart
- lib/src/cli/metrics/metrics.dart
realm_generator|realm_object_builder:
realm_generator:
generate_for:
include:
- test/**.dart
Expand All @@ -18,7 +18,6 @@ builders:
realm_generator:
import: "package:realm_generator/realm_generator.dart"
builder_factories: ["generateRealmObjects"]
build_extensions: { ".dart": ["realm_objects.g.part"] }
build_extensions: { ".dart": [".realm.dart"] }
auto_apply: dependents
build_to: cache
applies_builders: ["source_gen|combining_builder"]
build_to: source
4 changes: 2 additions & 2 deletions packages/realm_dart/example/bin/myapp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:async';
import 'dart:io';
import 'package:realm_dart/realm.dart';

part 'myapp.g.dart';
part 'myapp.realm.dart';

@RealmModel()
class _Car {
Expand Down Expand Up @@ -57,7 +57,7 @@ void main(List<String> arguments) async {
print("Getting all Tesla cars from the Realm.");
var filteredCars = realm.all<Car>().query("make == 'Tesla'");
print('Found ${filteredCars.length} Tesla cars');

//allow changes event to fire
await Future<void>.delayed(Duration(milliseconds: 1));

Expand Down
2 changes: 1 addition & 1 deletion packages/realm_dart/lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,7 @@ class _RealmCore {
}
}

static void user_change_callback(Pointer<Void> userdata, int data) {
static void user_change_callback(Object userdata, int data) {
final controller = userdata as UserNotificationsController;

controller.onUserChanged();
Expand Down
2 changes: 1 addition & 1 deletion packages/realm_dart/test/backlinks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import 'package:test/test.dart' hide test, throws;
import 'package:realm_dart/realm.dart';
import 'test.dart';

part 'backlinks_test.g.dart';
part 'backlinks_test.realm.dart';

@RealmModel()
class _Source {
Expand Down
2 changes: 1 addition & 1 deletion packages/realm_dart/test/geospatial_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import 'package:test/test.dart' hide test, throws;
import 'package:realm_dart/realm.dart';
import 'test.dart';

part 'geospatial_test.g.dart';
part 'geospatial_test.realm.dart';

@RealmModel(ObjectType.embeddedObject)
class _Location {
Expand Down
2 changes: 1 addition & 1 deletion packages/realm_dart/test/indexed_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import 'test.dart';

import 'package:realm_dart/realm.dart';

part 'indexed_test.g.dart';
part 'indexed_test.realm.dart';

// Don't import our own test.dart here. It will break AOT compilation.
// We may use AOT compilation locally to manually run the performance
Expand Down
2 changes: 1 addition & 1 deletion packages/realm_dart/test/migration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import 'package:realm_dart/src/results.dart';
import 'package:realm_dart/src/realm_object.dart';
import 'package:realm_dart/src/list.dart';

part 'migration_test.g.dart';
part 'migration_test.realm.dart';

@RealmModel()
@MapTo("Person")
Expand Down
2 changes: 1 addition & 1 deletion packages/realm_dart/test/realm_map_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import 'package:realm_dart/realm.dart';

import 'test.dart';

part 'realm_map_test.g.dart';
part 'realm_map_test.realm.dart';

@RealmModel()
class _Car {
Expand Down
2 changes: 1 addition & 1 deletion packages/realm_dart/test/realm_object_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import 'package:realm_dart/realm.dart';

import 'test.dart';

part 'realm_object_test.g.dart';
part 'realm_object_test.realm.dart';

@RealmModel()
class _ObjectIdPrimaryKey {
Expand Down
2 changes: 1 addition & 1 deletion packages/realm_dart/test/realm_set_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import 'package:realm_dart/realm.dart';

import 'test.dart';

part 'realm_set_test.g.dart';
part 'realm_set_test.realm.dart';

class _NullableBool {}

Expand Down
2 changes: 1 addition & 1 deletion packages/realm_dart/test/realm_value_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import 'package:realm_dart/realm.dart';

import 'test.dart';

part 'realm_value_test.g.dart';
part 'realm_value_test.realm.dart';

@RealmModel(ObjectType.embeddedObject)
class _TuckedIn {
Expand Down
2 changes: 1 addition & 1 deletion packages/realm_dart/test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import 'baas_helper.dart';

export 'baas_helper.dart' show AppNames;

part 'test.g.dart';
part 'test.realm.dart';

@RealmModel()
class _Car {
Expand Down
File renamed without changes.
7 changes: 3 additions & 4 deletions packages/realm_generator/build.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
builders:
realm_object_builder:
realm_generator:
import: "package:realm_generator/realm_generator.dart"
builder_factories: ["generateRealmObjects"]
build_extensions: { ".dart": [".realm_objects.g.part"] }
build_extensions: { ".dart": [".realm.dart"] }
auto_apply: dependents
build_to: cache
applies_builders: ["source_gen|combining_builder"]
build_to: source
4 changes: 2 additions & 2 deletions packages/realm_generator/lib/realm_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export 'src/error.dart';

/// @nodoc
Builder generateRealmObjects([BuilderOptions? options]) {
return SharedPartBuilder(
return PartBuilder(
[RealmObjectGenerator()],
'realm_objects',
'.realm.dart',
);
}
2 changes: 1 addition & 1 deletion packages/realm_generator/lib/src/pseudo_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class PseudoType extends TypeImpl {
}

@override
String? get name => _name;
String? get name => _never;

@override
PseudoType withNullability(NullabilitySuffix nullabilitySuffix) {
Expand Down
7 changes: 5 additions & 2 deletions packages/realm_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ dependencies:
source_span: ^1.8.0

dev_dependencies:
build_runner: ^2.1.0
build_test: ^2.1.4

lints: ^3.0.0
test: ^1.14.3
logging: ^1.2.0
meta: ^1.7.0
path: ^1.0.0
term_glyph: ^1.2.0
test: ^1.14.3
4 changes: 4 additions & 0 deletions packages/realm_generator/test/common.realm.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// MOCK FILE! This file exists to ensure the parent file is valid Dart.
// The parent will be used as input to the realm_generator in a test, and the
// output compared to the .expected file.
part of 'common.dart';
Loading

0 comments on commit ca669a2

Please sign in to comment.