generate method

Future<void> generate({
  1. required Directory inputDirectory,
  2. required File templateFile,
  3. required Directory outputDirectory,
  4. required Directory bundleRoot,
})

Generates the nfpm configuration and changelog.

Reads the pubspec.yaml and templateFile from inputDirectory, writes the generated nfpm.yaml and changelog.yaml into outputDirectory and uses bundleRoot as the src of the generated bundle tree entry.

Implementation

Future<void> generate({
  required Directory inputDirectory,
  required File templateFile,
  required Directory outputDirectory,
  required Directory bundleRoot,
}) async {
  final pubspecFile = File.fromUri(
    inputDirectory.uri.resolve('pubspec.yaml'),
  );
  if (!pubspecFile.existsSync()) {
    throw Exception('$pubspecFile not found!');
  }
  if (!templateFile.existsSync()) {
    throw Exception('$templateFile not found!');
  }
  if (!bundleRoot.existsSync()) {
    throw Exception('$bundleRoot not found!');
  }

  final pubspecYaml = await pubspecFile.readAsString();
  final pubspec = Pubspec.parse(pubspecYaml, sourceUrl: pubspecFile.uri);
  final executables = _readExecutables(pubspecYaml);

  final editor = YamlEditor(await templateFile.readAsString());
  await outputDirectory.create(recursive: true);

  _applyMetadata(editor, pubspec);
  _applyVersion(editor, pubspec);

  _ensureContents(editor);
  _resolveContentSources(editor, inputDirectory, pubspec);
  _prependContents(editor, [
    _bundleTreeEntry(pubspec, bundleRoot),
    ..._symlinkEntries(pubspec, executables),
    ?_licenseEntry(pubspec, inputDirectory),
  ]);
  final changelogWritten = await _addChangelog(
    editor,
    inputDirectory,
    outputDirectory,
  );

  final nfpmFile = File.fromUri(outputDirectory.uri.resolve('nfpm.yaml'));
  await nfpmFile.writeAsString(editor.toString());

  print(p.basename(nfpmFile.path));
  if (changelogWritten) {
    print('changelog.yaml');
  }
}