!commands - One NSIS command instead of multiple

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


Description

Similar to MultiCmd, this macro allows the programmer to write a multiple line statement in a single line of code. The primary difference in this version is that the process is handled entirely by the compiler and does not rely on an external/pre-compiled executable. Thus allowing for increased portability.

Usage

!commands example.nsi

OutFile Test.exe
ShowInstDetails show
Caption "!commands Example"
SubCaption 3 " "
SubCaption 4 " "
 
## Uncomment to see dynamically generated include files after they are created.
;   !define Debug
 
## Were the magic lives
    !include !commands.nsh
 
## Simple define / undefines
    ${!commands} `!define hello "Hello"|!define hw "${hello} World"|!undef hello`
 
    Section Defines
        DetailPrint "${hw}"
        DetailPrint "${hello} <-- This was undefined."
        DetailPrint `The Delimiter is set to: ${CommandDelimiter.Get}`
    SectionEnd
 
## Change the Delimiter to a tilda so we can make a macro that uses a MessageBox
    ${CommandDelimiter.Set} "~"
    ${!commands} `!define OneLineMacro "!insertmacro _OneLineMacro"~!macro _OneLineMacro _Data~MessageBox MB_OK|MB_ICONEXCLAMATION  "Multiline Madness$\n_____________________$\n$\n${_DATA}"~!macroend`
 
    Section Macro
        ${OneLineMacro} "Imagine$\nThe possibilities!"
        DetailPrint `The Delimiter is now set to: ${CommandDelimiter.Get}`
    SectionEnd
 
## Uncommend these lines to see the error when attempting to execute more than 101 commands
;   ${CommandDelimiter.Set} "|"
;   ${!commands} `!echo '1'|!echo '2'|!echo '3'|!echo '4'|!echo '5'|!echo '6'|!echo '7'|!echo '8'|!echo '9'|!echo '10'|!echo '11'|!echo '12'|!echo '13'|!echo '14'|!echo '15'|!echo '16'|!echo '17'|!echo '18'|!echo '19'|!echo '20'|!echo '21'|!echo '22'|!echo '23'|!echo '24'|!echo '25'|!echo '26'|!echo '27'|!echo '28'|!echo '29'|!echo '30'|!echo '31'|!echo '32'|!echo '33'|!echo '34'|!echo '35'|!echo '36'|!echo '37'|!echo '38'|!echo '39'|!echo '40'|!echo '41'|!echo '42'|!echo '43'|!echo '44'|!echo '45'|!echo '46'|!echo '47'|!echo '48'|!echo '49'|!echo '50'|!echo '51'|!echo '52'|!echo '53'|!echo '54'|!echo '55'|!echo '56'|!echo '57'|!echo '58'|!echo '59'|!echo '60'|!echo '61'|!echo '62'|!echo '63'|!echo '64'|!echo '65'|!echo '66'|!echo '67'|!echo '68'|!echo '69'|!echo '70'|!echo '71'|!echo '72'|!echo '73'|!echo '74'|!echo '75'|!echo '76'|!echo '77'|!echo '78'|!echo '79'|!echo '80'|!echo '81'|!echo '82'|!echo '83'|!echo '84'|!echo '85'|!echo '86'|!echo '87'|!echo '88'|!echo '89'|!echo '90'|!echo '91'|!echo '92'|!echo '93'|!echo '94'|!echo '95'|!echo '96'|!echo '97'|!echo '98'|!echo '99'|!echo '100'|!echo '101'|!echo '102'`

NSIS Header

!commands.nsh

## Perform multiple commands from a single line
!ifndef ___!COMMANDS___
;!echo `###################`
;!echo `## !COMMANDS.NSH ##`
;!echo `##     v0.01     ##`
;!echo `###################`
!verbose push
!verbose 0
 
!define ___!COMMANDS___
 
