-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXML_Transform.cs
More file actions
347 lines (330 loc) · 12.3 KB
/
XML_Transform.cs
File metadata and controls
347 lines (330 loc) · 12.3 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//=============================================================================
// Jocys.com XML Transform Evaldas Jocys <evaldas@jocys.com>
//=============================================================================
using Microsoft.Web.XmlTransform;
using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml;
public class XML_Transform
{
public class Transform
{
public FileInfo Source;
public string Type;
public List<FileInfo> Transforms = new List<FileInfo>();
public List<string> Environments = new List<string>();
}
public static void ProcessArguments(string[] args)
{
// IMPORTANT: Make sure this class don't have any static references to x360ce.Engine library or
// program tries to load x360ce.Engine.dll before AssemblyResolve event is available and fails.
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
//for (int i = 0; i < args.Length; i++)
// Console.WriteLine(string.Format("{0}. {1}", i, args[i]));
// Requires System.Configuration.Installl reference.
var ic = new InstallContext(null, args);
var script = ic.Parameters["s"];
var environment = ic.Parameters["a"];
var scriptFile = new FileInfo(script);
var scriptName = System.IO.Path.GetFileNameWithoutExtension(scriptFile.Name);
// Show parameters
Console.Title = string.Format("{0} Script", scriptName);
Console.WriteLine("Searching. Please wait...");
var transforms = GetTransforms(scriptFile.Directory.FullName);
if (string.IsNullOrEmpty(environment))
{
environment = ShowEnvironmentPrompt(transforms);
// Return if no answer.
if (string.IsNullOrEmpty(environment))
return;
}
TransformFolder(transforms, environment);
}
public static string ShowEnvironmentPrompt(Transform[] transforms)
{
var environments = transforms
.SelectMany(x => x.Environments)
.Select(x => System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(x.ToLower()))
.Distinct().ToArray();
// <action> <working_folder> <pattern> <data_file> <script_file_name>
Console.WriteLine();
Console.WriteLine("Transform Configuration Files:");
Console.WriteLine("");
for (int i = 0; i < environments.Length; i++)
{
Console.WriteLine(string.Format(" {0} - {1}", i, environments[i]));
}
Console.WriteLine();
Console.Write("Type Number or press ENTER to exit: ");
var key = Console.ReadKey(true);
Console.WriteLine(string.Format("{0}", key.KeyChar));
int n;
if (!int.TryParse(key.KeyChar.ToString(), out n))
return null;
if (n < 0 || n >= environments.Length)
return null;
return environments[n];
}
public static Transform[] GetTransforms(string path)
{
var dir = new System.IO.DirectoryInfo(path);
var appFiles = dir.GetFiles("App.Transform.Source.config", SearchOption.AllDirectories);
var webFiles = dir.GetFiles("Web.Transform.Source.config", SearchOption.AllDirectories);
var files = new List<FileInfo>();
files.AddRange(appFiles);
files.AddRange(webFiles);
// Add default files.
var dirs = files.Select(x => x.Directory.FullName).ToArray();
var appConfigFiles = dir.GetFiles("App.config", SearchOption.AllDirectories);
appConfigFiles = appConfigFiles.Where(x => !dirs.Contains(x.Directory.FullName)).ToArray();
var webConfigFiles = dir.GetFiles("Web.config", SearchOption.AllDirectories);
webConfigFiles = webConfigFiles.Where(x => !dirs.Contains(x.Directory.FullName)).ToArray();
files.AddRange(appConfigFiles);
files.AddRange(webConfigFiles);
// Show menu.
Console.WriteLine();
Console.WriteLine("Path: {0}", path);
var rx = new Regex("^(?<type>[^\\.]+)");
// Put all list into files.
var ts = files.Select(x => new Transform() { Source = x }).ToArray();
for (int i = 0; i < ts.Length; i++)
{
var t = ts[i];
// Get 'app' or 'web'.
t.Type = rx.Match(t.Source.Name).Groups["type"].Value;
var xmlTranformPattern = string.Format("^{0}\\.(?<env>[^\\.]+)\\.config$", t.Type);
var xmlTranformRx = new Regex(xmlTranformPattern);
// Get transform files.
var xmlTransforms = t.Source.Directory.GetFiles(string.Format("{0}.*.config", t.Type));
foreach (var xmlTransform in xmlTransforms)
{
var match = xmlTranformRx.Match(xmlTransform.Name);
if (!match.Success)
continue;
// Store transform file and environment.
t.Transforms.Add(xmlTransform);
t.Environments.Add(match.Groups["env"].Value);
}
}
ts = ts.Where(x => x.Transforms.Count > 0).ToArray();
Console.WriteLine("Files to Transform: {0}", ts.Length);
return ts;
}
public static void TransformFolder(Transform[] transforms, string environment)
{
//Console.WriteLine(string.Format("Environment: {0}", environment));
var maxName = transforms.SelectMany(x => x.Transforms.Select(y => y.Name.Length)).Max();
var maxDest = 41;
for (int i = 0; i < transforms.Length; i++)
{
var transform = transforms[i];
Console.WriteLine();
Console.WriteLine(string.Format("{0}", transform.Source.FullName));
Console.WriteLine();
var currentPath = string.Format("{0}\\{1}.config", transform.Source.Directory.FullName, transform.Type);
var currentFile = new FileInfo(currentPath);
if (currentFile.Exists)
{
var currentEnvironment = GetEnvironment(currentFile.FullName).ToLower();
currentEnvironment = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(currentEnvironment);
//Console.WriteLine(string.Format(" {0} ({1})", Path.GetFileName(currentPath), currentEnvironment.ToUpper()));
}
for (int t = 0; t < transform.Transforms.Count; t++)
{
var tFi = transform.Transforms[t];
var env = transform.Environments[t];
var transformPath = string.Format("{0}\\{1}.{2}.config", transform.Source.Directory.FullName, transform.Type, env);
var destinationPath = string.Format("{0}\\{1}.Transform.Destination.{2}.config", transform.Source.Directory.FullName, transform.Type, env);
var success = TransformConfig(transform.Source.FullName, transformPath, destinationPath, maxName, maxDest);
// If success and match the choice.
if (success && string.Equals(env, environment, StringComparison.OrdinalIgnoreCase))
{
var bytes = File.ReadAllBytes(destinationPath);
if (IsDifferent(currentPath, bytes))
{
File.WriteAllBytes(currentPath, bytes);
}
Console.Write(" => {0}", Path.GetFileName(currentPath));
}
Console.WriteLine();
}
}
}
public static string GetEnvironment(string filename, string defaultValue = "Dev")
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(filename);
foreach (var de in xmlDoc.DocumentElement)
{
var element = de as XmlElement;
if (element == null)
continue;
if (!element.Name.Equals("appSettings"))
continue;
var elements = element.ChildNodes.OfType<System.Xml.XmlElement>();
foreach (var el in elements)
{
if (el == null)
continue;
if (el.Attributes["key"].Value == "RunMode" || el.Attributes["key"].Value == "Environment")
{
return el.Attributes["value"].Value.ToLower();
}
}
}
return defaultValue;
}
// C:\Program Files(x86)\MSBuild\Microsoft\VisualStudio\v11.0\Web\Microsoft.Web.XmlTransform.dll
public static bool TransformConfig(string sourcePath, string transformPath, string destinationPath, int maxName = 18, int maxDest = 28)
{
if (!File.Exists(sourcePath))
{
Console.WriteLine("Source file not found: {0}", sourcePath);
return false;
}
if (!File.Exists(transformPath))
{
Console.WriteLine("Transform file not found: {0}", transformPath);
return false;
}
var document = new XmlTransformableDocument();
document.PreserveWhitespace = false;
document.Load(sourcePath);
var transformation = new XmlTransformation(transformPath);
var status = transformation.Apply(document) ? "" : "Failure: ";
Console.Write(" {0}{1,-" + maxName + "} => {2,-" + maxDest + "}", status, Path.GetFileName(transformPath), Path.GetFileName(destinationPath));
var ms = new MemoryStream();
// Apply formatting.
var xws = new XmlWriterSettings();
xws.Indent = true;
xws.IndentChars = "\t";
xws.CheckCharacters = true;
var xw = XmlWriter.Create(ms, xws);
document.WriteTo(xw);
xw.Close();
var bytes = ms.ToArray();
// If file is missing or different then...
if (!File.Exists(destinationPath) || IsDifferent(destinationPath, bytes))
{
// Save file.
File.WriteAllBytes(destinationPath, bytes);
}
document.Dispose();
return true;
}
#region CurrentDomain_AssemblyResolve
static List<string> LoadedAssemblies = new List<string>();
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs e)
{
var dllName = e.Name.Contains(",") ? e.Name.Substring(0, e.Name.IndexOf(',')) : e.Name.Replace(".dll", "");
//Console.WriteLine(string.Format("AssemblyResolve [Name={0}]: {1}", e.Name, dllName));
string path = null;
switch (dllName)
{
case "Microsoft.Web.XmlTransform":
case "Microsoft.Web.XmlTransform.resources":
var editions = new string[] { "Community", "Professional", "Enterprise" };
var versions = new List<KeyValuePair<string, string>>();
versions.Add(new KeyValuePair<string, string>("2017", "15.0"));
versions.Add(new KeyValuePair<string, string>("2019", "16.0"));
foreach (var edition in editions)
{
foreach (var version in versions)
{
var p = string.Format(@"c:\Program Files (x86)\Microsoft Visual Studio\{0}\{1}\MSBuild\Microsoft\VisualStudio\v{2}\Web\Microsoft.Web.XmlTransform.dll",
version.Key, edition, version.Value);
if (System.IO.File.Exists(p))
path = p;
}
if (!string.IsNullOrEmpty(path))
break;
}
break;
default:
break;
}
if (path == null)
{
Console.WriteLine(string.Format("AssemblyResolve: {0} - No Path!", dllName));
return null;
}
if (LoadedAssemblies.Contains(path))
{
//Console.WriteLine(string.Format("AssemblyResolve: {0} - Already Loaded!", path));
return null;
}
if (!System.IO.File.Exists(path))
{
Console.WriteLine(string.Format("AssemblyResolve: {0} - File not found!", path));
return null;
}
var bytes = System.IO.File.ReadAllBytes(path);
//Console.WriteLine(string.Format("AssemblyResolve Name: {0}", e.Name));
//Console.WriteLine(string.Format("AssemblyResolve Loading: {0}", path));
var asm = Assembly.Load(bytes);
LoadedAssemblies.Add(path);
return asm;
}
#endregion
#region File Comparison
public static bool IsDifferent(string name, byte[] bytes)
{
var fi = new FileInfo(name);
var isDifferent = false;
// If file doesn't exists or file size is different then...
if (!fi.Exists || fi.Length != bytes.Length)
{
isDifferent = true;
}
else
{
// Compare checksums.
var byteHash = GetHashFromBytes(bytes);
var fileHash = GetHashFromFile(fi.FullName);
for (int i = 0; i < byteHash.Length; i++)
if (byteHash[i] != fileHash[i])
return false;
}
return isDifferent;
}
public static byte[] GetHashFromBytes(byte[] bytes)
{
var algorithm = System.Security.Cryptography.SHA256.Create();
return algorithm.ComputeHash(bytes);
}
public static byte[] GetHashFromFile(string path, object sender = null)
{
var algorithm = System.Security.Cryptography.SHA256.Create();
using (var stream = File.OpenRead(path))
{
var totalBytes = stream.Length;
var totalBytesRead = 0L;
// 4096 buffer preferable because the CPU cache can hold such amounts.
var buffer = new byte[0x1000];
bool done;
int bytesRead;
do
{
bytesRead = stream.Read(buffer, 0, buffer.Length);
totalBytesRead += bytesRead;
// True if reading of all bytes completed.
done = totalBytesRead == totalBytes;
// If more bytes left to read then...
if (done)
algorithm.TransformFinalBlock(buffer, 0, bytesRead);
else
algorithm.TransformBlock(buffer, 0, bytesRead, null, 0);
// Continue if not done...
} while (!done);
}
var hash = algorithm.Hash;
algorithm.Dispose();
return hash;
}
#endregion
}