示例#1
0
        private void ReadModules(IEnumerable <ObjectElement> allElements)
        {
            allElements = allElements as ObjectElement[] ?? allElements.ToArray();

            IEnumerable <IGrouping <string, ObjectElement> > groups =
                allElements.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 module name '{0}' detected. Module names must be unique. See the module headings below:",
                    duplicate.First());
                Log.IndentedCollection(duplicate, Log.Error);
            }

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

            using (new Log.ScopedTimer(Log.Level.Info, "Read All Modules (Async)"))
            {
                // One module at a time for debugging if needed.
//            foreach (ObjectElement element in allElements)
//            {
//                Task<ModuleReader.Result> task = ReadModuleAsync(element);
//                task.Wait();
//                excludedProjects.UnionWith(task.Result.ExcludedProjects);
//                Modules[task.Result.Module.Name] = task.Result.Module;
//            }

                Task <ModuleReader.Result>[] tasks = allElements.Select(ReadModuleAsync).ToArray();
                try
                {
                    Task.WaitAll(tasks.Cast <Task>().ToArray());
                }
                catch (AggregateException ae)
                {
                    Log.Error("One or more exceptions occured while reading modules asynchronously:");
                    foreach (Exception ex in ae.Flatten().InnerExceptions)
                    {
                        Log.Error(ex.Message);
                    }

                    throw;
                }

                foreach (Task <ModuleReader.Result> taskResult in tasks)
                {
                    ModuleReader.Result result = taskResult.Result;
                    excludedProjects.UnionWith(result.ExcludedProjects);
                    Modules[result.Module.Name] = result.Module;
                }
            }
        }
示例#2
0
        private Task <ModuleReader.Result> ReadModuleAsync(ObjectElement moduleElement)
        {
            var reader      = new ModuleReader(Solution, Templates, templateReader);
            var baseVars    = new Dictionary <string, string>(ExpandableVars.Instance.Variables);
            int indentLevel = Log.Instance.IndentLevel;

            return(Task.Run(() =>
            {
                using (new Log.BufferedTaskOutput("RM-" + moduleElement.Heading.Name, indentLevel))
                {
                    ExpandableVars.Init(baseVars);
                    ModuleReader.Result moduleResult = reader.Read(moduleElement);
                    return moduleResult;
                }
            }));
        }