-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrumpy.Entity.UpdateAppConfig.tt
112 lines (89 loc) · 4.23 KB
/
Grumpy.Entity.UpdateAppConfig.tt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ import namespace="System.Collections" #>
<#@ output extension=".txt" #>
<#@ include file="EF6.Utility.CS.ttinclude"#>Checking for database connection in App.config and updating
<#
var textTransform = DynamicTextTransformation.Create(this);
var loader = new EdmMetadataLoader(textTransform.Host, textTransform.Errors);
var solutionDir = Host.ResolveAssemblyReference("$(SolutionDir)");
var projectDir = Host.ResolveAssemblyReference("$(ProjectDir)");
var modelFiles = Directory.EnumerateFiles(projectDir).Where(f => f.EndsWith(".edmx"));
if (!modelFiles.Any())
return "No Entity Framework Model (.edmx) found";
var modelFile = modelFiles.SingleOrDefault();
if (modelFile == null)
return "This template only supports one Entity Framework Model (.edmx) file per project";
var modelName = Path.GetFileNameWithoutExtension(modelFile);
var itemCollection = loader.CreateEdmItemCollection(modelFile);
var container = itemCollection.OfType<EntityContainer>().FirstOrDefault();
if (container == null)
return "Entity Container not found";
var databaseLinkFiles = Directory.EnumerateFiles(solutionDir, "*.entity_database", SearchOption.AllDirectories);
if (!databaseLinkFiles.Any())
return "No database link file found";
if (databaseLinkFiles.Any())
{
foreach (var databaseLinkFile in databaseLinkFiles)
{
#>Database link file found <#= databaseLinkFile #>
<#
var databaseName = Path.GetFileNameWithoutExtension(databaseLinkFile);
var databaseServer = File.ReadLines(databaseLinkFile).FirstOrDefault();
UpdateAppConfig($@"{projectDir}\App.config", container.Name, EntityConnectionString(databaseServer, databaseName, modelName));
}
}
#>
<#+
private static void UpdateAppConfig(string appConfig, string connectionName, string connectionString)
{
var xmlDocument = new XmlDocument();
if (File.Exists(appConfig)) {
File.SetAttributes(appConfig, File.GetAttributes(appConfig) & ~FileAttributes.ReadOnly);
xmlDocument.Load(appConfig);
}
else
CreateAppConfig(xmlDocument);
var connectionStringsNode = GetNode(xmlDocument, GetNode(xmlDocument, xmlDocument, "configuration"), "connectionStrings");
var addNode = FindNode(connectionStringsNode.SelectNodes("add")?.GetEnumerator(), "name", connectionName) ?? AddNode(xmlDocument, connectionStringsNode, "add", "name", connectionName);
addNode.SetAttribute("connectionString", connectionString);
addNode.SetAttribute("providerName", "System.Data.EntityClient");
xmlDocument.Save(appConfig);
}
private static string EntityConnectionString(string databaseServer, string databaseName, string modelFileName)
{
return $"metadata=res://*/{modelFileName}.csdl|res://*/{modelFileName}.ssdl|res://*/{modelFileName}.msl;provider=System.Data.SqlClient;provider connection string=\"data source={databaseServer};initial catalog={databaseName};integrated security=True;MultipleActiveResultSets=True;App=EntityFramework\"";
}
private static XmlElement FindNode(IEnumerator addNodes, string attributeName, string attributeValue)
{
XmlElement addNode = null;
while (addNodes != null && addNodes.MoveNext() && addNode == null)
{
if ((addNodes.Current as XmlElement)?.GetAttribute(attributeName) == attributeValue)
addNode = addNodes.Current as XmlElement;
}
return addNode;
}
private static XmlElement AddNode(XmlDocument xmlDocument, XmlNode xmlNode, string nodeName, string attributeName, string attributeValue)
{
var node = xmlDocument.CreateElement(nodeName);
xmlNode.AppendChild(node);
node.SetAttribute(attributeName, attributeValue);
return node;
}
private static XmlNode GetNode(XmlDocument xmlDocument, XmlNode xmlNode, string nodeName)
{
return xmlNode.SelectSingleNode(nodeName) ?? xmlNode.AppendChild(xmlDocument.CreateElement(nodeName));
}
private static void CreateAppConfig(XmlDocument xmlDocument)
{
xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null));
xmlDocument.AppendChild(xmlDocument.CreateElement("configuration"));
}
public static void ArgumentNotNull<T>(T arg, string name) where T : class
{
if (arg == null)
{
throw new ArgumentNullException(name);
}
}
#>