Exemplo n.º 1
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));
                tc.AddTemplate(CodeService.BuildEnumTemplate(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()));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Renders this instance.
        /// </summary>
        /// <returns></returns>
        public string Render()
        {
            if (String.IsNullOrEmpty(TemplateText))
            {
                return(String.Empty);
            }

            TurboCompiler engine = new TurboCompiler();

            engine.AddTemplate(this);
            engine.Run();
            return(engine.Templates[0].FinalCode);
        }
    protected void btnGo_Click(object sender, EventArgs e)
    {
        string sOutPath = txtOut.Text;

        if (!Directory.Exists(sOutPath))
        {
            Directory.CreateDirectory(sOutPath);
        }
        try
        {
            ICodeLanguage language = CodeLanguageFactory.GetByShortName(languageSelect.SelectedValue);

            string providerName = (string)Session[PROVIDER_NAME];
            if (!String.IsNullOrEmpty(providerName))
            {
                DataProvider provider = DataService.GetInstance(providerName);
                if (provider != null)
                {
                    TurboCompiler turboCompiler = new TurboCompiler();
                    StringBuilder sbTableList = new StringBuilder();
                    foreach (ListItem item in chkTables.Items)
                    {
                        if (item.Selected)
                        {
                            sbTableList.AppendLine(item.Value);
                        }
                    }

                    foreach (ListItem item in chkTables.Items)
                    {
                        if (item.Selected)
                        {

                            TableSchema.Table t = DataService.GetTableSchema(item.Value, provider.Name, TableType.Table);
                            if (t.ForeignKeys.Count > 0)
                            {
                                //provider.GetManyToManyTables(t, evalTables);
                            }
                            string className = DataService.GetTableSchema(item.Value, providerName, TableType.Table).ClassName;
                            string filePath = Path.Combine(sOutPath, className + language.FileExtension);
                            TurboTemplate classTemplate = CodeService.BuildClassTemplate(item.Value, language, provider);
                            classTemplate.OutputPath = filePath;
                            turboCompiler.AddTemplate(classTemplate);
                            //SubSonic.Sugar.Files.CreateToFile(filePath, usings + CodeService.RunClass(item.Value, providerName, language));

                            if(chkODS.Checked)
                            {
                                filePath = Path.Combine(sOutPath, className + "Controller" + language.FileExtension);
                                TurboTemplate odsTemplate = CodeService.BuildODSTemplate(item.Value, language, provider);
                                odsTemplate.OutputPath = filePath;
                                turboCompiler.AddTemplate(odsTemplate);
                                //SubSonic.Sugar.Files.CreateToFile(filePath, usings + CodeService.RunODS(item.Value, providerName, language));
                            }
                        }
                    }

                    //output the Views
                    foreach (ListItem item in chkViews.Items)
                    {
                        if (item.Selected)
                        {
                            string className = DataService.GetTableSchema(item.Value, providerName, TableType.View).ClassName;
                            string filePath = Path.Combine(sOutPath, className + language.FileExtension);
                            TurboTemplate viewTemplate = CodeService.BuildViewTemplate(item.Value, language, provider);
                            viewTemplate.OutputPath = filePath;
                            turboCompiler.AddTemplate(viewTemplate);
                            //SubSonic.Sugar.Files.CreateToFile(filePath, usings + CodeService.RunReadOnly(item.Value, providerName, language));
                        }
                    }
                    //output the SPs
                    if (chkSP.Checked)
                    {
                        string filePath = Path.Combine(sOutPath, "StoredProcedures" + language.FileExtension);
                        TurboTemplate spTemplate = CodeService.BuildSPTemplate(language, provider);
                        spTemplate.OutputPath = filePath;
                        turboCompiler.AddTemplate(spTemplate);
                        //SubSonic.Sugar.Files.CreateToFile(filePath, usings + CodeService.RunSPs(providerName, language));
                    }

                    //structs
                    string structPath = Path.Combine(sOutPath, "AllStructs" + language.FileExtension);
                    TurboTemplate structsTemplate = CodeService.BuildStructsTemplate(language, provider);
                    structsTemplate.OutputPath = structPath;
                    turboCompiler.AddTemplate(structsTemplate);
                    //SubSonic.Sugar.Files.CreateToFile(structPath, usings + CodeService.RunStructs(language));

                    if (turboCompiler.Templates.Count > 0)
                    {
                        turboCompiler.Run();
                        foreach (TurboTemplate template in turboCompiler.Templates)
                        {
                            Utility.WriteTrace("Writing " + template.TemplateName + " as " + template.OutputPath.Substring(template.OutputPath.LastIndexOf("\\") + 1));
                            SubSonic.Sugar.Files.CreateToFile(template.OutputPath, template.FinalCode);
                        }
                    }
                    lblResult.Text = "View your files: <a href='file://" + sOutPath + "'>" + sOutPath + "</a>";
                }
            }
        }
        catch (Exception x)
        {
            lblResult.Text = "Error: " + x.Message;
        }
    }
