-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpubspec_config.dart
102 lines (91 loc) · 3.43 KB
/
pubspec_config.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import 'dart:io';
import 'package:meta/meta.dart';
import 'package:model_generator/util/language_version.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:yaml/yaml.dart';
class PubspecConfig {
static final _DEFAULT_CONFIG_PATH = // ignore: non_constant_identifier_names
'model_generator${Platform.pathSeparator}config.yaml';
static const _defaultBaseDirectory = 'model';
late String projectName;
late String baseDirectory;
late bool useFvm;
late bool generateForGenerics;
late String configPath;
late bool equalsHashCode;
late bool explicitToJson;
late bool generateToString;
late bool staticCreate;
late bool uppercaseEnums;
late bool retrofitMappers;
late bool disallowNullForDefaults;
LanguageVersion? languageVersion;
final extraImports = <String>[];
final extraAnnotations = <String>[];
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 = true;
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'] ?? true) == 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()));
}
}
@visibleForTesting
static LanguageVersion? parseLanguageVersion(YamlMap doc) {
final environmentRoot = doc['environment'];
if (environmentRoot is! YamlMap) return null;
final sdk = environmentRoot['sdk'];
if (sdk is! String) return null;
final range = VersionConstraint.parse(sdk);
if (range is Version) {
return LanguageVersion(range.major, range.minor, range.patch);
} else if (range is VersionRange) {
final min = range.min;
if (min == null) return null;
return LanguageVersion(min.major, min.minor, min.patch);
}
return null;
}
}