build function Null safety

Future build(
  1. List<String> arguments,
  2. {String filenameMapper(
    1. String source
    )?,
  3. Encoding encoding = utf8,
  4. List<String>? imports}
)

Compile changed RSP files. This method shall be called within build.dart, with the arguments passed to main() as its arguments.

Notice that it accepts files ending with .rsp.whatever.

  • filenameMapper - returns the filename of the destination file, which must end with .dart. If omitted, it will be generated under the webapp folder with the same path structure.
  • imports - additional imported packages, such as ["package:foo/foo.dart"].

Implementation

Future build(List<String> arguments, {String filenameMapper(String source)?,
    Encoding encoding: utf8, List<String>? imports}) async {
  final ArgParser argParser = new ArgParser()
    ..addMultiOption("changed")
    ..addMultiOption("removed")
    ..addFlag("clean", negatable: false)
    ..addFlag("machine", negatable: false)
    ..addFlag("full", negatable: false);

  final args = argParser.parse(arguments),
    changed = args["changed"] as List<String>,
    removed = args["removed"] as List<String>,
    clean = args["clean"] as bool;

  if (clean) { // clean only
    Directory.current.list(recursive: true).listen((fse) {
      if (fse is File && fse.path.endsWith(".rsp.dart"))
        fse.delete();
    });

  } else if (removed.isEmpty && changed.isEmpty) { // full build
    Directory.current.list(recursive: true).listen((fse) {
      if (fse is File && _rspSource(fse.path) >= 0)
        compileFile(fse.path, encoding: encoding,
          destinationName: filenameMapper != null ? filenameMapper(fse.path): null,
          imports: imports);
    });

  } else {
    for (String name in removed) {
      final i = _rspSource(name);
      if (i >= 0) {
        final File gen = new File("${name.substring(0, i)}dart");
        if (await gen.exists())
          gen.delete();
      }
    }

    for (String name in changed) {
      if (_rspSource(name) >= 0)
        compileFile(name, encoding: encoding,
          destinationName: filenameMapper != null ? filenameMapper(name): null,
          imports: imports);
    }
  }
}