Пример #1
0
        private static IEnumerable <InnerBuildContext> GetInnerContexts(DocumentBuildParameters parameters, IEnumerable <IDocumentProcessor> processors)
        {
            var filesGroupedByProcessor =
                from file in parameters.Files.EnumerateFiles()
                group file by(from processor in processors
                              let priority = processor.GetProcessingPriority(file)
                              where priority != ProcessingPriority.NotSupportted
                              orderby priority descending
                              select processor).FirstOrDefault();

            var toHandleItems    = filesGroupedByProcessor.Where(s => s.Key != null);
            var notToHandleItems = filesGroupedByProcessor.Where(s => s.Key == null);

            foreach (var item in notToHandleItems)
            {
                var sb = new StringBuilder();
                sb.AppendLine("Cannot handle following file:");
                foreach (var f in item)
                {
                    sb.Append("\t");
                    sb.AppendLine(f.File);
                }
                Logger.LogWarning(sb.ToString());
            }

            foreach (var item in toHandleItems)
            {
                yield return(new InnerBuildContext(new HostService(
                                                       from file in item
                                                       select Load(item.Key, parameters.Metadata, parameters.FileMetadata, file)),
                                                   item.Key));
            }
        }
Пример #2
0
        public void Build(DocumentBuildParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }
            if (parameters.OutputBaseDir == null)
            {
                throw new ArgumentException("Output folder cannot be null.", nameof(parameters) + "." + nameof(parameters.OutputBaseDir));
            }
            if (parameters.Files == null)
            {
                throw new ArgumentException("Source files cannot be null.", nameof(parameters) + "." + nameof(parameters.Files));
            }
            if (parameters.Metadata == null)
            {
                parameters.Metadata = ImmutableDictionary <string, object> .Empty;
            }

            using (new LoggerPhaseScope(Phase))
            {
                Directory.CreateDirectory(parameters.OutputBaseDir);
                var context = new DocumentBuildContext(
                    Path.Combine(Environment.CurrentDirectory, parameters.OutputBaseDir),
                    parameters.Files.EnumerateFiles(),
                    parameters.ExternalReferencePackages);
                Logger.LogInfo("Start building document ...");
                foreach (var item in
                         from file in parameters.Files.EnumerateFiles()
                         group file by(from processor in Processors
                                       let priority = processor.GetProcessingPriority(file)
                                                      where priority != ProcessingPriority.NotSupportted
                                                      orderby priority descending
                                                      select processor).FirstOrDefault())
                {
                    if (item.Key != null)
                    {
                        BuildCore(item.Key, item, parameters.Metadata, parameters.FileMetadata, context);
                    }
                    else
                    {
                        var sb = new StringBuilder();
                        sb.AppendLine("Cannot handle following file:");
                        foreach (var f in item)
                        {
                            sb.Append("\t");
                            sb.AppendLine(f.File);
                        }
                        Logger.LogWarning(sb.ToString());
                    }
                }

                context.SerializeTo(parameters.OutputBaseDir);
                Logger.LogInfo("Building document completed.");
            }
        }
Пример #3
0
        private static IEnumerable <InnerBuildContext> GetInnerContexts(DocumentBuildParameters parameters, IEnumerable <IDocumentProcessor> processors, TemplateProcessor templateProcessor)
        {
            var k = from fileItem in (
                from file in parameters.Files.EnumerateFiles()
                from p in (from processor in processors
                           let priority = processor.GetProcessingPriority(file)
                                          where priority != ProcessingPriority.NotSupported
                                          group processor by priority into ps
                                          orderby ps.Key descending
                                          select ps.ToList()).FirstOrDefault() ?? new List <IDocumentProcessor> {
                null
            }
                select new { file, p })
                    group fileItem by fileItem.p;

            var toHandleItems    = k.Where(s => s.Key != null);
            var notToHandleItems = k.Where(s => s.Key == null);

            foreach (var item in notToHandleItems)
            {
                var sb = new StringBuilder();
                sb.AppendLine("Cannot handle following file:");
                foreach (var f in item)
                {
                    sb.Append("\t");
                    sb.AppendLine(f.file.File);
                }
                Logger.LogWarning(sb.ToString());
            }

            return(from item in toHandleItems.AsParallel().WithDegreeOfParallelism(Constants.DefaultParallelism)
                   select new InnerBuildContext(
                       new HostService(
                           parameters.Files.DefaultBaseDir,
                           from file in item
                           select Load(item.Key, parameters.Metadata, parameters.FileMetadata, file.file)
                           into model
                           where model != null
                           select model),
                       item.Key,
                       templateProcessor));
        }
