compile method
Compiles this script and optionally installs it to ~/.dcli/bin
The resulting executable is compiled into the script's directory.
Packages with build hooks are built with dart build cli. Their bundled
executable and libraries live in a hidden .name.bundle/ directory beside
a native launcher. Installation moves the launcher and bundle together.
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.
Returns the compiled executable at its final location. Set report to
false when the caller will install it elsewhere before reporting outputs.
Throws InvalidArgumentException.
@Throwing(ArgumentError)
@Throwing(CopyException)
@Throwing(DeleteException)
@Throwing(InvalidArgumentException)
@Throwing(MoveException)
Implementation
CompiledExecutable compile({
bool install = false,
bool overwrite = false,
bool packed = false,
bool report = true,
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',
);
}
final compiled = DartSdk().runDartCompiler(
this,
packed: packed,
pathToExe: pathToExe,
progress: Progress((line) {
if (!line.startsWith('Generated: ')) {
print(line);
}
}, stderr: print),
workingDirectory: workingDirectory,
);
if (install) {
print('');
print(orange('Installing $pathToExe into $pathToInstalledExe'));
final installed = compiled.install(pathToInstalledExe, overwrite: true);
if (report) {
installed.report();
}
return installed;
} else {
if (report) {
compiled.report();
}
return compiled;
}
}