From 92d88ae4621c77f1bf5149fb6290afac18f70f2a Mon Sep 17 00:00:00 2001 From: nre Date: Wed, 29 Jun 2022 17:40:41 +0200 Subject: [PATCH] Support pyenv on Windows --- src/com/ableton/VirtualEnv.groovy | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/com/ableton/VirtualEnv.groovy b/src/com/ableton/VirtualEnv.groovy index dae3d0a..c71c52a 100644 --- a/src/com/ableton/VirtualEnv.groovy +++ b/src/com/ableton/VirtualEnv.groovy @@ -37,10 +37,6 @@ 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 @@ -48,13 +44,21 @@ class VirtualEnv implements Serializable { 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}"]) {