generatePubignore method

void generatePubignore({
  1. bool basedOnRootGitignore = true,
  2. Iterable<File> basedOn = const [],
  3. List<String> additionalIgnores = const [],
})

Generate a .pubignore file at the package root.

  1. The .pubignore file might be generated from other ignore files, such as .gitignore files, located inside the package root directory.
  2. When generating from a .gitignore file inside a child package, all items inside will be added with the relative path from root to the parent directory where the specified basedOn file locates.
  3. The specified additionalIgnores will be added to the prefix.
  4. It's strongly recommended to use absoluted and normalized path.

Implementation

void generatePubignore({
  bool basedOnRootGitignore = true,
  Iterable<File> basedOn = const [],
  List<String> additionalIgnores = const [],
}) {
  final buffer = StringBuffer(additionalIgnores.join('\n'));
  if (additionalIgnores.isNotEmpty) buffer.writeln();

  for (final file in [if (basedOnRootGitignore) rootGitignore, ...basedOn]) {
    final basePath = normalize(relative(file.parent.path, from: root.path));
    final resolvedBase = basePath == '.' ? '' : '$basePath/';
    buffer.writeln('\n# Synced from $basePath');
    file
        .readAsStringSync()
        .split('\n')
        .map((line) => line.trim())
        .where((line) => line.isNotEmpty && !line.startsWith('#'))
        .map((line) => '$resolvedBase$line')
        .forEach(buffer.writeln);
  }
  pubignore.writeAsStringSync('$buffer\n');
}