private void DoTransform(string sourceFile, string transformFile, string destFile) { if (!File.Exists(sourceFile)) { Log.Warn(string.Format("No config file found in directory {0}, transformation ignored.", ProjectDirectory.FullName)); return; } if (!File.Exists(transformFile)) { Log.Info( string.Format( "No target config file found for target {0} in directory {1}, transformation ignored.", Target, ProjectDirectory.FullName)); return; } Log.Info(string.Format("Transforming file {0} and {1} ==> {2}", sourceFile, transformFile, destFile)); var task = new TransformXml { Source = sourceFile, Destination = destFile, Transform = transformFile, BuildEngine = new FakeBuildEngine() }; task.Execute(); }
public void Execute_Updates_WebConfig_Correctly(string envVariables, int expected) { string envTemplatePath = Path.GetTempFileName(); string webConfigPath = Path.GetTempFileName(); string tempDir = Path.GetDirectoryName(envTemplatePath); try { // Arrange List <XDocument> locationWebConfigTemplateList = new List <XDocument>() { WebConfigTransformTemplates.WebConfigTemplate }; foreach (var locationWebConfigTemplate in locationWebConfigTemplateList) { _environmentTransformWithLocationTemplate.Save(envTemplatePath, SaveOptions.None); XDocument webConfigTemplate = locationWebConfigTemplate; webConfigTemplate.Save(webConfigPath); GenerateEnvTransform env = new GenerateEnvTransform() { WebConfigEnvironmentVariables = envVariables, EnvTransformTemplatePaths = new List <string>() { envTemplatePath }.ToArray(), PublishTempDirectory = tempDir }; // Act bool isSuccess = env.Execute(); Assert.True(isSuccess); foreach (var generatedPath in env.GeneratedTransformFullPaths) { Assert.True(File.Exists(generatedPath)); TransformXml transformTask = new TransformXml() { Source = webConfigPath, Destination = webConfigPath, Transform = generatedPath, SourceRootPath = Path.GetTempPath(), TransformRootPath = Path.GetTempPath(), StackTrace = true }; bool success = transformTask.RunXmlTransform(isLoggingEnabled: false); // Assert Assert.Equal(expected, XDocument.Parse(File.ReadAllText(webConfigPath)).Root.Descendants("environmentVariable").Count()); } } } finally { File.Delete(envTemplatePath); File.Delete(webConfigPath); } }
public void TransformXml() { TransformXml t = new TransformXml(); string sourcePath = Path.Combine(Directory.GetCurrentDirectory(), "sample"); string destinationPath = Path.Combine(Directory.GetCurrentDirectory(), "xmlencrypt"); this.CopyDirectory(sourcePath, destinationPath); t.Execute(@"xmlencrypt\config\appSettings\sample.config.securetext", false); t.Execute(@"xmlencrypt\config\connectionStrings\sample.config.securetext", false); sourcePath = Path.Combine(Directory.GetCurrentDirectory(), "xmlencrypt"); destinationPath = Path.Combine(Directory.GetCurrentDirectory(), "xmldecrypt"); this.CopyDirectory(sourcePath, destinationPath); t.Execute(@"xmldecrypt\config\appSettings\sample.config.securetext", true); t.Execute(@"xmldecrypt\config\connectionStrings\sample.config.securetext", true); FileInfo encrypt = new FileInfo(@"xmlencrypt\sample.config"); FileInfo decrypt = new FileInfo(@"xmldecrypt\sample.config"); Assert.IsTrue(encrypt.Length == decrypt.Length); encrypt = new FileInfo(@"xmlencrypt\config\appSettings\sample.config"); decrypt = new FileInfo(@"xmldecrypt\config\appSettings\sample.config"); Assert.IsTrue(encrypt.Length > decrypt.Length); encrypt = new FileInfo(@"xmlencrypt\config\connectionStrings\sample.config"); decrypt = new FileInfo(@"xmldecrypt\config\connectionStrings\sample.config"); Assert.IsTrue(encrypt.Length > decrypt.Length); }
static void Main(string[] args) { Options options = new Options(); var optionParseResults = CommandLine.Parser.Default.ParseArguments(args, options); if (!optionParseResults) { return; } var baseFileInfo = new FileInfo(options.BaseConfig); var transformFileInfo = new FileInfo(options.TransformConfig); var myBasePath = new FileInfo($".\\workspace\\{baseFileInfo.Name}"); var myTransformPath = new FileInfo($".\\workspace\\{transformFileInfo.Name}"); var myResultPath = new FileInfo($".\\workspace\\{baseFileInfo.Name.Replace(baseFileInfo.Extension, "")}_Result{baseFileInfo.Extension}"); CopyFile(baseFileInfo, myBasePath); CopyFile(transformFileInfo, myTransformPath); var diffCommand = ConfigurationManager.AppSettings["diffCommand"]; var diffCommandArgs = ConfigurationManager.AppSettings["diffCommand:Args"]; var diffCommandWait = bool.Parse(ConfigurationManager.AppSettings["diffCommand:Wait"]); var transformer = new TransformXml { BuildEngine = new TransformBuildEngine(), SourceRootPath = @"./workspace/", Source = myBasePath.Name, Transform = myTransformPath.Name, Destination = myResultPath.FullName }; var transformationSucceeded = transformer.Execute(); if (transformationSucceeded) { var processInfo = new ProcessStartInfo(diffCommand); processInfo.Arguments = diffCommandArgs.Replace("${Source}", myBasePath.FullName) .Replace("${Result}", myResultPath.FullName); Console.WriteLine($"\"{processInfo.FileName}\" {processInfo.Arguments}"); var process = Process.Start(processInfo); if (diffCommandWait) { process?.WaitForExit(); } } if (Debugger.IsAttached) { Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } }
static void Main(string[] args) { var soapFile = @"C:\Temp\soap-req1.xml"; var doc = XDocument.Load(soapFile); var output = TransformXml.RemoveNameSpace(doc.ToString()); StreamWriter sw = new StreamWriter(@"c:\temp\1.xml"); sw.Write(output); sw.Close(); Console.Read(); }
public void XmlTransform_AppliesRemoveAllTransform() { // Arrange string sourceFile = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); string transformFile = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); string outputFile = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); try { _webConfigTemplate.Save(sourceFile); _webConfigTransformRemoveAll.Save(transformFile); // Act TransformXml transformTask = new TransformXml() { Source = sourceFile, Destination = outputFile, Transform = transformFile, SourceRootPath = Path.GetTempPath(), TransformRootPath = Path.GetTempPath(), StackTrace = true }; bool success = transformTask.RunXmlTransform(isLoggingEnabled: false); // Assert Assert.True(success); Assert.True(XDocument.Parse(File.ReadAllText(sourceFile)).Descendants("handlers").Count() == 1); Assert.True(XDocument.Parse(File.ReadAllText(sourceFile)).Descendants("aspNetCore").Count() == 1); Assert.True(XDocument.Parse(File.ReadAllText(outputFile)).Descendants("handlers").Count() == 0); Assert.True(XDocument.Parse(File.ReadAllText(outputFile)).Descendants("aspNetCore").Count() == 0); } finally { File.Delete(sourceFile); File.Delete(transformFile); File.Delete(outputFile); } }
static void Main(string[] args) { var optionSet = LoadCommandLineArguments(args); if(UserNeedsHelp()) { WriteHelpToConsole(optionSet); return; } EnsureDestinationIsCreated(_destinationPathAndFilename); var task = new TransformXml { Source = new TaskItem(_sourcePathAndFilename), Destination = new TaskItem(_destinationPathAndFilename), Transform = new TaskItem(_transformPathAndFilename), StackTrace = false, BuildEngine = new CommandLineBuildEngine(), }; task.Execute(); }
private static bool ProcessTransform(TransformArgs args) { if (string.Equals(args.SourceFile, args.DestinationFile)) { var tempFileName = Path.GetTempFileName(); File.Delete(tempFileName); File.Copy(args.SourceFile, tempFileName); args.SourceFile = tempFileName; } var engine = new TransformOnlyBuildEngine(); var transformer = new TransformXml { BuildEngine = engine, Source = args.SourceFile, Destination = args.DestinationFile, Transform = args.TransformFile }; return transformer.Execute(); }