!ifndef CommandDelimiter
    !define CommandDelimiter.Set `!insertmacro _CommandDelimiter`
    !define CommandDelimiter.Get `${_CommandDelimiter}`
    !define _CommandDelimiter "|"
    !macro _CommandDelimiter _DELIMITER
        !echo `CommandDelimiter = "${_DELIMITER}"`
        !verbose push
        !verbose 0
        ${!redef} _CommandDelimiter `${_DELIMITER}`
        !verbose pop
    !macroend
!endif
 
## Redefine in one line
## ${!redef} DefineName DefineValue
    !ifndef !redef 
        !define !redef "!insertmacro _!redef"
        !ifmacrondef _!redef
            !macro _!redef _DEFNAME _DEFVALUE
                !ifdef ${_DEFNAME}
                    !undef ${_DEFNAME}
                !endif
                !define ${_DEFNAME} `${_DEFVALUE}`
            !macroend
        !endif
    !endif
 
!define !doCommand "!insertmacro _!doCommand"
!macro _!doCommand _string
    !if `_string` != ""
        !searchparse /noerrors `${_string}` "" _COMMAND "${_CommandDelimiter}" _MORECOMMANDS
        !if `${_COMMAND}` != ``
            !verbose 4
            !ifdef NULL
                !undef NULL
            !endif
            !searchreplace _COMMAND `${_COMMAND}` `$\n` `$\${NULL}n`
            !searchreplace _COMMAND `${_COMMAND}` `$\r` `$\${NULL}r`
            !searchreplace _COMMAND `${_COMMAND}` `$\t` `$\${NULL}t`
            !define NULL
            !appendfile `${_commandsFile}` `${_COMMAND}$\n`
            !verbose 0
        !endif
        !undef _COMMAND
        !if `${_MORECOMMANDS}` == ''
            !undef _MORECOMMANDS
        !endif
    !endif
!macroend
 
!define !doCommand10 "!insertmacro _!doCommand10"
!macro _!doCommand10
    !ifdef _MORECOMMANDS
        ${!doCommand} `${_MORECOMMANDS}`
        ${!doCommand} `${_MORECOMMANDS}`
        ${!doCommand} `${_MORECOMMANDS}`
        ${!doCommand} `${_MORECOMMANDS}`
        ${!doCommand} `${_MORECOMMANDS}`
        ${!doCommand} `${_MORECOMMANDS}`
        ${!doCommand} `${_MORECOMMANDS}`
        ${!doCommand} `${_MORECOMMANDS}`
        ${!doCommand} `${_MORECOMMANDS}`
        ${!doCommand} `${_MORECOMMANDS}`
    !endif
!macroend
 
!define !commands '!insertmacro _!commands'
!macro _!commands _string
    !verbose push
    !verbose 0
 
    !tempfile _commandsFile
    !ifdef DEBUG 
        !appendfile `${_commandsFile}` `## DEBUG MODE$\n## The following is the include file dynamically created.$\n##$\n$\n`
    !endif
    !appendfile `${_commandsFile}` `!verbose pop$\n`
    ${!doCommand} `${_string}`
    ${!doCommand10}
    ${!doCommand10}
    ${!doCommand10}
    ${!doCommand10}
    ${!doCommand10}
    ${!doCommand10}
    ${!doCommand10}
    ${!doCommand10}
    ${!doCommand10}
    ${!doCommand10}
    !appendfile `${_commandsFile}` `!verbose push$\n!verbose 0$\n`
    !ifdef DEBUG
        !system `notepad "${_commandsFile}"`
    !endif
    !include `${_commandsFile}`
    !delfile `${_commandsFile}`
    !undef _commandsFile
 
    !ifdef _MORECOMMANDS
        !verbose 4
        !echo `ERROR: Unable to process the following commands: "${_MORECOMMANDS}"`
        !echo `ERROR: '!commands' can only process 101 commands.`
        !echo `ERROR: To many commands in queue, Aborting Compilation`
        !verbose 0
        !error 
    !endif
    !verbose pop
!macroend
!verbose pop
!endif