将.NetCore+Electron程序制作成Linux系统的桌面应用程序安装包

很多单位换了国产操作系统,其实就是Linux。原先有个给windows电脑使用的单机程序是用.NetCore+Electron做的,其本质就是一个BS应用,现在需要移植到Linux。需要前端给出Linux环境的Electron包,我这边再把前后端的东西合并成一个安装包。安装后需要将.NetCore添加为服务。windows是使用nsis打包的,参考我的这个文章:windows打包,下文介绍的打包方式适用于基于Debian或Ubuntu的Linux版本
首先创建这几个目录
在这里插入图片描述

DEBIAN目录包含了控制包的安装、配置、升级和卸载过程的脚本。示例中DEBIAN文件夹下包含了control、postinst、postrm三个文件,都是无后缀名的。其中control 文件包含了关于软件包的元数据,如包名、版本、描述、依赖关系等。内容如下

Package: mytestapp
Version: 1.0.7
Section: utils
Priority: optional
Architecture: amd64
Depends: libc6 (>= 2.15), libgcc1 (>= 1:3.0), libstdc++6 (>= 4.1.1)
Maintainer:xxxxxx公司
Description:xxxxxxx管理系统

需要注意Linux的换行符用的是LF,在VS的右下角选一下
在这里插入图片描述
postinst是安装时执行的脚本,脚本启动了服务,创建了桌面快捷方式,内容如下:

#!/bin/bash
set -e
systemctl daemon-reload
systemctl start mytestapp
systemctl enable mytestapp

desktop_link_path=/usr/share/applications/mytestapp.desktop
for file in /home/*; do
    if [ -d "$file" ]; then
        desktop_path=$file/桌面
        if [ -d "$desktop_path" ]; then
            cp $desktop_link_path $desktop_path/         #创建桌面快捷方式
            chmod 777 $desktop_path/mytestapp.desktop
        fi
        desktop_path=$file/Desktop
        if [ -d "$desktop_path" ]; then
            cp $desktop_link_path $desktop_path/         #创建桌面快捷方式
            chmod 777 $desktop_path/mytestapp.desktop
        fi
    fi
done
exit 0

postrm是卸载时执行的脚本,内容就是关闭服务,删除桌面快捷方式,具体如下

#!/bin/bash
set -e
systemctl stop mytestapp

for file in /home/*; do
    if [ -d "$file" ]; then
        desktop_path=$file/桌面
        if [ -d "$desktop_path" ]; then
            if [ -f "$desktop_path/mytestapp.desktop" ]; then
                rm $desktop_path/mytestapp.desktop
            fi
        fi
        desktop_path=$file/Desktop
        if [ -d "$desktop_path" ]; then
            if [ -f "$desktop_path/mytestapp.desktop" ]; then
                rm $desktop_path/mytestapp.desktop
            fi
        fi
    fi
done

exit 0

opt目录就是所有第三方应用安装后的目录,打包时opt目录下包含的东西,在安装时会写到用户的电脑的对应目录,比如这里在opt下创建了mytestapp目录,包含后端的.netcore程序,Client目录包含前端electron客户端,那么用户安装时候也会将这些东西放到opt的mytestapp目录下。

usr目录是用于创建服务和桌面快捷方式的,和opt类似,也是打包时里面有的内容,安装时也会写到用户的对应目录下。
usr\lib\systemd\system\mytestapp.service文件就是用来创建服务的,内容如下

[Unit]
Description=xxxxxxx系统守护进程	    #当前配置文件的描述信息
After=network.target    #表示当前服务是在那个服务后面启动,一般定义为网络服务启动后启动

[Service]
WorkingDirectory=/opt/mytestapp #需要启动的服务所在的目录,就是上文提到的那个opt目录
ExecStart=/opt/mytestapp/mytestapp start #具体需要启动的程序,我这里是不依赖框架的打包方式,如果是依赖框架的话,就在目标电脑上安装.netcore运行时,并使用dotnet命令启动

# 程序崩溃后自动启动
Restart=always
RestartSec=0
KillSignal=SIGINT
SyslogIdentifier=mytestapp.service
# 用户角色
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
# The default value is 90 seconds for most distributions.
TimeoutStopSec=90

[Install]
WantedBy=multi-user.target

usr\share\applications\mytestapp.desktop是桌面快捷方式,这里需要指向前端的Electron程序

[Desktop Entry]
Name=xxxxxxxx管理系统
Type=Application
Exec="/opt/mytestapp/Client/mytestapp" %U
Terminal=false
Icon=/opt/mytestapp/hl.png #图标位置
Categories=Utility;

打包的时候将上文图中的Linux目录下的所有文件拷贝到一台Linux的电脑上,创建一个打包脚本,放到和你的DEBIAN目录同级上,脚本内容如下。直接运行这个脚本就能得到一个名为mytestapp.deb的安装包

chmod 0755 ./Linux/opt/mytestapp/Client/mytestapp;#给前端这个程序权限
dpkg-deb --build --root-owner-group ./Linux mytestapp.deb;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值