Skip to content

Commit

Permalink
Support generate latest-version.json
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxianqiao committed Jun 28, 2022
1 parent 0037554 commit 2febe23
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/action/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@actions/core": "1.8.2",
"@actions/github": "5.0.3",
"@tauri-apps/action-core": "0.3.1",
"node-fetch": "3.2.6",
"string-argv": "0.3.1",
"tslib": "2.4.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/action/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as core from '@actions/core'
import { join, resolve, dirname, basename } from 'path'
import { existsSync } from 'fs'
import uploadReleaseAssets from './upload-release-assets'
import uploadVersionJSON from './upload-version-json'
import createRelease from './create-release'
import { getPackageJson, buildProject, getInfo, execCommand } from '@tauri-apps/action-core'
import type { BuildOptions } from '@tauri-apps/action-core'
Expand Down Expand Up @@ -109,6 +110,7 @@ async function run(): Promise<void> {
}
}
await uploadReleaseAssets(releaseId, artifacts)
await uploadVersionJSON({ version: info.version, notes: body, releaseId, artifacts });
}
} catch (error) {
core.setFailed(error.message)
Expand Down
7 changes: 2 additions & 5 deletions packages/action/src/upload-release-assets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getOctokit, context } from '@actions/github'
import fs from 'fs'
import path from 'path'
import { getAssetName } from './utils'

export default async function uploadAssets(
releaseId: number,
Expand All @@ -21,11 +22,7 @@ export default async function uploadAssets(
'content-length': contentLength(assetPath)
}

const ext = path.extname(assetPath)
const filename = path.basename(assetPath).replace(ext, '')
const assetName = path.dirname(assetPath).includes(`target${path.sep}debug`)
? `${filename}-debug${ext}`
: `${filename}${ext}`
const assetName = getAssetName(assetPath)
console.log(`Uploading ${assetName}...`)
await github.rest.repos.uploadReleaseAsset({
headers,
Expand Down
72 changes: 72 additions & 0 deletions packages/action/src/upload-version-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { getOctokit, context } from "@actions/github";
import { resolve } from "path";
import { readFileSync, writeFileSync } from "fs";
import uploadAssets from "./upload-release-assets";
import fetch from "node-fetch";
import { arch, platform } from "os";
import { getAssetName } from "./utils";

export default async function uploadVersionJSON({
version,
notes,
releaseId,
artifacts,
}: {
version: string;
notes: string;
releaseId: number;
artifacts: string[];
}) {
if (process.env.GITHUB_TOKEN === undefined) {
throw new Error("GITHUB_TOKEN is required");
}

const github = getOctokit(process.env.GITHUB_TOKEN);

const versionFilename = "latest-version.json";
const versionFile = resolve(process.cwd(), versionFilename);
const versionContent = {
version,
notes,
pub_date: new Date().toISOString(),
platforms: {},
};

const assets = await github.rest.repos.listReleaseAssets({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: releaseId,
});
const asset = assets.data.find((e) => e.name === versionFilename);

if (asset) {
versionContent.platforms = (
(await (await fetch(asset.browser_download_url)).json()) as any
).platforms;

// https://docs.github.com/en/rest/releases/assets#update-a-release-asset
await github.rest.repos.deleteReleaseAsset({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: releaseId,
asset_id: asset.id,
});
}

const sigFile = artifacts.find((s) => s.endsWith(".sig"));
const assetNames = new Set(artifacts.map((p) => getAssetName(p)));

versionContent.platforms[`${platform()}-${arch()}`] = {
signature: sigFile ? readFileSync(sigFile).toString() : undefined,
url: assets.data
.filter((e) => assetNames.has(e.name))
.find((s) => s.name.endsWith(".tar.gz") || s.name.endsWith(".zip"))
.browser_download_url,
};

writeFileSync(versionFile, JSON.stringify(versionContent, null, 2));

console.log(`Uploading ${versionFile}...`)
await uploadAssets(releaseId, [versionFile]);

}
9 changes: 9 additions & 0 deletions packages/action/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import path from 'path';

export function getAssetName(assetPath: string) {
const ext = path.extname(assetPath)
const filename = path.basename(assetPath).replace(ext, '')
return path.dirname(assetPath).includes(`target${path.sep}debug`)
? `${filename}-debug${ext}`
: `${filename}${ext}`
}
39 changes: 39 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,11 @@ ctrlc-windows@^2.0.0:
"@mapbox/node-pre-gyp" "^1.0.5"
neon-cli "^0.8.1"

data-uri-to-buffer@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.0.tgz#b5db46aea50f6176428ac05b73be39a57701a64b"
integrity sha512-Vr3mLBA8qWmcuschSLAOogKgQ/Jwxulv3RNE4FXnYWRGujzrRWQI4m12fQqRkwX06C0KanhLr4hK+GydchZsaA==

debug@4:
version "4.3.2"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.2.tgz#f0a49c18ac8779e31d4a0c6029dfb76873c7428b"
Expand Down Expand Up @@ -789,6 +794,14 @@ fault@^1.0.0:
dependencies:
format "^0.2.0"

fetch-blob@^3.1.2, fetch-blob@^3.1.4:
version "3.1.5"
resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.1.5.tgz#0077bf5f3fcdbd9d75a0b5362f77dbb743489863"
integrity sha512-N64ZpKqoLejlrwkIAnb9iLSA3Vx/kjgzpcDhygcqJ2KKjky8nCgUQ+dzXtbrLaWZGZNmNfQTsiQ0weZ1svglHg==
dependencies:
node-domexception "^1.0.0"
web-streams-polyfill "^3.0.3"

figures@^3.0.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/figures/-/figures-3.2.0.tgz#625c18bd293c604dc4a8ddb2febf0c88341746af"
Expand Down Expand Up @@ -823,6 +836,13 @@ format@^0.2.0:
resolved "https://registry.yarnpkg.com/format/-/format-0.2.2.tgz#d6170107e9efdc4ed30c9dc39016df942b5cb58b"
integrity sha1-1hcBB+nv3E7TDJ3DkBbflCtctYs=

formdata-polyfill@^4.0.10:
version "4.0.10"
resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423"
integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==
dependencies:
fetch-blob "^3.1.2"

fs-minipass@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-2.1.0.tgz#7f5036fdbf12c63c169190cbe4199c852271f9fb"
Expand Down Expand Up @@ -1349,6 +1369,20 @@ neon-cli@^0.8.1:
validate-npm-package-license "^3.0.4"
validate-npm-package-name "^3.0.0"

node-domexception@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==

node-fetch@3.2.6:
version "3.2.6"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.2.6.tgz#6d4627181697a9d9674aae0d61548e0d629b31b9"
integrity sha512-LAy/HZnLADOVkVPubaxHDft29booGglPFDr2Hw0J1AercRh01UiVFm++KMDnJeH9sHgNB4hsXPii7Sgym/sTbw==
dependencies:
data-uri-to-buffer "^4.0.0"
fetch-blob "^3.1.4"
formdata-polyfill "^4.0.10"

node-fetch@^2.6.1, node-fetch@^2.6.7:
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
Expand Down Expand Up @@ -1962,6 +1996,11 @@ vfile@^4.0.0:
unist-util-stringify-position "^2.0.0"
vfile-message "^2.0.0"

web-streams-polyfill@^3.0.3:
version "3.2.1"
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz#71c2718c52b45fd49dbeee88634b3a60ceab42a6"
integrity sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==

webidl-conversions@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
Expand Down

0 comments on commit 2febe23

Please sign in to comment.