WiX - Copy compiled web.config based on environment to website root

泄露秘密 提交于 2019-12-21 06:01:27

问题


As a part of my WiX installation, I am copying all the transformed/compiled web.config files to the install directory. The names of the compiled web.config are in format web.{ENV}.config. In my install UI I created a Custom Dialog where I parse the ENV and populate a combobox so that the user can select the environment to which we deploy to. This comboBox sets a Property ENV.

I need to understand how i could use this property to copy the installed config files to the website root.

Update: @Rob_Mensching - your solution works, however, on compilation WiX forces me to have a GUID created for each such component. Is there a way I could avoid it? The thing is i am going to generate this piece of code by running XSLT on my wxs file which gets generated using heat; and there is no way I could generate a GUID using XSLT (or can I?)

This is how my code looks now:

<ComponentGroup Id='web.config' Directory='CONFIGLOCATION'>
  <Component Id='CopyWebConfigForDev1' Guid='{F207C26A-5D9C-4F19-96A3-D818BB226EFC}' >
    <Condition>ENV="Dev1"</Condition>
    <CopyFile Id='CopyDev1Config' FileId='fil9C4CFE42035F1A63180142352CF441BC' DestinationDirectory='CONFIGLOCATION' DestinationName='web.config'/>
  </Component>
  <Component Id='CopyWebConfigForQA1' Guid='{F207C26A-5D9C-4F19-96A3-D818BB226EFC}' >
    <Condition>ENV="QA1"</Condition>
    <CopyFile Id='CopyQA1Config' FileId='fil12F8B50F03F1BD91A579F6B6CE7195DF' DestinationDirectory='CONFIGLOCATION' DestinationName='web.config'/>
  </Component>
</ComponentGroup>

回答1:


I would use a "Component Condition"b to do so. Something like the following should work well:

<Fragment>
  <ComponentGroup Id='web.config' Directory='ConfigFolder'>
    <Component>
      <Condition>ENV~="Production"</Condition>
      <File Source='web.Production.config'>
         <CopyFile DestinationDirectory='INSTALLFOLDER' DestinationName='web.config' />
      </File>
    </Component>

    <Component>
      <Condition>ENV~="Test"</Condition>
      <File Source='web.Test.config'>
         <CopyFile DestinationDirectory='INSTALLFOLDER' DestinationName='web.config' />
      </File>
    </Component>
  </ComponentGroup>
</Fragment>

The condition syntax is documented here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa368012(v=vs.85).aspx




回答2:


With help from the code that Rob provided and after some more research i found out how to avoid having to provide the Guid to each component if your installation folder is not a standard folder. Simply specify the ComponentGuidGenerationSeed for the Custom Directory under which you are trying to install the component to. The directory where you specify this attribute need not be the immediate parent directory of the location where you intend to install the component. This is how my directory structure now looks:

<Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="INETPUBFOLDER" Name="inetpub">
        <Directory Id="WWWROOTFOLDER" Name="wwwroot" ComponentGuidGenerationSeed="PUT-YOUR-GUID">
          <Directory Id="CONFIGLOCATION" Name="$(var.PublishLocation)" />
          <Directory Id="INSTALLLOCATION" Name="$(var.PublishLocation)" >
            <Directory Id="APPFOLDER" Name="bin" />
            <Directory Id="MyProject.Web.Content" />
            <Directory Id="CONFIGSFOLDER" Name="Configs">
              <Directory Id="WEBFOLDER" Name="Web">
                <Directory Id="WEBCONFIGFILES" />
              </Directory>
              <Directory Id="NLOGFOLDER" Name="NLog">
                <Directory Id="NLOGCONFIGFILES" />
              </Directory>
            </Directory>
          </Directory>
        </Directory>
      </Directory>
    </Directory>

This is how now my harvested and xml transformed wxs file looks:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:wix="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="WEBCONFIGFILES">
            <Component Id="cmp9CAF0D4A0C62775945002986D1D99926" Guid="PUT-YOUR-GUID">
                <File Id="fil9C4CFE42035F1A63180142352CF441BC" KeyPath="yes" Source="$(var.WebConfigFilesDir)\Web.Dev1.config" />
            </Component>
            <Component Id="cmpB5117E2029EA9A7CC3AFC247EA4483AD" Guid="PUT-YOUR-GUID">
                <File Id="fil0F80FEAFAD0333C3B74BB742C4FE118C" KeyPath="yes" Source="$(var.WebConfigFilesDir)\Web.Prod.config" />
            </Component>
            <Component Id="cmp340743041F12BBE6C7C40D4351407D08" Guid="PUT-YOUR-GUID">
                <File Id="fil12F8B50F03F1BD91A579F6B6CE7195DF" KeyPath="yes" Source="$(var.WebConfigFilesDir)\Web.QA1.config" />
            </Component>
        </DirectoryRef>
    </Fragment>
    <Fragment>
        <ComponentGroup Id="WebConfigFiles">
            <ComponentRef Id="cmp9CAF0D4A0C62775945002986D1D99926" />
            <ComponentRef Id="cmpB5117E2029EA9A7CC3AFC247EA4483AD" />
            <ComponentRef Id="cmp340743041F12BBE6C7C40D4351407D08" />
        </ComponentGroup>
    </Fragment>
    <Fragment>
        <UI Id="EnvironmentComboBox">
            <ComboBox Property="ENV">
                <ListItem Value="Dev1" Text="Dev1" />
                <ListItem Value="Prod" Text="Prod" />
                <ListItem Value="QA1" Text="QA1" />
            </ComboBox>
        </UI>
    </Fragment>
    <Fragment>
        <ComponentGroup Id="web.config" Directory="CONFIGLOCATION">
            <Component Id="cmpWebConfigForDev1">
                <Condition>ENV="Dev1"</Condition>
                <File Id="CopyDev1Config" Source="$(var.WebConfigFilesDir)\Web.Dev1.config" Name="web.config" />
            </Component>
            <Component Id="cmpWebConfigForProd">
                <Condition>ENV="Prod"</Condition>
                <File Id="CopyProdConfig" Source="$(var.WebConfigFilesDir)\Web.Prod.config" Name="web.config" />
            </Component>
            <Component Id="cmpWebConfigForQA1">
                <Condition>ENV="QA1"</Condition>
                <File Id="CopyQA1Config" Source="$(var.WebConfigFilesDir)\Web.QA1.config" Name="web.config" />
            </Component>
        </ComponentGroup>
    </Fragment>
</Wix>

In the above file only the first two fragments are generated by the harvest tool. The next two fragments containing the UI/ComboBox Definition and ComponentGroup for WebconfigFiles are created using XML transform by reading the information from File elements in the first Fragment.



来源:https://stackoverflow.com/questions/15377149/wix-copy-compiled-web-config-based-on-environment-to-website-root

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!