runDartCompiler method

CompiledExecutable runDartCompiler(
  1. DartScript script, {
  2. required String pathToExe,
  3. Progress? progress,
  4. String? workingDirectory,
  5. bool packed = false,
})

Run the 'dart compiler' command. script is the path to the dcli script we are compiling. pathToExe is the path (including the filename) to write the compiled ex to . 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. Returns the executable and its bundled libraries. Use CompiledExecutable.install to relocate them together. Throws DCliException. @Throwing(core.DCliException)

Implementation

CompiledExecutable runDartCompiler(
  DartScript script, {
  required String pathToExe,
  Progress? progress,
  String? workingDirectory,
  bool packed = false,
}) {
  final runArgs = <String>[];

  workingDirectory ??= script.pathToScriptDirectory;
  pathToExe = normalize(join(absolute(workingDirectory), pathToExe));
  progress ??= Progress.print();

  if (packed || hasBuildHooks(script)) {
    return _buildWithAssets(
      script,
      pathToExe,
      progress,
      workingDirectory,
      packed: packed,
    );
  }

  RunnableProcess process;
  if (useDartCommand) {
    /// use dart compile exe
    runArgs
      ..add('compile')
      ..add('exe')
      ..add(script.pathToScript)
      ..add('--output=$pathToExe');
    process = RunnableProcess.fromCommandArgs(
      dartExeName,
      runArgs,
      workingDirectory: workingDirectory,
    );
  } else {
    if (pathToDartToNativeExe == null) {
      throw DCliException(
        'Unable to compile as the dart2native executable '
        'not found on your path.',
      );
    }

    /// use old dart2native
    runArgs
      ..add(script.pathToScript)
      ..add('--output=$pathToExe');

    process = RunnableProcess.fromCommandArgs(
      pathToDartToNativeExe!,
      runArgs,
      workingDirectory: workingDirectory,
    );
  }

  process.start(extensionSearch: false, progress: progress as ProgressImpl);
  return CompiledExecutable(pathToExe);
}