예제 #1
0
        /// <summary>
        /// Prepares the template.
        /// </summary>
        /// <param name="templateName">Name of the template.</param>
        /// <param name="templateType">Type of the template.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="language">The language.</param>
        /// <param name="provider">The provider.</param>
        /// <returns></returns>
        private static TurboTemplate PrepareTemplate(string templateName, TemplateType templateType, IEnumerable <Replacement> settings, ICodeLanguage language,
                                                     DataProvider provider)
        {
            Utility.WriteTrace(String.Format("Getting template for {0}", templateName));
            string templateText = GetTemplateText(templateType, language);

            //if (templateType== TemplateType.Enum) System.IO.File.AppendAllText(@"C:\temp\enumlog.txt", templateName + "\r\n");

            // set the provider and tablename
            Utility.WriteTrace(String.Format("Preparing template for {0}", templateName));

            foreach (Replacement var in settings)
            {
                string replaceHolder = String.Concat("#", Enum.GetName(typeof(ReplacementVariable), var.Variable).ToUpper(new CultureInfo("en")), "#");
                templateText = Utility.FastReplace(templateText, replaceHolder, var.ReplaceWith, StringComparison.InvariantCultureIgnoreCase);
            }

            TurboTemplate t = new TurboTemplate(templateText, language, provider)
            {
                TemplateName = templateName
            };

            //if (templateType == TemplateType.Enum) System.IO.File.AppendAllText(@"C:\temp\enumlog.txt", t.TemplateText + "\r\n");

            return(t);
        }
예제 #2
0
        /// <summary>
        /// Builds the template.
        /// </summary>
        /// <param name="templateType">Type of the template.</param>
        /// <param name="values">The values.</param>
        /// <param name="language">The language.</param>
        /// <param name="provider">The provider.</param>
        /// <returns></returns>
        public static TurboTemplate BuildTemplate(TemplateType templateType, NameValueCollection values, ICodeLanguage language, DataProvider provider)
        {
            string templateText = GetTemplateText(templateType, language);

            for (int i = 0; i < values.Count; i++)
            {
                templateText = templateText.Replace(values.GetKey(i), values.Get(i));
            }

            TurboTemplate t = new TurboTemplate(templateText, language, provider);

            return(t);
        }
예제 #3
0
        /// <summary>
        /// Not currently used, but will be the basis for user defined templates. Please don't remove!
        /// </summary>
        /// <param name="templateFile">The template file.</param>
        /// <param name="values">The values.</param>
        /// <param name="language">The language.</param>
        /// <param name="provider">The provider.</param>
        /// <returns></returns>
        public static string RunTemplate(string templateFile, NameValueCollection values, ICodeLanguage language, DataProvider provider)
        {
            string templatePath = Path.Combine(TemplateDirectory, templateFile);
            string templateText = Files.GetFileText(templatePath);

            for (int i = 0; i < values.Count; i++)
            {
                templateText = templateText.Replace(values.GetKey(i), values.Get(i));
            }

            TurboTemplate t = new TurboTemplate(templateText, language, provider);

            return(t.Render());
        }
예제 #4
0
        /// <summary>
        /// Adds the template.
        /// </summary>
        /// <param name="template">The template.</param>
        public void AddTemplate(TurboTemplate template)
        {
            if (template != null)
            {
                if (templates.Count == 0)
                {
                    References = template.References;
                    Language   = template.CompileLanguage;
                }

                template.EntryPoint          = "Render";
                template.GeneratedRenderType = String.Concat("Parser", Templates.Count);
                template.TemplateText        =
                    Utility.FastReplace(template.TemplateText, "#TEMPLATENUMBER#", Templates.Count.ToString(), StringComparison.InvariantCultureIgnoreCase);
                templates.Add(template);
            }
        }
예제 #5
0
        /// <summary>
        /// Runs the template.
        /// </summary>
        /// <param name="templateType">Type of the template.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="language">The language.</param>
        /// <param name="provider">The provider.</param>
        /// <returns></returns>
        public static string RunTemplate(TemplateType templateType, IEnumerable <Replacement> settings, ICodeLanguage language, DataProvider provider)
        {
            Utility.WriteTrace("Getting Template");
            string templateText = GetTemplateText(templateType, language);

            Utility.WriteTrace("Replacing values in template");
            foreach (Replacement var in settings)
            {
                string replaceHolder = String.Concat("#", Enum.GetName(typeof(ReplacementVariable), var.Variable).ToUpper(new CultureInfo("en")), "#");
                templateText = Utility.FastReplace(templateText, replaceHolder, var.ReplaceWith, StringComparison.InvariantCultureIgnoreCase);
            }

            TurboTemplate t = new TurboTemplate(templateText, language, provider);

            Utility.WriteTrace("Rendering template");
            string output = t.Render();

            Utility.WriteTrace("Finished :)");
            return(output);
        }
