toolOriginLine function

String toolOriginLine({
  1. required String resolvedExecutable,
  2. required Uri script,
})

The origin line for a run whose executable is resolvedExecutable and whose entry point is script.

binary: /path/to/tool when the process IS the tool — an AOT build. source: /path/to/entry.dart when a Dart runtime is executing the tool, in which case the runtime is the same string for every tool in the workspace and the SCRIPT is the thing worth naming.

It does not try to label the source kind. A working tree, a pub-cache copy and a .dart_tool/pub/bin snapshot are told apart by their paths, which is what the reader needs and what cannot go out of date.

Implementation

String toolOriginLine({
  required String resolvedExecutable,
  required Uri script,
}) {
  final executableName = resolvedExecutable
      .split(RegExp(r'[/\\]'))
      .last
      .toLowerCase();
  // Matched on the WHOLE name, not a substring: `d4rtgen` contains no "dart",
  // but a tool that did would otherwise report itself as its own interpreter.
  final isDartRuntime =
      executableName == 'dart' || executableName == 'dart.exe';
  if (isDartRuntime) {
    return 'source: ${script.toFilePath()}';
  }
  return 'binary: $resolvedExecutable';
}