static void GenerateInternal(IProgressMonitor monitor, ProjectFile file, SingleFileCustomToolResult result) { if (file.Project.SupportedLanguages.All(l => l != "C#")) { const string msg = "Razor templates are only supported in C# projects"; result.Errors.Add(new CompilerError(file.Name, -1, -1, null, msg)); monitor.Log.WriteLine(msg); return; } var host = new PreprocessedRazorHost(file.FilePath); var defaultOutputName = file.FilePath.ChangeExtension(".cs"); var ns = CustomToolService.GetFileNamespace(file, defaultOutputName); host.DefaultNamespace = ns; CompilerErrorCollection errors; var code = host.GenerateCode(out errors); result.Errors.AddRange(errors); var writer = new MonoDevelop.DesignerSupport.CodeBehindWriter(); writer.WriteFile(defaultOutputName, code); writer.WriteOpenFiles(); result.GeneratedFilePath = defaultOutputName; foreach (var err in result.Errors) { monitor.Log.WriteLine(err); } }
private void HandleException(Exception ex, ProjectFile file, SingleFileCustomToolResult result) { if (ex is SpecFlowParserException) { SpecFlowParserException sfpex = (SpecFlowParserException)ex; if (sfpex.ErrorDetails == null || sfpex.ErrorDetails.Count == 0) { result.UnhandledException = ex; } else { var compilerErrors = new CompilerErrorCollection(); foreach (var errorDetail in sfpex.ErrorDetails) { var compilerError = new CompilerError(file.Name, errorDetail.ForcedLine, errorDetail.ForcedColumn, "0", errorDetail.Message); compilerErrors.Add(compilerError); } result.Errors.AddRange(compilerErrors); } } else { result.UnhandledException = ex; } }
public async Task Generate( ProgressMonitor monitor, ProjectFile file, SingleFileCustomToolResult result) { Bootstrapper.Initialize(); using var traceListener = new DisposableTraceListener( new LoggingServiceTraceListener( new ProgressMonitorLoggingService(monitor, "Generating code..."))); var swaggerFile = file.FilePath; var outputFile = swaggerFile.ChangeExtension(".cs"); result.GeneratedFilePath = outputFile; var customToolNamespace = file.CustomToolNamespace; if (string.IsNullOrWhiteSpace(customToolNamespace)) { customToolNamespace = CustomToolService.GetFileNamespace(file, outputFile); } var generator = GetCodeGenerator(swaggerFile, customToolNamespace); var progressReporter = new ProgressReporter(monitor); var contents = await Task.Run(() => generator.GenerateCode(progressReporter)); await Task.Run(() => File.WriteAllText(outputFile, contents)); }
public IAsyncOperation Generate(IProgressMonitor monitor, ProjectFile file, SingleFileCustomToolResult result) { return(new ThreadAsyncOperation(delegate { try { GenerateInternal(monitor, file, result); } catch (Exception ex) { result.UnhandledException = ex; } }, result)); }
public Task Generate(ProgressMonitor monitor, ProjectFile file, SingleFileCustomToolResult result) { return(Task.Run(delegate { try { GenerateInternal(monitor, file, result); } catch (Exception ex) { result.UnhandledException = ex; } })); }
public ThreadAsyncOperation(Action task, SingleFileCustomToolResult result) { if (result == null) { throw new ArgumentNullException("result"); } Task = task; Result = result; Thread = new Thread(Run); Thread.Start(); }
public IAsyncOperation Generate(IProgressMonitor monitor, ProjectFile file, SingleFileCustomToolResult result) { return(new ThreadAsyncOperation(() => { FilePath codeFilePath = file.FilePath.ChangeExtension(".feature.cs"); try { codeFilePath = GenerateFeatureCodeFileFor(file); } catch (Exception ex) { HandleException(ex, file, result); } result.GeneratedFilePath = codeFilePath; }, result)); }
public async Task Generate( ProgressMonitor monitor, ProjectFile file, SingleFileCustomToolResult result) { string generatorName; if (GetType() == typeof(OpenApiSingleFileCustomTool)) { generatorName = "OpenAPI Generator"; } else if (GetType() == typeof(SwaggerSingleFileCustomTool)) { generatorName = "Swagger Codegen CLI"; } else { generatorName = GetType().Name.Replace("SingleFileCustomTool", string.Empty); } Logger.Instance.TrackFeatureUsage(generatorName, "VSMac"); Bootstrapper.Initialize(); var swaggerFile = file.FilePath; var outputFile = swaggerFile.ChangeExtension(".cs"); result.GeneratedFilePath = outputFile; using var traceListener = new DisposableTraceListener( new LoggingServiceTraceListener( new ProgressMonitorLoggingService(monitor, "Generating code..."))); var customToolNamespace = file.CustomToolNamespace; if (string.IsNullOrWhiteSpace(customToolNamespace)) { customToolNamespace = CustomToolService.GetFileNamespace(file, outputFile); } var generator = GetCodeGenerator(swaggerFile, customToolNamespace); var progressReporter = new ProgressReporter(monitor); var contents = await Task.Run(() => generator.GenerateCode(progressReporter)); await Task.Run(() => File.WriteAllText(outputFile, contents)); }
public Task Generate(ProgressMonitor monitor, ProjectFile file, SingleFileCustomToolResult result) { return(Task.Run(delegate { using (var host = new ProjectFileTemplatingHost(file, IdeApp.Workspace.ActiveConfiguration)) { string outputFile; Generate(host, file, out outputFile); result.GeneratedFilePath = outputFile; result.Errors.AddRange(host.Errors); foreach (var err in host.Errors) { monitor.Log.WriteLine(err); } } })); }
public IAsyncOperation Generate(IProgressMonitor monitor, ProjectFile file, SingleFileCustomToolResult result) { return(new ThreadAsyncOperation(delegate { using (var h = TextTemplatingService.GetTemplatingDomain()) { var host = (MonoDevelopTemplatingHost)h.Domain.CreateInstanceAndUnwrap( typeof(MonoDevelopTemplatingHost).Assembly.FullName, typeof(MonoDevelopTemplatingHost).FullName); var defaultOutputName = file.FilePath.ChangeExtension(".cs"); //cs extension for VS compat host.ProcessTemplate(file.FilePath, defaultOutputName); result.GeneratedFilePath = host.OutputFile; result.Errors.AddRange(host.Errors); foreach (var err in host.Errors) { monitor.Log.WriteLine(err.ToString()); } } }, result)); }
public IAsyncOperation Generate(IProgressMonitor monitor, ProjectFile file, SingleFileCustomToolResult result) { return(new ThreadAsyncOperation(delegate { var host = new MonoDevelopTemplatingHost(); var dnp = file.Project as DotNetProject; if (dnp == null) { var msg = "Precompiled T4 templates are only supported in .NET projects"; result.Errors.Add(new CompilerError(file.Name, -1, -1, null, msg)); monitor.Log.WriteLine(msg); return; } var provider = dnp.LanguageBinding.GetCodeDomProvider(); if (provider == null) { var msg = "Precompiled T4 templates are only supported for .NET languages with CodeDOM providers"; result.Errors.Add(new CompilerError(file.Name, -1, -1, null, msg)); monitor.Log.WriteLine(msg); return; } ; var outputFile = file.FilePath.ChangeExtension(provider.FileExtension); var encoding = System.Text.Encoding.UTF8; string langauge; string[] references; string className = provider.CreateValidIdentifier(file.FilePath.FileNameWithoutExtension); string classNamespace = GetNamespaceHint(file, outputFile); LogicalSetData("NamespaceHint", classNamespace, result.Errors); host.PreprocessTemplate(file.FilePath, className, classNamespace, outputFile, encoding, out langauge, out references); result.GeneratedFilePath = outputFile; result.Errors.AddRange(host.Errors); foreach (var err in host.Errors) { monitor.Log.WriteLine(err.ToString()); } }, result)); }
public async Task BuildDotNetCoreProjectAfterGeneratingResources(string projectName) { FilePath solFile = Util.GetSampleProject("DotNetCoreResources", "DotNetCoreResources.sln"); var sol = (Solution)await Services.ProjectService.ReadWorkspaceItem(Util.GetMonitor(), solFile); var p = (DotNetProject)sol.Items.FirstOrDefault(item => item.Name == projectName); p.RequiresMicrosoftBuild = true; var resourceFile = p.Files.FirstOrDefault(f => f.FilePath.FileName == "Resources.resx"); var customToolResult = new SingleFileCustomToolResult(); await ResXFileCodeGenerator.GenerateFile(resourceFile, customToolResult, true); Assert.IsTrue(customToolResult.Success); // Running a restore for a .NET Core project can take a long time if // no packages are cached. So instead we just check the generated resource file. //var res = await p.RunTarget (Util.GetMonitor (), "Restore", ConfigurationSelector.Default); //Assert.AreEqual (0, res.BuildResult.Errors.Count); //res = await p.RunTarget (Util.GetMonitor (), "Build", ConfigurationSelector.Default); //Assert.AreEqual (0, res.BuildResult.Errors.Count); var generatedResourceFile = resourceFile.FilePath.ChangeExtension(".Designer.cs"); bool foundLine = false; foreach (string line in File.ReadAllLines(generatedResourceFile)) { if (line.Contains("typeof")) { string lineWithoutSpaces = line.Replace(" ", ""); foundLine = lineWithoutSpaces.EndsWith("typeof(Resources).GetTypeInfo().Assembly);", StringComparison.Ordinal); break; } } Assert.IsTrue(foundLine); sol.Dispose(); }
public async Task Generate(ProgressMonitor monitor, ProjectFile featureFile, SingleFileCustomToolResult result) { await Task.Run(() => { var ideSingleFileGenerator = new IdeSingleFileGenerator(); ideSingleFileGenerator.GenerationError += delegate(TestGenerationError error) { result.Errors.Add(new CompilerError(featureFile.Name, error.Line + 1, error.LinePosition + 1, "0", error.Message)); }; ideSingleFileGenerator.OtherError += delegate(Exception exception) { result.UnhandledException = exception; }; string outputFilePath = ideSingleFileGenerator.GenerateFile(featureFile.FilePath, null, () => new MonoDevelopGeneratorServices(featureFile.Project)); result.GeneratedFilePath = outputFilePath; }); }
public IAsyncOperation Generate(IProgressMonitor monitor, ProjectFile file, SingleFileCustomToolResult result) { return(new ThreadAsyncOperation(delegate { using (var host = new MonoDevelopTemplatingHost()) { var defaultOutputName = file.FilePath.ChangeExtension(".cs"); //cs extension for VS compat string ns = TextTemplatingFilePreprocessor.GetNamespaceHint(file, defaultOutputName); TextTemplatingFilePreprocessor.LogicalSetData("NamespaceHint", ns, result.Errors); host.ProcessTemplate(file.FilePath, defaultOutputName); result.GeneratedFilePath = host.OutputFile; result.Errors.AddRange(host.Errors); foreach (var err in host.Errors) { monitor.Log.WriteLine(err.ToString()); } } }, result)); }
public Task Generate(ProgressMonitor monitor, ProjectFile file, SingleFileCustomToolResult result) { return(Task.Run(delegate { using (var host = new ProjectFileTemplatingHost(file, IdeApp.Workspace.ActiveConfiguration)) { host.AddMonoDevelopHostImport(); var defaultOutputName = file.FilePath.ChangeExtension(".cs"); //cs extension for VS compat string ns = CustomToolService.GetFileNamespace(file, defaultOutputName); LogicalSetData("NamespaceHint", ns); host.ProcessTemplate(file.FilePath, defaultOutputName); result.GeneratedFilePath = host.OutputFile; result.Errors.AddRange(host.Errors); foreach (var err in host.Errors) { monitor.Log.WriteLine(err); } } })); }
void GenerateInternal(IProgressMonitor monitor, ProjectFile file, SingleFileCustomToolResult result) { var dnp = file.Project as DotNetProject; if (dnp == null || dnp.LanguageName != "C#") { var msg = "Razor templates are only supported in C# projects"; result.Errors.Add(new CompilerError(file.Name, -1, -1, null, msg)); monitor.Log.WriteLine(msg); return; } var host = PreprocessedRazorHost.Create(file.FilePath); var defaultOutputName = file.FilePath.ChangeExtension(".cs"); var ns = GetNamespaceHint(file, defaultOutputName); host.DefaultNamespace = ns; CompilerErrorCollection errors; var code = host.GenerateCode(out errors); result.Errors.AddRange(errors); var writer = new MonoDevelop.DesignerSupport.CodeBehindWriter(); writer.WriteFile(defaultOutputName, code); writer.WriteOpenFiles(); result.GeneratedFilePath = defaultOutputName; foreach (var err in result.Errors) { monitor.Log.WriteLine(err.ToString()); } }
public async Task Generate(ProgressMonitor monitor, ProjectFile featureFile, SingleFileCustomToolResult result) { await Task.Run(() => { // This is the project directory containing the .imfl file string dir = System.IO.Path.GetDirectoryName(featureFile.FilePath); monitor.Log.WriteLine("Creating images for " + featureFile.FilePath); var processor = new ImageProcessor(monitor, result); var lines = System.IO.File.ReadLines(featureFile.FilePath); int lineNumber = 0; var outputSpecifiers = new List <OutputSpecifier>(); foreach (var line in lines) { lineNumber++; if (String.IsNullOrWhiteSpace(line)) { continue; } if (line.StartsWith("#")) { continue; } //monitor.Log.WriteLine("Interpreting " + line); try { // Slight hack ... if (line.Contains("type:")) { var ts = JsonConvert.DeserializeObject <OutputSpecifier>(line); if (ts != null) { string testPath = processor.GetFullOutputPath(dir, ts, "test.svg"); var directory = Path.GetDirectoryName(testPath); if (!Directory.Exists(directory)) { string shortDirectory = Path.GetDirectoryName(string.Format(ts.path, "x.png")); result.Errors.Add(new CompilerError(featureFile.FilePath, lineNumber, 1, "Err17", "Directory not found " + shortDirectory)); } else { outputSpecifiers.Add(ts); monitor.Log.WriteLine("Added output specifier " + line); } } else { result.Errors.Add(new CompilerError(featureFile.FilePath, lineNumber, 1, "Err2", "Could not parse output specifier")); } } else if (line.Contains("process:")) { var ps = JsonConvert.DeserializeObject <ProcessSpecifier>(line); if (ps != null) { // Process output var subdir = Path.GetDirectoryName(ps.process); var searchPattern = Path.GetFileName(ps.process); var inputDirectory = Path.Combine(dir, subdir); if (Directory.Exists(inputDirectory)) { var outputters = outputSpecifiers.Where(s => [email protected](s.type)); processor.Process(dir, Directory.GetFiles(inputDirectory, searchPattern), outputters, lineNumber); } else { result.Errors.Add(new CompilerError(featureFile.FilePath, lineNumber, 1, "Err17", "Directory not found " + subdir)); } } else { result.Errors.Add(new CompilerError(featureFile.FilePath, lineNumber, 1, "Err2", "Could not parse process specifier")); } } else { result.Errors.Add(new CompilerError(featureFile.FilePath, lineNumber, 1, "Err2", "Could not parse this line")); } } catch (Exception ex) { result.Errors.Add(new CompilerError(featureFile.FilePath, lineNumber, 1, "Err1", ex.ToString())); } } result.GeneratedFilePath = ""; }); }
public ImageProcessor(ProgressMonitor monitor, SingleFileCustomToolResult result) { this.monitor = monitor; this.result = result; }