Esempio n. 1
0
        protected RamlBaseGenerator(string targetNamespace, string templatesFolder,
                                    string targetFileName, string destinationFolder)
        {
            TemplatesFolder        = templatesFolder;
            this.targetNamespace   = targetNamespace;
            this.targetFileName    = targetFileName;
            this.destinationFolder = destinationFolder;

            host   = new CustomCmdLineHost();
            engine = new Engine();
        }
        public void Generate(AmfModel ramlDoc, string targetFileName, string targetNamespace, string templatesFolder,
                             string destinationFolder)
        {
            templatesFolder = string.IsNullOrWhiteSpace(templatesFolder)
                ? GetTemplateDefaultPath()
                : templatesFolder;


            var model = new ClientGeneratorService(ramlDoc, targetFileName + "Client", targetNamespace, targetNamespace + ".Models").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);
        }