From 1c0780298bfae889d4f332a813abb18a4dfd593c Mon Sep 17 00:00:00 2001 From: msojocs Date: Fri, 18 Feb 2022 14:40:16 +0800 Subject: [PATCH] feat: reduce wine --- .github/workflows/release.yml | 74 +++++++++++++++------------ bin/wechat-devtools | 25 +++++++-- compiler/generatemd5.js | 84 +++++++++++++++++++++++++++++++ compiler/wine/wcc | 6 ++- compiler/wine/wcsc | 7 ++- readme.md | 10 +++- test/reduce-wine | 7 +++ test/test-bash | 18 ++++--- tools/fix-core | 41 +++++++++++---- tools/fix-other | 33 +++++++----- tools/update-wechat-devtools-node | 16 ++---- 11 files changed, 242 insertions(+), 79 deletions(-) create mode 100644 compiler/generatemd5.js create mode 100644 test/reduce-wine diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1c07ed5..8b261e3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -9,7 +9,7 @@ on: push: tags: - v* - branches: [ master ] + branches: [ master, reduce-wine] pull_request: branches: [ master ] # # Allows you to run this workflow manually from the Actions tab @@ -57,41 +57,55 @@ jobs: - name: Build run: docker-compose up - - name: Build AppImage + - name: Refactor Directory Structure run: | - sudo chmod -R 777 tmp package.nw node - bash tools/appimage.sh - wget "https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-x86_64.AppImage" - chmod a+x appimagetool-x86_64.AppImage - ./appimagetool-x86_64.AppImage ./tmp/AppDir + sudo chmod -R 777 package.nw node + ls -l + sudo rm -rf cache tmp .git + mkdir ${{ env. name }} + sudo find -maxdepth 1 -not -name ${{ env.name }} -not -name . -exec mv {} ${{ env.name }} \; + sudo cp -r "${{ env.name }}/package.nw/node_modules/nodegit" nodegit + sudo cp -r "${{ env.name }}/compiler" compiler + ls -l env: - name: 'release-${{ steps.tag.outputs.tag }}' - ARCH: '${{ matrix.ARCH }}' - - - name: Delete unused file - run: | - ls -l - sudo rm -rf cache tmp appimagetool-x86_64.AppImage - mkdir ${{ env. name }} - sudo find -maxdepth 1 -not -name ${{ env.name }} -not -name . -exec mv {} ${{ env.name }} \; - sudo cp -r "${{ env.name }}/package.nw/node_modules/nodegit" nodegit - sudo cp -r "${{ env.name }}/compiler" compiler - sudo mv ${{ env.name }}/WeChat*.AppImage . - ls -l - cd "${{ env.name }}" - env: - name: 'release-${{ steps.tag.outputs.tag }}' + name: 'WeChat_Dev_Tools_${{ steps.tag.outputs.tag }}' - name: Compress run: | ls -l - tar -zvcf ${{ env.name }}.tar.gz ${{ env.name }} tar -zvcf compiler.tar.gz compiler tar -zvcf nodegit.tar.gz nodegit - sudo rm -rf ${{ env.name }} compiler nodegit + sudo rm -rf compiler nodegit ls -l env: - name: 'release-${{ steps.tag.outputs.tag }}' + name: 'WeChat_Dev_Tools_${{ steps.tag.outputs.tag }}' + + - name: Build AppImage && Pack + run: | + bash ./${{ env.name }}/tools/appimage.sh + wget "https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-x86_64.AppImage" + chmod a+x appimagetool-x86_64.AppImage + # build AppImage + ./appimagetool-x86_64.AppImage ./${{ env.name }}/tmp/AppDir ${{ env.name }}_${{ env.ARCH }}_wine.AppImage + # pack + tar -zvcf ${{ env.name }}_${{ env.ARCH }}_wine.tar.gz ${{ env.name }} + + # reduce wine + export REDUCE_WINE=true + bash ${{ env.name }}/tools/fix-core + bash ${{ env.name }}/tools/fix-other + bash ./${{ env.name }}/tools/appimage.sh + # build AppImage + ./appimagetool-x86_64.AppImage ./${{ env.name }}/tmp/AppDir ${{ env.name }}_${{ env.ARCH }}_reduce_wine.AppImage + # pack + tar -zvcf ${{ env.name }}_${{ env.ARCH }}_reduce_wine.tar.gz ${{ env.name }} + + sudo rm -rf appimagetool-x86_64.AppImage ${{ env.name }} + ls -l + env: + name: 'WeChat_Dev_Tools_${{ steps.tag.outputs.tag }}' + ARCH: '${{ matrix.ARCH }}' + - name: Create release and upload artifacts if: startsWith(github.ref, 'refs/heads/') @@ -111,9 +125,5 @@ jobs: name: ${{ steps.tag.outputs.name }} tag_name: ${{ steps.tag.outputs.tag }} files: | - ${{ env.name }}.tar.gz - compiler.tar.gz - nodegit.tar.gz - *.AppImage - env: - name: 'release-${{ steps.tag.outputs.tag }}' \ No newline at end of file + *.tar.gz + *.AppImage \ No newline at end of file diff --git a/bin/wechat-devtools b/bin/wechat-devtools index 78df22c..d49bc96 100755 --- a/bin/wechat-devtools +++ b/bin/wechat-devtools @@ -7,9 +7,28 @@ export APPDATA="$( echo ~ )/.config/wechat_devtools" export PATH="$root_dir/wine:$root_dir/node/bin:$root_dir/nwjs:$PATH" export USERPROFILE=$( echo ~ ) -if [ -d ~/.config/wechat_devtools/WeappCache ];then - echo "删除缓存" - rm -rf ~/.config/wechat_devtools/WeappCache +current=`date "+%Y-%m-%d %H:%M:%S"` +timeStamp=`date -d "$current" +%s` + +# 获取首次运行时间 +if [[ -f "$APPDATA/.run_time" ]];then + run_time=`cat "$APPDATA/.run_time"` +else + run_time=0 +fi + +# 获取构建时间 +if [[ -f "$root_dir/package.nw/.build_time" ]];then + build_time=`cat "$root_dir/package.nw/.build_time"` +else + build_time=timeStamp +fi + +if [[ ! $build_time < $run_time ]];then + echo "清理缓存" + echo $timeStamp > "$APPDATA/.run_time" + rm -rf "$APPDATA/WeappCache" + rm -rf "$APPDATA/WeappVendor" fi EXTENSION1="--load-extension=$root_dir/nwjs/package.nw/js/ideplugin" diff --git a/compiler/generatemd5.js b/compiler/generatemd5.js new file mode 100644 index 0000000..9694bba --- /dev/null +++ b/compiler/generatemd5.js @@ -0,0 +1,84 @@ +const path = require('path') +const fs = require('fs') +const crypto = require('crypto') +const os = require('os') +const child_process = require('child_process') +let platform = process.argv[2] + +let exeList = [] +const macExeList = [ + 'wcc', + 'wcsc', + 'wcc.bin' +] +const winExeList = [ + 'wcc.exe', + 'wcsc.exe' +] +if(platform === 'darwin') { + exeList = macExeList; +} else if(platform === 'win32') { + exeList= winExeList; +} else { + exeList = macExeList.concat(winExeList) +} + + + +function generate(vendorPath) { + let dirList = fs.readdirSync(vendorPath) + console.log(dirList) + let libs = {} + dirList.forEach((item) => { + if (item == 'dev' || item == 'quickstart' || item == 'beta') { + return + } + + let itemPath = path.join(vendorPath, item) + let stat = fs.statSync(itemPath) + if (stat.isDirectory()) { + let fileList = fs.readdirSync(itemPath) + + let md5Info = {} + fileList.forEach((file) => { + if (fs.statSync(path.join(itemPath, file)).isFile()) { + let fileData = fs.readFileSync(path.join(itemPath, file)) + let md5sum = crypto.createHash('md5') + md5sum.update(fileData) + md5Info[file] = md5sum.digest('hex') + } + }) + + libs[item] = md5Info + } + }) + + let oldConfig = JSON.parse(fs.readFileSync(path.join(vendorPath, 'config.json'), 'utf8')) + for (var key in oldConfig.libs) { + // libs[key] = Object.assign({}, oldConfig.libs[key], libs[key]) + oldConfig.libs[key] = Object.assign({}, oldConfig.libs[key], libs[key]) + } + + // let newLib = {} + // let keys = Object.keys(libs) + // keys.sort() + // for (var i = 0; i < keys.length; i++) { + // let key = keys[i] + // newLib[key] = libs[key] + // } + + // 更新 + oldConfig.configVersion = Date.now() + + for (let key in exeList) { + let file = exeList[key] + let fileData = fs.readFileSync(path.join(vendorPath, file)) + let md5sum = crypto.createHash('md5') + md5sum.update(fileData) + oldConfig[file] = md5sum.digest('hex') + } + + fs.writeFileSync(path.join(vendorPath, 'config.json'), JSON.stringify(oldConfig, null, '\t')) +} + +generate(__dirname) diff --git a/compiler/wine/wcc b/compiler/wine/wcc index 6f916dd..3522172 100644 --- a/compiler/wine/wcc +++ b/compiler/wine/wcc @@ -8,4 +8,8 @@ while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symli done DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" -wine "$DIR/wcc.exe" "$@" +if [[ $@ =~ -ll ]];then + wine "$DIR/wcc.exe" "$@" +else + "$DIR/wcc.bin" "$@" +fi diff --git a/compiler/wine/wcsc b/compiler/wine/wcsc index df8ba01..fec1de1 100644 --- a/compiler/wine/wcsc +++ b/compiler/wine/wcsc @@ -8,4 +8,9 @@ while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symli done DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" -wine "$DIR/wcsc.exe" "$@" +if [[ $@ =~ -ll ]];then + wine "$DIR/wcsc.exe" "$@" +else + "$DIR/wcsc.bin" "$@" +fi + diff --git a/readme.md b/readme.md index 6ec355e..1cfe17d 100644 --- a/readme.md +++ b/readme.md @@ -66,7 +66,13 @@ Docker容器启动方法 如需要映射外部目录,请自行修改Docker启动命令 -# 构筑方法0(推荐) +# 自行构建 + +> 注: +> 如需进行降低`wine`依赖的操作,请添加环境变量:`REDUCE_WINE=true` + + +## 构筑方法0(推荐) 理论上此方法99%成功;在网络不稳定时容易失败(原因懂得都懂) @@ -87,7 +93,7 @@ docker-compose up 之后即可通过点击应用图标启动微信开发者工具,也可以运行`bin/wechat-devtools`通过命令行启动 -# 构筑方法1 +## 方法1 由于使用到`node-gyp` `nw-gyp`,此方法会受`python`、`node`版本影响出现一些难以预料的异常(比如使用`node15.0.1`时,大部分模块构建会被忽略,但没有任何报错信息)。但是,`Docker`构建方式会帮你处理好这些问题。 diff --git a/test/reduce-wine b/test/reduce-wine new file mode 100644 index 0000000..68283b8 --- /dev/null +++ b/test/reduce-wine @@ -0,0 +1,7 @@ +#!/bin/bash +export REDUCE_WINE=true + + +root_dir=$(cd `dirname $0`/.. && pwd -P) +$root_dir/tools/fix-core +$root_dir/tools/fix-other \ No newline at end of file diff --git a/test/test-bash b/test/test-bash index 6c4be6d..097ee12 100644 --- a/test/test-bash +++ b/test/test-bash @@ -1,13 +1,19 @@ #!/bin/bash + +if [[ $REDUCE_WINE == 'true' ]];then + echo "not wine" +fi + +if [[ $@ =~ -ll ]];then + echo "include" +fi +exit + +echo "$@ $0 $1 $2" + root_dir=$(cd `dirname $0`/.. && pwd -P) export PATH="$root_dir/node/bin:$PATH" -export http_proxy="http://127.0.0.1:54321" - - -if [[ ! -z $https_proxy || ! -z $http_proxy ]]; then - echo -e "\033[41;37m 警告: 你设置了代理,有可能导致安装出现异常 \033[0m" -fi PY_VERSION=`python -V 2>&1|awk '{print $2}'|awk -F '.' '{print $1}'` echo $PY_VERSION diff --git a/tools/fix-core b/tools/fix-core index 5c36065..17f95a1 100755 --- a/tools/fix-core +++ b/tools/fix-core @@ -17,25 +17,48 @@ node "$root_dir/tools/wxvpkg/unpack" "$package_dir/core.wxvpkg" "$tmp_dir/core.w # # find -open_find_result=$( grep -lr "this.props.onWindowOpenFail());" "$tmp_dir/core.wxvpkg" ) +open_find_result=$( grep -lr "this.props.onWindowOpenFail());if" "$tmp_dir/core.wxvpkg" ) echo "云开发控制台启动点: $open_find_result" -# replace -new_cb_handle="this.props.onWindowOpenFail());Object.keys(window).forEach(key=>{if(!e.window[key]){try{e.window[key]=window[key];}catch(e){console.error(e);}}});" -sed -i "s/this.props.onWindowOpenFail());/$new_cb_handle/g" $open_find_result +if [[ ! -z $open_find_result ]];then + # replace + new_cb_handle="this.props.onWindowOpenFail());Object.keys(window).forEach(key=>{if(!e.window[key]){try{e.window[key]=window[key];}catch(e){console.error(e);}}});" + sed -i "s/this.props.onWindowOpenFail());/$new_cb_handle/g" $open_find_result +fi token_find_result=$( grep -lr "constructor(){this._sessionToken=\"\",this._tokenMap={}}" "$tmp_dir/core.wxvpkg" ) echo "WebSocket token存储对象位置: $token_find_result" -new_constructor="constructor(){if(window.tokenData){/*有就直接用*/this._sessionToken=window.tokenData._sessionToken;this._tokenMap=window.tokenData._tokenMap;}else{/*没有就新建*/this._sessionToken=\"\",this._tokenMap={};window.tokenData=this;/*新建完要给中间人*/}}" -sed -i "s#constructor(){this._sessionToken=\"\",this._tokenMap={}}#$new_constructor#g" "$token_find_result" +if [[ ! -z $token_find_result ]];then + new_constructor="constructor(){if(window.tokenData){/*有就直接用*/this._sessionToken=window.tokenData._sessionToken;this._tokenMap=window.tokenData._tokenMap;}else{/*没有就新建*/this._sessionToken=\"\",this._tokenMap={};window.tokenData=this;/*新建完要给中间人*/}}" + sed -i "s#constructor(){this._sessionToken=\"\",this._tokenMap={}}#$new_constructor#g" "$token_find_result" +fi # open -a Terminal "`pwd`" --> gnome-terminal find_result=$( grep -lr 'open -a Terminal "`pwd`"' "$tmp_dir/core.wxvpkg" ) -echo "Terminal启动位置: $find_result" -new_str="gnome-terminal" -sed -i "s#open -a Terminal \"\`pwd\`\"#$new_str#g" "$find_result" +if [[ ! -z $find_result ]];then + echo "Terminal启动位置: $find_result" + new_str="gnome-terminal" + sed -i "s#open -a Terminal \"\`pwd\`\"#$new_str#g" "$find_result" +fi +# wcc、wcsc处理,设置REDUCE_WINE环境变量生效 +if [[ $REDUCE_WINE == 'true' ]];then + # "wcc.exe":!0,"wcsc.exe":!0 + find_result=$( grep -lr '{wcc:!0,wcsc:!0,DevToolProtector:!0}' "$tmp_dir/core.wxvpkg" ) + if [[ ! -z $find_result ]];then + echo "wcc: $find_result" + new_str='{"wcc.exe":!0,"wcsc.exe":!0,"wcc.bin":!0,"wcsc.bin":!0,wcc:!0,wcsc:!0,DevToolProtector:!0}' + sed -i "s#{wcc:!0,wcsc:!0,DevToolProtector:!0}#$new_str#g" "$find_result" + new_str='"linux"===process.platform' + sed -i "s#\"darwin\"===process.platform#$new_str#g" "$find_result" + fi + + current=`date "+%Y-%m-%d %H:%M:%S"` + timeStamp=`date -d "$current" +%s` + echo $timeStamp > "${package_dir}/.build_time" +fi # pack 路径 到 文件 +echo "pack" node "$root_dir/tools/wxvpkg/pack" "$tmp_dir/core.wxvpkg" "$package_dir/core.wxvpkg" rm -rf "$tmp_dir/core.wxvpkg" diff --git a/tools/fix-other b/tools/fix-other index a578d76..ec2c10d 100755 --- a/tools/fix-other +++ b/tools/fix-other @@ -1,5 +1,5 @@ #!/bin/bash - +set -e root_dir=$(cd `dirname $0`/.. && pwd -P) srcdir=$root_dir tmp_dir="$root_dir/tmp" @@ -14,20 +14,11 @@ sed -i 's#module.exports = createWebviewManager;#module.exports = createWebviewM # 修复:可视化用的wcc,wcsc echo "fix: wcc,wcsc" -mkdir "$tmp_dir/node_modules" +if [[ ! -d "$tmp_dir/node_modules" ]];then + mkdir "$tmp_dir/node_modules" +fi cd $tmp_dir && npm install miniprogram-compiler # wcc wcsc -# 预览编译 -# cd "${package_dir}/js/vendor/" && rm -rf "wcc.exe" -# cp "${package_dir}/node_modules_tmp/node_modules/miniprogram-compiler/bin/linux/wcc" "${package_dir}/js/vendor/wcc.exe" -# wcc_md5=$( md5sum wcc.exe|cut -d ' ' -f1 ) && \ -# sed -i 's/wcc.exe": ".*"/wcc.exe": "'"${wcc_md5}"'"/g' "config.json" -# cd "${package_dir}/js/vendor/" && rm -rf "wcsc.exe" -# cp "${package_dir}/node_modules_tmp/node_modules/miniprogram-compiler/bin/linux/wcsc" "${package_dir}/js/vendor/wcsc.exe" -# cd "${package_dir}/js/vendor" && \ -# wcsc_md5=$( md5sum wcsc.exe|cut -d ' ' -f1 ) && \ -# sed -i 's/wcsc.exe": ".*"/wcsc.exe": "'"${wcsc_md5}"'"/g' "config.json" - # 可视化编译 (cd "${package_dir}/node_modules/" \ && rm -rf wcc wcsc \ @@ -37,4 +28,20 @@ cd $tmp_dir && npm install miniprogram-compiler && cp -r "${srcdir}/compiler/wcc_node"/* "wcc" \ && cp -r "${srcdir}/compiler/wcsc_node"/* "wcsc" ) + +# 预览编译,设置REDUCE_WINE环境变量生效 +# 如果是mac执行wcc,否则wcc.exe +if [[ $REDUCE_WINE == 'true' ]];then + \cp -rf "${srcdir}/compiler/generatemd5.js" "${package_dir}/js/vendor/generatemd5.js" + \cp -rf "${srcdir}/compiler/wine/wcc" "${package_dir}/js/vendor/wcc" + \cp -rf "${srcdir}/compiler/wine/wcsc" "${package_dir}/js/vendor/wcsc" + \cp -rf "${tmp_dir}/node_modules/miniprogram-compiler/bin/linux/wcc" "${package_dir}/js/vendor/wcc.bin" + \cp -rf "${tmp_dir}/node_modules/miniprogram-compiler/bin/linux/wcsc" "${package_dir}/js/vendor/wcsc.bin" + node "${package_dir}/js/vendor/generatemd5.js" + + current=`date "+%Y-%m-%d %H:%M:%S"` + timeStamp=`date -d "$current" +%s` + echo $timeStamp > "${package_dir}/.build_time" +fi + rm -rf "$tmp_dir/node_modules" \ No newline at end of file diff --git a/tools/update-wechat-devtools-node b/tools/update-wechat-devtools-node index 19d203b..8442762 100755 --- a/tools/update-wechat-devtools-node +++ b/tools/update-wechat-devtools-node @@ -187,10 +187,8 @@ const upgrade = function (extractPath) { execSync(`rm -rf ${path.resolve(__dirname, "../nwjs/package.nw")}`) console.log(`创建链接 ${path.resolve(__dirname, "../package.nw")}--->${path.resolve(__dirname, "../nwjs/package.nw")}`) // 链接 - fs.symlinkSync( - path.resolve(__dirname, "../package.nw"), - path.resolve(__dirname, "../nwjs/package.nw") - ); + execSync(`cd ${path.resolve(__dirname, "../nwjs")} && ln -s ../package.nw package.nw`) + } } @@ -283,14 +281,6 @@ const patch_other = function () { info("Patching Other"); return new Promise((resolve, reject) => { - fs.copyFileSync( - path.resolve(__dirname, "../compiler/wine/wcc"), - path.resolve(__dirname, "../package.nw/js/vendor/wcc") - ); - fs.copyFileSync( - path.resolve(__dirname, "../compiler/wine/wcsc"), - path.resolve(__dirname, "../package.nw/js/vendor/wcsc") - ); execSync(path.resolve(__dirname, "fix-other")) resolve(); @@ -310,6 +300,8 @@ const start = async () => { await rebuild_wechat_devtools_node_modules(); await patch_wechat_devtools(); await patch_other(); + // 写入构建时间戳 + fs.writeFileSync(path.resolve(__dirname, '../package.nw/.build_time'), '' + parseInt(new Date().getTime()/1000)) console.log( `Succeeded upgrading wechat-devtools to version ${version}` );