Config.fromJson constructor

Config.fromJson(
  1. Map<String, dynamic> json, {
  2. BuildType type = BuildType.debug,
  3. bool app = true,
  4. bool installer = true,
  5. required String? buildArgs,
  6. required String? appVersion,
  7. required String? signToolName,
  8. required String? signToolCommand,
  9. required String? signToolParams,
})

Creates a Config instance from a JSON map, typically read from pubspec.yaml.

Validates the configuration and exits with an error if invalid values are found.

Implementation

factory Config.fromJson(
  Map<String, dynamic> json, {
  BuildType type = BuildType.debug,
  bool app = true,
  bool installer = true,
  required String? buildArgs,
  required String? appVersion,
  required String? signToolName,
  required String? signToolCommand,
  required String? signToolParams,
}) {
  if (json['inno_bundle'] is! Map<String, dynamic>) {
    CliLogger.exitError("inno_bundle section is missing from pubspec.yaml.");
  }
  final Map<String, dynamic> inno = json['inno_bundle'];

  if (inno['id'] is! String) {
    CliLogger.exitError(
        "inno_bundle.id attribute is missing from pubspec.yaml. "
        "Run `dart run inno_bundle:guid` to generate a new one, "
        "then put it in your pubspec.yaml.");
  } else if (!Uuid.isValidUUID(fromString: inno['id'])) {
    CliLogger.exitError("inno_bundle.id from pubspec.yaml is not valid. "
        "Run `dart run inno_bundle:guid` to generate a new one, "
        "then put it in your pubspec.yaml.");
  }
  final String id = inno['id'];

  if (json['name'] is! String) {
    CliLogger.exitError("name attribute is missing from pubspec.yaml.");
  }
  final String pubspecName = json['name'];

  if (inno['name'] != null && !validFilenameRegex.hasMatch(inno['name'])) {
    CliLogger.exitError("inno_bundle.name from pubspec.yaml is not valid. "
        "`${inno['name']}` is not a valid file name.");
  }
  final String name = inno['name'] ?? pubspecName;

  if ((appVersion ?? inno['version'] ?? json['version']) is! String) {
    CliLogger.exitError("version attribute is missing from pubspec.yaml.");
  }
  final String version = appVersion ?? inno['version'] ?? json['version'];

  if ((inno['description'] ?? json['description']) is! String) {
    CliLogger.exitError(
        "description attribute is missing from pubspec.yaml.");
  }
  final String description = inno['description'] ?? json['description'];

  if ((inno['publisher'] ?? json['maintainer']) is! String) {
    CliLogger.exitError("maintainer or inno_bundle.publisher attributes are "
        "missing from pubspec.yaml.");
  }
  final String publisher = inno['publisher'] ?? json['maintainer'];

  final url = (inno['url'] ?? json['homepage'] ?? "") as String;
  final supportUrl = (inno['support_url'] as String?) ?? url;
  final updatesUrl = (inno['updates_url'] as String?) ?? url;

  if (inno['installer_icon'] != null && inno['installer_icon'] is! String) {
    CliLogger.exitError("inno_bundle.installer_icon attribute is invalid "
        "in pubspec.yaml.");
  }
  final installerIcon = inno['installer_icon'] != null
      ? p.join(
          Directory.current.path,
          p.fromUri(inno['installer_icon']),
        )
      : defaultInstallerIconPlaceholder;
  if (installerIcon != defaultInstallerIconPlaceholder &&
      !File(installerIcon).existsSync()) {
    CliLogger.exitError(
        "inno_bundle.installer_icon attribute value is invalid, "
        "`$installerIcon` file does not exist.");
  }

  if (inno['languages'] != null && inno['languages'] is! List<String>) {
    CliLogger.exitError("inno_bundle.languages attribute is invalid "
        "in pubspec.yaml, only a list of strings is allowed.");
  }
  final languages = (inno['languages'] as List<String>?)?.map((l) {
        final language = Language.getByNameOrNull(l);
        if (language == null) {
          CliLogger.exitError("problem in inno_bundle.languages attribute "
              "in pubspec.yaml, language `$l` is not supported.");
        }
        return language!;
      }).toList(growable: false) ??
      Language.values;

  if (inno['admin'] != null &&
      inno['admin'] is! bool &&
      inno['admin'] != "auto") {
    CliLogger.exitError("inno_bundle.admin attribute is invalid value "
        "in pubspec.yaml");
  }
  final admin = AdminMode.fromOption(inno['admin'] ?? true);

  if (inno['license_file'] != null && inno['license_file'] is! String) {
    CliLogger.exitError("inno_bundle.license_file attribute is invalid "
        "in pubspec.yaml.");
  }

  final licenseFilePath = p.join(
    Directory.current.path,
    inno['license_file'] != null
        ? p.fromUri(inno['license_file'])
        : 'LICENSE',
  );
  final licenseFile =
      File(licenseFilePath).existsSync() ? licenseFilePath : '';

  final signToolError = SignTool.validationError(
    inno["sign_tool"],
    signToolName: signToolName,
    signToolCommand: signToolCommand,
    signToolParams: signToolParams,
  );
  if (signToolError != null) {
    CliLogger.exitError(signToolError);
  }
  final signTool = SignTool.fromOption(
    inno['sign_tool'],
    signToolName: signToolName,
    signToolCommand: signToolCommand,
    signToolParams: signToolParams,
  );

  final archError = BuildArch.validationError(inno['arch']);
  if (archError != null) {
    CliLogger.exitError(archError);
  }
  final arch = BuildArch.fromOption(inno['arch']);

  return Config(
    buildArgs: buildArgs,
    id: id,
    pubspecName: pubspecName,
    name: name,
    description: description,
    version: version,
    publisher: publisher,
    url: url,
    supportUrl: supportUrl,
    updatesUrl: updatesUrl,
    installerIcon: installerIcon,
    languages: languages,
    admin: admin,
    type: type,
    app: app,
    installer: installer,
    licenseFile: licenseFile,
    signTool: signTool,
    arch: arch,
  );
}