Ejemplo n.º 1
0
        public Result Read(ObjectElement moduleElement)
        {
            Log.Heading("Reading module element: {0}", moduleElement);

            Result result;

            using (new CompositeDisposable(
                       new Log.ScopedIndent(),
                       new Log.ScopedTimer(Log.Level.Debug, "Read Module", moduleElement)))
            {
                string moduleName   = moduleElement.Heading.Name;
                string templateName = moduleElement.Heading.InheritedObjectName;

                excludedProjects = new HashSet <string>();
                idLookup         = new Dictionary <string, Project.Identifier>();

                if (!string.IsNullOrEmpty(templateName) && !templates.ContainsKey(templateName))
                {
                    throw new UndefinedTemplateException(templateName);
                }

                Template template = templateReader.Read(moduleElement);

                using (new ExpandableVars.ScopedVariable(ExpandableVars.Instance, ExpandableVars.VAR_MODULE_NAME,
                                                         moduleName))
                {
                    Dictionary <Configuration, ModuleConfiguration> moduleConfigs =
                        CreateModuleConfigs(template, moduleName);

                    result = new Result(new Module(solution, moduleName, moduleConfigs, idLookup), excludedProjects);
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        private void ReadTemplates(IEnumerable <ObjectElement> allTemplateElements)
        {
            List <ObjectElement> templateList =
                allTemplateElements as List <ObjectElement> ?? allTemplateElements.ToList();

            IEnumerable <IGrouping <string, ObjectElement> > groups =
                templateList.GroupBy(t => t.Heading.Name);

            IGrouping <string, ObjectElement>[] duplicates = groups
                                                             .Where(g => g.Count() > 1)
                                                             .ToArray();

            foreach (IGrouping <string, ObjectElement> duplicate in duplicates)
            {
                Log.Error(
                    "Duplicate template name '{0}' detected. Template names must be unique. See the template headings below:",
                    duplicate.First());
                Log.IndentedCollection(duplicate, Log.Error);
            }

            if (duplicates.Length > 0)
            {
                ObjectElement[] duplicate = duplicates.First().ToArray();
                throw new DuplicateTemplateNameException(duplicate[0], duplicate[1]);
            }

            // Basic technique for processing templates in dependency order and catch cyclic dependency
            // A template can only be processed after the template it inherits has been processed.
            while (templateList.Count > 0)
            {
                var readTemplates = new List <ObjectElement>();
                foreach (ObjectElement template in templateList)
                {
                    if (string.IsNullOrEmpty(template.Heading.InheritedObjectName) ||
                        Templates.ContainsKey(template.Heading.InheritedObjectName))
                    {
                        Templates[template.Heading.Name] = templateReader.Read(template);
                        readTemplates.Add(template);
                    }
                }

                if (readTemplates.Count > 0)
                {
                    templateList.RemoveAll(t => readTemplates.Contains(t));
                }
                else if (templateList.Count > 0)
                {
                    throw new InvalidOperationException(
                              "A cyclic dependency was detected in template inheritance. These templates could not be read: " +
                              string.Join(", ", templateList));
                }
            }
        }