execute method

  1. @override
bool execute(
  1. ReleasePacker releasePacker,
  2. Directory rootDirectory, {
  3. ReleaseBundle? releaseBundle,
  4. int expectedExitCode = 0,
})
override

Implementation

@override
bool execute(ReleasePacker releasePacker, Directory rootDirectory,
    {ReleaseBundle? releaseBundle, int expectedExitCode = 0}) {
  var executablePath = args[args.length - 2];
  var executableOutputPath = args.last;

  var inputPath =
      pack_path.normalize(pack_path.join(rootDirectory.path, executablePath));

  var outputPath = pack_path
      .normalize(pack_path.join(rootDirectory.path, executableOutputPath));

  var inputFile = File(inputPath);
  if (!inputFile.existsSync()) {
    print("  ▒  Can't find Windows executable file: $inputPath");
    return false;
  }

  var outputFile = File(outputPath);
  if (outputFile.existsSync() && inputFile.path != outputFile.path) {
    print("  ▒  Can't overwrite output file: $outputPath");
    return false;
  }

  var argGUI = args.contains('--windows-gui');
  var argConsole = args.contains('--windows-console');

  bool gui;
  if (argGUI && argConsole) {
    print("  ▒  Ambiguous parameters: $args");
    return false;
  } else if (argGUI) {
    gui = true;
  } else if (argConsole) {
    gui = false;
  } else {
    print("  ▒  No `--windows-gui` or `--windows-console` parameters: $args");
    return false;
  }

  print(
      '   »  Windows Subsystem command> ${rootDirectory.path} -> GUI: $gui ; executable: $inputPath');

  WindowsPEFile windowsPEFile;
  try {
    windowsPEFile = WindowsPEFile(inputFile);
  } catch (e, s) {
    print("  ▒  Error opening Windows Executable: $inputPath");
    print(e);
    print(s);
    return false;
  }

  try {
    if (!windowsPEFile.isValidExecutable) {
      print(
          "   »  IGNORING Windows Subsystem command> Not a valid Windows Executable: $inputPath");
      return false;
    }

    windowsPEFile.setWindowsSubsystem(gui: gui);

    if (inputFile.path == outputFile.path) {
      var inputFileCp = _resolveInputFileCopy(inputFile);

      if (inputFileCp != null) {
        inputFile.renameSync(inputFileCp.path);
        windowsPEFile.save(outputFile);

        if (!outputFile.existsSync() ||
            outputFile.lengthSync() != windowsPEFile.fileBuffer.length) {
          print(
              '  ▒  Error saving executable file: ${outputFile.path} (copy: ${inputFileCp.path})');
          return false;
        }

        inputFileCp.deleteSync();

        print('   »  Executable saved: ${outputFile.path}');
      }
    } else {
      windowsPEFile.save(outputFile);
      print('   »  New executable saved: ${outputFile.path}');
    }

    // Re-open and test:
    windowsPEFile = WindowsPEFile(outputFile);

    var windowsSubsystem = windowsPEFile.readWindowsSubsystem();
    var expectedWindowsSubsystem = gui ? 2 : 3;

    if (windowsSubsystem != expectedWindowsSubsystem) {
      print("  ▒  Windows Subsystem Error> "
          "Value not set to `$expectedWindowsSubsystem` (${WindowsPEFile.windowsSubsystemName(expectedWindowsSubsystem)}). "
          "Read value: `$windowsSubsystem` (${WindowsPEFile.windowsSubsystemName(windowsSubsystem)})");
      return false;
    } else {
      print("   »  Windows Subsystem> "
          "Current value: `$windowsSubsystem` (${WindowsPEFile.windowsSubsystemName(windowsSubsystem)}) "
          "@ $outputFile");
    }
  } catch (e, s) {
    print(e);
    print(s);
    return false;
  }

  return true;
}