|
11 | 11 | // ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ |
12 | 12 | //.title~ |
13 | 13 |
|
| 14 | +import 'dart:io'; |
| 15 | +import 'dart:isolate'; |
| 16 | + |
14 | 17 | import 'package:df_generate_dart_models/df_generate_dart_models.dart'; |
15 | 18 |
|
16 | 19 | // ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
17 | 20 |
|
18 | 21 | void main(List<String> args) async { |
| 22 | + final version = await _resolvePackageVersion(); |
| 23 | + final tag = version != null ? 'v$version' : 'main'; |
19 | 24 | await generateDartModels( |
20 | 25 | args, |
21 | 26 | defaultTemplatePathOrUrl: |
22 | | - 'https://raw.githubusercontent.com/dev-cetera/df_generate_dart_models/main/templates/featured_v1.dart.md', |
| 27 | + 'https://raw.githubusercontent.com/dev-cetera/df_generate_dart_models/$tag/templates/featured_v1.dart.md', |
23 | 28 | ); |
24 | 29 | } |
| 30 | + |
| 31 | +/// Resolves the installed package's version from its own pubspec.yaml so the |
| 32 | +/// default template URL points at the matching git tag. |
| 33 | +/// |
| 34 | +/// Without this, every binary — including ones installed months ago — would |
| 35 | +/// pull the *current* `main` template, so any breaking interpolator/template |
| 36 | +/// change in HEAD would silently corrupt old users' generated output. |
| 37 | +/// |
| 38 | +/// Returns `null` if the pubspec can't be located or parsed; the caller then |
| 39 | +/// falls back to `main`, which preserves historical behaviour for |
| 40 | +/// development checkouts where this lookup may not succeed. |
| 41 | +Future<String?> _resolvePackageVersion() async { |
| 42 | + try { |
| 43 | + final libraryUri = await Isolate.resolvePackageUri( |
| 44 | + Uri.parse('package:df_generate_dart_models/df_generate_dart_models.dart'), |
| 45 | + ); |
| 46 | + if (libraryUri == null) return null; |
| 47 | + final libDir = File.fromUri(libraryUri).parent; |
| 48 | + final pubspec = File('${libDir.parent.path}/pubspec.yaml'); |
| 49 | + if (!pubspec.existsSync()) return null; |
| 50 | + final match = RegExp( |
| 51 | + r'^version:\s*(\S+)', |
| 52 | + multiLine: true, |
| 53 | + ).firstMatch(pubspec.readAsStringSync()); |
| 54 | + return match?.group(1); |
| 55 | + } catch (_) { |
| 56 | + return null; |
| 57 | + } |
| 58 | +} |
0 commit comments