dynamicCode method

Future<String> dynamicCode()

Generates the dynamic code that will be added to the script.

This code includes information about the current environment, such as the path to the Dart executable, the architecture, and the channel.

Implementation

Future<String> dynamicCode() async {
  String executable = Platform.resolvedExecutable;

  if (Platform.script.path.endsWith('.dart') || executable.endsWith('/dart')) {
    // If we are running from a dart file or from a dart executable, add the
    // executable to the script.
    executable += ' ${Platform.script.path}';
  }

  String xOS = Platform.operatingSystem;
  bool isWindows = xOS == 'windows';
  bool isLinux = xOS == 'linux';
  bool isMacOS = xOS == 'macos';
  bool isAndroid = xOS == 'android';

  String xARCH = getArchitecture();
  String xCHANNEL = channelChosen;

  String xTMP = (await XPM.temp(packageName)).path;

  return '''
readonly XPM="$executable";
readonly xOS="$xOS";
readonly isWindows="$isWindows";
readonly isLinux="$isLinux";
readonly isMacOS="$isMacOS";
readonly isAndroid="$isAndroid";
readonly xARCH="$xARCH";
readonly xCHANNEL="$xCHANNEL";
readonly xBIN="${binDirectory().path}";
readonly xHOME="${XPM.userHome.path}";
readonly xTMP="$xTMP";
readonly xSUDO="${Global.sudoPath}";
readonly hasSnap="${Global.hasSnap}";
readonly hasFlatpak="${Global.hasFlatpak}";
''';
}