Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Harvesting files for use in PerUser installers

I have an installer that needs to explicitly be a PerUser msi. Current issue is that to use Heat for harvesting files doesn't provide a way to generate components that fully comply with PerUser package. Especially ICE64 "The directory dir7956CF617C38D877C93B5A7D33313596 is in the user profile but is not listed in the RemoveFile table.

Now as long as i know the structure it's not a problem as i can create a component that contains all directories and include it in the Feature. However, if the structure changes then it is a manual work to find out all the directories and add them manually. This removes the flexibility for other devs to freely add/remove files.

For those interested here is the heat command that i use: heat.exe dir "SourceDir" -dr INSTALLDIR -cg CG_Binaries -nologo -gg -g1 -sfrag -srd -svb6 -scom -sreg -var "var.Binaries" -out "Binaries.wxs"' />

Here is the original XML that heat.exe produces without any transformation:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="INSTALLDIR">
            <Directory Id="dir7956CF617C38D877C93B5A7D33313596" Name="Folder1">
                <Component Id="cmp1CBAE345A14FB4F0343E1FC307F5EE1D" Guid="CBB5AF6A-D0A2-464D-BFAA-49F9C102A4F4">
                    <File Id="filB5C5E94E5D59CC7DB489F6823F341316" KeyPath="yes" Source="$(var.Binaries)\Folder1\NewTextFile0.txt" />
                </Component>
            </Directory>
            <Directory Id="dir2BA1778353CDCC6DE102110034297AC9" Name="Folder2">
                <Component Id="cmp55726990D3629FC63F840780B5BC7729" Guid="49CAB541-CEB5-423B-89E2-A42D51AD9E7E">
                    <File Id="fil98449FB9E91E1F95E2BA0E6AD712D2E6" KeyPath="yes" Source="$(var.Binaries)\Folder2\NewTextFile1.txt" />
                </Component>
                <Directory Id="dir52EB3739C4D4F7DFB9D67E41E7687374" Name="Folder3">
                    <Component Id="cmpEC9C0276E68BB6794098A24DCF44E0BD" Guid="B495A9DB-3449-45CC-9197-6FD9C8C29C72">
                        <File Id="filB96F911B8B2DE84CED663406AD87D508" KeyPath="yes" Source="$(var.Binaries)\Folder2\Folder3\NewTextFile4.txt" />
                    </Component>
                    <Component Id="cmp873EC3B1583A11ED180C2B5A55082E5E" Guid="B9C57A66-DF0A-480D-83E6-C8E7624974BD">
                        <File Id="fil48004A44BAD8605014735C6A3D6F7004" KeyPath="yes" Source="$(var.Binaries)\Folder2\Folder3\NewTextFile5.txt" />
                    </Component>
                    <Component Id="cmp1614194F7F0694F7AC043F7778BA8109" Guid="B9807535-AC38-41B3-AD9E-A5EB58AC6E12">
                        <File Id="filF11AF0F827CF7E0D02F918F3FD7502AC" KeyPath="yes" Source="$(var.Binaries)\Folder2\Folder3\NewTextFile6.txt" />
                    </Component>
                </Directory>
            </Directory>
        </DirectoryRef>
    </Fragment>
    <Fragment>
        <ComponentGroup Id="CG_Binaries">
            <ComponentRef Id="cmp1CBAE345A14FB4F0343E1FC307F5EE1D" />
            <ComponentRef Id="cmp55726990D3629FC63F840780B5BC7729" />
            <ComponentRef Id="cmpEC9C0276E68BB6794098A24DCF44E0BD" />
            <ComponentRef Id="cmp873EC3B1583A11ED180C2B5A55082E5E" />
            <ComponentRef Id="cmp1614194F7F0694F7AC043F7778BA8109" />
        </ComponentGroup>
    </Fragment>
</Wix>