예제 #6
0
        /// <summary>
        /// Generates source code for the virtual path of the build provider, and adds the source code to a specified assembly builder.
        /// </summary>
        /// <param name="assemblyBuilder">The assembly builder that references the source code generated by the build provider.</param>
        public override void GenerateCode(AssemblyBuilder assemblyBuilder)
        {
            Utility.WriteTrace("Invoking BuildProvider");
            DataService.LoadProviders();
            ICodeLanguage language = new CSharpCodeLanguage();

            DirectoryInfo di = new DirectoryInfo(HostingEnvironment.ApplicationPhysicalPath + "\\App_Code");

            FileInfo[] fi = di.GetFiles("*.vb");
            if (fi.Length > 0)
            {
                language = new VBCodeLanguage();
            }

            StringBuilder code = new StringBuilder(language.DefaultUsingStatements);
            TurboCompiler tc   = new TurboCompiler();

            // loop over the providers, and generate code for each
            foreach (DataProvider provider in DataService.Providers)
            {
                Utility.WriteTrace(String.Format("Creating code for {0}", provider.Name));
                string[] tableList = DataService.GetTableNames(provider.Name);
                string[] viewList  = DataService.GetViewNames(provider.Name);

                foreach (string tbl in tableList)
                {
                    TurboTemplate tt = CodeService.BuildClassTemplate(tbl, language, provider);
                    tc.AddTemplate(tt);
                    if (provider.GenerateODSControllers)
                    {
                        TurboTemplate tODS = CodeService.BuildODSTemplate(tbl, language, provider);
                        tc.AddTemplate(tODS);
                    }
                }

                foreach (string view in viewList)
                {
                    TurboTemplate tt = CodeService.BuildViewTemplate(view, language, provider);
                    tc.AddTemplate(tt);
                }

                tc.AddTemplate(CodeService.BuildSPTemplate(language, provider));
            }

            if (DataService.Providers.Count > 0)
            {
                tc.AddTemplate(CodeService.BuildStructsTemplate(language, DataService.Provider));
            }

            foreach (TurboTemplate tt in tc.Templates)
            {
                tt.AddUsingBlock = false;
            }
            tc.Run();

            foreach (TurboTemplate tt in tc.Templates)
            {
                code.Append(tt.FinalCode);
            }

            assemblyBuilder.AddCodeCompileUnit(this, new CodeSnippetCompileUnit(code.ToString()));
        }
예제 #7
0
        /// <summary>
        /// Gets the template text.
        /// </summary>
        /// <param name="t">The t.</param>
        /// <param name="language">The language.</param>
        /// <returns></returns>
        private static string GetTemplateText(TemplateType t, ICodeLanguage language)
        {
            string template;
            string templateText = null;

            switch (t)
            {
            case TemplateType.Class:
                template = TemplateName.CLASS;
                break;

            case TemplateType.ReadOnly:
                template = TemplateName.VIEW;
                break;

            case TemplateType.SP:
                template = TemplateName.STORED_PROCEDURE;
                break;

            case TemplateType.Structs:
                template = TemplateName.STRUCTS;
                break;

            case TemplateType.ODSController:
                template = TemplateName.ODS_CONTROLLER;
                break;

            case TemplateType.DynamicScaffold:
                template = TemplateName.DYNAMIC_SCAFFOLD;
                break;

            case TemplateType.GeneratedScaffoldCodeBehind:
                template = TemplateName.GENERATED_SCAFFOLD_CODE_BEHIND;
                break;

            case TemplateType.GeneratedScaffoldMarkup:
                template = TemplateName.GENERATED_SCAFFOLD_MARKUP;
                break;

            default:
                template = TemplateName.CLASS;
                break;
            }

            template = String.Concat(language.TemplatePrefix, template, FileExtension.DOT_ASPX);

            // decide where to pull the text from
            if (!String.IsNullOrEmpty(templateDirectory))
            {
                Utility.WriteTrace(String.Concat("Looking for template ", template, " in ", templateDirectory));

                // make sure the template exists
                string templatePath = Path.Combine(templateDirectory, template);

                if (File.Exists(templatePath))
                {
                    templateText = Files.GetFileText(templatePath);
                }
                else
                {
                    Utility.WriteTrace(String.Concat("Template ", template, " NOT FOUND in directory ", templateDirectory, "; using embedded resource template instead..."));
                }
            }

            if (String.IsNullOrEmpty(templateText))
            {
                Utility.WriteTrace(String.Format("Loading template from resource: {0}", template));
                templateText = TurboTemplate.LoadTextFromManifest(template);
            }

            if (String.IsNullOrEmpty(templateText))
            {
                throw new Exception(String.Format("The template \"{0}\" is empty or cannot be found.", template));
            }

            return(templateText);
        }
예제 #8
0
        /// <summary>
        /// Runs the structs.
        /// </summary>
        /// <param name="language">The language.</param>
        /// <param name="provider">The provider.</param>
        /// <returns></returns>
        public static string RunStructs(ICodeLanguage language, DataProvider provider)
        {
            TurboTemplate tt = BuildStructsTemplate(language, provider);

            return(tt.Render());
        }
예제 #9
0
        /// <summary>
        /// Runs the class.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="language">The language.</param>
        /// <param name="provider">The provider.</param>
        /// <returns></returns>
        public static string RunClass(string tableName, ICodeLanguage language, DataProvider provider)
        {
            TurboTemplate tt = BuildClassTemplate(tableName, language, provider);

            return(tt.Render());
        }
예제 #10
0
        /// <summary>
        /// Runs the read only.
        /// </summary>
        /// <param name="viewName">Name of the view.</param>
        /// <param name="language">The language.</param>
        /// <param name="provider">The provider.</param>
        /// <returns></returns>
        public static string RunReadOnly(string viewName, ICodeLanguage language, DataProvider provider)
        {
            TurboTemplate tt = BuildViewTemplate(viewName, language, provider);

            return(tt.Render());
        }