<#@ template language="C#" hostspecific="True" debug="True" #>
<#@ output extension=".cs"#>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ import namespace="System.Xml.XPath" #>
<#@ output extension="txt" #>
using System.Collections.Generic;
<#
string targetNamespace="ProjectNamespace";
var dcmKeys=ReadKeys("DynamicCM.config","//add[@key]");
var webKeys=ReadKeys("..\\web.config","//appSettings/add[@key]");
if(string.IsNullOrEmpty( targetNamespace)==false){
#>
namespace <#=targetNamespace#> {
<# }
#>
public static class Config {
<#
foreach (string key in dcmKeys.Concat(webKeys)){ // did not do another distinct. We would rather spot the conflicting names
#>
public const string <#= RemoveAll(key,new[]{".","-",":"}) #> = "<#=key#>";
<# }#>
public readonly static IEnumerable<string> AllKeys = new []{
<#
foreach (string key in dcmKeys.Concat(webKeys)){
#> "<#= key #>",
<# }#>
};
}<# if(string.IsNullOrEmpty( targetNamespace)==false) WriteLine("}");#>
<#+ IEnumerable<string> ReadKeys(string basePath,string xPath)
{
var hostPath=Host.ResolvePath(basePath);
WriteLine("//"+hostPath);
var config=System.IO.File.ReadAllText(hostPath);
var x= System.Xml.Linq.XDocument.Parse(config);
return x.Root.XPathSelectElements(xPath)
.Select(n=>n.Attribute(XNamespace.None+ "key").Value)
.Distinct().ToArray();
}
#>
<#+ string RemoveAll(string text, IEnumerable<string> removals)
{
if(string.IsNullOrEmpty(text))
return text;
if(removals.Any()==false)
return text;
return RemoveAll(text.Replace(removals.First(),string.Empty),removals.Skip(1));
}#>