load static method

Future<TemplateDefinition> load(
  1. String template
)

Loads the definition for the specified template.

Returns the parsed TemplateDefinition for the template.

Implementation

static Future<TemplateDefinition> load(String template) async {
  final root = const PackageLocator().packageRoot();
  final templateRoot = '${root.path}/templates/$template';

  final manifest = File('$templateRoot/template.yaml');

  if (!manifest.existsSync()) {
    throw Exception('Template "$template" not found.');
  }

  final map = await _readYaml(manifest);
  if (map.isEmpty) {
    throw Exception(
      'Template "$template" is unavailable: template.yaml is empty. '
      'Available templates: ${await availableTemplates()}.',
    );
  }

  final missing =
      _requiredManifestKeys.where((key) => !map.containsKey(key)).toList();
  if (missing.isNotEmpty) {
    throw Exception(
      'Template "$template" is invalid: missing manifest fields '
      '${missing.join(', ')}.',
    );
  }

  final files = Map<dynamic, dynamic>.from(map['files'] ?? const {});
  final missingSections = <String>[];

  for (final section in _sections) {
    if (!files.containsKey(section)) missingSections.add(section);
    map[section] = await _loadSection(
        templateRoot: templateRoot, files: files, section: section);
  }

  if (missingSections.isNotEmpty) {
    throw Exception(
      'Template "$template" is invalid: missing section mappings '
      '${missingSections.join(', ')}.',
    );
  }

  await _validateReferences(
    template: template,
    templateRoot: templateRoot,
    manifest: map,
  );

  try {
    return TemplateDefinition.fromMap(map);
  } on TypeError catch (error) {
    throw Exception(
      'Template "$template" is invalid: $error',
    );
  }
}