From 053daa621ba403920fe39234c8b7b2ab2eccf051 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Mon, 22 Apr 2024 17:18:44 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E8=87=AA=E5=8A=A8=E5=8F=91=E5=B8=83And?= =?UTF-8?q?roid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/publish-android.yml | 55 ++++++++++++++ app/Http/Controllers/IndexController.php | 25 +------ app/Module/Base.php | 4 +- electron/build.js | 93 +++++++++++++++++++++++- resources/mobile | 2 +- 5 files changed, 150 insertions(+), 29 deletions(-) create mode 100644 .github/workflows/publish-android.yml diff --git a/.github/workflows/publish-android.yml b/.github/workflows/publish-android.yml new file mode 100644 index 000000000..f04037e46 --- /dev/null +++ b/.github/workflows/publish-android.yml @@ -0,0 +1,55 @@ +name: Publish Android + +on: + push: + tags: + - 'v*' + +jobs: + Build: + name: Build Android + runs-on: ubuntu-latest + environment: build + + if: startsWith(github.event.ref, 'refs/tags/v') + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Use Node.js 20.x + uses: actions/setup-node@v1 + with: + node-version: 20.x + + - name: Build Js + run: | + git submodule init + git submodule update --remote "resources/mobile" + ./cmd appbuild + + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + distribution: 'zulu' + java-version: '11' + + - name: Build Android + run: | + cd resources/mobile/platforms/android/eeuiApp + chmod +x ./gradlew + ./gradlew assembleRelease --quiet + + - name: Upload File + env: + DP_KEY: ${{ secrets.DP_KEY }} + run: | + node ./electron/build.js android-upload + + - name: Release + uses: softprops/action-gh-release@v2 + env: + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} + GITHUB_REPOSITORY: ${{ github.repository }} + with: + files: | + resources/mobile/platforms/android/eeuiApp/app/build/outputs/apk/release/*.apk diff --git a/app/Http/Controllers/IndexController.php b/app/Http/Controllers/IndexController.php index 0bc327100..d18b7d7ed 100755 --- a/app/Http/Controllers/IndexController.php +++ b/app/Http/Controllers/IndexController.php @@ -195,7 +195,7 @@ class IndexController extends InvokeController $publishPath = "uploads/desktop/{$publishVersion}/"; $res = Base::upload([ "file" => Request::file('file'), - "type" => 'desktop', + "type" => 'publish', "path" => $publishPath, "fileName" => true ]); @@ -239,29 +239,6 @@ class IndexController extends InvokeController ]; } // - $path = "uploads/android"; - $dirPath = public_path($path); - $lists = Base::readDir($dirPath); - $apkFile = null; - foreach ($lists as $file) { - if (!str_ends_with($file, '.apk')) { - continue; - } - if ($apkFile && strtotime($apkFile['time']) > filemtime($file)) { - continue; - } - $fileName = Base::leftDelete($file, $dirPath); - $fileSize = filesize($file); - $apkFile = [ - 'name' => substr($fileName, 1), - 'time' => date("Y-m-d H:i:s", filemtime($file)), - 'size' => $fileSize > 0 ? Base::readableBytes($fileSize) : 0, - 'url' => Base::fillUrl($path . $fileName), - ]; - } - if ($apkFile) { - $files = array_merge([$apkFile], $files); - } return view('desktop', ['version' => $name, 'files' => $files]); } // 下载 diff --git a/app/Module/Base.php b/app/Module/Base.php index 03b44af28..8487bbc6c 100755 --- a/app/Module/Base.php +++ b/app/Module/Base.php @@ -2241,8 +2241,8 @@ class Base case 'md': $type = ['md']; break; - case 'desktop': - $type = ['yml', 'yaml', 'dmg', 'pkg', 'blockmap', 'zip', 'exe', 'msi']; + case 'publish': + $type = ['yml', 'yaml', 'dmg', 'pkg', 'blockmap', 'zip', 'exe', 'msi', 'apk']; break; case 'more': $type = []; // 不限制上传文件类型 diff --git a/electron/build.js b/electron/build.js index e55fc56c8..acc7bf471 100644 --- a/electron/build.js +++ b/electron/build.js @@ -1,4 +1,3 @@ -const os = require('os') const fs = require('fs'); const fse = require('fs-extra'); const path = require('path') @@ -108,6 +107,78 @@ function axiosAutoTry(data) { }) } +/** + * 上传app应用 + * @param url + */ +function androidUpload(url) { + if (!DP_KEY) { + console.error("Missing Deploy Key or GitHub Token and Repository!"); + process.exit() + } + const releaseDir = path.resolve(__dirname, "../resources/mobile/platforms/android/eeuiApp/app/build/outputs/apk/release"); + if (!fs.existsSync(releaseDir)) { + console.error("Release not found"); + process.exit() + } + fs.readdir(releaseDir, async (err, files) => { + if (err) { + console.warn(err) + } else { + const uploadOras = {} + for (const filename of files) { + const localFile = path.join(releaseDir, filename) + if (/\.apk$/.test(filename) && fs.existsSync(localFile)) { + const fileStat = fs.statSync(localFile) + if (fileStat.isFile()) { + uploadOras[filename] = ora(`Upload [0%] ${filename}`).start() + const formData = new FormData() + formData.append("file", fs.createReadStream(localFile)); + formData.append("file_num", 1); + await axiosAutoTry({ + axios: { + method: 'post', + url: url, + data: formData, + headers: { + 'Publish-Version': config.version, + 'Publish-Key': DP_KEY, + 'Content-Type': 'multipart/form-data;boundary=' + formData.getBoundary(), + }, + onUploadProgress: progress => { + const complete = Math.min(99, Math.round(progress.loaded / progress.total * 100 | 0)) + '%' + uploadOras[filename].text = `Upload [${complete}] ${filename}` + }, + }, + onRetry: _ => { + uploadOras[filename].warn(`Upload [retry] ${filename}`) + uploadOras[filename] = ora(`Upload [0%] ${filename}`).start() + }, + retryNumber: 3 + }).then(({status, data}) => { + if (status !== 200) { + uploadOras[filename].fail(`Upload [fail:${status}] ${filename}`) + return + } + if (!utils.isJson(data)) { + uploadOras[filename].fail(`Upload [fail:not json] ${filename}`) + return + } + if (data.ret !== 1) { + uploadOras[filename].fail(`Upload [fail:ret ${data.ret}] ${filename}`) + return + } + uploadOras[filename].succeed(`Upload [100%] ${filename}`) + }).catch(_ => { + uploadOras[filename].fail(`Upload [fail] ${filename}`) + }) + } + } + } + } + }); +} + /** * 通用发布 * @param url @@ -169,7 +240,19 @@ function genericPublish({url, key, version, output}) { uploadOras[filename] = ora(`Upload [0%] ${filename}`).start() }, retryNumber: 3 - }).then(_ => { + }).then(({status, data}) => { + if (status !== 200) { + uploadOras[filename].fail(`Upload [fail:${status}] ${filename}`) + return + } + if (!utils.isJson(data)) { + uploadOras[filename].fail(`Upload [fail:not json] ${filename}`) + return + } + if (data.ret !== 1) { + uploadOras[filename].fail(`Upload [fail:ret ${data.ret}] ${filename}`) + return + } uploadOras[filename].succeed(`Upload [100%] ${filename}`) }).catch(_ => { uploadOras[filename].fail(`Upload [fail] ${filename}`) @@ -341,6 +424,12 @@ if (["dev"].includes(argv[2])) { notarize: false, } }, false, false) +} else if (["android-upload"].includes(argv[2])) { + config.app.forEach(({publish}) => { + if (publish.provider === 'generic') { + androidUpload(publish.url) + } + }) } else if (["all", "win", "mac"].includes(argv[2])) { // 自动编译 platforms.filter(p => { diff --git a/resources/mobile b/resources/mobile index 16153670c..a0c290ae8 160000 --- a/resources/mobile +++ b/resources/mobile @@ -1 +1 @@ -Subproject commit 16153670cdcea2e5e7d02c778f69365098e82857 +Subproject commit a0c290ae876d4526925d835da242169f0d99c875