SlowCheetah-现在可以对任何XML配置文件通用化Web.config转换语法

I did a post last year called If You're Using XCopy, You're Doing It Wrong that also included a video of my talk at Mix10 where I show how to deploy website with Web Deploy. One of the cooler not-very-well-known features is called Web.config Transformation. Once folks see it, then immediately want a general solution.

去年我写了一篇名为《如果您正在使用XCopy,您做错了》的文章,其中还包括我在Mix10上的演讲视频,其中展示了如何使用Web Deploy来部署网站。 不太流行的较凉爽功能之一就是Web.config Transformation。 一旦人们看到了它,便立即需要一个通用的解决方案。

First, from the previous post:

首先,来自上一篇文章:

You can right-click on your web.config and click "Add Config Transforms." When you do this, you'll get a web.debug.config and a web.release.config. You can make a web.whatever.config if you like, as long as the name lines up with a configuration profile. These files are just the changes you want made, not a complete copy of your web.config.

您可以右键单击web.config,然后单击“添加配置转换”。 执行此操作时,将获得一个web.debug.config和一个web.release.config。 您可以根据需要创建一个web.whatever.config,只要该名称与配置配置文件对齐即可。 这些文件只是您要进行的更改,而不是web.config的完整副本。

You might think you'd want to use XSLT to transform a web.config, but while they feels intuitively right it's actually very verbose.

您可能会认为您想使用XSLT来转换web.config,但是尽管他们直观地感觉很对,但实际上却很冗长。

Here's two transforms, one using XSLT and the same one using the XML Document Transform syntax/namespace. As with all things there's multiple ways in XSLT to do this, but you get the general idea. XSLT is a generalized tree transformation language, while this deployment one is optimized for a specific subset of common scenarios. But, the cool part is that each XDT transform is a .NET plugin, so you can make your own.

这是两种转换,一种使用XSLT,另一种使用XML Document Transform语法/命名空间。 与所有事物一样,XSLT中有多种方法可以执行此操作,但是您可以大致了解。 XSLT是一种通用的树转换语言,而此部署语言则针对常见方案的特定子集进行了优化。 但是,最酷的部分是每个XDT转换都是一个.NET插件,因此您可以自己制作。

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()">
  <xsl:copy>          
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="/configuration/appSettings">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
    <xsl:element name="add">
      <xsl:attribute name="key">NewSetting</xsl:attribute>
      <xsl:attribute name="value">New Setting Value</xsl:attribute>
    </xsl:element>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>

Or the same thing via the deployment transform:

或通过部署转换进行相同的操作:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add name="NewSetting" value="New Setting Value" xdt:Transform="Insert"/>
</appSettings>
</configuration>

This kind of config file transformation is so useful in fact, that it's one of the #1 feature requests...as a generalized solution. Folks want to transform their app.configs, or any XML file as part of their builds. Additionally, the current system only runs the transforms as a part of the publish process and folks would rather the transform happen "on F5" or on build. So, my team members Sayed Ibrahim Hashimi and Chuck England have done just that as a small VSiX called SlowCheetah XML Transforms.

实际上,这种配置文件转换非常有用,它是功能请求中的#1之一...作为通用解决方案。 人们希望将其app.configs或任何XML文件作为其构建的一部分进行转换。 此外,当前系统仅将转换作为发布过程的一部分来运行,而人们宁愿转换在“ F5”或构建上进行。 因此,我的团队成员Sayed Ibrahim HashimiChuck England做到了这一点,就像一个名为SlowCheetah XML Transforms的小型VSiX

In this screenshot I've created a simple Console Application, made a basic app.config, then right clicked and selected "Add Transform." This gives an app.debug.config and app.release.config. You could make and name these however you like, for example app.testing.config, etc.

在此屏幕快照中,我创建了一个简单的控制台应用程序,创建了一个基本的app.config,然后右键单击并选择“添加转换”。 这给出了一个app.debug.config和app.release.config。 您可以根据自己的喜好命名和命名这些名称,例如app.testing.config等。

For example, here's a basic app.config for my Console app:

例如,这是我的控制台应用程序的基本app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration >
<appSettings>
<add key="appName" value="Something"/>
<add key="url" value="http://hanselman.com/"/>
<add key="email" value="awesome@hanselman.com" />
</appSettings>
</configuration>

And here's the transform to change my one value in my development time config to a debug value (or test, staging, etc). You can do this for connectionStrings, app keys, compiler settings, anything in an XML or config file.

这是将我在开发时间配置中的一个值更改为调试值(或测试,暂存等)的转换。 您可以对connectionStrings,应用程序密钥,编译器设置,XML或配置文件中的任何内容执行此操作

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="appName" value="Demo-debug" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
<add key="email" value="debug@contoso.com" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>
</configuration>

Note the Transform="Replace?" That can be replace, or insert, etc. Lots of choices. The ShowCheetah XML Transform Add-In also adds a "Preview Transform" right-click menu which makes writing these way easier.

注意Transform =“ Replace?” 那可以被替换,或插入等等。很多选择。 ShowCheetah XML Transform加载项还添加了一个“ Preview Transform”右键菜单,这使编写这些方法变得更加容易。

The clever part is that there's no magic. All the functionality is installed to %LOCALAPPDATA%\Microsoft\MSBuild\SlowCheetah\v1\ and lives in a standard MSBuild .targets file. You don't even need the plugin if you are installing this on a build server, just copy the files or even check them in with your source and refer to them in your project or MSBuild files. It's added in your project file for you via the tooling like any custom targets:

聪明的部分是,没有魔术。 所有功能都安装在%LOCALAPPDATA%\ Microsoft \ MSBuild \ SlowCheetah \ v1 \中,并且位于标准的MSBuild .targets文件中。 如果要将其安装在构建服务器上,则甚至不需要插件,只需复制文件,甚至将其与源一起签入并在项目或MSBuild文件中引用它们即可。 它像任何自定义目标一样通过工具添加到您的项目文件中:

<Import Project="$(LOCALAPPDATA)\Microsoft\MSBuild\SlowCheetah\v1\Microsoft.Transforms.targets" Condition="Exists('$(LOCALAPPDATA)\Microsoft\MSBuild\SlowCheetah\v1\Microsoft.Transforms.targets')" />

Between the build targets and the added menu items, you get:

在构建目标和添加的菜单项之间,您将获得:

  • Added tooling to desktop project to create XDT transforms

    向桌面项目添加了工具以创建XDT转换
  • Ability to transform

    改造能力
    • app.config for desktop projects based on build configuration

      基于构建配置的桌面项目的app.config
    • any XML file to the output folder based on build configuration

      基于构建配置的任何XML文件到输出文件夹
  • Added tooling to enable previewing XDT transforms

    添加了工具以启用XDT转换预览
  • For web projects you can easily transform other XML files during package/publish

    对于Web项目,您可以在打包/发布期间轻松转换其他XML文件

Let Sayed and the team know what you think, if it's useful, and if you use it at Sayed's blog, or the SlowCheetah project site on the VS Gallery. Enjoy!

让Sayed和团队知道您的想法,如果有用,以及是否在Sayed的博客VS GallerySlowCheetah项目站点上使用它。 请享用!

翻译自: https://www.hanselman.com/blog/slowcheetah-webconfig-transformation-syntax-now-generalized-for-any-xml-configuration-file

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值