createWindowsScript static method

File createWindowsScript({
  1. required String contents,
  2. File? file,
  3. WindowsScriptType type = WindowsScriptType.batch,
  4. int? windowsCodePage = WindowsCodePage.utf8,
  5. bool echoOff = true,
})

Implementation

static io.File createWindowsScript({
  required String contents,
  io.File? file,
  WindowsScriptType type = WindowsScriptType.batch,
  int? windowsCodePage = WindowsCodePage.utf8,
  bool echoOff = true,
}) {
  if (!io.Platform.isWindows) {
    throw UnsupportedError(
      "This method can only be used on Windows systems.",
    );
  }

  var adjustedContents = contents;

  if (null != windowsCodePage) {
    adjustedContents = """
chcp $windowsCodePage > nul

$adjustedContents
""";
  }

  switch (type) {
    case WindowsScriptType.batch:
      if (echoOff) {
        adjustedContents = """
@echo off

$adjustedContents
""";
      }
      break;

    case WindowsScriptType.powershell:
      break;
  }

  file ??=
      io.Directory.systemTemp.getUniqueFileSync(extension: type._extension);

  file.writeAsStringSync(
    adjustedContents,
    mode: io.FileMode.writeOnly,
    flush: true,
    encoding: io.systemEncoding,
  );

  return file;
}