I have an XSL that changes KeyPath attribute for File to no to comply with ICE38 and add RegistryValue to fix the error when no KeyPaths are set for File.

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"
    xmlns="http://schemas.microsoft.com/wix/2006/wi"
    exclude-result-prefixes="xsl wix">

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

    <xsl:strip-space elements="*" />

    <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
  </xsl:template>

  <!--File keypath to no and add registry keypath-->
  <xsl:template match="wix:Component/wix:File[@Id]">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:attribute name="KeyPath">
        <xsl:text>no</xsl:text>
    </xsl:attribute>
</xsl:copy>
<RegistryValue Root="HKCU" Key="!(wix.RegKeyLocation)" Name="installed" Type="integer" Value="1" KeyPath="yes" />
</xsl:template>

<xsl:template match="wix:Component/wix:File[not(@Id)]">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Here is the Desired output

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="INSTALLDIR">
          <Component Id="SomeName" Guid="A99D16EF-80A3-4C98-A91D-3E95C7BD98AE">
            <RemoveFolder Id="dir7956CF617C38D877C93B5A7D33313596" Directory="dir7956CF617C38D877C93B5A7D33313596" On="uninstall" />
            <RemoveFolder Id="dir2BA1778353CDCC6DE102110034297AC9" Directory="dir2BA1778353CDCC6DE102110034297AC9" On="uninstall"/>
            <RemoveFolder Id="dir52EB3739C4D4F7DFB9D67E41E7687374" Directory="dir52EB3739C4D4F7DFB9D67E41E7687374" On="uninstall"/>
            <RegistryValue Root="HKCU" Key="!(wix.RegKeyLocation)" Name="installed" Type="integer" Value="1" KeyPath="yes" />
        </Component>
        <Directory Id="dir7956CF617C38D877C93B5A7D33313596" Name="Folder1">
            <Component Id="cmp1CBAE345A14FB4F0343E1FC307F5EE1D" Guid="CBB5AF6A-D0A2-464D-BFAA-49F9C102A4F4">
                <File Id="filB5C5E94E5D59CC7DB489F6823F341316" KeyPath="no" Source="$(var.Binaries)\Folder1\NewTextFile0.txt" />
                <RegistryValue Root="HKCU" Key="!(wix.RegKeyLocation)" Name="installed" Type="integer" Value="1" KeyPath="yes" />
            </Component>
        </Directory>
        <Directory Id="dir2BA1778353CDCC6DE102110034297AC9" Name="Folder2">
            <Component Id="cmp55726990D3629FC63F840780B5BC7729" Guid="49CAB541-CEB5-423B-89E2-A42D51AD9E7E">
                <File Id="fil98449FB9E91E1F95E2BA0E6AD712D2E6" KeyPath="no" Source="$(var.Binaries)\Folder2\NewTextFile1.txt" />
                <RegistryValue Root="HKCU" Key="!(wix.RegKeyLocation)" Name="installed" Type="integer" Value="1" KeyPath="yes" />
            </Component>
            <Directory Id="dir52EB3739C4D4F7DFB9D67E41E7687374" Name="Folder3">
                <Component Id="cmpEC9C0276E68BB6794098A24DCF44E0BD" Guid="B495A9DB-3449-45CC-9197-6FD9C8C29C72">
                    <File Id="filB96F911B8B2DE84CED663406AD87D508" KeyPath="no" Source="$(var.Binaries)\Folder2\Folder3\NewTextFile4.txt" />
                    <RegistryValue Root="HKCU" Key="!(wix.RegKeyLocation)" Name="installed" Type="integer" Value="1" KeyPath="yes" />
                </Component>
                <Component Id="cmp873EC3B1583A11ED180C2B5A55082E5E" Guid="B9C57A66-DF0A-480D-83E6-C8E7624974BD">
                    <File Id="fil48004A44BAD8605014735C6A3D6F7004" KeyPath="no" Source="$(var.Binaries)\Folder2\Folder3\NewTextFile5.txt" />
                    <RegistryValue Root="HKCU" Key="!(wix.RegKeyLocation)" Name="installed" Type="integer" Value="1" KeyPath="yes" />
                </Component>
                <Component Id="cmp1614194F7F0694F7AC043F7778BA8109" Guid="B9807535-AC38-41B3-AD9E-A5EB58AC6E12">
                    <File Id="filF11AF0F827CF7E0D02F918F3FD7502AC" KeyPath="no" Source="$(var.Binaries)\Folder2\Folder3\NewTextFile6.txt" />
                    <RegistryValue Root="HKCU" Key="!(wix.RegKeyLocation)" Name="installed" Type="integer" Value="1" KeyPath="yes" />
                </Component>
            </Directory>
        </Directory>
    </DirectoryRef>
