refactor: Move functions functions to their own file

This commit is contained in:
Edmund Miller 2022-11-12 23:00:23 -06:00 committed by Thomas A. Christensen II
parent db82c135bd
commit b9a7fb9499
4 changed files with 107 additions and 97 deletions

96
src/functions.ts Normal file
View file

@ -0,0 +1,96 @@
import * as core from '@actions/core'
import * as fs from 'fs'
import retry = require('async-retry')
import semver = require('semver')
const NEXTFLOW_REPO = {owner: 'nextflow-io', repo: 'nextflow'}
async function all_nf_releases(ok) {
return await ok.paginate(
ok.rest.repos.listReleases,
NEXTFLOW_REPO,
response => response.data
)
}
async function latest_stable_release_data(ok) {
const {data: stable_release} = await ok.rest.repos.getLatestRelease(
NEXTFLOW_REPO
)
return stable_release
}
export async function release_data(version, ok) {
// Setup tag-based filtering
let filter = r => {
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
filter = r => {
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
const all_releases = await all_nf_releases(ok)
let matching_releases = all_releases.filter(filter)
matching_releases.sort(function (x, y) {
semver.compare(x.tag_name, y.tag_name, true)
})
return matching_releases[0]
}
export function nextflow_bin_url(release, get_all) {
const release_assets = release.assets
const all_asset = release_assets.filter(a => {
return a.browser_download_url.endsWith('-all')
})[0]
const regular_asset = release_assets.filter(a => {
return a.name == 'nextflow'
})[0]
const dl_asset = get_all ? all_asset : regular_asset
return dl_asset.browser_download_url
}
export async function install_nextflow(url, version) {
core.debug(`Downloading Nextflow from ${url}`)
const nf_dl_path = await retry(
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`
fs.renameSync(nf_dl_path, nf_path)
fs.chmodSync(nf_path, '0711')
return temp_install_dir
}

View file

@ -3,100 +3,7 @@ import * as exec from '@actions/exec'
import * as fs from 'fs' import * as fs from 'fs'
import * as github from '@actions/github' import * as github from '@actions/github'
import * as tc from '@actions/tool-cache' import * as tc from '@actions/tool-cache'
import retry = require('async-retry') import {release_data, nextflow_bin_url, install_nextflow} from './functions'
import semver = require('semver')
const NEXTFLOW_REPO = {owner: 'nextflow-io', repo: 'nextflow'}
async function all_nf_releases(ok) {
return await ok.paginate(
ok.rest.repos.listReleases,
NEXTFLOW_REPO,
response => response.data
)
}
async function latest_stable_release_data(ok) {
const {data: stable_release} = await ok.rest.repos.getLatestRelease(
NEXTFLOW_REPO
)
return stable_release
}
async function release_data(version, ok) {
// Setup tag-based filtering
let filter = r => {
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
filter = r => {
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
const all_releases = await all_nf_releases(ok)
let matching_releases = all_releases.filter(filter)
matching_releases.sort(function (x, y) {
semver.compare(x.tag_name, y.tag_name, true)
})
return matching_releases[0]
}
function nextflow_bin_url(release, get_all) {
const release_assets = release.assets
const all_asset = release_assets.filter(a => {
return a.browser_download_url.endsWith('-all')
})[0]
const regular_asset = release_assets.filter(a => {
return a.name == 'nextflow'
})[0]
const dl_asset = get_all ? all_asset : regular_asset
return dl_asset.browser_download_url
}
async function install_nextflow(url, version) {
core.debug(`Downloading Nextflow from ${url}`)
const nf_dl_path = await retry(
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`
fs.renameSync(nf_dl_path, nf_path)
fs.chmodSync(nf_path, '0711')
return temp_install_dir
}
async function run() { async function run() {
// Set environment variables // Set environment variables

7
test/functions.ts Normal file
View file

@ -0,0 +1,7 @@
import test from 'ava'
test.todo('all_nf_releases')
test.todo('lastest_stable_release_data')
test.todo('release_data')
test.todo('nextflow_bin_url')
test.todo('install_nextflow')

View file

@ -1,8 +1,8 @@
import test from 'ava'; import test from 'ava'
const fn = () => 'foo'; const fn = () => 'foo'
test('fn() returns foo', t => { test('fn() returns foo', t => {
t.pass() t.pass()
// t.is(fn(), 'foo'); // t.is(fn(), 'foo');
}); })