Skip to content

Commit 3761429

Browse files
Merge branch '152-source-gen-3' into 'main'
Generator: migrate to analyzer element2 API, allow latest dependencies #154 See merge request objectbox/objectbox-dart!109
2 parents 5f15053 + 30bce46 commit 3761429

File tree

15 files changed

+288
-231
lines changed

15 files changed

+288
-231
lines changed

.github/workflows/code-analysis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ permissions:
1515
env:
1616
# Do code analysis with latest SDK.
1717
# Pick a concrete version instead of "latest" to avoid builds breaking due to changes in new SDKs.
18-
FLUTTER_VERSION: 3.29.2 # Available versions: https://docs.flutter.dev/release/archive
19-
DART_VERSION: 3.7.2 # Available versions: https://dart.dev/get-dart/archive
18+
FLUTTER_VERSION: 3.35.2 # Available versions: https://docs.flutter.dev/release/archive
19+
DART_VERSION: 3.9.2 # Available versions: https://dart.dev/get-dart/archive
2020

2121
jobs:
2222
format-and-analyze:

.github/workflows/test.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ env:
2020
# Run generator integration tests and init script with latest SDK.
2121
# Pick a concrete version instead of "latest" to avoid builds breaking due to changes in new SDKs.
2222
# NOTE: also update matrix configurations below!
23-
FLUTTER_VERSION: 3.29.2 # Available versions: https://docs.flutter.dev/release/archive
24-
DART_VERSION: 3.7.2 # Available versions: https://dart.dev/get-dart/archive
23+
FLUTTER_VERSION: 3.35.2 # Available versions: https://docs.flutter.dev/release/archive
24+
DART_VERSION: 3.9.2 # Available versions: https://dart.dev/get-dart/archive
2525

2626
jobs:
2727

@@ -67,8 +67,8 @@ jobs:
6767
# Pick a concrete version instead of "latest" to avoid builds breaking due to changes in new SDKs.
6868
# Can not use env variables here, only within 'steps'.
6969
# For available versions see https://dart.dev/get-dart/archive
70-
- 3.7.2
71-
- 3.5.4 # Use latest bugfix release to get tooling fixes
70+
- 3.9.2
71+
- 3.7.2 # Use latest bugfix release to get tooling fixes
7272
runs-on: ${{ matrix.os }}
7373
steps:
7474
- uses: dart-lang/setup-dart@e51d8e571e22473a2ddebf0ef8a2123f0ab2c02c # v1.7.1
@@ -111,17 +111,17 @@ jobs:
111111
# Can not use env variables here, only within 'steps'.
112112
# For available versions see https://docs.flutter.dev/release/archive
113113
# NOTE Also update versions for Ubuntu below!
114-
- 3.29.2
115-
- 3.24.5 # Use latest bugfix release to get tooling fixes
114+
- 3.35.2
115+
- 3.29.3 # Use latest bugfix release to get tooling fixes
116116
include:
117117
# Test building for Android with highest supported JDK on latest Flutter SDK,
118118
# lowest supported JDK on lowest supported Flutter SDK. (Android is only built if a Linux
119119
# OS is detected, see integration-test.sh)
120120
- os: ubuntu-24.04
121-
flutter-version: 3.29.2
121+
flutter-version: 3.35.2
122122
jdk: 21
123123
- os: ubuntu-24.04
124-
flutter-version: 3.24.5 # Use latest bugfix release to get tooling fixes
124+
flutter-version: 3.29.3 # Use latest bugfix release to get tooling fixes
125125
jdk: 17
126126
runs-on: ${{ matrix.os }}
127127
steps:

