distributeYaml static method
Creates a ConfigParser instance by parsing a YAML file.
This method reads and parses a YAML configuration file, processes variables, validates required fields, and creates a complete ConfigParser instance.
Parameters:
path- The path to the YAML configuration fileglobalResults- Global command line argument results
Returns a new ConfigParser instance with the parsed configuration
Throws an Exception if:
- The configuration file does not exist
- Required keys (
tasks,name,description) are missing - Job configuration is invalid (missing
package_name)
Implementation
static Future<ConfigParser> distributeYaml(
String path,
ArgResults? globalResults,
) async {
final file = File(path);
if (!file.existsSync()) {
throw Exception("$path file not found, please run init command");
}
Map<String, dynamic> configJson = jsonDecode(
jsonEncode(loadYaml(file.readAsStringSync())),
);
List<Task> jobTasks;
final yamlVariables = Map<String, dynamic>.from(configJson["variables"]);
final environments = Map<String, dynamic>.from(Platform.environment.cast());
for (var key in yamlVariables.keys) {
await Variables.processBySystem(
yamlVariables[key].toString(),
globalResults,
).then((value) {
yamlVariables[key] = value;
});
}
environments.addAll(yamlVariables);
Variables variables = Variables(environments, globalResults);
if (configJson["tasks"] == null) {
throw Exception("tasks's key not found in $path");
}
if (configJson["name"] == null) {
throw Exception("name key not found in $path");
}
if (configJson["description"] == null) {
throw Exception("description key not found in $path");
}
Job parseJob(Map<String, dynamic> json) {
Map<String, dynamic>? builder = json.containsKey("builder")
? json["builder"]
: null;
Map<String, dynamic>? publisher = json.containsKey("publisher")
? json["publisher"]
: null;
final packageName = json["package_name"];
final key = json["key"];
if (packageName == null) {
throw Exception("package_name is required for each job");
}
BuilderJob? builderArguments;
PublisherJob? publisherArguments;
if (builder != null) {
builderArguments = BuilderJob.fromJson(builder, variables);
}
if (publisher != null) {
publisherArguments = PublisherJob.fromJson(publisher, variables);
}
final output = Job(
name: json["name"] as String,
description: json["description"],
packageName: packageName,
environments: environments,
builder: builderArguments,
publisher: publisherArguments,
key: key,
);
return output;
}
jobTasks = (configJson["tasks"] as List)
.map<Task>(
(item) => Task(
name: item["name"],
jobs: (item["jobs"] as List)
.map<Job>((item) => parseJob(item))
.toList(),
key: item["key"],
workflows: item["workflows"] != null
? List<String>.from(item["workflows"])
: null,
description: item["description"],
),
)
.toList();
return ConfigParser(
globalResults: globalResults,
tasks: jobTasks,
arguments: (configJson["arguments"] as Map<String, dynamic>?)?.map(
(key, value) => MapEntry(key, value as dynamic),
),
environments: environments,
);
}