PubspecConfig constructor

PubspecConfig(
  1. String pubspecContent
)

Implementation

PubspecConfig(String pubspecContent) {
  final doc = loadYaml(pubspecContent);
  if (doc is! YamlMap) {
    throw Exception('Could not parse the pubspec.yaml');
  }
  final projectName = doc['name'];
  languageVersion = parseLanguageVersion(doc);

  if (projectName == null || projectName.isEmpty) {
    throw Exception(
        'Could not parse the pubspec.yaml, project name not found');
  }

  this.projectName = projectName;
  final config = doc['model_generator'];
  if (config == null) {
    baseDirectory = _defaultBaseDirectory;
    generateForGenerics = false;
    useFvm = false;
    configPath = _DEFAULT_CONFIG_PATH;
    equalsHashCode = false;
    explicitToJson = true;
    generateToString = false;
    staticCreate = false;
    uppercaseEnums = false;
    retrofitMappers = false;
    disallowNullForDefaults = false;
    return;
  }

  baseDirectory = config['base_directory'] ?? _defaultBaseDirectory;
  useFvm = (config['use_fvm'] ?? false) == true;
  generateForGenerics = (config['generate_for_generics'] ?? false) == true;
  configPath = config['config_path'] ?? _DEFAULT_CONFIG_PATH;
  equalsHashCode = (config['equals_and_hash_code'] ?? false) == true;
  explicitToJson = (config['explicit_to_json'] ?? true) == true;
  generateToString = (config['to_string'] ?? false) == true;
  staticCreate = (config['static_create'] ?? false) == true;
  uppercaseEnums = (config['uppercase_enums'] ?? false) == true;
  retrofitMappers = (config['retrofit_compute'] ?? false) == true;
  disallowNullForDefaults =
      (config['disallow_null_for_defaults'] ?? false) == true;

  final extraImports = config['extra_imports'];
  if (extraImports != null) {
    extraImports
        .forEach((element) => this.extraImports.add(element.toString()));
  }
  final extraAnnotations = config['extra_annotations'];
  if (extraAnnotations != null) {
    extraAnnotations
        .forEach((element) => this.extraAnnotations.add(element.toString()));
  }
}