// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
import 'package:brick_generator/logger.dart';
import 'package:brick_generator/models/brick_variable.dart';
import 'package:yaml/yaml.dart';
const _nameKey = 'name';
const _descriptionKey = 'description';
const _versionKey = 'version';
const _pathToBrickifyKey = 'path_to_brickify';
const _varsKey = 'vars';
class BrickConfig {
String? name;
String? description;
String? version;
String? pathToBrickify;
List? variables;
BrickConfig({
required this.name,
required this.description,
required this.version,
required this.pathToBrickify,
required this.variables,
});
BrickConfig._();
factory BrickConfig.parser() => BrickConfig._();
static BrickConfig? from(YamlMap? data) => data != null
? BrickConfig(
name: data[_nameKey] as String?,
description: data[_descriptionKey] as String?,
version: data[_versionKey] as String?,
pathToBrickify: data[_pathToBrickifyKey] as String?,
variables: (data[_varsKey] as YamlMap?)
?.map(
(dynamic key, dynamic value) =>
MapEntry(
key,
BrickVariable.from(value as YamlMap?),
),
)
.values
.toList(),
)
: null;
void checkFormat() {
if (name == null || description == null || pathToBrickify == null) {
final err = 'Yaml file is not conform';
Logger.throwError(ArgumentError(err), err);
}
if (variables != null) {
for (final variable in variables!) {
variable?.checkFormat();
}
}
}
String toBrickYaml() {
String content = '''
name: $name
description: $description
version: $version
environment:
mason: ">=0.1.0-dev.26 <0.1.0"
''';
if (variables?.isNotEmpty ?? false) {
content += 'vars:\n';
for (final BrickVariable? v in variables!) {
if (v != null) {
final vString = '''
${v.name}:
type: ${v.type.toString()}
description: ${v.description}
default: ${v.defaultValue}
prompt: ${v.prompt}
''';
content += vString;
}
}
}
return content;
}
}