protected void Application_Start() { //Template template = new Template(new DirectoryFileProvider()); Template template = new Template(new ResourceFileProvider("LBi.LostDoc.Repository.Web.App_Data.Template")); template.Load(AppConfig.TemplatePath); ContentManager.Initialize(new ContentSettings { ContentPath = AppConfig.ContentPath, IgnoreVersionComponent = VersionComponent.Patch, RepositoryPath = AppConfig.RepositoryPath, Template = template }); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); BundleTable.Bundles.RegisterTemplateBundles(); }
public void Invoke() { var traceListener = new ConsolidatedConsoleTraceListener( new Dictionary<string, string> { { "LostDoc.Core.Template", "Template" }, { "LostDoc.Core.Bundle", "Bundle" }, { "LostDoc.Core.Template.AssetResolver", "Resolve" } }); TraceSources.TemplateSource.Listeners.Add(traceListener); TraceSources.AssetResolverSource.Listeners.Add(traceListener); try { if (this.Verbose.IsPresent) { const SourceLevels verboseLevel = SourceLevels.All; TraceSources.TemplateSource.Switch.Level = verboseLevel; TraceSources.AssetResolverSource.Switch.Level = verboseLevel; TraceSources.BundleSource.Listeners.Add(traceListener); } else { const SourceLevels normalLevel = SourceLevels.Information | SourceLevels.Warning | SourceLevels.Error | SourceLevels.ActivityTracing; TraceSources.TemplateSource.Switch.Level = normalLevel; TraceSources.AssetResolverSource.Switch.Level = normalLevel; } LinkedList<FileInfo> includedFiles = new LinkedList<FileInfo>(); if (File.Exists(this.Path)) includedFiles.AddLast(new FileInfo(this.Path)); else if (Directory.Exists(this.Path)) { Directory.GetFiles(this.Path, "*.ldoc", SearchOption.AllDirectories) .Aggregate(includedFiles, (l, f) => l.AddLast(new FileInfo(f)).List); } else throw new FileNotFoundException(System.IO.Path.GetFullPath(this.Path)); Bundle bundle = new Bundle(this.IgnoreVersionComponent); TraceSources.TemplateSource.TraceInformation("Merging LostDoc files into bundle."); var xmlSettings = new XmlReaderSettings { CheckCharacters = false }; foreach (FileInfo file in includedFiles) { TraceSources.TemplateSource.TraceEvent(TraceEventType.Information, 0, "Path: {0}", file.Name); XDocument fileDoc; using (var reader = XmlReader.Create(file.FullName, xmlSettings)) fileDoc = XDocument.Load(reader); bundle.Add(fileDoc); } // find template string appDir = Assembly.GetExecutingAssembly().Location; string cwDir = Directory.GetCurrentDirectory(); IFileProvider fsProvider = new DirectoryFileProvider(); IFileProvider resourceProvider = new ResourceFileProvider("LBi.LostDoc.ConsoleApplication.Templates"); IFileProvider selectedFileProvider = null; string templatePath = null; if (System.IO.Path.IsPathRooted(this.Template) && fsProvider.FileExists(System.IO.Path.Combine(this.Template, "template.xml"))) { selectedFileProvider = fsProvider; templatePath = this.Template; } else if (!System.IO.Path.IsPathRooted(this.Template)) { string tmp = System.IO.Path.Combine(cwDir, this.Template, "template.xml"); if (fsProvider.FileExists(tmp)) { selectedFileProvider = fsProvider; templatePath = tmp; } else { tmp = System.IO.Path.Combine(appDir, this.Template, "template.xml"); if (fsProvider.FileExists(tmp)) { selectedFileProvider = fsProvider; templatePath = tmp; } else { tmp = System.IO.Path.Combine(this.Template, "template.xml"); if (resourceProvider.FileExists(tmp)) { selectedFileProvider = resourceProvider; templatePath = tmp; } } } } if (templatePath == null) throw new FileNotFoundException(this.Template); string outputDir = this.Output ?? (Directory.Exists(this.Path) ? this.Path : System.IO.Path.GetDirectoryName(this.Path)); Template template = new Template(selectedFileProvider); template.Load(templatePath); AssetRedirectCollection assetRedirects; XDocument mergedDoc = bundle.Merge(out assetRedirects); var templateData = new TemplateData { OverwriteExistingFiles = this.Force.IsPresent, AssetRedirects = assetRedirects, Document = mergedDoc, IgnoredVersionComponent = this.IgnoreVersionComponent, Arguments = this.Arguments, TargetDirectory = outputDir }; template.Generate(templateData); } finally { TraceSources.TemplateSource.Listeners.Remove(traceListener); TraceSources.AssetResolverSource.Listeners.Remove(traceListener); } }