Skip to content

Commit

Permalink
Generate Scoop manifest during packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
c3er committed Jul 20, 2023
1 parent a974af6 commit c22c38c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
47 changes: 44 additions & 3 deletions scripts/afterAllArtifactBuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const crypto = require("crypto")
const fs = require("fs/promises")
const path = require("path")

const ALGORITHMS = ["sha1", "sha256"]
const PACKAGE_JSON_PATH = "package.json"

const DEFAULT_ALGORITHM = "sha256"
const ALGORITHMS = ["sha1", DEFAULT_ALGORITHM]

const CHUNK_SIZE = 1000000

async function calcChecksum(filePath, algorithm) {
Expand All @@ -24,15 +28,23 @@ async function calcChecksum(filePath, algorithm) {
return hash.digest("hex")
}

exports.default = async context => {
const artifactPaths = context.artifactPaths
async function parseJson(path) {
return JSON.parse(await fs.readFile(path, { encoding: "utf-8" }))
}

async function createChecksumFiles(artifactPaths) {
const maxArtifactPathLength = Math.max(
...artifactPaths.map(artifactPath => artifactPath.length),
)
const maxAlgorithmNameLength = Math.max(...ALGORITHMS.map(algorithm => algorithm.length))
const artifacts = []
for (const artifactPath of artifactPaths) {
const artifact = {
path: artifactPath,
}
for (const algorithm of ALGORITHMS) {
const checksum = await calcChecksum(artifactPath, algorithm)
artifact[algorithm] = checksum
await fs.writeFile(
`${artifactPath}.${algorithm}`,
`${checksum} ${path.basename(artifactPath)}\n`,
Expand All @@ -43,5 +55,34 @@ exports.default = async context => {
)} ${checksum}`,
)
}
artifacts.push(artifact)
}
return artifacts
}

async function createScoopManifest(outDir, artifacts) {
const MANIFEST_TEMPLAE_PATH = "scripts/scoop-manifest.template.json"
const MANIFEST_NAME = "mdview.json"
const INDENTATION = 4

const zipArtifact = artifacts.find(artifact => artifact.path.toLowerCase().endsWith(".zip"))
if (!zipArtifact) {
return
}

const version = (await parseJson(PACKAGE_JSON_PATH)).version
const manifest = await parseJson(MANIFEST_TEMPLAE_PATH)

manifest.version = version
manifest.url = manifest.url.replaceAll("{{ VERSION }}", version)
manifest.hash = zipArtifact[DEFAULT_ALGORITHM]

const manifestPath = path.join(outDir, MANIFEST_NAME)
await fs.writeFile(manifestPath, JSON.stringify(manifest, null, INDENTATION))
console.log(`Genrated Scoop manifest: ${manifestPath}`)
}

exports.default = async context => {
const artifacts = await createChecksumFiles(context.artifactPaths)
await createScoopManifest(context.outDir, artifacts)
}
18 changes: 18 additions & 0 deletions scripts/scoop-manifest.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": "",
"description": "Graphical application to view Markdown files nicely rendered",
"homepage": "https://github.com/c3er/mdview",
"license": "MIT",
"url": "https://github.com/c3er/mdview/releases/download/v{{ VERSION }}/mdview-{{ VERSION }}-x64.zip",
"hash": "",
"bin": "mdview.exe",
"shortcuts": [["mdview.exe", "Markdown Viewer"]],
"persist": ".data",
"checkver": "github",
"autoupdate": {
"url": "https://github.com/c3er/mdview/releases/download/v$version/mdview-$version-x64.zip",
"hash": {
"url": "https://github.com/c3er/mdview/releases/download/v$version/mdview-$version-x64.zip.sha256"
}
}
}

0 comments on commit c22c38c

Please sign in to comment.