writeVersion function

  1. @aktor
Future<void> writeVersion(
  1. C c
)

Read the version from the pubspec.yaml file and save it to the version.dart file.

Implementation

@aktor
Future<void> writeVersion(C c) async {
  final fPubspec = c.root.f("pubspec.yaml");

  Future<void> update() async {
    final fileContent = await fPubspec.readAsString();
    final pubspec = PubspecYaml.loadFromYamlString(fileContent);
    final version = pubspec.version.unsafe!;
    final versionFile = c.dir.f("version.dart");
    await versionFile.writeAsString("""
/// Auto generated by `${relative(c.file.path, from: c.root.path)}`
const String version = '$version';
""");
    print("Updated");
  }

  await update();

  if (c.mode == Mode.dev) {
    await for (final _ in fPubspec.watch()) {
      await update();
    }
  }
}