from static method

ReleasePackerCommand? from(
  1. Object command
)

Implementation

static ReleasePackerCommand? from(Object command) {
  if (command is ReleasePackerCommand) {
    return command;
  }

  if (command is String) {
    var fullCommand = command.trim();

    var fullCommandLc = fullCommand.toLowerCase();
    if (fullCommandLc == 'dart_pub_get' || fullCommandLc == 'dart pub get') {
      return ReleasePackerDartPubGet();
    }

    if (fullCommand.isEmpty) return null;

    var list = parseInlineCommand(fullCommand);

    if (list.isNotEmpty) {
      if (list[0] == 'dart') {
        list.removeAt(0);
        return ReleasePackerDartCommand.fromList(list);
      } else {
        return ReleasePackerProcessCommand.fromList(list);
      }
    }

    return null;
  } else if (command is List) {
    var cmd = ReleasePackerProcessCommand.fromList(command);
    var args = cmd.args;

    if (cmd.command == 'dart') {
      if (args.equals(['pub', 'get'])) {
        return ReleasePackerDartPubGet();
      } else if (args.length == 3 &&
          args[0] == 'compile' &&
          args[1] == 'exe') {
        return ReleasePackerDartCompileExe(args[2]);
      } else if (args.length == 3 &&
          args[0] == 'compile' &&
          args[1] == 'kernel') {
        return ReleasePackerDartCompileKernel(args[2]);
      }
    } else if (cmd.command == 'release_utility' && args.length >= 2) {
      var gui = args[0].toLowerCase().contains('gui');
      var input = cmd.args[1];
      var output = cmd.args.length > 2 ? cmd.args[2] : input;
      return ReleasePackerWindowsSubsystemCommand(gui, input, output);
    } else if (cmd.command == 'windows_gui' && args.isNotEmpty) {
      var input = cmd.args[0];
      var output = cmd.args.length > 1 ? cmd.args[1] : input;
      return ReleasePackerWindowsSubsystemCommand(true, input, output);
    }

    return cmd;
  } else if (command is Map) {
    var map = command.asJsonMap;

    var dartCompileExe = map.get<String>('dart_compile_exe');
    if (dartCompileExe != null && dartCompileExe.isNotEmpty) {
      return ReleasePackerDartCompileExe(dartCompileExe);
    }

    var dartCompileKernel = map.get<String>('dart_compile_kernel');
    if (dartCompileKernel != null && dartCompileKernel.isNotEmpty) {
      return ReleasePackerDartCompileKernel(dartCompileKernel);
    }

    var dartPubGet = map.get<String>('dart_pub_get');
    if (dartPubGet != null && dartPubGet.isNotEmpty) {
      dartPubGet = dartPubGet.trim().toLowerCase();
      if (dartPubGet != 'false') {
        return ReleasePackerDartPubGet();
      }
    }

    var dart = map.get('dart');
    if (dart != null) {
      return ReleasePackerDartCommand.from(dart);
    }

    var windowsGUI = map.get('windows_gui');
    if (windowsGUI != null) {
      String? input;
      String? output;

      if (windowsGUI is List && windowsGUI.isNotEmpty) {
        input = windowsGUI[0];
        output = windowsGUI.length > 1 ? windowsGUI[1] : input;
      } else if (windowsGUI is Map && windowsGUI.isNotEmpty) {
        input = windowsGUI['input'];
        output = windowsGUI['output'] ?? input;
      } else if (windowsGUI is String) {
        input = output = windowsGUI;
      }

      if (input == null || output == null) {
        throw StateError(
            "Can't define `windows_gui` input and output: $windowsGUI");
      }

      return ReleasePackerWindowsSubsystemCommand(
          true, input.toString(), output.toString());
    }

    var rm = map.get<String>('rm') ?? map.get<String>('del');
    if (rm != null) {
      rm = rm.trim();
      if (rm.isNotEmpty) {
        return ReleasePackerCommandDelete(rm);
      }
    }

    var cmd = map.get('command') ?? map.get('cmd');
    if (cmd != null) {
      var stdout = map.get<String>('stdout');
      var stderr = map.get<String>('stderr');
      return ReleasePackerProcessCommand.from(cmd,
          stdoutFilePath: stdout, stderrFilePath: stderr);
    }

    var url = map.get('url');
    if (url != null) {
      return ReleasePackerCommandURL.fromJson(url);
    }

    var uploadRelease =
        map.get('upload_release') ?? map.get('upload_release_bundle');
    if (uploadRelease != null) {
      return ReleasePackerCommandUploadReleaseBundle.fromJson(uploadRelease);
    }

    return null;
  } else {
    throw ArgumentError("Unknown command type: $command");
  }
}