Inject MSI Property at Compile-Time: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(Initial Release)
 
(Undo revision 19493 by Zinthose (talk))
 
(3 intermediate revisions by the same user not shown)
Line 5: Line 5:
== Macro ==
== Macro ==
<highlight-nsis>/********************
<highlight-nsis>/********************
     !MSIProperty
     !MSIPropertyGet
         This macro provides a compile-time ability to inject Microsoft Installer Database file properties at compile time.
         This macro provides a compile-time ability to inject Microsoft Installer Database file properties at compile time.
          
          
     Example:
     Example:
         ${!MSIProperty} "SomePackage.ProductName"    "SomePackage.msi" "ProductName"
         ${!MSIPropertyGet} "SomePackage.ProductName"    "SomePackage.msi" "ProductName"
         ${!MSIProperty} "SomePackage.ProductVersion" "SomePackage.msi" "ProductVersion"
         ${!MSIPropertyGet} "SomePackage.ProductVersion" "SomePackage.msi" "ProductVersion"
          
          
         !define DisplayName `${SomePackage.ProductName} v${SomePackage.ProductVersion}`
         !define DisplayName `${SomePackage.ProductName} v${SomePackage.ProductVersion}`
        OutFile Test.exe
         Name `${DisplayName} Special Build`
         Name `${DisplayName} Special Build`
        Section
        SectionEnd
  ********************/
  ********************/
!verbose push
!verbose push
!verbose 0
!verbose 0
!define !MSIProperty `!insertmacro _!MSIProperty`
!define !MSIPropertyGet `!insertmacro _!MSIPropertyGet`
!macro _!MSIProperty _DefineToCreate _MSIFilePath _PropertyToRead
!macro _!MSIPropertyGet _DefineToCreate _MSIFilePath _PropertyToRead
     !verbose Push
     !verbose Push
     !verbose 0
     !verbose 0
Line 28: Line 31:
     !ifdef `${__DefineToCreate}`
     !ifdef `${__DefineToCreate}`
         !verbose 1
         !verbose 1
         !error "!MSIProperty: Value previously defined! [${__DefineToCreate}=${${__DefineToCreate}}]"
         !error "!MSIPropertyGet: Value previously defined! [${__DefineToCreate}=${${__DefineToCreate}}]"
     !else
     !else
         !tempfile MSIProperty_VBScript
         !tempfile MSIPropertyGet_VBScript
         !appendfile `${MSIProperty_VBScript}` `Set A=WScript.Arguments:Set B=CreateObject("WindowsInstaller.Installer").OpenDatabase(A(2),0)`
         !appendfile `${MSIPropertyGet_VBScript}` `Set A=WScript.Arguments:Set B=CreateObject("WindowsInstaller.Installer").OpenDatabase(A(2),0)`
         !appendfile `${MSIProperty_VBScript}` `.OpenView("SELECT Value FROM Property WHERE Property='"&A(3)&"'"):B.Execute:Set C=CreateObjec`
         !appendfile `${MSIPropertyGet_VBScript}` `.OpenView("SELECT Value FROM Property WHERE Property='"&A(3)&"'"):B.Execute:Set C=CreateObjec`
         !appendfile `${MSIProperty_VBScript}` `t("Scripting.Filesystemobject"):Set D=C.OpenTextFile(A(0),2,1):D.WriteLine "!define $\`"&A(1)`
         !appendfile `${MSIPropertyGet_VBScript}` `t("Scripting.Filesystemobject"):Set D=C.OpenTextFile(A(0),2,1):D.WriteLine "!define $\`"&A(1)`
         !appendfile `${MSIProperty_VBScript}` `&"$\` $\`"&B.Fetch.StringData(1)&"$\`":D.Close`
         !appendfile `${MSIPropertyGet_VBScript}` `&"$\` $\`"&B.Fetch.StringData(1)&"$\`":D.Close`
         !tempfile MSIProperty_Result
         !tempfile MSIPropertyGet_Result
         !system `WScript.exe "${MSIProperty_VBScript}" //B //E:VBSCRIPT //T:30 "${MSIProperty_Result}" "${__DefineToCreate}" "${_MSIFilePath}" "${_PropertyToRead}"` = 0
         !system `WScript.exe "${MSIPropertyGet_VBScript}" //B //E:VBSCRIPT //T:30 "${MSIPropertyGet_Result}" "${__DefineToCreate}" "${_MSIFilePath}" "${_PropertyToRead}"` = 0
         !include "${MSIProperty_Result}"
         !include "${MSIPropertyGet_Result}"
        !system `Notepad "${MSIProperty_Result}"`
         !delfile `${MSIPropertyGet_VBScript}`
         !delfile `${MSIProperty_VBScript}`
         !delfile `${MSIPropertyGet_Result}`
         !delfile `${MSIProperty_Result}`
         !undef MSIPropertyGet_VBScript
         !undef MSIProperty_VBScript
         !undef MSIPropertyGet_Result
         !undef MSIProperty_Result
         !ifndef `${__DefineToCreate}`
         !ifndef `${__DefineToCreate}`
             !error "!MSIProperty: Value was not defined! [${__DefineToCreate}] Ensure the MSI file exists and is not locked from reading"  
             !error "!MSIPropertyGet: Value was not defined! [${__DefineToCreate}] Ensure the MSI file exists and is not locked from reading"  
         !else if `${${__DefineToCreate}}` == ``
         !else if `${${__DefineToCreate}}` == ``
             !warning "!MSIProperty: Value is a zero length string! [${__DefineToCreate}=${${__DefineToCreate}}]"
             !warning "!MSIPropertyGet: Value is a zero length string! [${__DefineToCreate}=${${__DefineToCreate}}]"
         !endif
         !endif
     !endif
     !endif

Latest revision as of 19:22, 25 February 2011

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