public async Task <IHttpActionResult> PostPublishAsync(RouteIdentifier identifier) { // Load route to get source code. var route = await _routeRepository.GetRouteByIdentifierAsync(identifier); var code = route.Code; // Compile code and get assembly as byte array. var compilationResult = _compilationService.Compile(code); // We'll save the assembly and mark the route as published if the compilation was successful. if (compilationResult.Success) { route.Assembly = compilationResult.Assembly; route.IsCurrent = true; route.IsOnline = true; route.PublishedOn = DateTimeOffset.UtcNow; route = await _routeRepository.UpdateRouteAsync(route); } var result = new PublishResponse { Compilation = compilationResult, Route = RouteResponse.Map(route) }; // Return an error response if compile was unsuccessful, otherwise the response was successful. return(Content(compilationResult.Success ? HttpStatusCode.OK : HttpStatusCode.InternalServerError, result)); }
private TemplateFactoryResult Compile(RazorCodeDocument document) { var content = document.GetCSharpDocument().GeneratedCode; var result = _compilationService.Compile(content); return(result.Messages.Any() ? CompilationError(document, result) : TemplateFactoryResult.Ok(result.CompiledType)); }
private async Task <CompilationResult> CompileCore(IFileInfo file) { var host = new MvcRazorHost(); var engine = new RazorTemplateEngine(host); GeneratorResults results; using (TextReader rdr = new StreamReader(file.CreateReadStream())) { results = engine.GenerateCode(rdr, '_' + Path.GetFileNameWithoutExtension(file.Name), "Asp", file.PhysicalPath ?? file.Name); } string generatedCode; using (var writer = new StringWriter()) using (var codeProvider = new CSharpCodeProvider()) { codeProvider.GenerateCodeFromCompileUnit(results.GeneratedCode, writer, new CodeGeneratorOptions()); generatedCode = writer.ToString(); } if (!results.Success) { return(CompilationResult.Failed(generatedCode, results.ParserErrors.Select(e => new CompilationMessage(e.Message)))); } Directory.CreateDirectory(_tempPath); string tempFile = Path.Combine(_tempPath, Path.GetRandomFileName() + ".cs"); File.WriteAllText(tempFile, generatedCode); _tempFileSystem.TryGetFileInfo(tempFile, out file); return(await _baseCompilationService.Compile(file)); }
/// <inheritdoc /> public CompilationResult Compile(RelativeFileInfo file) { if (file == null) { throw new ArgumentNullException(nameof(file)); } GeneratorResults results; using (var inputStream = file.FileInfo.CreateReadStream()) { _logger.RazorFileToCodeCompilationStart(file.RelativePath); var startTimestamp = _logger.IsEnabled(LogLevel.Debug) ? Stopwatch.GetTimestamp() : 0; results = GenerateCode(file.RelativePath, inputStream); _logger.RazorFileToCodeCompilationEnd(file.RelativePath, startTimestamp); } if (!results.Success) { return(GetCompilationFailedResult(file, results.ParserErrors)); } return(_compilationService.Compile(file, results.GeneratedCode)); }
public virtual async Task <TemplateResult> RunTemplateAsync([NotNull] string content, [NotNull] dynamic templateModel, CancellationToken cancellationToken = default(CancellationToken)) { Check.NotNull(content, nameof(content)); Check.NotNull(templateModel, nameof(templateModel)); cancellationToken.ThrowIfCancellationRequested(); var host = new RazorTemplatingHost(typeof(RazorReverseEngineeringBase)); var engine = new RazorTemplateEngine(host); using (var reader = new StringReader(content)) { var generatorResults = engine.GenerateCode(reader); if (!generatorResults.Success) { var messages = generatorResults.ParserErrors.Select(e => e.Message); return(new TemplateResult { GeneratedText = string.Empty, ProcessingException = new TemplateProcessingException(messages) }); } var references = _metadataReferencesProvider.GetApplicationReferences(); var templateResult = _compilationService.Compile( new List <string> { generatorResults.GeneratedCode }, references); if (templateResult.Messages.Any()) { return(new TemplateResult { GeneratedText = string.Empty, ProcessingException = new TemplateProcessingException(templateResult.Messages) }); } var compiledObject = Activator.CreateInstance(templateResult.CompiledType); var razorTemplate = compiledObject as RazorReverseEngineeringBase; var result = String.Empty; if (razorTemplate != null) { razorTemplate.Model = templateModel; razorTemplate.ModelUtilities = _modelUtilities; razorTemplate.CSharpUtilities = _csharpUtiliies; //ToDo: If there are errors executing the code, they are missed here. result = await razorTemplate.ExecuteTemplateAsync(cancellationToken); } return(new TemplateResult { GeneratedText = result, ProcessingException = null }); } }
public IHttpActionResult PostCompile(CompileRequest request) { var source = new Source(request.Code, request.Dependencies); var compilationResult = _compilationService.Compile(source); // Return an error response if compile was unsuccessful, otherwise the response was successful. return(Content(compilationResult.Success ? HttpStatusCode.OK : HttpStatusCode.InternalServerError, compilationResult)); }
public async Task <TemplateResult> RunTemplateAsync(string content, dynamic templateModel) { // Don't care about the RazorProject as we already have the content of the .cshtml file // and don't need to deal with imports. var fileSystem = RazorProjectFileSystem.Create(Directory.GetCurrentDirectory()); var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem, (builder) => { RazorExtensions.Register(builder); builder.AddDefaultImports(@" @using System @using System.Threading.Tasks "); }); var templateItem = new TemplateRazorProjectItem(content); var codeDocument = projectEngine.Process(templateItem); var generatorResults = codeDocument.GetCSharpDocument(); if (generatorResults.Diagnostics.Any()) { var messages = generatorResults.Diagnostics.Select(d => d.GetMessage()); return(new TemplateResult() { GeneratedText = string.Empty, ProcessingException = new TemplateProcessingException(messages, generatorResults.GeneratedCode) }); } var templateResult = _compilationService.Compile(generatorResults.GeneratedCode); if (templateResult.Messages.Any()) { return(new TemplateResult() { GeneratedText = string.Empty, ProcessingException = new TemplateProcessingException(templateResult.Messages, generatorResults.GeneratedCode) }); } var compiledObject = Activator.CreateInstance(templateResult.CompiledType); var razorTemplate = compiledObject as RazorTemplateBase; string result = String.Empty; if (razorTemplate != null) { razorTemplate.Model = templateModel; //ToDo: If there are errors executing the code, they are missed here. result = await razorTemplate.ExecuteTemplate(); } return(new TemplateResult() { GeneratedText = result, ProcessingException = null }); }
public async Task <IHttpActionResult> PostCompileAsync() { var code = await Request.Content.ReadAsStringAsync(); var compilationResult = _compilationService.Compile(code); // Return an error response if compile was unsuccessful, otherwise the response was successful. return(Content(compilationResult.Success ? HttpStatusCode.OK : HttpStatusCode.InternalServerError, compilationResult)); }
public async Task <IView> CreateInstance(string virtualPath) { IFileInfo fileInfo; if (_fileSystem.TryGetFileInfo(virtualPath, out fileInfo)) { CompilationResult result = await _compilationService.Compile(fileInfo); return((IView)Activator.CreateInstance(result.CompiledType)); } return(null); }
public virtual async Task <TemplateResult> RunTemplateAsync(string content, dynamic templateModel, IDatabaseMetadataModelProvider provider, CancellationToken cancellationToken = default(CancellationToken)) { RazorTemplatingHost host = new RazorTemplatingHost(typeof(RazorReverseEngineeringBase)); RazorTemplateEngine engine = new RazorTemplateEngine(host); using (var reader = new StringReader(content)) { var generatorResults = engine.GenerateCode(reader); if (!generatorResults.Success) { var messages = generatorResults.ParserErrors.Select(e => e.Message); return(new TemplateResult() { GeneratedText = string.Empty, ProcessingException = new TemplateProcessingException(messages, generatorResults.GeneratedCode) }); } provider.AddReferencesForTemplates(_metadataReferencesProvider); var references = _metadataReferencesProvider.GetApplicationReferences(); var templateResult = _compilationService.Compile(generatorResults.GeneratedCode, references); if (templateResult.Messages.Any()) { return(new TemplateResult() { GeneratedText = string.Empty, ProcessingException = new TemplateProcessingException(templateResult.Messages, generatorResults.GeneratedCode) }); } var compiledObject = Activator.CreateInstance(templateResult.CompiledType); var razorTemplate = compiledObject as RazorReverseEngineeringBase; string result = String.Empty; if (razorTemplate != null) { razorTemplate.Model = templateModel; //ToDo: If there are errors executing the code, they are missed here. result = await razorTemplate.ExecuteTemplateAsync(); } return(new TemplateResult() { GeneratedText = result, ProcessingException = null }); } }
/// <inheritdoc /> public CompilationResult Compile([NotNull] RelativeFileInfo file) { GeneratorResults results; using (var inputStream = file.FileInfo.CreateReadStream()) { results = _razorHost.GenerateCode(file.RelativePath, inputStream); } if (!results.Success) { return(GetCompilationFailedResult(file, results.ParserErrors)); } return(_compilationService.Compile(file, results.GeneratedCode)); }
/// <inheritdoc /> public CompilationResult Compile([NotNull] RelativeFileInfo file) { GeneratorResults results; using (var inputStream = file.FileInfo.CreateReadStream()) { results = _razorHost.GenerateCode( file.RelativePath, inputStream); } if (!results.Success) { var messages = results.ParserErrors.Select(e => new CompilationMessage(e.Message)); return(CompilationResult.Failed(file.FileInfo, results.GeneratedCode, messages)); } return(_compilationService.Compile(file.FileInfo, results.GeneratedCode)); }
internal CompilationResult CompileCore(IFileInfo file) { GeneratorResults results; using (var inputStream = file.CreateReadStream()) { Contract.Assert(file.PhysicalPath.StartsWith(_appRoot, StringComparison.OrdinalIgnoreCase)); var rootRelativePath = file.PhysicalPath.Substring(_appRoot.Length); results = _razorHost.GenerateCode(rootRelativePath, inputStream); } if (!results.Success) { var messages = results.ParserErrors.Select(e => new CompilationMessage(e.Message)); return(CompilationResult.Failed(file, results.GeneratedCode, messages)); } return(_baseCompilationService.Compile(file, results.GeneratedCode)); }
/// <inheritdoc /> public CompilationResult Compile(RelativeFileInfo file) { if (file == null) { throw new ArgumentNullException(nameof(file)); } GeneratorResults results; using (var inputStream = file.FileInfo.CreateReadStream()) { results = GenerateCode(file.RelativePath, inputStream); } if (!results.Success) { return(GetCompilationFailedResult(file, results.ParserErrors)); } return(_compilationService.Compile(file, results.GeneratedCode)); }
/// <inheritdoc /> public CompilationResult Compile([NotNull] RelativeFileInfo file) { GeneratorResults results; using (var inputStream = file.FileInfo.CreateReadStream()) { results = _razorHost.GenerateCode( file.RelativePath, inputStream); } if (!results.Success) { var messages = results.ParserErrors .Select(parseError => new RazorCompilationMessage(parseError, file.RelativePath)); var failure = new RazorCompilationFailure( file.RelativePath, ReadFileContentsSafely(file.FileInfo), messages); return(CompilationResult.Failed(failure)); } return(_compilationService.Compile(file, results.GeneratedCode)); }
protected override async Task <TemplateResult> ProcessSingleTemplate(string content, dynamic templateModel) { var razorEngine = RazorEngine.Create((builder) => { RazorExtensions.Register(builder); }); // Don't care about the RazorProject as we already have the content of the .cshtml file // and don't need to deal with imports. var razorProject = RazorProject.Create(Directory.GetCurrentDirectory()); var razorTemplateEngine = new RazorTemplateEngine(razorEngine, razorProject); var imports = new RazorSourceDocument[] { RazorSourceDocument.Create(@" @using System @using System.Threading.Tasks ", fileName: null) }; var razorDocument = RazorCodeDocument.Create(RazorSourceDocument.Create(content, "Template"), imports); var generatorResults = razorTemplateEngine.GenerateCode(razorDocument); if (generatorResults.Diagnostics.Any()) { var messages = generatorResults.Diagnostics.Select(d => d.GetMessage()); return(new TemplateResult() { GeneratedText = string.Empty, ProcessingException = new TemplateProcessingException(messages, generatorResults.GeneratedCode) }); } var templateResult = _compilationService.Compile(generatorResults.GeneratedCode); if (templateResult.Messages.Any()) { return(new TemplateResult() { GeneratedText = string.Empty, ProcessingException = new TemplateProcessingException(templateResult.Messages, generatorResults.GeneratedCode) }); } var compiledObject = Activator.CreateInstance(templateResult.CompiledType); var razorTemplate = compiledObject as RazorTemplateBase; string result = String.Empty; if (razorTemplate != null) { razorTemplate.Model = templateModel; //ToDo: If there are errors executing the code, they are missed here. result = await razorTemplate.ExecuteTemplate(); } return(new TemplateResult() { GeneratedText = result, ProcessingException = null }); }