NSIS基于文件夹创建组件

时间:2018-04-16 19:35:15

标签: dynamic components nsis sections

我正在创建一个安装程序,其中包含可以选择安装的多个应用程序。我想编写我的安装程序脚本,这样每次将新应用程序添加到我的apps目录时都不需要更新它。这可能与NSIS有关吗?我有代码搜索文件夹中的子文件夹并提取其名称,但是在使用它来制作安装程序中的组件时遇到了麻烦。

文件夹结构

-Install_Directory
    -Main_Application    
    -Applications
        -App_A
        -App_B
        -App_C
        -...

当前代码

!macro insert_application_section name
  Section "${name}"
    SetOutPath "{PRJ_BASE}\releases"
    File /nonfatal /r "..\releases\${name}"
  SectionEnd
!macroend

SectionGroup "Applications"
    !insertmacro insert_release_section "App_A"
SectionGroupEnd

安装程序编译但在尝试从宏创建的应用程序的子文件夹中安装文件时,会抛出“写入错误文件”。有没有办法实现我想用NSIS做的事情?

我目前的工作计划是有一个python文件,为Applications的每个子文件夹生成一个部分,并将其放在“autogen_sections.nsh”文件中,实际的安装程序包含在该文件的末尾。这似乎有效,但似乎有点笨重。任何有关如何使用NSIS执行此操作的提示将不胜感激!

1 个答案:

答案 0 :(得分:0)

"{PRJ_BASE}\releases"不是有效路径。 SetOutPath通常设置为$InstDir$InstDir内的子文件夹。如果PRJ_BASE是以$InstDir开头的定义,那么您必须使用"${PRJ_BASE}\releases"而不是"{PRJ_BASE}\releases"

NSIS具有!system命令,允许您在编译时调用外部程序和批处理文件:

; Generate batch file on the fly because this is a self-contained example
!delfile "creatensh.bat"
!appendfile "creatensh.bat" '@echo off$\r$\n'
!appendfile "creatensh.bat" 'cd /D %1$\r$\n'
!appendfile "creatensh.bat" 'FOR /D %%A in (*) DO ($\r$\n'
!appendfile "creatensh.bat" '   >> %2 echo !insertmacro insert_application_section "%%~A"$\r$\n'
!appendfile "creatensh.bat" ')$\r$\n'

; Running the batch file
!define PRJ_BASE "$InstDir\Something"

!macro insert_application_section name
  Section "${name}"
    SetOutPath "${PRJ_BASE}\releases"
    File /nonfatal /r "..\releases\${name}"
  SectionEnd
!macroend

!tempfile tmpnsh
!system '"creatensh.bat" "c:\myfiles\Install_Directory\Applications" "${tmpnsh}"'
!include "${tmpnsh}"
!delfile "${tmpnsh}"

NSIS无法在编译时枚举目录树,您必须使用某种外部应用程序或脚本。