Exemplo n.º 1
0
        // TODO maybe move some (or all) of this to the TemplateResolver
        public static TemplateInfo Load(TemplateResolver resolver, IFileProvider source, string name)
        {
            string specPath = System.IO.Path.Combine(name, Template.TemplateDefinitionFileName);
            if (!source.FileExists(specPath))
                throw new FileNotFoundException("Couldn't find template specification: " + specPath + " from " + source.ToString(), specPath);

            Dictionary<string, TemplateParameterInfo> parameters = new Dictionary<string, TemplateParameterInfo>();

            TemplateInfo inheritedTemplate = null;

            using (var fileStream = source.OpenFile(specPath, FileMode.Open))
            {
                XDocument templateSpec = XDocument.Load(fileStream);
                XAttribute inheritsAttr = templateSpec.Element("template").Attribute("inherits");
                
                if (inheritsAttr != null)
                {
                    string inheritedTemplateName = inheritsAttr.Value;
                    if (!resolver.TryResolve(inheritedTemplateName, out inheritedTemplate))
                        throw new Exception("Failed to resolve inherted template: " + inheritedTemplateName);

                    // add inherited parameters
                    foreach (TemplateParameterInfo param in inheritedTemplate.Parameters)
                        parameters.Add(param.Name, param);
                }

                IEnumerable<XElement> parameterElements = templateSpec.XPathSelectElements("/template/parameter");
                foreach (XElement parameterElement in parameterElements)
                {
                    string paramName = parameterElement.Attribute("name").Value;
                    string defaultValue = ReadOptionalAttribute(parameterElement, "select");
                    string description = ReadOptionalAttribute(parameterElement, "description");

                    // add or override inherited parameter default value
                    parameters[paramName] = new TemplateParameterInfo(paramName, description, defaultValue);
                }
            }

            return new TemplateInfo(source, specPath, name, parameters.Values.ToArray(), inheritedTemplate);
        }