NSI脚本等同于bat脚本

时间:2015-09-03 18:17:03

标签: batch-file nsis

我在.bat文件中写了以下命令,这对我很有用。现在我正在尝试编写基于NSIS的图形安装程序,并需要使用NSIS重现相同的内容。我不明白我是怎么做到的。

set PATH=%PATH%;C:\RailsInstaller\Ruby2.1.0\bin
set RAILS_ENV=production
cd C:\myapp
bundle install --local

我想知道如何编写一个nsi脚本,它等同于上面的命令在shell中一个接一个地运行。

1 个答案:

答案 0 :(得分:7)

在更新NSIS中的%PATH%时需要小心,因为NSIS字符串长度限制短于%PATH%长度限制。您可以通过直接调用Windows API来解决此问题:

!define ERROR_ENVVAR_NOT_FOUND 203

!if "${NSIS_PTR_SIZE}" <= 4
!include LogicLib.nsh
Function ProcessEnvAppendPath ; IN:Path OUT:N/A
System::Store S
Pop $1
System::Call 'KERNEL32::GetEnvironmentVariable(t "PATH", t, i0)i.r0'
${If} $0 = 0
    System::Call 'KERNEL32::SetEnvironmentVariable(t "PATH", tr1)'
${Else}
    StrLen $2 $1
    System::Call '*(&t$0,&t1,&t$2)i.r9'
    System::Call 'KERNEL32::GetEnvironmentVariable(t "PATH", ir9, ir0)i.r0'
    StrCpy $2 0
    ${IfThen} $0 > 0 ${|} IntOp $2 $0 - 1 ${|} 
    System::Call '*$9(&t$2,&t1.r2)' ; Store the last character from %PATH% in $2
    StrCpy $3 ';'
    ${IfThen} $2 == ';' ${|} StrCpy $3 "" ${|}
    System::Call 'KERNEL32::lstrcat(ir9, tr3)' ; Append ";" or ""
    System::Call 'KERNEL32::lstrcat(ir9, tr1)'
    System::Call 'KERNEL32::SetEnvironmentVariable(t "PATH", ir9)'
    System::Free $9
${EndIf}
System::Store L
FunctionEnd
!endif

Section
Push "C:\RailsInstaller\Ruby2.1.0\bin"
Call ProcessEnvAppendPath
System::Call 'KERNEL32::SetEnvironmentVariable(t "RAILS_ENV", t "production")'
SetOutPath "C:\myapp" ; Sets the process working directory
ExecWait '"bundle" install --local' ; You should probably use the full path to bundle here.
SectionEnd

另一种方法是动态编写批处理文件并使用nsExec插件执行它。