Created
July 28, 2017 01:28
-
-
Save dcomartin/3ae493a3f6eadb6bd8e517bf33ba0a16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Xml; | |
using Cake.Core; | |
using Cake.Core.Annotations; | |
using Cake.Core.Diagnostics; | |
namespace Cake.AppSettingReplacer | |
{ | |
public static class Replacer | |
{ | |
[CakeMethodAlias] | |
public static void ReplaceAppSetting(this ICakeContext context, string filename, string key, string newValue) | |
{ | |
var xml = new XmlDocument(); | |
xml.Load(filename); | |
var node = xml.SelectSingleNode($"/configuration/appSettings/add[@key='{key}']"); | |
if (node == null) | |
{ | |
throw new InvalidOperationException($"ApplicationSetting Key ({key}) does not exist."); | |
} | |
var valueAttribute = node.Attributes["value"]; | |
if (valueAttribute == null) | |
{ | |
valueAttribute = xml.CreateAttribute("value"); | |
node.Attributes.SetNamedItem(valueAttribute); | |
} | |
valueAttribute.Value = newValue; | |
xml.Save(filename); | |
context.Log.Write(Verbosity.Normal, LogLevel.Debug, $"Replacing {filename} appSetting key={key} with value={newValue}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment