Linux shell 实现node-webkit的自动跨平台打包


今天下午发现了个好玩的东西(node-webkit),这东西有一直是我想实现的功能:使用html编写桌面应用,实现跨平台;

具体实现方法:结合chrome浏览器内核和node.js搭建一个跨平台的应用运行环境(node-webkit is an app runtime based on Chromium and node.js.)

可是这东西好像还没有一个像样的IDE,没有自动打包成应用的功能,虽然手动打包的步骤也不麻烦,但作为程序猿的我,不得不用更“高级”方法实现啦~~O(∩_∩)O哈哈~

先说说手动打包吧:

windows:将你的app文件用zip压缩打包,更名为成app.nw,然后直接将nw.exe和app.nw合并为app.exe即可(注意nw.exe文件夹下的库和nw.pak不能删)。

linux:将你的app文件用zip压缩打包,更名为成app.nw,然后直接将nw和app.nw合并为app即可(注意要给app文件加上运行权限:chmod +x app)

mac:node-webkit.app/Contents/Resources/,将system-info打包成app.nw后,放入该目录即可(还有一些图标文件也要处理,由于我还没在mac平台上测试,就先不详述了,等我上我的黑苹果测试通过了再说吧)

我是在Ubuntu上打包的,不喜欢用windows的命令行,在windows下工作的伙伴们可以试试cygwin.当然我不是啥linux大神啦,我是菜鸟级的,之前写过最长的shell代码应该没有超过30行,今天破纪录了,空了好多空行,看了下有90行了,O(∩_∩)O哈哈~废话不多说了,先直接上代码了:

#!/bin/bash
# by VellBibi
# init your app info
app_name="vview"
app_dir="/files/Web/Projects/vview/"

win_nw_zipfile="/files/Web/Codes/node-webkit-v0.9.2-win-ia32.zip"
linux_nw_tarfile="/files/Web/Codes/node-webkit-v0.9.2-linux-x64.tar.gz"
mac_nw_zipfile="/files/Web/Codes/node-webkit-v0.9.2-osx-ia32.zip"

# read pack_flag
w=false && l=false && m=false && o=false
while getopts "wlmo" arg # -w:windows -l:linux -m:mac -o:overwrite
do
case $arg in
            w)
                w=true
                ;;
            l)
                l=true
                ;;
            m)
                m=true
                ;;
o)
o=true
;;
            ?)
            echo "unkonw argument"
        exit 1
        ;;
    esac
done

if [ ${o} = true ]; then
#remove old file
[ ${w} = true ] && rm -rf ${app_name}_win
[ ${l} = true ] && rm -rf ${app_name}_linux
[ ${m} = true ] && rm -rf ${app_name}_mac
fi

# create app.nw file
cd $app_dir
zip app.nw ./*
app_nwfile=${app_dir}/app.nw

if [ ${w} = true ]; then
echo "creating windows *.exe file..."
unzip $win_nw_zipfile -d ${app_name}_win && cd ${app_name}_win
cat nw.exe $app_nwfile > ${app_name}.exe
rm -rf nw.exe nwsnapshot.exe credits.html
cd ..
echo "create windows app success!"
else
echo "ignore windows app"
fi

if [ ${l} = true ]; then
echo "creating linux execute file..."
tar -xvf $linux_nw_tarfile -C ./
tardir=${linux_nw_tarfile%.tar*} && tardir=${tardir##*/
} # rename tardir
mv  $tardir ${app_name}_linux && cd ${app_name}_linux
cat nw $app_nwfile > ${app_name} && chmod +x ${app_name}
rm -rf nw nwsnapshot credits.html
cd ..
echo "create linux app success!"
else
echo "ignore linux app"
fi

if [ ${m} = true ]; then
echo "creating mac execute file..."
unzip $mac_nw_zipfile -d ${app_name}_mac && cd ${app_name}_mac
if [ -f ${app_dir}/Info.plist ];then
    cp ${app_dir}/Info.plist node-webkit.app/Contents/
fi
cp $app_nwfile node-webkit.app/Contents/Resources/
if [ -f ${app_dir}/app.icns ];then
    cp ${app_dir}/app.icns node-webkit.app/Contents/Resources/
fi
mv node-webkit.app ${app_name}.app
rm -rf nwsnapshot credits.html
cd ..
echo "create mac app success!"
else
echo "ignore mac app"
fi

# remove app.nw
rm -f app.nw

使用方法:

将代码保存到一个.sh文件里并添加运行权限,修改你的app_name(应用名称),app_dir(你的应用所在的文件夹全路径),win_nw_zipfile(你的windows版本node-webkit的zip文件路径),linux_nw_tarfile(你的linux版本node-webkit的tar文件路径),mac_nw_zipfile(你的mac版本node-webkit的zip文件路径)

运行你的.sh文件(如:packer.sh),有四个参数可以给定:

1.-w : 打包windows下的运行文件到 [your_app_name]_win文件夹下

2.-l : 打包linux下的运行文件到[your_app_name]_linux文件夹下

3.-m : 打包mac下的运行文件到[your_app_name]_mac文件夹下

4.-o : 覆盖之前打包过的文件(没有加这一项的话中间会有提示是否覆盖文件)

如果你想的到windows下和linux下的运行文件,同时覆盖老版本文件:sh packer.sh –wlo

你会发现在你的项目文件夹下多出了两个文件夹:[your_app_name]_win ,[your_app_name]_linux

Node.js 的详细介绍:请点这里
Node.js 的下载地址:请点这里

Node.Js入门[PDF+相关代码]

Node.js入门开发指南中文版

Node.js安装与配置

Ubuntu 编译安装Node.js

相关内容