Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashaag committed May 12, 2022
1 parent c663874 commit 755a954
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 126 deletions.
134 changes: 71 additions & 63 deletions dist/main/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 71 additions & 63 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const PATHS = {
micromambaEnvs: path.join(os.homedir(), 'micromamba', 'envs')
}

// --- Utils ---

function getInputAsArray (name) {
// From https://github.com/actions/cache/blob/main/src/utils/actionUtils.ts
return core
Expand All @@ -53,35 +55,6 @@ function executePwsh (command) {
return executeShell('powershell', '-command', command)
}

async function retry (callback, backoffTimes = [2000, 5000, 10000]) {
for (const backoff of backoffTimes.concat(null)) {
if (backoff) {
try {
return await callback()
} catch (error) {
core.warning(`${callback} failed, retrying in ${backoff} seconds: ${error}`)
await sleep(backoff)
}
} else {
return await callback()
}
}
}

function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}

async function tryRestoreCache (path, key, ...args) {
try {
const hitKey = await cache.restoreCache([path], key, ...args)
core.info(`Cache ${hitKey ? 'hit' : 'miss'} for key '${key}'`)
return hitKey
} catch (error) {
core.warning(error.message)
}
}

function sha256 (s) {
const h = crypto.createHash('sha256')
h.update(s)
Expand All @@ -107,12 +80,43 @@ function today () {
return new Date().toDateString()
}

async function tryRestoreCache (path, key, ...args) {
try {
const hitKey = await cache.restoreCache([path], key, ...args)
core.info(`Cache ${hitKey ? 'hit' : 'miss'} for key '${key}'`)
return hitKey
} catch (error) {
core.warning(error.message)
}
}

function saveCacheOnPost (paths, key, options) {
core.info(`Will save to cache with key ${key}`)
const old = JSON.parse(core.getState('postCacheArgs') || '[]')
core.saveState('postCacheArgs', JSON.stringify([...old, [paths, key, options]]))
}

// --- Micromamba download + installation ---

function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}

async function retry (callback, backoffTimes = [2000, 5000, 10000]) {
for (const backoff of backoffTimes.concat(null)) {
if (backoff) {
try {
return await callback()
} catch (error) {
core.warning(`${callback} failed, retrying in ${backoff} seconds: ${error}`)
await sleep(backoff)
}
} else {
return await callback()
}
}
}

async function installMicromambaPosix (micromambaUrl) {
const posixDownloader = `curl ${micromambaUrl} -Ls --retry 5 --retry-delay 1 \
| tar --strip-components=1 -vxjC ${PATHS.micromambaBinFolder} bin/micromamba`
Expand Down Expand Up @@ -192,6 +196,42 @@ if(-not($success)){exit}`
await executePwsh(`${PATHS.micromambaExe} shell init -s cmd.exe -p ~\\micromamba -y`)
}

async function installMicromamba (inputs, extraChannels) {
// Setup .condarc
touch(PATHS.condarc)
let condarcOpts = `
always_yes: true
show_channel_urls: true
channel_priority: strict
`
const channels = inputs.channels + (extraChannels || []).join(', ')
if (channels) {
condarcOpts += `channels: [${channels}]`
}
fs.appendFileSync(PATHS.condarc, condarcOpts)
core.debug(`Contents of ${PATHS.condarc}:\n${fs.readFileSync(PATHS.condarc)}`)

// Install micromamba
if (!fs.existsSync(PATHS.micromambaBinFolder)) {
core.startGroup('Install micromamba ...')
const installer = {
win: installMicromambaWindows,
linux: installMicromambaPosix,
osx: installMicromambaPosix
}[MAMBA_PLATFORM]
const micromambaUrl = MICROMAMBA_PLATFORM_URL + inputs.micromambaVersion
await installer(micromambaUrl)
core.exportVariable('MAMBA_ROOT_PREFIX', PATHS.micromambaRoot)
core.exportVariable('MAMBA_EXE', PATHS.micromambaExe)
core.addPath(PATHS.micromambaBinFolder)
core.endGroup()
}

touch(PATHS.bashprofile)
}

// --- Environment installation ---

function isSelected (item) {
if (/sel\(.*\):.*/gi.test(item)) {
return new RegExp('sel\\(' + MAMBA_PLATFORM + '\\):.*', 'gi').test(item)
Expand Down Expand Up @@ -228,40 +268,6 @@ async function createOrUpdateEnv (envName, envFilePath, extraSpecs) {
}
}

async function installMicromamba (inputs, extraChannels) {
// Setup .condarc
touch(PATHS.condarc)
let condarcOpts = `
always_yes: true
show_channel_urls: true
channel_priority: strict
`
const channels = inputs.channels + (extraChannels || []).join(', ')
if (channels) {
condarcOpts += `channels: [${channels}]`
}
fs.appendFileSync(PATHS.condarc, condarcOpts)
core.debug(`Contents of ${PATHS.condarc}:\n${fs.readFileSync(PATHS.condarc)}`)

// Install micromamba
if (!fs.existsSync(PATHS.micromambaBinFolder)) {
core.startGroup('Install micromamba ...')
const installer = {
win: installMicromambaWindows,
linux: installMicromambaPosix,
osx: installMicromambaPosix
}[MAMBA_PLATFORM]
const micromambaUrl = MICROMAMBA_PLATFORM_URL + inputs.micromambaVersion
await installer(micromambaUrl)
core.exportVariable('MAMBA_ROOT_PREFIX', PATHS.micromambaRoot)
core.exportVariable('MAMBA_EXE', PATHS.micromambaExe)
core.addPath(PATHS.micromambaBinFolder)
core.endGroup()
}

touch(PATHS.bashprofile)
}

function determineEnvironmentName (inputs, envFilePath, envYaml) {
if (envFilePath) {
// Have environment.yml or .lock file
Expand Down Expand Up @@ -358,6 +364,8 @@ Write-Host "Profile already exists and new content added"
core.endGroup()
}

// --- Main ---

async function main () {
const inputs = {
micromambaVersion: core.getInput('micromamba-version'),
Expand Down

0 comments on commit 755a954

Please sign in to comment.