Last active
November 30, 2015 13:53
-
-
Save regisbsb/58ab712da85355cb2961 to your computer and use it in GitHub Desktop.
Generate enum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template language="C#" debug="true" hostspecific="True" #> | |
<#@ output extension=".cs"#> | |
<#@ assembly name="System.Core" #> | |
<#@ assembly name="System.Data" #> | |
<#@ assembly name="System.Configuration" #> | |
<#@ import namespace="System" #> | |
<#@ import namespace="System.Data" #> | |
<#@ import namespace="System.Data.SqlClient" #> | |
<#@ import namespace="System.IO" #> | |
<#@ import namespace="System.Text" #> | |
<#@ import namespace="System.Text.RegularExpressions" #> | |
<#@ import namespace="System.Configuration" #> | |
<# | |
//*** Edit these settings below *** | |
var map = new ExeConfigurationFileMap(); | |
map.ExeConfigFilename = this.Host.ResolvePath(@"..\App.config"); | |
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); | |
var connectionString = config.ConnectionStrings.ConnectionStrings["Doceditor"].ConnectionString; | |
var sourceTableName = "Table"; | |
var sourceValueColumnName = "Value"; | |
var sourceTypeNameColumnName = "Name"; | |
var flagsAttribute = false; | |
string customNamespace = null; | |
//*** that's it *** | |
#> | |
//------------------------------------------------------------------------------ | |
// <auto-generated> | |
// This code was generated from a template. | |
// | |
// Manual changes to this file may cause unexpected behavior in your application. | |
// Manual changes to this file will be overwritten if the code is regenerated. | |
// </auto-generated> | |
//------------------------------------------------------------------------------ | |
<# if(flagsAttribute) { WriteLine("using System;"); } #> | |
namespace <#= customNamespace ?? System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint") #> | |
{ | |
<# if(flagsAttribute) { WriteLine("\t[Flags]"); } #> | |
public enum <#=Path.GetFileNameWithoutExtension(Host.TemplateFile)#> | |
{ | |
<# | |
try { | |
using (SqlConnection connection = new SqlConnection(connectionString)) | |
{ | |
SqlCommand command = new SqlCommand("SELECT " + sourceValueColumnName + ", " + sourceTypeNameColumnName + " FROM " + sourceTableName + " ORDER BY " + sourceValueColumnName, connection); | |
connection.Open(); | |
SqlDataReader reader = command.ExecuteReader(); | |
if (reader.HasRows) | |
{ | |
var i = 0; | |
while (reader.Read()) | |
{ | |
if(i > 0) { | |
WriteLine(","); | |
} | |
i++; | |
var name = reader[sourceTypeNameColumnName].ToString(); | |
var value = reader[sourceValueColumnName].ToString(); | |
if(!String.IsNullOrWhiteSpace(name)) | |
{ | |
StringBuilder sb = new StringBuilder(name.Length); | |
if (name.Length < 2) | |
name = name.ToUpper(); | |
string[] words = name.Split(new char[] { ' ', '-' }, StringSplitOptions.RemoveEmptyEntries); | |
foreach (string word in words) | |
sb.Append(string.Format("{0}{1}", word.Substring(0, 1).ToUpper(), word.Substring(1))); | |
name = sb.ToString(); | |
} | |
Write("\t\t{0} = {1}", name, value); | |
//Write("\t\t{0}", name); | |
} | |
WriteLine(""); | |
} | |
else | |
{ | |
WriteLine("\t\t//No data found in source table"); | |
} | |
reader.Close(); | |
} | |
} catch(Exception ex) { | |
WriteLine("\t\t/*"); | |
WriteLine("\t\tError running template:"); | |
WriteLine("\t\t" + ex.Message); | |
WriteLine("\t\t*/"); | |
} | |
#> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment