getDefinition method
Get a serialized Definition for a script string if it exists. This function will throw errors if the script is not defined or if the script is not valid.
Implementation
Definition getDefinition(String scriptString) {
if (!serializedDefinitions.containsKey(scriptString)) {
final scriptFound = lookup(scriptString);
/// for when script is not defined at all
if (scriptFound == null) {
throw MerryError(
type: ErrorCode.scriptNotDefined,
body: {'script': scriptString, 'suggestions': getPaths()},
);
}
// for when script is not a type we want
if (scriptFound is! Map && scriptFound is! List && scriptFound is! String) {
throw MerryError(
type: ErrorCode.invalidScript,
body: {'script': scriptString},
);
}
// for when script is a map
if (scriptFound is Map) {
// check for a platform-specific script first
final platformKey = currentPlatformKey;
if (platformKey != null) {
final platformScripts = scriptFound[platformKey];
if (platformScripts != null && (platformScripts is List || platformScripts is String)) {
serializedDefinitions[scriptString] = Definition.from(
platformScripts,
);
return serializedDefinitions[scriptString]!;
}
}
final scripts = scriptFound[scriptsDefinitionKey];
final validity = scripts != null && (scripts is List || scripts is String);
if (!validity) {
// check for (default) key to support default scripts in nested groups
final defaultScript = scriptFound[defaultDefinitionKey];
if (defaultScript != null && (defaultScript is List || defaultScript is String)) {
serializedDefinitions[scriptString] = Definition.from(
defaultScript,
);
return serializedDefinitions[scriptString]!;
}
throw MerryError(
type: ErrorCode.invalidScript,
body: {'script': scriptString, 'paths': getPaths()},
);
}
}
serializedDefinitions[scriptString] = Definition.from(scriptFound);
}
return serializedDefinitions[scriptString]!;
}