Skip to content

Instantly share code, notes, and snippets.

@mattbrailsford
Created July 25, 2015 11:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattbrailsford/4809f2b4e4ae7611b230 to your computer and use it in GitHub Desktop.
Save mattbrailsford/4809f2b4e4ae7611b230 to your computer and use it in GitHub Desktop.
using System;
using System.Xml;
using Microsoft.Web.XmlTransform;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using umbraco.interfaces;
namespace Our.Umbraco.Packaging
{
public class ConfigTransformations : IPackageAction
{
public string Alias()
{
return "configTransformations";
}
public bool Execute(string packageName, XmlNode xmlData)
{
foreach (XmlNode transConfig in xmlData.SelectNodes("//Transformation"))
{
// Get config paths
var srcFile = IOHelper.MapPath(transConfig.Attributes["src"].Value);
var targetFile = IOHelper.MapPath(transConfig.Attributes["target"].Value);
try
{
// Load input file
var doc = new XmlDocument();
doc.Load(targetFile);
// Transform file
var trans = new XmlTransformation(srcFile);
trans.Apply(doc);
// Resave input file
doc.Save(targetFile);
}
catch (Exception ex)
{
LogHelper.Error<ConfigTransformations>(string.Format("Error applying config transformation {0} to config file {1}",
srcFile,
targetFile), ex);
}
}
return true;
}
public bool Undo(string packageName, XmlNode xmlData)
{
return true; // Don't support uninstall
}
public XmlNode SampleXml()
{
return null;
}
}
}
Make sure to install / reference via Nuget 'Microsoft.Web.Xdt'
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<umbPackage>
<info>
...
</info>
<Languages />
<DictionaryItems />
<Stylesheets />
<DataTypes />
<Macros />
<Templates />
<DocumentTypes />
<Actions>
<Action runat="install" alias="configTransformations">
<Transformation src="~/App_Plugins/MyPackage/ConfigTransformations/ExamineIndex.config" target="~/config/ExamineIndex.config" />
<Transformation src="~/App_Plugins/MyPackage/ConfigTransformations/ExamineSettings.config" target="~/config/ExamineSettings.config" />
<Transformation src="~/App_Plugins/MyPackage/ConfigTransformations/Log4Net.config" target="~/config/log4net.config" />
<Transformation src="~/App_Plugins/MyPackage/ConfigTransformations/UmbracoSettings.config" target="~/config/umbracoSettings.config" />
<Transformation src="~/App_Plugins/MyPackage/ConfigTransformations/ViewsWeb.config" target="~/Views/Web.config" />
<Transformation src="~/App_Plugins/MyPackage/ConfigTransformations/Web.config" target="~/Web.config" />
</Action>
</Actions>
<control />
<files />
</umbPackage>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment