mirror of https://github.com/AMT-Cheif/drift.git
Add version for intermediate sources if needed
This commit is contained in:
parent
6bb2c3d2c5
commit
b2b37666bd
|
@ -1,6 +1,11 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:isolate';
|
||||
|
||||
import 'package:build/build.dart';
|
||||
import 'package:package_config/package_config.dart';
|
||||
import 'package:package_config/package_config_types.dart';
|
||||
import 'package:pub_semver/pub_semver.dart';
|
||||
import 'package:yaml/yaml.dart';
|
||||
|
||||
import '../../analysis/driver/driver.dart';
|
||||
import '../../analysis/options.dart';
|
||||
|
@ -54,6 +59,14 @@ class DriftAnalyzer extends Builder {
|
|||
await buildStep.writeAsBytes(jsonOutput, asJson);
|
||||
|
||||
if (serialized.dartTypes.isNotEmpty) {
|
||||
// We're using general typedefs in the generated helper code, which rely
|
||||
// on Dart 2.13. We could just add a // @2.13 comment at the top, but
|
||||
// we also want to be able to use newer features if the source package
|
||||
// opted in to them (e.g. to be able to express records in here).
|
||||
// So, we need to know about the package version from the pubspec.
|
||||
final version = await buildStep.languageVersionForPackage ??
|
||||
_languageVersionForGeneralizedTypedefs;
|
||||
|
||||
final imports = LibraryInputManager();
|
||||
final writer = Writer(
|
||||
options,
|
||||
|
@ -61,6 +74,11 @@ class DriftAnalyzer extends Builder {
|
|||
);
|
||||
imports.linkToWriter(writer);
|
||||
|
||||
// We prefer newer versions, but we really need 2.13
|
||||
if (version.compareTo(_languageVersionForGeneralizedTypedefs) < 0) {
|
||||
writer.leaf().write('// @dart=2.13');
|
||||
}
|
||||
|
||||
for (var i = 0; i < serialized.dartTypes.length; i++) {
|
||||
writer.leaf()
|
||||
..write('typedef T$i = ')
|
||||
|
@ -72,4 +90,29 @@ class DriftAnalyzer extends Builder {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
static final _languageVersionForGeneralizedTypedefs = LanguageVersion(2, 13);
|
||||
}
|
||||
|
||||
extension on BuildStep {
|
||||
Future<LanguageVersion?> get languageVersionForPackage async {
|
||||
try {
|
||||
final configUri = await Isolate.packageConfig;
|
||||
if (configUri == null) return null;
|
||||
|
||||
final packageConfig = await loadPackageConfigUri(configUri);
|
||||
|
||||
for (final package in packageConfig.packages) {
|
||||
if (package.name == inputId.package) {
|
||||
return package.languageVersion;
|
||||
}
|
||||
}
|
||||
} on Object {
|
||||
// Can't read version, so be it
|
||||
log.fine('Could not resolve language version of package to determine '
|
||||
'whether a //@dart comment is necessary for intermediate sources');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue