Inject MSI Property at Compile-Time

From NSIS Wiki
Jump to navigationJump to search
Author: Zinthose (talk, contrib)


About

This macro provides a compile-time ability to inject Microsoft Installer Database file properties at compile time. It uses a dynamically created VBScript to open the MSI file and read the property then creates the requested property value.

Macro

/********************
    !MSIPropertyGet
        This macro provides a compile-time ability to inject Microsoft Installer Database file properties at compile time.
 
    Example:
        ${!MSIPropertyGet} "SomePackage.ProductName"    "SomePackage.msi" "ProductName"
        ${!MSIPropertyGet} "SomePackage.ProductVersion" "SomePackage.msi" "ProductVersion"
 
        !define DisplayName `${SomePackage.ProductName} v${SomePackage.ProductVersion}`
        OutFile Test.exe
        Name `${DisplayName} Special Build`
        Section
        SectionEnd
 ********************/
!verbose push
!verbose 0
!define !MSIPropertyGet `!insertmacro _!MSIPropertyGet`
!macro _!MSIPropertyGet _DefineToCreate _MSIFilePath _PropertyToRead
    !verbose Push
    !verbose 0
    !if `${_DefineToCreate}` == ``
        !define __DefineToCreate `${_PropertyToRead}`
    !else
        !define __DefineToCreate `${_DefineToCreate}`
    !endif
    !ifdef `${__DefineToCreate}`
        !verbose 1
        !error "!MSIPropertyGet: Value previously defined! [${__DefineToCreate}=${${__DefineToCreate}}]"
    !else
        !tempfile MSIPropertyGet_VBScript
        !appendfile `${MSIPropertyGet_VBScript}` `Set A=WScript.Arguments:Set B=CreateObject("WindowsInstaller.Installer").OpenDatabase(A(2),0)`
        !appendfile `${MSIPropertyGet_VBScript}` `.OpenView("SELECT Value FROM Property WHERE Property='"&A(3)&"'"):B.Execute:Set C=CreateObjec`
        !appendfile `${MSIPropertyGet_VBScript}` `t("Scripting.Filesystemobject"):Set D=C.OpenTextFile(A(0),2,1):D.WriteLine "!define $\`"&A(1)`
        !appendfile `${MSIPropertyGet_VBScript}` `&"$\` $\`"&B.Fetch.StringData(1)&"$\`":D.Close`
        !tempfile MSIPropertyGet_Result
        !system `WScript.exe "${MSIPropertyGet_VBScript}" //B //E:VBSCRIPT //T:30 "${MSIPropertyGet_Result}" "${__DefineToCreate}" "${_MSIFilePath}" "${_PropertyToRead}"` = 0
        !include "${MSIPropertyGet_Result}"
        !delfile `${MSIPropertyGet_VBScript}`
        !delfile `${MSIPropertyGet_Result}`
        !undef MSIPropertyGet_VBScript
        !undef MSIPropertyGet_Result
        !ifndef `${__DefineToCreate}`
            !error "!MSIPropertyGet: Value was not defined! [${__DefineToCreate}] Ensure the MSI file exists and is not locked from reading" 
        !else if `${${__DefineToCreate}}` == ``
            !warning "!MSIPropertyGet: Value is a zero length string! [${__DefineToCreate}=${${__DefineToCreate}}]"
        !endif
    !endif
    !undef __DefineToCreate
    !verbose pop
!macroend
!verbose pop