public static void Main(string[] args) { var projectDirectory = args[0]; using (var hostManager = new HostManager(projectDirectory)) { foreach (string arg in args.Skip(1)) { var inputFilePath = args[1]; var outputPath = $"{Path.Combine(Path.GetDirectoryName(inputFilePath), Path.GetFileNameWithoutExtension(inputFilePath))}.generated.cs"; var projectRelativePath = inputFilePath.Substring(projectDirectory.Length); var host = hostManager.CreateHost(inputFilePath, projectRelativePath, CodeDomProvider.CreateProvider("C#"), null); host.Error += (o, eventArgs) => { Console.Error.WriteLine($"In line ({eventArgs.LineNumber}:{eventArgs.ColumnNumber}): {eventArgs.ErrorMessage}"); Environment.Exit(1); }; host.Progress += (o, eventArgs) => { //RenderProgressBar(eventArgs.Completed, (uint) (eventArgs.Total * (args.Length - 1))); }; var content = host.GenerateCode(); var outpuToBytes = ConvertToBytes(content); Console.OpenStandardOutput().Write(outpuToBytes, 0, outpuToBytes.Length); } } }
public void TestTransformerType(string testName, RazorRuntime runtime) { string workingDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); try { using (var razorGenerator = new HostManager(workingDirectory, loadExtensions: false, defaultRuntime: runtime, assemblyDirectory: Environment.CurrentDirectory)) { string inputFile = SaveInputFile(workingDirectory, testName); var host = razorGenerator.CreateHost(inputFile, testName + ".cshtml", string.Empty); host.DefaultNamespace = GetType().Namespace; host.EnableLinePragmas = false; var output = host.GenerateCode(); AssertOutput(testName, output, runtime); } } finally { try { Directory.Delete(workingDirectory); } catch { } } }
private byte[] GenerateFromHost() { var projectDirectory = Path.GetDirectoryName(GetProject().FullName); var projectRelativePath = InputFilePath.Substring(projectDirectory.Length); using (var hostManager = new HostManager(projectDirectory)) { var host = hostManager.CreateHost(InputFilePath, projectRelativePath, GetCodeProvider()); host.DefaultNamespace = FileNameSpace; host.Error += (o, eventArgs) => { GeneratorError(0, eventArgs.ErrorMessage, eventArgs.LineNumber, eventArgs.ColumnNumber); }; host.Progress += (o, eventArgs) => { if (CodeGeneratorProgress != null) { CodeGeneratorProgress.Progress(eventArgs.Completed, eventArgs.Total); } }; var content = host.GenerateCode(); return(ConvertToBytes(content)); } }
private bool ExecuteCore() { if (FilesToPrecompile == null || !FilesToPrecompile.Any()) { return(true); } string projectRoot = String.IsNullOrEmpty(ProjectRoot) ? Directory.GetCurrentDirectory() : ProjectRoot; using (var hostManager = new HostManager(projectRoot)) { foreach (var file in FilesToPrecompile) { string filePath = file.GetMetadata("FullPath"); string fileName = Path.GetFileName(filePath); var projectRelativePath = GetProjectRelativePath(filePath, projectRoot); string itemNamespace = GetNamespace(file, projectRelativePath); string outputPath = Path.Combine(CodeGenDirectory, projectRelativePath.TrimStart(Path.DirectorySeparatorChar)) + ".cs"; if (!RequiresRecompilation(filePath, outputPath)) { Log.LogMessage(MessageImportance.Low, "Skipping file {0} since {1} is already up to date", filePath, outputPath); continue; } EnsureDirectory(outputPath); Log.LogMessage(MessageImportance.Low, "Precompiling {0} at path {1}", filePath, outputPath); var host = hostManager.CreateHost(filePath, projectRelativePath, itemNamespace); bool hasErrors = false; host.Error += (o, eventArgs) => { Log.LogError("RazorGenerator", eventArgs.ErorrCode.ToString(), helpKeyword: "", file: file.ItemSpec, lineNumber: (int)eventArgs.LineNumber, columnNumber: (int)eventArgs.ColumnNumber, endLineNumber: (int)eventArgs.LineNumber, endColumnNumber: (int)eventArgs.ColumnNumber, message: eventArgs.ErrorMessage); hasErrors = true; }; try { string result = host.GenerateCode(); if (!hasErrors) { // If we had errors when generating the output, don't write the file. File.WriteAllText(outputPath, result); } } catch (Exception exception) { Log.LogErrorFromException(exception, showStackTrace: true, showDetail: true, file: null); return(false); } if (hasErrors) { return(false); } var taskItem = new TaskItem(outputPath); taskItem.SetMetadata("AutoGen", "true"); taskItem.SetMetadata("DependentUpon", "fileName"); _generatedFiles.Add(taskItem); } } return(true); }