forked from tauri-apps/tauri-action
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support generate
latest-version.json
See tauri-apps#251
- Loading branch information
wangxianqiao
committed
Jun 28, 2022
1 parent
0037554
commit 2febe23
Showing
6 changed files
with
125 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters