Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Last active July 20, 2017 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/b28468c58b7dac921d13e4faa9104cfc to your computer and use it in GitHub Desktop.
Save dcomartin/b28468c58b7dac921d13e4faa9104cfc to your computer and use it in GitHub Desktop.
using System;
using System.Xml;
using Cake.Core;
using Cake.Core.Annotations;
using LogLevel = Cake.Core.Diagnostics.LogLevel;
using Verbosity = Cake.Core.Diagnostics.Verbosity;
namespace Cake.AppSettingsReplacer
{
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