compile method

void compile({
  1. bool install = false,
  2. bool overwrite = false,
  3. String? workingDirectory,
})

Compiles this script and optionally installs it to ~/.dcli/bin

The resulting executable is compiled into the script's directory.

If install is true (default = false) then the resulting executable will be moved into ~/.dcli/bin.

If install is true and overwrite is true (default) it will overwrite any existing exe in ~/.dcli/bin. If install is true and overwrite is false and an exe of the same name already exists in ~/.dcli/bin the install will fail and a MoveException will be thrown.

If workingDirectory is not passed then the current working directory is used. The workingDirectory should contain the pubspec.yaml that is used to compile the script.

Implementation

void compile({
  bool install = false,
  bool overwrite = false,
  String? workingDirectory,
}) {
  verbose(
    () => '\nCompiling with pubspec.yaml:\n'
        '${read(pathToPubSpec).toParagraph()}\n',
  );

  workingDirectory ??= pwd;

  if (install && isInstalled && !overwrite) {
    throw InvalidArgumentException(
      'You selected to install the compiled exe however an installed '
      'exe of that name already exists. Use overwrite=true',
    );
  }

  DartSdk().runDartCompiler(
    this,
    pathToExe: pathToExe,
    progress: Progress(print, stderr: print),
    workingDirectory: workingDirectory,
  );

  if (install) {
    print('');
    print(orange('Installing $pathToExe into $pathToInstalledExe'));
    move(pathToExe, pathToInstalledExe, overwrite: true);
  }
}