/// <summary> /// Executes the compilation process. /// </summary> /// <param name="compilation">The compilation object.</param> /// <param name="outputStream">The output stream to write to.</param> /// <returns>The compilation result.</returns> public CompilationResult Execute(CompilationRequest input, Stream outputStream) { var context = new CompilationContext { Input = input, OutputStream = outputStream, Result = new CompilationResult() }; // TODO: move model registry into // compilation context instance // this locks the current scope ModelRegistry.BeginRegistration(); CompilationContext.Current = context; var profiler = new ProcessProfiler(); try { foreach (var proc in _processes) { profiler.Execute(proc, context); if (context.Result.HasErrors) { break; } } } catch (CompilationException compEx) { context.Result.AddError(compEx.ToErrorMessage()); } catch (Exception ex) { context.Result.AddError("Unexpected Error: " + ex.Message); } finally { // release lock ModelRegistry.EndRegistration(); CompilationContext.Current = null; profiler.End(); } return(context.Result); }
// non specific compilation creation private CommonCompilation CreateCompilation(CompilationRequest input) { if (input.SourceFilePaths == null || !input.SourceFilePaths.Any()) return null; if (input.SourceFilePaths.All(p => p.EndsWith(".cs", StringComparison.OrdinalIgnoreCase))) { // compile as C# compilation return CreateCSharpCompilation(input); } // list the invalid (non .cs) extension file types var invalidFiles = input.SourceFilePaths.Where(p => !p.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)); throw new CompilationException("The following invalid file types were encountered: " + String.Join(", ", invalidFiles.Select(f => Path.GetExtension(f)))); }
/// <summary> /// Executes the compilation process. /// </summary> /// <param name="compilation">The compilation object.</param> /// <param name="outputStream">The output stream to write to.</param> /// <returns>The compilation result.</returns> public CompilationResult Execute(CompilationRequest input, Stream outputStream) { var context = new CompilationContext { Input = input, OutputStream = outputStream, Result = new CompilationResult() }; // TODO: move model registry into // compilation context instance // this locks the current scope ModelRegistry.BeginRegistration(); CompilationContext.Current = context; var profiler = new ProcessProfiler(); try { foreach (var proc in _processes) { profiler.Execute(proc, context); if (context.Result.HasErrors) break; } } catch (CompilationException compEx) { context.Result.AddError(compEx.ToErrorMessage()); } catch (Exception ex) { context.Result.AddError("Unexpected Error: " + ex.Message); } finally { // release lock ModelRegistry.EndRegistration(); CompilationContext.Current = null; profiler.End(); } return context.Result; }
/// <summary> /// Compiles the specified input object. /// </summary> /// <param name="input">The compiler input information.</param> /// <returns>The compilation result.</returns> public CompilationResult Compile(CompilationRequest input) { if (input == null) throw new ArgumentNullException("input", "The compilation input cannot be null."); var pipeline = new CompilationPipeline(); pipeline.AddProcess(new BuildVerificationProcess()); pipeline.AddProcess(new SyntaxParsingProcess()); pipeline.AddProcess(new SyntaxValidationProcess()); pipeline.AddProcess(new PreprocessingProcess()); pipeline.AddProcess(new TransformationProcess()); pipeline.AddProcess(new ModelValidationProcess()); pipeline.AddProcess(new TranslationProcess()); using (var stream = File.Create(input.TargetPath + ".js")) { return pipeline.Execute(input, stream); } }
// non specific compilation creation private CommonCompilation CreateCompilation(CompilationRequest input) { if (input.SourceFilePaths == null || !input.SourceFilePaths.Any()) { return(null); } if (input.SourceFilePaths.All(p => p.EndsWith(".cs", StringComparison.OrdinalIgnoreCase))) { // compile as C# compilation return(CreateCSharpCompilation(input)); } // list the invalid (non .cs) extension file types var invalidFiles = input.SourceFilePaths.Where(p => !p.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)); throw new CompilationException("The following invalid file types were encountered: " + String.Join(", ", invalidFiles.Select(f => Path.GetExtension(f)))); }
// create a C# compilation private Roslyn.Compilers.CSharp.Compilation CreateCSharpCompilation(CompilationRequest input) { // create compiler options var compileOpts = new Roslyn.Compilers.CSharp.CompilationOptions(OutputKind.DynamicallyLinkedLibrary); // create parser options var parseOpts = Roslyn.Compilers.CSharp.ParseOptions.Default; if (input.Definitions != null && input.Definitions.Any()) parseOpts = parseOpts.WithPreprocessorSymbols(input.Definitions); // build syntax trees var trees = input.SourceFilePaths.Select(path => CSharpTree.ParseCompilationUnit(File.ReadAllText(path), path, parseOpts)); // create assembly references var asmRefs = input.ReferencePaths.Select(p => new Roslyn.Compilers.AssemblyFileReference(p)); // create compilation return CSharpCompilation.Create("BladeCsComp.dll", compileOpts, trees, asmRefs); }
/// <summary> /// Compiles the specified input object. /// </summary> /// <param name="input">The compiler input information.</param> /// <returns>The compilation result.</returns> public CompilationResult Compile(CompilationRequest input) { if (input == null) { throw new ArgumentNullException("input", "The compilation input cannot be null."); } var pipeline = new CompilationPipeline(); pipeline.AddProcess(new BuildVerificationProcess()); pipeline.AddProcess(new SyntaxParsingProcess()); pipeline.AddProcess(new SyntaxValidationProcess()); pipeline.AddProcess(new PreprocessingProcess()); pipeline.AddProcess(new TransformationProcess()); pipeline.AddProcess(new ModelValidationProcess()); pipeline.AddProcess(new TranslationProcess()); using (var stream = File.Create(input.TargetPath + ".js")) { return(pipeline.Execute(input, stream)); } }
// create a C# compilation private Roslyn.Compilers.CSharp.Compilation CreateCSharpCompilation(CompilationRequest input) { // create compiler options var compileOpts = new Roslyn.Compilers.CSharp.CompilationOptions(OutputKind.DynamicallyLinkedLibrary); // create parser options var parseOpts = Roslyn.Compilers.CSharp.ParseOptions.Default; if (input.Definitions != null && input.Definitions.Any()) { parseOpts = parseOpts.WithPreprocessorSymbols(input.Definitions); } // build syntax trees var trees = input.SourceFilePaths.Select(path => CSharpTree.ParseCompilationUnit(File.ReadAllText(path), path, parseOpts)); // create assembly references var asmRefs = input.ReferencePaths.Select(p => new Roslyn.Compilers.AssemblyFileReference(p)); // create compilation return(CSharpCompilation.Create("BladeCsComp.dll", compileOpts, trees, asmRefs)); }