mirror of
https://github.com/erlef/setup-beam.git
synced 2026-07-23 07:06:07 +00:00
New option to work with one or more hex.pm mirrors (#197)
* New option to work with one or more hex.pm mirrors Add a new option called `hexpm-mirrors` for one or more hex.pm mirrors. Default list is `builds.hex.pm`, so behavior is unaffected unless option is used. Signed-off-by: Paul Guyot <pguyot@kallisys.net> * Implement suggested rewording changes from @paulo-ferraz-oliveira Co-authored-by: Paulo F. Oliveira <paulo.ferraz.oliveira@gmail.com> Signed-off-by: Paul Guyot <pguyot@kallisys.net> --------- Signed-off-by: Paul Guyot <pguyot@kallisys.net> Co-authored-by: Paulo F. Oliveira <paulo.ferraz.oliveira@gmail.com>
This commit is contained in:
+68
-28
@@ -30,16 +30,23 @@ async function main() {
|
||||
const elixirSpec = getInput('elixir-version', false, 'elixir', versions)
|
||||
const gleamSpec = getInput('gleam-version', false, 'gleam', versions)
|
||||
const rebar3Spec = getInput('rebar3-version', false, 'rebar', versions)
|
||||
const hexMirrors = core.getMultilineInput('hexpm-mirrors', {
|
||||
required: false,
|
||||
})
|
||||
|
||||
if (otpSpec !== 'false') {
|
||||
await installOTP(otpSpec, osVersion)
|
||||
const elixirInstalled = await maybeInstallElixir(elixirSpec, otpSpec)
|
||||
await installOTP(otpSpec, osVersion, hexMirrors)
|
||||
const elixirInstalled = await maybeInstallElixir(
|
||||
elixirSpec,
|
||||
otpSpec,
|
||||
hexMirrors,
|
||||
)
|
||||
|
||||
if (elixirInstalled === true) {
|
||||
const shouldMixRebar = getInput('install-rebar', false)
|
||||
await mix(shouldMixRebar, 'rebar')
|
||||
await mix(shouldMixRebar, 'rebar', hexMirrors)
|
||||
const shouldMixHex = getInput('install-hex', false)
|
||||
await mix(shouldMixHex, 'hex')
|
||||
await mix(shouldMixHex, 'hex', hexMirrors)
|
||||
}
|
||||
} else if (!gleamSpec) {
|
||||
throw new Error('otp-version=false is only available when installing Gleam')
|
||||
@@ -49,12 +56,12 @@ async function main() {
|
||||
await maybeInstallRebar3(rebar3Spec)
|
||||
}
|
||||
|
||||
async function installOTP(otpSpec, osVersion) {
|
||||
const otpVersion = await getOTPVersion(otpSpec, osVersion)
|
||||
async function installOTP(otpSpec, osVersion, hexMirrors) {
|
||||
const otpVersion = await getOTPVersion(otpSpec, osVersion, hexMirrors)
|
||||
console.log(
|
||||
`##[group]Installing Erlang/OTP ${otpVersion} - built on ${osVersion}`,
|
||||
)
|
||||
await installer.installOTP(osVersion, otpVersion)
|
||||
await installer.installOTP(osVersion, otpVersion, hexMirrors)
|
||||
core.setOutput('otp-version', otpVersion)
|
||||
core.addPath(`${process.env.RUNNER_TEMP}/.setup-beam/otp/bin`)
|
||||
console.log('##[endgroup]')
|
||||
@@ -62,13 +69,17 @@ async function installOTP(otpSpec, osVersion) {
|
||||
return otpVersion
|
||||
}
|
||||
|
||||
async function maybeInstallElixir(elixirSpec, otpVersion) {
|
||||
async function maybeInstallElixir(elixirSpec, otpSpec, hexMirrors) {
|
||||
let installed = false
|
||||
|
||||
if (elixirSpec) {
|
||||
const elixirVersion = await getElixirVersion(elixirSpec, otpVersion)
|
||||
const elixirVersion = await getElixirVersion(
|
||||
elixirSpec,
|
||||
otpSpec,
|
||||
hexMirrors,
|
||||
)
|
||||
console.log(`##[group]Installing Elixir ${elixirVersion}`)
|
||||
await installer.installElixir(elixirVersion)
|
||||
await installer.installElixir(elixirVersion, hexMirrors)
|
||||
core.setOutput('elixir-version', elixirVersion)
|
||||
const disableProblemMatchers = getInput('disable_problem_matchers', false)
|
||||
if (disableProblemMatchers === 'false') {
|
||||
@@ -87,12 +98,28 @@ async function maybeInstallElixir(elixirSpec, otpVersion) {
|
||||
return installed
|
||||
}
|
||||
|
||||
async function mix(shouldMix, what) {
|
||||
async function mixWithMirrors(cmd, args, hexMirrors) {
|
||||
if (hexMirrors.length === 0) {
|
||||
throw new Error('mix failed with every mirror')
|
||||
}
|
||||
const [hexMirror, ...hexMirrorsT] = hexMirrors
|
||||
process.env.HEX_MIRROR = hexMirror
|
||||
try {
|
||||
return await exec(cmd, args)
|
||||
} catch (err) {
|
||||
core.info(
|
||||
`mix failed with mirror ${process.env.HEX_MIRROR} with message ${err.message})`,
|
||||
)
|
||||
}
|
||||
return mixWithMirrors(cmd, args, hexMirrorsT)
|
||||
}
|
||||
|
||||
async function mix(shouldMix, what, hexMirrors) {
|
||||
if (shouldMix === 'true') {
|
||||
const cmd = 'mix'
|
||||
const args = [`local.${what}`, '--force']
|
||||
console.log(`##[group]Running ${cmd} ${args}`)
|
||||
await exec(cmd, args)
|
||||
await mixWithMirrors(cmd, args, hexMirrors)
|
||||
console.log('##[endgroup]')
|
||||
}
|
||||
}
|
||||
@@ -136,8 +163,8 @@ async function maybeInstallRebar3(rebar3Spec) {
|
||||
return installed
|
||||
}
|
||||
|
||||
async function getOTPVersion(otpSpec0, osVersion) {
|
||||
const otpVersions = await getOTPVersions(osVersion)
|
||||
async function getOTPVersion(otpSpec0, osVersion, hexMirrors) {
|
||||
const otpVersions = await getOTPVersions(osVersion, hexMirrors)
|
||||
let otpSpec = otpSpec0 // might be a branch (?)
|
||||
const otpVersion = getVersionFromSpec(
|
||||
otpSpec,
|
||||
@@ -156,10 +183,10 @@ async function getOTPVersion(otpSpec0, osVersion) {
|
||||
return otpVersions.get(otpVersion) // from the reference, for download
|
||||
}
|
||||
|
||||
async function getElixirVersion(exSpec0, otpVersion0) {
|
||||
async function getElixirVersion(exSpec0, otpVersion0, hexMirrors) {
|
||||
const otpVersion = otpVersion0.match(/^([^-]+-)?(.+)$/)[2]
|
||||
const otpVersionMajor = otpVersion.match(/^([^.]+).*$/)[1]
|
||||
const elixirVersions = await getElixirVersions()
|
||||
const elixirVersions = await getElixirVersions(hexMirrors)
|
||||
const semverVersions = Array.from(elixirVersions.keys()).sort()
|
||||
const exSpec = exSpec0.replace(/-otp-.*$/, '')
|
||||
const elixirVersionFromSpec = getVersionFromSpec(exSpec, semverVersions, true)
|
||||
@@ -219,19 +246,19 @@ async function getRebar3Version(r3Spec) {
|
||||
return rebar3Version
|
||||
}
|
||||
|
||||
async function getOTPVersions(osVersion) {
|
||||
let originListing
|
||||
let pageIdxs
|
||||
async function getOTPVersions(osVersion, hexMirrors) {
|
||||
let otpVersionsListings
|
||||
if (process.platform === 'linux') {
|
||||
originListing = `https://builds.hex.pm/builds/otp/${osVersion}/builds.txt`
|
||||
pageIdxs = [null]
|
||||
otpVersionsListings = await getWithMirrors(
|
||||
`/builds/otp/${osVersion}/builds.txt`,
|
||||
hexMirrors,
|
||||
)
|
||||
} else if (process.platform === 'win32') {
|
||||
originListing =
|
||||
const originListing =
|
||||
'https://api.github.com/repos/erlang/otp/releases?per_page=100'
|
||||
pageIdxs = [1, 2, 3]
|
||||
otpVersionsListings = await get(originListing, [1, 2, 3])
|
||||
}
|
||||
|
||||
const otpVersionsListings = await get(originListing, pageIdxs)
|
||||
const otpVersions = new Map()
|
||||
|
||||
if (process.platform === 'linux') {
|
||||
@@ -261,10 +288,10 @@ async function getOTPVersions(osVersion) {
|
||||
return otpVersions
|
||||
}
|
||||
|
||||
async function getElixirVersions() {
|
||||
const elixirVersionsListings = await get(
|
||||
'https://builds.hex.pm/builds/elixir/builds.txt',
|
||||
[null],
|
||||
async function getElixirVersions(hexMirrors) {
|
||||
const elixirVersionsListings = await getWithMirrors(
|
||||
'/builds/elixir/builds.txt',
|
||||
hexMirrors,
|
||||
)
|
||||
const otpVersionsForElixirMap = new Map()
|
||||
|
||||
@@ -448,6 +475,19 @@ async function get(url0, pageIdxs) {
|
||||
return Promise.all(pageIdxs.map(getPage))
|
||||
}
|
||||
|
||||
async function getWithMirrors(resourcePath, hexMirrors) {
|
||||
if (hexMirrors.length === 0) {
|
||||
throw new Error(`Could not fetch ${resourcePath} from any hex.pm mirror`)
|
||||
}
|
||||
const [hexMirror, ...hexMirrorsT] = hexMirrors
|
||||
try {
|
||||
return await get(`${hexMirror}${resourcePath}`, [null])
|
||||
} catch (err) {
|
||||
core.info(`get failed for URL ${hexMirror}${resourcePath}`)
|
||||
}
|
||||
return getWithMirrors(resourcePath, hexMirrorsT)
|
||||
}
|
||||
|
||||
function maybePrependWithV(v) {
|
||||
if (isVersion(v)) {
|
||||
return `v${v.replace('v', '')}`
|
||||
|
||||
Reference in New Issue
Block a user