首页 > 解决方案 > 如何为 [Setup] AppVersion 使用编译时环境变量?

问题描述

我正在使用 InnoSetup 6(Windows 10、Visual Studio Code 和 WSL 作为我的 shell)来创建安装程序。这个 ISCC 编译器是从一个自动化脚本中调用的。该脚本负责设置保存生成的安装程序版本和生成的安装程序名称的环境变量。它们都旨在用于iscc.exe脚本调用的 InnoSetup 安装程序编译器 ( )。我使用 envvars 是因为我不想用硬连线的版本号弄乱我的 ISS 文件。

我认为在 Innosetup 中使用环境变量是可能的,根据 StackOverflow 上的这个问题(Can one use environment variables in Inno Setup scripts?)。

不幸的是,Innosetup 抱怨"The [Setup] section must include an AppVersion or AppVerName directive.". 就像GetEnv()产生一个空字符串一样。

我尝试像这样从命令行手动调用 ISCC:

export JFROG_DEFAULTGROUPS_VERSION=1.0.0-1 && [path..]\iscc.exe myiss.iss

甚至使用以下命令导出环境变量export

export JFROG_DEFAULTGROUPS_VERSION=1.0.0-1

,然后检查环境变量是否实际设置并启动编译器: env [path..]\iscc.exe myiss.iss

在这种情况下,env产生以下内容:

JFROG_DEFAULTGROUPS_VERSION=1.0.0-1
SHELL=/bin/bash
PATH=[...]
[...]

这让我觉得根据我的 shell (WSL) 设置了环境。但两者都没有给出更好的结果。

我的 ISS 文件如下:

#define MyInstName "MyApp"
#define MyInstVersion GetEnv("JFROG_DEFAULTGROUPS_VERSION")

[Setup]
AppName={#MyInstName}
AppVersion={#MyInstVersion}

DefaultGroupName={#MyInstName}
OutputBaseFilename=MyApp_{#MyInstVersion}_setup
AppendDefaultDirName=no
DefaultDirName={commonpf}

[Components]
Name: default_groups_conf; Description: Default Groups Configuration; Types: full


[Run]

[Code]
function InitializeSetup(): Boolean;
begin
    if not ('{#MyInstVersion}' = '') then begin
        MsgBox('MyInstVersion Env Var not set.', mbInformation, MB_OK);
        Abort;
    end;
end;

这有什么问题?如何在 [Setup] 和 [Code] 部分正确捕获环境变量?

标签: environment-variableswindows-subsystem-for-linux

解决方案


我已经使用以下步骤实现了这一点。请注意,我使用 git-bash shell 执行了 iscc。在 git-bash shell 中,我定义环境变量并使用“export <ENV_VAR>”命令公开它们,以便 iscc 可以获得它的值

1. In .iss file, I have added following environment variables using #define

    #define XXXAppVersion GetEnv('XXX_VERSION')
    #define YYYRootDir GetEnv('YYY_ROOR_DIR')

2. use these in `[Setup]` section of .iss file

    AppVersion={#XXXAppVersion} 
    OutputDir={#YYYRootDir}/bin

3. After these changes, open git-bash shell and define these environment variables as follows

export XXX_VERSION="4.0.0.1"
export YYY_ROOT_DIR="C:\YYY_Home"

4. then execute following iscc using .iss file as follows in same shell which executes successfully. 

    iscc.exe "<.iss file>"

推荐阅读