protected override TemplatesCompilationResult OnCompile() { TemplatesCompilationResult result = new TemplatesCompilationResult(); foreach (var source in this.Sources) { using (StreamReader reader = new StreamReader(source.SourceStream)) { InternalCSharpTemplateCompiler compiler = new InternalCSharpTemplateCompiler(reader, ns, source.ReferenceFilePath); compiler.generate(); generatedResourcesClassNames.Add(new GeneratedResourceClass { ClassName = compiler.className, DiscoveryPath = source.DiscoveryPath }); } } generateDiscoveryClass(); #if DEBUG using (FileStream fs = new FileStream("/home/thepumpkin/template.cs", FileMode.Create, FileAccess.Write)) { using (StreamWriter writer = new StreamWriter(fs)) { csharp.GenerateCodeFromCompileUnit(this.codeCompilerUnit, writer, null); } } #endif string[] assemblies = new string[] { "BlackLinks" }; CompilerParameters prms = new CompilerParameters(assemblies); if (!string.IsNullOrEmpty(this.OutputAssemblyPath)) { prms.OutputAssembly = this.OutputAssemblyPath; } CompilerResults results = this.csharp.CompileAssemblyFromDom(prms, new CodeCompileUnit[] { this.codeCompilerUnit }); Console.WriteLine("Errors Count= {0}", results.Errors.Count); if (results.CompiledAssembly != null) { result.Assemblies.Add(results.CompiledAssembly); } #if DEBUG foreach (CompilerError error in results.Errors) { Console.Error.WriteLine("\t{0} at file {1}#{2}", error.ErrorText, error.FileName, error.Line); result.CompilationErrors.Add(error); } #endif return(result); }
protected override TemplatesCompilationResult OnCompile () { TemplatesCompilationResult result = new TemplatesCompilationResult (); foreach (var source in this.Sources) { using (StreamReader reader = new StreamReader (source.SourceStream)) { InternalCSharpTemplateCompiler compiler = new InternalCSharpTemplateCompiler (reader, ns,source.ReferenceFilePath); compiler.generate (); generatedResourcesClassNames.Add(new GeneratedResourceClass { ClassName = compiler.className, DiscoveryPath = source.DiscoveryPath }); } } generateDiscoveryClass(); #if DEBUG using (FileStream fs = new FileStream ("/home/thepumpkin/template.cs", FileMode.Create, FileAccess.Write)) { using (StreamWriter writer = new StreamWriter (fs)) { csharp.GenerateCodeFromCompileUnit (this.codeCompilerUnit, writer, null); } } #endif string[] assemblies = new string[] { "BlackLinks" }; CompilerParameters prms = new CompilerParameters (assemblies); if (!string.IsNullOrEmpty (this.OutputAssemblyPath)) { prms.OutputAssembly = this.OutputAssemblyPath; } CompilerResults results = this.csharp.CompileAssemblyFromDom (prms, new CodeCompileUnit[] { this.codeCompilerUnit }); Console.WriteLine ("Errors Count= {0}", results.Errors.Count); if(results.CompiledAssembly != null) { result.Assemblies.Add(results.CompiledAssembly); } #if DEBUG foreach (CompilerError error in results.Errors) { Console.Error.WriteLine ("\t{0} at file {1}#{2}", error.ErrorText, error.FileName, error.Line); result.CompilationErrors.Add(error); } #endif return result; }
/// <summary> /// Compiles a Template Filer. /// </summary> /// <param name="filePath"> /// A <see cref="System.String"/> /// </param> /// <returns> /// A <see cref="TemplateCompilationResult"/> /// </returns> /// <exception cref="TemplateCompilerNotFoundException">No template compiler was found for the given file extension</exception> public TemplatesCompilationResult Compile(TemplatesCompilationParameters parameters) { if (parameters == null) { throw new ArgumentNullException("parameters"); } TemplatesCompilationResult generalResult = new TemplatesCompilationResult(); Dictionary <string, TemplateCompiler> compilerInstances = new Dictionary <string, TemplateCompiler> (); foreach (TemplateSource source in parameters.Templates) { var fileExtension = normalizeFileExtension(source.TemplateFileExtension); if (!this.fileCompilers.ContainsKey(fileExtension)) { throw new TemplateCompilerNotFoundException(string.Format("No template compiler was found for extension '{0}'", fileExtension)); } TemplateCompiler compiler = null; if (compilerInstances.ContainsKey(fileExtension)) { compiler = compilerInstances[fileExtension]; } else { compiler = TemplateCompiler.CreateInstance(this.fileCompilers[fileExtension]); compilerInstances[fileExtension] = compiler; } compiler.Sources.Add(source); } foreach (TemplateCompiler compiler in compilerInstances.Values) { var compilationResult = compiler.Compile(); foreach (var error in compilationResult.CompilationErrors) { generalResult.CompilationErrors.Add(error); } foreach (var assembly in compilationResult.Assemblies) { generalResult.Assemblies.Add(assembly); extractInstanceDiscoveryProvider(assembly); } } return(generalResult); }