コード例 #1
0
        private GenerationResult GenerateModel <T>(T parameter, string parameterKey, string t4TemplateName, IDictionary <string, bool> extraParams = null) where T : IHasName
        {
            host.TemplateFileValue = Path.Combine(TemplatesFolder, t4TemplateName);
            var extensionPath = Path.GetDirectoryName(GetType().Assembly.Location) + Path.DirectorySeparatorChar;

            // Read the T4 from disk into memory
            var templateFileContent = File.ReadAllText(Path.Combine(TemplatesFolder, t4TemplateName));

            templateFileContent = templateFileContent.Replace("$(binDir)", extensionPath);
            templateFileContent = templateFileContent.Replace("$(ramlFile)", targetFileName.Replace("\\", "\\\\"));
            templateFileContent = templateFileContent.Replace("$(namespace)", targetNamespace);

            host.Session = host.CreateSession();
            host.Session[parameterKey] = parameter;

            if (extraParams != null)
            {
                foreach (var extraParam in extraParams)
                {
                    host.Session[extraParam.Key] = extraParam.Value;
                }
            }

            var output = engine.ProcessTemplate(templateFileContent, host);

            return(new GenerationResult
            {
                Content = output,
                Encoding = host.FileEncoding,
                Errors = host.Errors
            });
        }
コード例 #2
0
        public void Generate(RamlDocument ramlDoc, string targetFileName, string targetNamespace, string templatesFolder,
                             string destinationFolder)
        {
            templatesFolder = string.IsNullOrWhiteSpace(templatesFolder)
                ? GetTemplateDefaultPath()
                : templatesFolder;


            var model = new ClientGeneratorService(ramlDoc, targetFileName + "Client", targetNamespace).BuildModel();

            var templateFilePath = Path.Combine(templatesFolder, ClientT4TemplateName);
            var extensionPath    = Path.GetDirectoryName(GetType().Assembly.Location) + Path.DirectorySeparatorChar;

            var host   = new CustomCmdLineHost();
            var engine = new Engine();

            host.TemplateFileValue = templateFilePath;

            // Read the T4 from disk into memory
            var templateFileContent = File.ReadAllText(templateFilePath);

            templateFileContent = templateFileContent.Replace("$(binDir)", extensionPath);
            templateFileContent = templateFileContent.Replace("$(ramlFile)", targetFileName.Replace("\\", "\\\\"));
            templateFileContent = templateFileContent.Replace("$(namespace)", targetNamespace);

            host.Session          = host.CreateSession();
            host.Session["model"] = model;

            var output = engine.ProcessTemplate(templateFileContent, host);

            foreach (CompilerError error in host.Errors)
            {
                Console.WriteLine(error.ToString());
            }

            //var t4Service = new T4Service(ServiceProvider.GlobalProvider);
            //var res = t4Service.TransformText(templateFilePath, model, extensionPath, opts.Source, targetNamespace);
            File.WriteAllText(Path.Combine(destinationFolder, targetFileName.Replace(".raml", ".cs")), output, host.FileEncoding);
        }