electron编写nsis脚本,设置安装向导

需求:利用eletron-builder打包程序时发现,每次安装完后需要手动打开安装目录,找到配置文件修改配置参数,比较麻烦,也不够帅气,能否在安装时就提示用户输入参数呢。

nsis(一个开源的 Windows 系统下安装程序制作程序),electron-builder提供了windows下nsis参数配置:https://www.electron.build/configuration/nsis.html#custom-nsis-script

package.json配置如下:


image.png

image.png

installer.nsh编写自定义图形界面脚本:

!include nsDialogs.nsh
!include LogicLib.nsh

#OutFile nsDialogs.exe
#RequestExecutionLevel user
#ShowInstDetails show

Var Dialog
Var name
Var entry
Var website
Var openMode

Page custom pgPageCreate pgPageLeave

Function pgPageCreate

    nsDialogs::Create 1018
    Pop $Dialog

    ${If} $Dialog == error
        Abort
    ${EndIf}

    ${NSD_CreateGroupBox} 10% 10u 80% 100u "Settings"
    Pop $0

        ${NSD_CreateLabel} 20% 26u 20% 10u "name:"
        Pop $0

        ${NSD_CreateText} 40% 24u 40% 12u "electron"
        Pop $name

        ${NSD_CreateLabel} 20% 40u 20% 10u "entry:"
        Pop $0

        ${NSD_CreateText} 40% 38u 40% 12u "https://www.baidu.com"
        Pop $entry

        ${NSD_CreateLabel} 20% 54u 20% 10u "website:"
        Pop $0

        ${NSD_CreateText} 40% 52u 40% 12u "https://www.baidu.com"
        Pop $website
        
        ${NSD_CreateLabel} 20% 68u 20% 10u "openMode:"
        Pop $0

        ${NSD_CreateText} 40% 66u 40% 12u "false"
        Pop $openMode

    nsDialogs::Show
FunctionEnd

Function PgPageLeave
    ${NSD_GetText} $name $0
    ${NSD_GetText} $entry $1
    ${NSD_GetText} $website $2
    ${NSD_GetText} $openMode $3

    FileOpen $9 $DESKTOP\.electron w
    FileWrite $9 "{name:$0,entry:$1,website:$2,openMode:$3}"
    FileClose $9
    SetFileAttributes $DESKTOP\.electron NORMAL
FunctionEnd

Section
SectionEnd

效果如下:


image.png

在pageLeave的时候,将用户输入的参数写入到配置文件(.electron)中,electron启动后,在主进程中读取.electron,获取参数。

传送门:https://omega.idv.tw/nsis/Contents.html

推荐阅读更多精彩内容