ReposManifest.parse constructor
ReposManifest.parse(
- String text
Parses text, checking the rules of repos.schema.json and that no
path is listed twice; throws ReposManifestException naming every
problem. Unknown keys and a newer version are notes, not problems.
Implementation
factory ReposManifest.parse(String text) {
final problems = <String>[];
final notes = <String>[];
void unknownKeys(YamlMap map, Set<String> known, String where) {
for (final key in map.keys) {
if (!known.contains(key)) {
notes.add('$where: unknown key `$key` (ignored)');
}
}
}
final Object? doc;
try {
doc = loadYaml(text);
} on YamlException catch (e) {
throw ReposManifestException(['(file): ${e.message}']);
}
if (doc is! YamlMap) {
throw ReposManifestException(['(top): not a mapping']);
}
unknownKeys(doc, const {
'version',
'root',
'repos',
'links',
'unmanaged',
}, '(top)');
final version = doc['version'];
if (version is! int || version < 1) {
problems.add('version: must be a whole number from 1');
} else if (version > knownVersion) {
notes.add(
'version: $version is newer than this reader ($knownVersion): '
'read as version $knownVersion',
);
}
var rootUrl = '';
var rootBranch = '';
String? rootDescription;
final root = doc['root'];
if (root is! YamlMap) {
problems.add('root: missing (url and branch)');
} else {
rootUrl = _string(root, 'url', 'root', problems, _url) ?? '';
rootBranch = _string(root, 'branch', 'root', problems, _branch) ?? '';
unknownKeys(root, const {'url', 'branch', 'description'}, 'root');
rootDescription = _description(root, 'root', problems);
}
final entries = <RepoEntry>[];
final repos = doc['repos'];
if (repos is! YamlList) {
problems.add('repos: missing (a list)');
} else {
for (var i = 0; i < repos.length; i++) {
final entry = _entry(repos[i], 'repos/$i', problems, notes);
if (entry != null) entries.add(entry);
}
}
final links = <RepoLink>[];
final rawLinks = doc['links'];
if (rawLinks is YamlList) {
for (var i = 0; i < rawLinks.length; i++) {
final l = rawLinks[i];
if (l is! YamlMap) {
problems.add('links/$i: not a mapping');
continue;
}
unknownKeys(l, const {'path', 'target', 'notes'}, 'links/$i');
final path = _string(l, 'path', 'links/$i', problems, _path);
final target = _string(l, 'target', 'links/$i', problems);
if (path != null && target != null) links.add(RepoLink(path, target));
}
} else if (rawLinks != null) {
problems.add('links: must be a list');
}
final unmanaged = <String>[];
final rawUnmanaged = doc['unmanaged'];
if (rawUnmanaged is YamlList) {
for (var i = 0; i < rawUnmanaged.length; i++) {
final u = rawUnmanaged[i];
final path = u is YamlMap
? _string(u, 'path', 'unmanaged/$i', problems, _path)
: null;
if (u is! YamlMap) problems.add('unmanaged/$i: not a mapping');
if (u is YamlMap) {
unknownKeys(u, const {'path', 'notes'}, 'unmanaged/$i');
}
if (path != null) unmanaged.add(path);
}
} else if (rawUnmanaged != null) {
problems.add('unmanaged: must be a list');
}
final seen = <String>{};
for (final p in [...entries.map((e) => e.path), ...unmanaged]) {
if (!seen.add(p)) problems.add('$p: listed twice');
}
if (problems.isNotEmpty) throw ReposManifestException(problems);
return ReposManifest(
rootUrl: rootUrl,
rootBranch: rootBranch,
entries: entries,
links: links,
unmanaged: unmanaged,
rootDescription: rootDescription,
notes: notes,
);
}