-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
204 lines (176 loc) · 7.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const core = require('@actions/core')
const yaml = require('js-yaml')
const fs = require('fs')
const os = require('os')
const exec = require('@actions/exec').exec
const path = require('path')
const process = require('process')
async function executeNoCatch (command) {
await exec('bash', ['-c', command])
}
async function execute (command) {
try {
await exec('bash', ['-c', command])
} catch (error) {
core.setFailed(error.message)
}
}
async function execPwsh (command) {
try {
await exec('powershell', ['-command', command])
} catch (error) {
core.setFailed(error.message)
}
}
function touch (filename) {
// https://remarkablemark.org/blog/2017/12/17/touch-file-nodejs/
const time = new Date()
try {
fs.utimesSync(filename, time, time)
} catch (err) {
fs.closeSync(fs.openSync(filename, 'w'))
}
}
async function run () {
try {
const baseUrl = 'https://micro.mamba.pm/api/micromamba'
const envFileName = core.getInput('environment-file')
const micromambaVersion = core.getInput('micromamba-version')
const envFilePath = path.join(process.env.GITHUB_WORKSPACE || '', envFileName)
const envYaml = yaml.safeLoad(fs.readFileSync(envFilePath, 'utf8'))
const extraSpecs = core.getInput('extra-specs').split("\n").filter(x => x !== "");
const envName = core.getInput('environment-name') || envYaml.name
const condarc = path.join(os.homedir(), '.condarc')
const profile = path.join(os.homedir(), '.bash_profile')
const bashrc = path.join(os.homedir(), '.bashrc')
const bashrcBak = path.join(os.homedir(), '.bashrc.actionbak')
const micromambaBinFolder = path.join(os.homedir(), 'micromamba-bin');
const micromambaLoc = path.join(micromambaBinFolder, 'micromamba');
console.log(`The bin folder is ${micromambaBinFolder}`);
core.startGroup('Configuring micromamba...')
touch(condarc)
fs.appendFileSync(condarc, 'always_yes: true\n')
fs.appendFileSync(condarc, 'show_channel_urls: true\n')
fs.appendFileSync(condarc, 'channel_priority: strict\n')
if (envYaml.channels !== undefined) {
fs.appendFileSync(condarc, 'channels: [' + envYaml.channels.join(', ') + ']\n')
}
if (process.platform !== 'win32')
{
await execute('cat ' + condarc)
}
else
{
await execute('cat $(cygpath "' + condarc + '")')
// await execute('type ' + condarc)
}
core.endGroup()
const quotedExtraSpecsStr = extraSpecs.map(function(e) {
return '"' + e + '"';
}).join(" ");
if (process.platform !== 'win32') {
core.startGroup('Installing environment ' + envName + ' from ' + envFilePath + ' ...')
touch(profile)
await execute('mkdir -p ' + micromambaBinFolder)
if (process.platform === 'darwin') {
// macos
try {
await executeNoCatch(`curl -Ls --retry 5 --retry-delay 1 ${baseUrl}/osx-64/${micromambaVersion} | tar -xvjO bin/micromamba > ${micromambaLoc}`)
} catch (error) {
await execute(`curl -Ls --retry 5 --retry-delay 1 ${baseUrl}/osx-64/${micromambaVersion} | tar -xvzO bin/micromamba > ${micromambaLoc}`)
}
await execute(`chmod u+x ${micromambaLoc}`)
await execute(`${micromambaLoc} shell init -s bash -p ~/micromamba -y`)
// TODO need to fix a check in micromamba so that this works
// https://github.com/mamba-org/mamba/issues/925
// await execute(`${micromambaLoc} shell init -s zsh -p ~/micromamba -y`)
} else if (process.platform === 'linux') {
// linux
try {
await executeNoCatch(`wget -qO- --retry-connrefused --waitretry=10 -t 5 ${baseUrl}/linux-64/${micromambaVersion} | tar -xvjO bin/micromamba > ${micromambaLoc}`)
} catch (error) {
await execute(`wget -qO- --retry-connrefused --waitretry=10 -t 5 ${baseUrl}/linux-64/${micromambaVersion} | tar -xvzO bin/micromamba > ${micromambaLoc}`)
}
await execute(`chmod u+x ${micromambaLoc}`)
// on linux we move the bashrc to a backup and then restore
await execute('mv ' + bashrc + ' ' + bashrcBak)
touch(bashrc)
try {
await execute(`${micromambaLoc} shell init -s bash -p ~/micromamba -y`)
await execute(`${micromambaLoc} shell init -s zsh -p ~/micromamba -y`)
fs.appendFileSync(profile, '\n' + fs.readFileSync(bashrc, 'utf8'), 'utf8')
await execute('mv ' + bashrcBak + ' ' + bashrc)
} catch (error) {
await execute('mv ' + bashrcBak + ' ' + bashrc)
core.setFailed(error.message)
}
} else {
core.setFailed('Platform ' + process.platform + ' not supported.')
}
// final bits of the install
await execute('mkdir -p ' + path.join(os.homedir(), 'micromamba/pkgs/'))
await execute('source ' + profile + ' && micromamba create -n ' + envName + ' ' + quotedExtraSpecsStr + ' --strict-channel-priority -y -f ' + envFilePath)
fs.appendFileSync(profile, 'set -eo pipefail\n')
fs.appendFileSync(profile, 'micromamba activate ' + envName + '\n')
await execute("cat " + profile);
core.addPath(micromambaBinFolder);
core.exportVariable("MAMBA_ROOT_PREFIX", path.join(os.homedir(), 'micromamba'));
core.exportVariable("MAMBA_EXE", micromambaLoc);
core.endGroup()
await execute('source ' + profile + ' && micromamba info && micromamba list')
} else {
// handle win32!
const powershellAutoActivateEnv = `if (!(Test-Path $profile))
{
New-Item -path $profile -type "file" -value "CONTENTPLACEHOLDER"
Write-Host "Created new profile and content added"
}
else
{
Add-Content -path $profile -value "CONTENTPLACEHOLDER"
Write-Host "Profile already exists and new content added"
}`
const powershellDownloader = `$count = 0
do{
try
{
Invoke-Webrequest -URI ${baseUrl}/win-64/${micromambaVersion} -OutFile ${micromambaBinFolder}\\micromamba.tar.bz2
$success = $true
}
catch
{
Start-sleep -Seconds (10 * ($count + 1))
}
$count++
}until($count -eq 5 -or $success)
if(-not($success)){exit}`
const autoactivate = powershellAutoActivateEnv.replace(/CONTENTPLACEHOLDER/g, `micromamba activate ${envName}`)
core.startGroup(`Installing environment ${envName} from ${envFilePath} ...`)
touch(profile)
await execPwsh('mkdir -path ' + micromambaBinFolder);
await execPwsh(powershellDownloader)
await execPwsh(
'$env:Path = (get-item (get-command git).Path).Directory.parent.FullName + "\\usr\\bin;" + $env:Path;' +
'tar.exe -xvjf ~/micromamba-bin/micromamba.tar.bz2 --strip-components 2 -C ~/micromamba-bin Library/bin/micromamba.exe;'
)
const micromambaExe = `${micromambaLoc}.exe`;
await execPwsh(`${micromambaExe} --help`)
await execPwsh(`${micromambaExe} shell init -s powershell -p $HOME\\micromamba`)
await execPwsh('$env:Path = (get-item (get-command git).Path).Directory.parent.FullName + "\\usr\\bin;" + $env:Path;' +
`${micromambaExe} shell init -s bash -p ~\\micromamba -y`)
await execPwsh(`${micromambaExe} shell init -s cmd.exe -p ~\\micromamba -y`)
await execPwsh(`${micromambaExe} create -n ${envName} ${quotedExtraSpecsStr} --strict-channel-priority -y -f ${envFilePath}`);
await execPwsh(autoactivate);
fs.appendFileSync(profile, `micromamba activate ${envName}\n`)
core.exportVariable("MAMBA_ROOT_PREFIX", path.join(os.homedir(), 'micromamba'));
core.exportVariable("MAMBA_EXE", micromambaExe);
core.addPath(micromambaBinFolder);
core.endGroup()
await execPwsh('micromamba info')
await execPwsh('micromamba list')
}
} catch (error) {
core.setFailed(error.message)
}
}
run()