Пример #4
0
        public void Build(DocumentBuildParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }
            if (parameters.OutputBaseDir == null)
            {
                throw new ArgumentException("Output folder cannot be null.", nameof(parameters) + "." + nameof(parameters.OutputBaseDir));
            }
            if (parameters.Files == null)
            {
                throw new ArgumentException("Source files cannot be null.", nameof(parameters) + "." + nameof(parameters.Files));
            }
            if (parameters.Metadata == null)
            {
                parameters.Metadata = ImmutableDictionary <string, object> .Empty;
            }

            using (new LoggerPhaseScope(PhaseName))
            {
                Directory.CreateDirectory(parameters.OutputBaseDir);
                var context = new DocumentBuildContext(
                    Path.Combine(Environment.CurrentDirectory, parameters.OutputBaseDir),
                    parameters.Files.EnumerateFiles(),
                    parameters.ExternalReferencePackages
                    );
                Logger.LogVerbose("Start building document...");
                IEnumerable <InnerBuildContext> innerContexts = Enumerable.Empty <InnerBuildContext>();
                try
                {
                    using (var processor = parameters.TemplateManager?.GetTemplateProcessor())
                    {
                        innerContexts = GetInnerContexts(parameters, Processors, processor).ToList();
                        var manifest = new List <ManifestItemWithContext>();
                        foreach (var item in innerContexts)
                        {
                            manifest.AddRange(BuildCore(item, context));
                        }

                        // Use manifest from now on
                        UpdateContext(context);
                        UpdateHref(manifest, context);

                        TemplateProcessor.Transform(processor, manifest.Select(s => s.Item).ToList(), context, parameters.ApplyTemplateSettings);

                        Logger.LogInfo($"Building {manifest.Count} file(s) completed.");
                    }
                }
                finally
                {
                    foreach (var item in innerContexts)
                    {
                        if (item.HostService != null)
                        {
                            Cleanup(item.HostService);
                            item.HostService.Dispose();
                        }
                    }
                }
            }
        }
Пример #5
0
        public void Build(DocumentBuildParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }
            if (parameters.OutputBaseDir == null)
            {
                throw new ArgumentException("Output folder cannot be null.", nameof(parameters) + "." + nameof(parameters.OutputBaseDir));
            }
            if (parameters.Files == null)
            {
                throw new ArgumentException("Source files cannot be null.", nameof(parameters) + "." + nameof(parameters.Files));
            }
            if (parameters.Metadata == null)
            {
                parameters.Metadata = ImmutableDictionary <string, object> .Empty;
            }

            using (new LoggerPhaseScope(PhaseName))
            {
                Directory.CreateDirectory(parameters.OutputBaseDir);
                var context = new DocumentBuildContext(
                    Path.Combine(Environment.CurrentDirectory, parameters.OutputBaseDir),
                    parameters.Files.EnumerateFiles(),
                    parameters.ExternalReferencePackages,
                    parameters.TemplateCollection
                    );
                Logger.LogInfo("Start building document...");
                IEnumerable <InnerBuildContext> innerContexts = Enumerable.Empty <InnerBuildContext>();
                try
                {
                    innerContexts = GetInnerContexts(parameters, Processors).ToList();
                    foreach (var item in innerContexts)
                    {
                        BuildCore(item.HostService, item.Processor, context);
                    }
                    foreach (var item in innerContexts)
                    {
                        UpdateHref(item.HostService, item.Processor, context);
                    }

                    UpdateContext(context);
                    if (parameters.ExportRawModel)
                    {
                        Logger.LogInfo("Start exporting raw model...");
                        foreach (var item in context.Manifest)
                        {
                            var model = item.Model;
                            if (model.Content != null)
                            {
                                var rawModelPath = Path.Combine(model.BaseDir, Path.ChangeExtension(model.File, RawModelExtension));
                                JsonUtility.Serialize(rawModelPath, model.Content);
                            }
                        }
                    }

                    Transform(context, parameters.TemplateCollection, parameters.ExportViewModel);
                }
                finally
                {
                    foreach (var item in innerContexts)
                    {
                        if (item.HostService != null)
                        {
                            Cleanup(item.HostService);
                            item.HostService.Dispose();
                        }
                    }
                }

                Logger.LogInfo("Building document completed.");
            }
        }