setup-nextflow/src/functions.ts

101 lines
2.8 KiB
TypeScript
Raw Normal View History

import * as core from "@actions/core"
import * as tc from "@actions/tool-cache"
import retry from "async-retry"
import * as fs from "fs"
import semver from "semver"
2022-11-13 21:56:49 +00:00
export async function release_data(
version: string,
ok: InstanceType<typeof GitHub>
): Promise<object> {
// Setup tag-based filtering
2022-11-13 21:56:49 +00:00
let filter = (r: object): boolean => {
return semver.satisfies(r["tag_name"], version, true)
}
// Check if the user passed a 'latest*' tag, and override filtering
// accordingly
if (version.includes("latest")) {
if (version.includes("-everything")) {
// No filtering
2022-11-13 21:56:49 +00:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
filter = (r: object) => {
return true
}
} else if (version.includes("-edge")) {
filter = r => {
return r["tag_name"].endsWith("-edge")
}
} else {
// This is special: passing 'latest' or 'latest-stable' allows us to use
// the latest stable GitHub release direct from the API
const stable_release = await latest_stable_release_data(ok)
return stable_release
}
}
// Get all the releases
2022-11-14 02:19:06 +00:00
const all_releases: object[] = await all_nf_releases(ok)
2022-11-13 21:56:49 +00:00
const matching_releases = all_releases.filter(filter)
2022-11-14 02:19:06 +00:00
matching_releases.sort((x, y) => {
// HACK IDK why the value flip is necessary with the return
return semver.compare(x["tag_name"], y["tag_name"], true) * -1
})
return matching_releases[0]
}
2022-11-13 21:56:49 +00:00
export async function install_nextflow(
url: string,
version: string
): Promise<string> {
core.debug(`Downloading Nextflow from ${url}`)
const nf_dl_path = await retry(
2022-11-13 21:56:49 +00:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async bail => {
return await tc.downloadTool(url)
},
{
onRetry: err => {
core.debug(`Download of ${url} failed, trying again. Error ${err}`)
}
}
)
const temp_install_dir = fs.mkdtempSync(`nxf-${version}`)
const nf_path = `${temp_install_dir}/nextflow`
try {
fs.renameSync(nf_dl_path, nf_path)
} catch (err: unknown) {
core.debug(`Failed to rename file: ${err}`)
fs.copyFileSync(nf_dl_path, nf_path)
fs.unlinkSync(nf_dl_path)
}
fs.chmodSync(nf_path, "0711")
return temp_install_dir
}
2023-05-16 23:13:04 +00:00
export function check_cache(version: string): boolean {
const cleaned_version = semver.clean(version, true)
if (cleaned_version === null) {
return false
}
const resolved_version = String(cleaned_version)
const nf_path = tc.find("nextflow", resolved_version)
if (!nf_path) {
core.debug(`Could not find Nextflow ${resolved_version} in the tool cache`)
return false
} else {
core.debug(`Found Nextflow ${resolved_version} at path '${nf_path}'`)
core.debug(`Adding '${nf_path}' to PATH`)
core.addPath(nf_path)
return true
}
}