Skip to content

Commit

Permalink
Support pyenv on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
nre-ableton committed Jul 19, 2022
1 parent 1ac2282 commit 92d88ae
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/com/ableton/VirtualEnv.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,28 @@ class VirtualEnv implements Serializable {
* @return New instance of VirtualEnv object.
*/
static VirtualEnv create(Object script, String python, String pyenvRoot) {
if (!script.isUnix()) {
script.error 'This method is not supported on Windows'
}

VirtualEnv venv = new VirtualEnv(script)
assert pyenvRoot

if (!script.fileExists(pyenvRoot)) {
script.error "pyenv root path '${pyenvRoot}' does not exist"
}

List activateCommands = ['export PATH=\$PYENV_ROOT/bin:\$PATH']
boolean isUnix = script.isUnix() // Cache for later usage
String safePyenvRoot = posixPath(pyenvRoot)
List activateCommands = ["export PATH=${safePyenvRoot}/bin:\$PATH"]
if (!script.env.PYENV_ROOT) {
// Only export PYENV_ROOT if it isn't part of the Jenkins environment
activateCommands.add("export PYENV_ROOT=${pyenvRoot}")
}
activateCommands.add('eval "$(pyenv init --path)"')
activateCommands.add('eval "$(pyenv init -)"')
if (isUnix) {
activateCommands.add('eval "$(pyenv init --path)"')
activateCommands.add('eval "$(pyenv init -)"')
} else {
// pyenv-win doesn't have the "init" command, so we have to manually add the shims
// to the PATH.
activateCommands.add("export PATH=${safePyenvRoot}/shims:\$PATH")
}

venv.activateCommands = activateCommands.join('\n') + '\n'
script.withEnv(["PYENV_VERSION=${python}"]) {
Expand Down

0 comments on commit 92d88ae

Please sign in to comment.