mamba-org / vscode-micromamba

A VSCode extension to generate development environments using micromamba and conda-forge package repository
BSD 3-Clause "New" or "Revised" License
82 stars 10 forks source link

Add command to generate bootstrap and activate scripts #34

Open jfchenier opened 1 year ago

jfchenier commented 1 year ago

This is more of a feature request than a bug, feel free to close the issue if it falls outside the current scope.

Currently, when utilizing this extension and including the environment.yml file in a project, the user is restricted to controlling the environment solely within VSCode. It would be highly beneficial to have a command within the extension that generates a script enabling the initialization and activation of the virtual environment outside of the VSCode environment. This enhancement would greatly enhance flexibility and convenience for users.

goyalyashpal commented 1 year ago

umh, micromamba activate <env_name>?

jfchenier commented 1 year ago

umh, micromamba activate <env_name>?

It is a bit more complicated than that if the user doesn't have a global installation of Micromamba. The shell init script must also get called. Here is what I use on my side for the activation :

if test -n "$BASH"; then
  SOURCE=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
elif test -n "$ZSH_NAME"; then
  SOURCE="$( dirname -- "$( readlink -f -- "$0"; )"; )"
else
  echo 'Error : Unable to detect shell. Only bash and zsh are supported'
  return 1
fi

ENV_FILE_PATH="$PROJECT_ROOT/environment.yml"

# Read project name from environment file.
PROJECT_NAME="$( grep '^name:' $ENV_FILE_PATH | sed 's/^name: //' )"

export MAMBA_ROOT_PREFIX=$SOURCE/.micromamba
export MAMBA_EXE="$MAMBA_ROOT_PREFIX/micromamba"

eval "$($MAMBA_ROOT_PREFIX/micromamba shell hook --shell=posix)"

if command -v micromamba &> /dev/null; then
    micromamba activate $PROJECT_NAME
else
    echo 'Error : Unable to activate environment.'
    return 1
fi

For the environment creation, the script must also download Micromamba.

PATH="$HOME/.local/bin:$PATH"

if test -n "$BASH"; then
  SOURCE=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
elif test -n "$ZSH_NAME"; then
  SOURCE="$( dirname -- "$( readlink -f -- "$0"; )"; )"
else
  echo 'Error : Unable to detect shell. Only bash and zsh are supported'
  return 1
fi

PROJECT_ROOT="$SOURCE"
export PROJECT_ROOT

OS=$(uname -s | tr '[:upper:]' '[:lower:]')

export MAMBA_ROOT_PREFIX="$SOURCE/.micromamba"
export MAMBA_EXE="$MAMBA_ROOT_PREFIX/micromamba"

ENV_FILE_PATH="$PROJECT_ROOT/environment.yml"

# Read project name from environment file.
PROJECT_NAME="$( grep '^name:' $ENV_FILE_PATH | sed 's/^name: //' )"

if [ -z "$PROJECT_NAME" ]; then
   echo 'Error : Unable to detect project name. Please check the environment file.'
   return 1
fi

if ! [ -f $MAMBA_ROOT_PREFIX/micromamba ]; then
   mkdir -p $MAMBA_ROOT_PREFIX/etc/profile.d

   echo 'Downloading Micromamba ...'
   cd $MAMBA_ROOT_PREFIX

   if [ $OS = "darwin" ]; then
      if [ "$(uname -m)" = "x86_64" ]; then
         curl -Ls https://micro.mamba.pm/api/micromamba/osx-64/1.0.0 | tar -xvj --strip-components=1 -C . bin/micromamba
      else
         curl -Ls https://micro.mamba.pm/api/micromamba/osx-arm64/1.0.0 | tar -xvj --strip-components=1 -C . bin/micromamba
      fi
   elif [ $OS = "linux" ]; then
      if [ "$(uname -m)" = "x86_64" ]; then
         curl -Ls https://micro.mamba.pm/api/micromamba/linux-64/1.0.0 | tar -xvj --strip-components=1 -C . bin/micromamba
      else
         echo 'Error : Unsupported system'
         return 1
      fi
   else
      echo 'Error : Unsupported system'
      return 1
   fi
   cd -

   eval "$(.micromamba/micromamba shell hook --shell=posix)"
   printf '%s\n' "$(.micromamba/micromamba shell hook --shell=posix)" > $MAMBA_ROOT_PREFIX/etc/profile.d/micromamba.sh

   touch $MAMBA_ROOT_PREFIX/.env.$PROJECT_NAME
fi

if command -v micromamba &> /dev/null; then
   #init micromamba environment.
   micromamba create -y --file $ENV_FILE_PATH
   micromamba activate $PROJECT_NAME
else
   echo 'Error : micromamba not installed properly'
   return 1
fi

return 0

I haven't figured out yet how to generate the same environment variables than the extension for the .env.{env_name} file. I would really like to help more to develop this feature, but I really lack typescript skills. (I am en embedded developer).

If there is some interest, I can provide the scripts for windows too.

goyalyashpal commented 1 year ago

oh, you are talking about linux environment??

If there is some interest, I can provide the scripts for windows too.

well, i don't think there's a need for this. on windows, it's just a standalone file - i put-ed it anywhere, entered it in $PATH environment variable (system wide), and it runs fine....

(i am using msys2 to use bash)

$ micromamba activate facerecog_env
critical libmamba Shell not initialized

'micromamba' is running as a subprocess and can't modify the parent shell.
Thus you must initialize your shell before using activate and deactivate. 

To initialize the current bash shell, run:
    $ eval "$(micromamba shell hook --shell bash)"
and then activate or deactivate with:
    $ micromamba activate
To automatically initialize all future (bash) shells, run:
    $ micromamba shell init --shell bash --root-prefix=~/micromamba
If your shell was already initialized, reinitialize your shell with:
    $ micromamba shell reinit --shell bash
Otherwise, this may be an issue. In the meantime you can run commands. See:
    $ micromamba run --help

Supported shells are {bash, zsh, csh, xonsh, cmd.exe, powershell, fish}.

$ eval "$(micromamba shell hook --shell bash)"

$ micromamba activate facerecog_env
warning  libmamba 'root_prefix' set with default value: C:\Users\...\micromamba
(facerecog_env)
$ 
corker commented 1 year ago

We don't need micromamba to activate the environment when we want to activate an environment created by the extension. A .env.<name> file is generated when the environment is created. To activate the environment, we could set environment variables from the .env.<name> file. I'm trying to figure out how to make it convenient and simple for users in all those different terminal types and can't see it yet.