NSIS - 安装程序:将套件版本与头文件定义变量进行比较,如果错误定义变量值则更新

Posted

技术标签:

【中文标题】NSIS - 安装程序:将套件版本与头文件定义变量进行比较,如果错误定义变量值则更新【英文标题】:NSIS - Installer: Compare Kit Version with Header file define variable and update if wrong define variable value 【发布时间】:2022-01-06 16:24:28 【问题描述】:

我是 NSIS 安装程序脚本的新手。我创建了一个 NSIS 脚本来替换我们的 WISE 安装程序脚本。我想编写一个逻辑来在编译期间将 Kit 版本(文件夹名称说 A1.2.3,其中 A 表示 Alhpa 套件)与 !define APP_Version(on header) 文件进行比较,如果定义变量错误,我想用正确的更新头文件价值。这是我想出的逻辑,但我不知道如何更新更新头文件。

部分

System::Call "kernel32::GetCurrentDirectory(i $NSIS_MAX_STRLEN, t .r0)"

$WordFind "$0" "\" "-1" $R0

!if "$R0" != "$APP_CLASSKIT$APP_VERSION"
    MessageBox MB_OK "No Match: $R0"
    !undef verifykit
    !define APP_VERSION "$R0"
    
!else
    MessageBox MB_OK "Match: $APP_CLASSKIT$APP_VERSION"
    !undef verifykit
    !define APP_VERSION "$APP_CLASSKIT$APP_VERSION"
!endif

部分结束

【问题讨论】:

【参考方案1】:

您不能像这样混合和匹配运行时和编译时指令。如果你想在你编译的机器上检测到这个,你只能使用以!开头的指令。

如果你只是想检测问题:

!macro PrepareExample
!system 'mkdir ".\A1.2.3"'
!macroend
!insertmacro PrepareExample ; For ***


!define APP_CLASSKIT A 
!define APP_VERSION 1.2.3

!if ! /FileExists ".\$APP_CLASSKIT$APP_VERSION\*"
!error "Incorrect kit version, $APP_CLASSKIT$APP_VERSION not found!"
!endif

Section
SetOutPath $InstDir
File /r ".\$APP_CLASSKIT$APP_VERSION"
SectionEnd

编译器的编译时部分并不适合搜索具有特定模式的文件夹。最好调用一个批处理/powershell/wsh 脚本并让它创建一个可以包含的 .nsh 文件:

!macro PrepareExample
!system 'mkdir ".\A1.2.3"'
!delfile 'findkit.bat'
!appendfile 'findkit.bat' '@echo off$\r$\n'
!appendfile 'findkit.bat' 'echo.TODO: Use FOR or some other batch command to find the correct kit folder.$\r$\n'
!appendfile 'findkit.bat' 'for /D %%A in (??.*) do ($\r$\n'
!appendfile 'findkit.bat' ' echo.This batch file just fakes it$\r$\n'
!appendfile 'findkit.bat' ' echo !define APP_CLASSKIT A >> "%~1"$\r$\n'
!appendfile 'findkit.bat' ' echo !define APP_VERSION 1.2.3 >> "%~1"$\r$\n'
!appendfile 'findkit.bat' ')$\r$\n'
!macroend
!insertmacro PrepareExample ; For ***


!tempfile KitNsh
!system '"findkit.bat" "$KitNsh"'
!include "$KitNsh"
!delfile "$KitNsh"
!ifndef APP_CLASSKIT | APP_VERSION
  !error "No kit detected"
!endif
!echo "Using kit $APP_CLASSKIT$APP_VERSION"

Section
...
SectionEnd

【讨论】:

感谢安德斯的回复。根据您的建议,我创建了一个脚本来创建一个批处理文件,并在执行时为 Build.nsi(主脚本)创建一个 compiler.nsh(头文件)。但是,我很困惑如何将它集成到我当前的主脚本中。 |我希望我的 Build.nsi 运行脚本首先创建并执行批处理文件,然后创建 compiler.nsh 文件,该文件将位于 Build.nsi 中以访问定义的变量以构建套件。 使用 !system 在您的 .nsi 中运行批处理,就像我在第二个示例中所做的那样。 我正在尝试解析我的 !appendfile 语句中的变量,例如 !appendfile 'findkit.bat' ' echo !define CLASSKIT $APP_CLASSKIT >> "%~1"$\r$\ n' 但 $APP_CLASSKIT 没有得到解决。我错过了什么吗?还是我需要添加任何转义命令来解析这个变量? 也许当时没有定义。无论如何,您不必使用 !appendfile,您可以在文本编辑器中编写一个普通的 bat 文件。

以上是关于NSIS - 安装程序:将套件版本与头文件定义变量进行比较,如果错误定义变量值则更新的主要内容,如果未能解决你的问题,请参考以下文章

extern 用法,全局变量与头文件(重复定义)

使用NSIS制作Windows安装程序快速入门

NSIS安装包脚本

NSIS - 强制用户选择不同的目录

NSIS问题

NSIS脚本