exe2msi.psm1



function Get-ScriptDirectory{
    (Get-Item $MyInvocation.ScriptName).DirectoryName
}


<#
.DESCRIPTION
The source folder should contains a file install.cmd which execute the installation
 
.EXAMPLE
New-MsiFromExe -Name WinMerge -Version 2.16.4 -SourcesFolder C:\dev\package\src
This will create an msi package from the files in C:\dev\package\src
#>

function New-MsiFromExe{
param(
    $Name,
    $Manufacturer='Corp.',
    [Version] $Version,
    $SourcesFolder

)

    $MODULE_PATH = (Get-Module exe2msi).ModuleBase
    $WIX = $MODULE_PATH + '\WiX Toolset v3.11\bin'

    $xml = [xml] (gc "$MODULE_PATH\Product-Template.wxs")

    $xml.Wix.Product.Name         = $Name
    $xml.Wix.Product.Manufacturer = $Manufacturer
    $xml.Wix.Product.Version      = $Version.ToString()
    $xml.Wix.Product.UpgradeCode  = ((New-Guid).Guid).ToString()

    $xml.Wix.Fragment[-1].ComponentGroup.Component.Guid = ((New-Guid).Guid).ToString()

    ls "src" | % {
        $file =  $_
        $f= $xml.CreateElement('File', $xml.Wix.NamespaceURI)
        $f.SetAttribute('Id', $file.BaseName.Replace('-', '_'))
        $f.SetAttribute('Source', "src\$($file.Name)")
        $f.RemoveAttribute('xmlns')
        $xml.Wix.Fragment[-1].ComponentGroup.Component.AppendChild($f)

    }

    $fileName = "MsiWrapper_$($Name)_$($Version)"
    $outName = "$pwd\$fileName.wxs"
    Write-Host "Save file to '$outName'"
    $xml.Save($outName)

    . "$WIX\candle.exe" "$($fileName).wxs"
    . "$WIX\light.exe" "$($fileName).wixobj" -ext WixUtilExtension.dll
}