Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
103 lines (88 sloc) 4.81 KB
<#@ template language="C#" debug="false" hostspecific="true"#><#@ output extension=".cs" #><#@ Assembly Name="System.Core.dll" #><#@ Assembly Name="EnvDTE" #><#@ Assembly Name="System.Configuration"#><#@ Import Namespace="System" #><#@ Import Namespace="System.IO" #><#@ Import Namespace="System.Configuration" #><# if (string.IsNullOrEmpty(GetConfigPath())) return String.Empty; ExeConfigurationFileMap configFile = new ExeConfigurationFileMap(); configFile.ExeConfigFilename = GetConfigPath(); if (string.IsNullOrEmpty(configFile.ExeConfigFilename)) throw new ArgumentNullException("The project does not contain App.config or Web.config file."); var config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None); var appSettings = config.AppSettings.Settings; if(appSettings.AllKeys.Length == 0)return string.Empty;#><# string namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint").ToString(); #><# string templateFileName = Path.GetFileNameWithoutExtension(Host.TemplateFile);#>using System;
using System.Configuration;
namespace <#=namespaceName#>
{
/// <summary>
/// This class was generated by the <#=templateFileName#> T4 template
/// </summary>
public static class <#=templateFileName#>
{
<#
foreach(var setting in appSettings.AllKeys){#>
public static string <#=CleanPropertyName(setting)#> { get { return GetConfigSettingItem("<#=setting#>"); } }
<#}#>
private const string MISSING_CONFIG = "Invalid configuration. Required AppSettings section is missing";
private const string INVALID_CONFIG_SETTING = "Invalid configuration setting name: {0}";
private static string GetConfigSettingItem(string name)
{
if (ConfigurationManager.AppSettings == null)
throw new ConfigurationErrorsException(MISSING_CONFIG);
string value = null;
if (ConfigurationManager.AppSettings.Count != 0)
{
try
{
value = ConfigurationManager.AppSettings.Get(name);
}
catch (Exception exception)
{
throw new ConfigurationErrorsException(SettingItemErrorMessage(name, exception));
}
}
return value;
}
private static string SettingItemErrorMessage(string name)
{
return string.Format(INVALID_CONFIG_SETTING, name);
}
private static string SettingItemErrorMessage(string name, Exception exception)
{
return string.Format(INVALID_CONFIG_SETTING, name) + exception.Message;
}
}
}
<#+
public EnvDTE.Project GetCurrentProject()
{
IServiceProvider serviceProvider = (IServiceProvider)Host;
if (serviceProvider == null)
throw new Exception("Host property returned unexpected value (null)");
EnvDTE.DTE dte = (EnvDTE.DTE)serviceProvider.GetService(typeof(EnvDTE.DTE));
if (dte == null)
throw new Exception("Unable to retrieve EnvDTE.DTE");
Array activeSolutionProjects = (Array)dte.ActiveSolutionProjects;
if (activeSolutionProjects == null)
throw new Exception("DTE.ActiveSolutionProjects returned null");
EnvDTE.Project project = (EnvDTE.Project)activeSolutionProjects.GetValue(0);
if (project == null)
throw new Exception("DTE.ActiveSolutionProjects[0] returned null");
return project;
}
private string CleanPropertyName(string name)
{
var regex = new System.Text.RegularExpressions.Regex(@"\! | \@ | \# | \$ | \% | \^ | \& | \* | \( | \) | \+ | \= | \< | \> | \, | \. | \/ | \\ | \? | \| | \{ | \} | \[ | \] | \: | \; | \' | \"" | \` | \~ | - ", System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace);
return regex.Replace(name, "_");
}
private string GetProjectPath()
{
EnvDTE.Project project = GetCurrentProject();
System.IO.FileInfo fileInfo = new System.IO.FileInfo(project.FullName);
return fileInfo.Directory.FullName;
}
private string GetConfigPath()
{
EnvDTE.Project project = GetCurrentProject();
foreach (EnvDTE.ProjectItem projectItem in project.ProjectItems)
{
// if it is the app.config file, then open it up
if (projectItem.Name.Equals("App.config", StringComparison.InvariantCultureIgnoreCase) || projectItem.Name.Equals("Web.config", StringComparison.InvariantCultureIgnoreCase))
return GetProjectPath() + "\\" + projectItem.Name;
}
return string.Empty;
}
public string GetDataDirectory()
{
EnvDTE.Project project = GetCurrentProject();
return System.IO.Path.GetDirectoryName(project.FileName)+"\\App_Data\\";
}
#>