inserter 0.2.0 copy "inserter: ^0.2.0" to clipboard
inserter: ^0.2.0 copied to clipboard

Tooling for inserting String into Files given their respective strategies.

example/example.dart

import 'dart:io';

import 'package:inserter/inserter.dart';

const keyOne = 'KEY_ONE';
const keyTwo = 'KEY_TWO';
const keyThree = 'KEY_THREE';

void main() async {
  final srcFile = File('example/src');

  final builders = [
    MatcherBuilder(
      matcher: (file, line) async => line.contains(keyOne),
      builder: (file, line) async => 'UPDATE FOR $keyOne',
    ),
    MatcherBuilder(
      matcher: (file, line) async => line.contains(keyTwo),
      builder: (file, line) async {
        const insert = 'UPDATE FOR $keyTwo';
        // a log to show when a new line is written
        stdout.writeln('writing $insert in ${file.path}');
        return insert;
      },
      strategy: BuilderStrategy.above,
    ),
    MatcherBuilder(
      matcher: (file, line) async => line.contains(keyThree),
      builder: (file, line) async =>
          line.split(' ').fold<String>('', (previous, word) {
        final forInsert = word == keyThree ? 'UPDATE FOR $keyThree' : word;
        return '${previous.isEmpty ? '' : '$previous '}$forInsert';
      }),

      // stop after one match
      stopWhen: (_, __, totalMatches) async => totalMatches > 0,
      strategy: BuilderStrategy.replace,
    ),
  ];

  // execute the inserter
  final inserter = Inserter(
    files: [await srcFile.copy('example/execute.test')],
    builders: builders,
  );

  // Run the inserter
  await inserter.execute();

  // Use "Inserter.run" for a one-off insertion.
  await Inserter.run(
    files: [await srcFile.copy('example/run.test')],
    builders: builders,
  );
}
0
likes
160
pub points
0%
popularity

Publisher

verified publisherelijahluckey.com

Tooling for inserting String into Files given their respective strategies.

Repository (GitHub)
View/report issues

Documentation

API reference

License

GPL-3.0 (license)

Dependencies

args, mason_logger

More

Packages that depend on inserter