.gitlab-ci.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ variables:
88
# # Pick a concrete version instead of "latest" to avoid builds breaking due to changes in new
99
# SDKs.
1010
# For available versions see https://hub.docker.com/_/dart/tags and https://dart.dev/get-dart/archive
11-
DART_VERSION_LATEST: '3.7.2'
12-
DART_VERSION_LOWEST: '3.5.4' # Use latest bugfix release to get tooling fixes
11+
DART_VERSION_LATEST: '3.9.2'
12+
DART_VERSION_LOWEST: '3.7.2' # Use latest bugfix release to get tooling fixes
1313
DART_VERSION: $DART_VERSION_LATEST # Also used for caching, see .cache template
1414

1515
.common:
@@ -84,7 +84,10 @@ generator-integ-tests:
8484
# Generator tests
8585
- cd generator
8686
- dart pub get
87-
- dart test
87+
# generator test suites must not run in parallel (see notes on GeneratorTestEnv):
88+
# Set concurrency=1 to run only one test suite (== test file) at a time.
89+
# Set --reporter expanded to print log for every completed test.
90+
- dart test --concurrency=1 --reporter expanded
8891
# ObjectBox tests
8992
- cd ../objectbox_test
9093
- ../install.sh

generator/build.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ builders:
2929
builder_factories: ["codeGeneratorFactory"]
3030
# build_extensions: Required. A map from input extension to the list of output extensions that may be created
3131
# for that input. This must match the merged buildExtensions maps from each Builder in builder_factories.
32+
# See notes on CodeBuilder.buildExtensions.
3233
build_extensions:
3334
"$lib$": ["objectbox.g.dart"]
34-
"$test": ["objectbox.g.dart"]
35+
"$test$": ["objectbox.g.dart"]
3536
required_inputs: ['.objectbox.info']
3637
auto_apply: dependents
3738
build_to: source

generator/integration-tests/part-partof/lib/frozen.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:objectbox/objectbox.dart';
44
part 'frozen.freezed.dart';
55

