Beispiel #1
0
        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));
        }
Beispiel #2
0
        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);
            }
        }
        static void Generate(TemplateGenerator host, ProjectFile file, out string outputFile)
        {
            outputFile = null;

            string content;

            try {
                content = File.ReadAllText(file.FilePath);
            }
            catch (IOException ex) {
                host.Errors.Add(new CompilerError {
                    ErrorText = GettextCatalog.GetString("Could not read input file '{0}':\n{1}", file.FilePath, ex)
                });
                return;
            }

            var pt = ParsedTemplate.FromText(content, host);

            if (pt.Errors.HasErrors)
            {
                host.Errors.AddRange(pt.Errors);
                return;
            }

            var settings = TemplatingEngine.GetSettings(host, pt);

            if (pt.Errors.HasErrors)
            {
                host.Errors.AddRange(pt.Errors);
                return;
            }

            outputFile         = file.FilePath.ChangeExtension(settings.Provider.FileExtension);
            settings.Name      = settings.Provider.CreateValidIdentifier(file.FilePath.FileNameWithoutExtension);
            settings.Namespace = CustomToolService.GetFileNamespace(file, outputFile);
            settings.IncludePreprocessingHelpers = string.IsNullOrEmpty(settings.Inherits);
            settings.IsPreprocessed = true;

            var ccu = TemplatingEngine.GenerateCompileUnit(host, content, pt, settings);

            host.Errors.AddRange(pt.Errors);
            if (pt.Errors.HasErrors)
            {
                return;
            }

            try {
                using (var writer = new StreamWriter(outputFile, false, System.Text.Encoding.UTF8)) {
                    settings.Provider.GenerateCodeFromCompileUnit(ccu, writer, new CodeGeneratorOptions());
                }
            }
            catch (IOException ex) {
                host.Errors.Add(new CompilerError {
                    ErrorText = GettextCatalog.GetString("Could not write output file '{0}':\n{1}", outputFile, ex)
                });
            }
        }
        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 IAsyncOperation Generate(IProgressMonitor monitor, ProjectFile file, SingleFileCustomToolResult result)
        {
            return(new ThreadAsyncOperation(delegate {
                var host = new ProjectFileTemplatingHost(file);

                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 = CustomToolService.GetFileNamespace(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));
        }
Beispiel #6
0
        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);
                    }
                }
            }));
        }
Beispiel #7
0
        public IAsyncOperation Generate(IProgressMonitor monitor, ProjectFile file, SingleFileCustomToolResult result)
        {
            return(new ThreadAsyncOperation(delegate {
                using (var host = new ProjectFileTemplatingHost(file)) {
                    host.AddMonoDevelopHostImport();
                    var defaultOutputName = file.FilePath.ChangeExtension(".cs");                      //cs extension for VS compat

                    string ns = CustomToolService.GetFileNamespace(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);
                    }
                }
            }, result));
        }