Exemplo n.º 4
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()));
        }
    protected void btnGo_Click(object sender, EventArgs e)
    {
        DataProvider provider = DataService.GetInstance(ddlProvider.SelectedValue);
        if (provider != null)
        {
            try
            {
                StringBuilder sbIndex = new StringBuilder();
                ArrayList fileNames = new ArrayList();
                bool outputCode = radOutputType.SelectedIndex == 0;

                ICodeLanguage language = CodeLanguageFactory.GetByShortName(languageSelect.SelectedValue);
                string masterPageText = masterPageName.Text.Trim();

                foreach (ListItem item in chkTables.Items)
                {
                    TurboCompiler turboCompiler = new TurboCompiler();
                    if(item.Selected)
                    {
                        string tableFileName = item.Value.Replace(SpecialString.SPACE, String.Empty);
                        string tableName = item.Value;
                        string className = DataService.GetSchema(tableName, provider.Name, TableType.Table).ClassName;
                        string fileNameNoExtension = tbxPrefix.Text.Trim() + FormatTableName(tableFileName) + tbxSuffix.Text.Trim();
                        string fileName = fileNameNoExtension + FileExtension.DOT_ASPX;
                        string filePath = txtOut.Text + "\\" + fileName;
                        fileNames.Add(filePath);

                        NameValueCollection nVal = new NameValueCollection();
                        nVal.Add(TemplateVariable.LANGUAGE, language.Identifier);
                        nVal.Add(TemplateVariable.CLASS_NAME, className);
                        nVal.Add(TemplateVariable.TABLE_NAME, tableName);
                        nVal.Add(TemplateVariable.MASTER_PAGE, masterPageText);

                        if(outputCode)
                        {
                            nVal.Add(TemplateVariable.LANGUAGE_EXTENSION, language.FileExtension);
                            nVal.Add(TemplateVariable.PROVIDER, provider.Name);
                            nVal.Add(TemplateVariable.PAGE_FILE, fileNameNoExtension);

                            TurboTemplate scaffoldCodeBehind = CodeService.BuildTemplate(CodeService.TemplateType.GeneratedScaffoldCodeBehind, nVal, language, provider);
                            scaffoldCodeBehind.AddUsingBlock = false;
                            scaffoldCodeBehind.OutputPath = filePath.Replace(FileExtension.DOT_ASPX, (FileExtension.DOT_ASPX + language.FileExtension));
                            turboCompiler.AddTemplate(scaffoldCodeBehind);

                            TurboTemplate scaffoldMarkup = CodeService.BuildTemplate(CodeService.TemplateType.GeneratedScaffoldMarkup, nVal, language, provider);
                            scaffoldMarkup.AddUsingBlock = false;
                            scaffoldMarkup.OutputPath = filePath;
                            turboCompiler.AddTemplate(scaffoldMarkup);
                        }
                        else
                        {
                            TurboTemplate dynamicScaffold = CodeService.BuildTemplate(CodeService.TemplateType.DynamicScaffold, nVal, language, provider);
                            dynamicScaffold.AddUsingBlock = false;
                            dynamicScaffold.OutputPath = filePath;
                            turboCompiler.AddTemplate(dynamicScaffold);
                        }
                        sbIndex.AppendLine("<a href=\"" + fileName + "\">" + FormatTableName(tableName) + "</a><br/>");
                    }
                    if(turboCompiler.Templates.Count > 0)
                    {
                        turboCompiler.Run();
                        foreach(TurboTemplate template in turboCompiler.Templates)
                        {
                            Utility.WriteTrace("Writing " + template.TemplateName + " as " + template.OutputPath.Substring(template.OutputPath.LastIndexOf("\\") + 1));
                            SubSonic.Sugar.Files.CreateToFile(template.OutputPath, template.FinalCode);
                        }
                    }
                }

                if (chkIndexPage.Checked && tbxIndexName.Text != String.Empty)
                {
                    string before = "<html><head><title>SubSonic Scaffold Index Page</title></head><body>";
                    string after = "</body></html>";
                    WriteToFile(txtOut.Text + "\\" + tbxIndexName.Text, before + sbIndex + after);
                }

                lblResult.Text = "Finished";
            }
            catch (Exception x)
            {
                lblResult.Text = "Error: " + x.Message;
            }
        }
    }