66
@freezed
7-
class FrozenEntity with _$FrozenEntity {
7+
sealed class FrozenEntity with _$FrozenEntity {
88
@Entity(realClass: FrozenEntity)
99
factory FrozenEntity(
1010
{@Id(assignable: true) required int id,
@@ -13,15 +13,15 @@ class FrozenEntity with _$FrozenEntity {
1313
}
1414

1515
@freezed
16-
class FrozenPerson with _$FrozenPerson {
16+
sealed class FrozenPerson with _$FrozenPerson {
1717
@Entity(realClass: FrozenPerson)
1818
factory FrozenPerson(
1919
{@Id(assignable: true) required int id,
2020
required String name}) = _FrozenPerson;
2121
}
2222

2323
@freezed
24-
class FrozenBook with _$FrozenBook {
24+
sealed class FrozenBook with _$FrozenBook {
2525
@Entity(realClass: FrozenBook)
2626
factory FrozenBook(
2727
{@Id(assignable: true) required int id,

generator/integration-tests/part-partof/pubspec.yaml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
name: objectbox_generator_test
22

33
environment:
4-
sdk: '>=2.14.0 <4.0.0'
4+
sdk: '^3.0.0' # Require 3.0 to use sealed classes for freezed
55

66
dependencies:
77
objectbox: any # override below to use local package
88
json_annotation: '^4.6.0'
9-
freezed_annotation: '^2.1.0'
9+
freezed_annotation: '^3.1.0'
1010

1111
dev_dependencies:
12-
json_serializable: '^6.3.1'
13-
freezed: '^2.0.4' # 3.0.4 requires Dart 3.6
12+
json_serializable: '^6.10.0' # Require 6.10.0 to allow build 3 required by objectbox_generator
13+
freezed: '^3.0.0' # Require 3.0.0 to allow build 3 and source_gen 3.1 required by objectbox_generator
1414
objectbox_generator: any # override below to use local package
1515
test: '^1.21.4'
1616
build_runner: '^2.3.3'
17-
build_test: '^2.1.3'
17+
build_test: '^3.0.0'
1818
io: '^1.0.4'
1919
path: '^1.8.3'
20-
# analyzer 7.4.0 introduced breaking API changes in a minor release, breaking json_serializable
21-
# https://github.com/google/json_serializable.dart/issues/1485
22-
analyzer: '<=7.3.0'
2320

2421
dependency_overrides:
2522
objectbox:

generator/lib/src/analysis/analysis.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@ import 'build_properties.dart';
1818
/// Requires [tokenFilePath] to exist, otherwise does nothing. See the
1919
/// associated test (analysis_test.dart) on how to create this file.
2020
class ObjectBoxAnalysis {
21-
static const _debug = false;
22-
2321
/// Path is relative to lib folder.
24-
static const tokenFilePath = "assets/analysis-token.txt";
22+
static const defaultTokenFilePath = "assets/analysis-token.txt";
2523

2624
static const _url = "api.mixpanel.com";
2725
static const _path = "track";
2826

27+
final bool _debug;
28+
final String _tokenFilePath;
29+
30+
ObjectBoxAnalysis(
31+
{bool debug = false, String tokenFilePath = defaultTokenFilePath})
32+
: _debug = debug,
33+
_tokenFilePath = tokenFilePath;
34+
2935
/// Builds a Build event and sends it with [sendEvent]. May not send if it
3036
/// fails to store a unique identifier and last time sent, or if no valid API
3137
/// token is found.
@@ -126,7 +132,7 @@ class ObjectBoxAnalysis {
126132
}
127133

128134
Future<String?> _getToken() async {
129-
final uri = Uri.parse("package:objectbox_generator/$tokenFilePath");
135+
final uri = Uri.parse("package:objectbox_generator/$_tokenFilePath");
130136
final resolvedUri = await Isolate.resolvePackageUri(uri);
131137
if (resolvedUri != null) {
132138
final file = File.fromUri(resolvedUri);

generator/lib/src/builder_dirs.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'package:build/build.dart';
21
import 'package:path/path.dart' as path;
32

43
import 'config.dart';
@@ -12,11 +11,15 @@ class BuilderDirs {
1211

1312
BuilderDirs._(this.root, this.out);
1413

15-
factory BuilderDirs(BuildStep buildStep, Config config) {
14+
/// For [pathInSourceRoot] expects the path to a file in the source root
15+
/// directory (currently only lib or test are supported). It's used to
16+
/// determine the [root] source path and [out] path for generated code.
17+
/// A [config] may customize the [out] path relative to [root].
18+
factory BuilderDirs(String pathInSourceRoot, Config config) {
1619
// Paths from Config are supplied by the user and may contain duplicate
1720
// slashes or be empty: normalize all paths to not return duplicate or
1821
// trailing slashes to ensure they can be compared via strings.
19-
final root = path.normalize(path.dirname(buildStep.inputId.path));
22+
final root = path.normalize(path.dirname(pathInSourceRoot));
2023
final String out;
2124
if (root.endsWith('test')) {
2225
out = path.normalize('$root/${config.outDirTest}');

generator/lib/src/code_builder.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@ class CodeBuilder extends Builder {
2929

3030
@override
3131
late final buildExtensions = {
32+
// lib/$lib$ and test/$test$ are placeholder files of build_runner which are
33+
// used as there is no clear primary input file for this builder.
3234
r'$lib$': [path.join(_config.outDirLib, _config.codeFile)],
3335
r'$test$': [path.join(_config.outDirTest, _config.codeFile)]
3436
};
3537

3638
@override
3739
FutureOr<void> build(BuildStep buildStep) async {
38-
final builderDirs = BuilderDirs(buildStep, _config);
40+
// This is only called twice, once with path lib/$lib$ and once with test/$test$
41+
final inputFilePath = buildStep.inputId.path;
42+
final builderDirs = BuilderDirs(inputFilePath, _config);
3943

4044
// build() will be called only twice, once for the `lib` directory and once for the `test` directory
4145
// map from file name to a 'json' representation of entities

0 commit comments

Comments
 (0)