Ejemplo n.º 1
0
 // TODO we can eliminate the TemplateResolver here once we remove it as a requirement for the Template class
 public TemplateInfo(IFileProvider source, string path, string name, TemplateParameterInfo[] parameters, TemplateInfo inheritedTemplate)
 {
     this.Source = source;
     this.Name = name;
     this.Parameters = parameters;
     this.Path = path;
     this.Inherits = inheritedTemplate;
 }
Ejemplo n.º 2
0
        private TemplateParameterModel CreateTemplateParameterModel(TemplateParameterInfo templateParameterInfo)
        {
            Dictionary<string, string> valueContainer = this.SettingsProvider.GetValueOrDefault<Dictionary<string, string>>(Settings.TemplateParameters);
            if (valueContainer == null)
                valueContainer = new Dictionary<string, string>();

            string value;
            if (!valueContainer.TryGetValue(templateParameterInfo.Name, out value))
                value = null;

            return new TemplateParameterModel
                   {
                       Name = templateParameterInfo.Name,
                       Description = templateParameterInfo.Description,
                       DefaultValue = templateParameterInfo.DefaultExpression,
                       Value = value
                   };
        }
Ejemplo n.º 3
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);
        }