</Fragment>
<Fragment>
    <ComponentGroup Id="CG_Binaries">
        <ComponentRef Id="SomeName"/>
        <ComponentRef Id="cmp1CBAE345A14FB4F0343E1FC307F5EE1D" />
        <ComponentRef Id="cmp55726990D3629FC63F840780B5BC7729" />
        <ComponentRef Id="cmpEC9C0276E68BB6794098A24DCF44E0BD" />
        <ComponentRef Id="cmp873EC3B1583A11ED180C2B5A55082E5E" />
        <ComponentRef Id="cmp1614194F7F0694F7AC043F7778BA8109" />
    </ComponentGroup>
</Fragment>
</Wix>

So all i want to achieve is for the transformer to loop through xml, find all Ids of Directory element and create a new element as below:

<Component Id="SomeName" Guid="A99D16EF-80A3-4C98-A91D-3E95C7BD98AE">
    <RemoveFolder Id="dir7956CF617C38D877C93B5A7D33313596" Directory="dir7956CF617C38D877C93B5A7D33313596" On="uninstall" />
    <RemoveFolder Id="dir2BA1778353CDCC6DE102110034297AC9" Directory="dir2BA1778353CDCC6DE102110034297AC9" On="uninstall"/>
    <RemoveFolder Id="dir52EB3739C4D4F7DFB9D67E41E7687374" Directory="dir52EB3739C4D4F7DFB9D67E41E7687374" On="uninstall"/>
    <RegistryValue Root="HKCU" Key="!(wix.RegKeyLocation)" Name="installed" Type="integer" Value="1" KeyPath="yes" />
</Component>

I made some attempts to achieve this but failed miserably. A help would be appreciated.

like image 720
IlirB Avatar asked Nov 09 '22 18:11

IlirB


1 Answers

From the looks of what you expect, all you need to do is a template that the DirectoryRef element...

<xsl:template match="wix:DirectoryRef[@Id='INSTALLDIR']">

And then you can select all the Directory elements with an xsl:for-each construct

<xsl:for-each select=".//wix:Directory[wix:Component/wix:File[@Id]]">
    <RemoveFolder Id="{@Id}" Directory="{@Id}" On="uninstall" />    
</xsl:for-each>

Try this XSLT (I hard-coded the Guid attribute as you will need to use some sort of extension function for that)

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"
    xmlns="http://schemas.microsoft.com/wix/2006/wi"
    exclude-result-prefixes="xsl wix">

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

    <xsl:strip-space elements="*" />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="wix:DirectoryRef[@Id='INSTALLDIR']">
        <xsl:copy>
         <xsl:apply-templates select="@*" />
         <Component Id="SomeName" Guid="A99D16EF-80A3-4C98-A91D-3E95C7BD98AE">
            <xsl:for-each select=".//wix:Directory[wix:Component/wix:File[@Id]]">
                <RemoveFolder Id="{@Id}" Directory="{@Id}" On="uninstall" />    
            </xsl:for-each>
            <RegistryValue Root="HKCU" Key="!(wix.RegKeyLocation)" Name="installed" Type="integer" Value="1" KeyPath="yes" />
        </Component>
        <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>

    <!--File keypath to no and add registry keypath-->
    <xsl:template match="wix:Component/wix:File[@Id]">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
            <xsl:attribute name="KeyPath">
                <xsl:text>no</xsl:text>
            </xsl:attribute>
        </xsl:copy>
        <RegistryValue Root="HKCU" Key="!(wix.RegKeyLocation)" Name="installed" Type="integer" Value="1" KeyPath="yes" />
    </xsl:template>
</xsl:stylesheet>
like image 53
Tim C Avatar answered Nov 15 '22 07:11

Tim C