From c9a8424669a09095095d87dafdb9f8954822edf6 Mon Sep 17 00:00:00 2001 From: Anthony Kim <62267334+anthonykim1@users.noreply.github.com> Date: Wed, 6 Sep 2023 19:55:13 -0700 Subject: [PATCH] Dev Container for lsprotocol (#267) Dev Container support for lsprotocol repository, referencing #234 and adding more things on the way, such as multiple Python versions, cargo, dotnet, venv, requirements, but in a way similar to how dev container is done in the VS Code Python repo: https://github.com/microsoft/vscode-python/pull/21675 - [x] Add devcontainer.json, onCreateCommand.sh, postCreateCommand.sh appropriately - [x] Add installation and append to PATH for Cargo, Rust, Python(s) - [x] Install requirements from postCreateCommand.sh after activating virtual environment --- .devcontainer/Dockerfile | 8 ++++++++ .devcontainer/devcontainer.json | 12 ++++++++---- scripts/onCreateCommand.sh | 28 ++++++++++++++++++++++++++++ scripts/postCreateCommand.sh | 3 +++ 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 scripts/onCreateCommand.sh create mode 100644 scripts/postCreateCommand.sh diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..dbe2fa1 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,8 @@ +FROM mcr.microsoft.com/devcontainers/base:bookworm +RUN sudo apt update + +# Pyenv installation of Python versions is missing below packages. +RUN sudo apt install libbz2-dev libncurses5-dev libffi-dev libreadline-dev libsqlite3-dev liblzma-dev -y + +# Install fish. +RUN sudo apt install fish -y \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index f33faf1..6b0dac3 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,7 +1,10 @@ { - "name": "Python 3", - "image": "mcr.microsoft.com/devcontainers/python:1-3.11-bullseye", - "features": { + "name": "LSP Dev Container", + "build": { + "dockerfile": "./Dockerfile", + "context": ".." + }, + "features": { "ghcr.io/devcontainers/features/powershell:1": { "version": "latest" }, @@ -20,5 +23,6 @@ ] } }, - "postCreateCommand": "python -m pip install nox && python -m pip install -r ./packages/python/requirements.txt -r ./requirements.txt" + "onCreateCommand": "bash scripts/onCreateCommand.sh", + "postCreateCommand": "bash scripts/postCreateCommand.sh" } \ No newline at end of file diff --git a/scripts/onCreateCommand.sh b/scripts/onCreateCommand.sh new file mode 100644 index 0000000..f867da4 --- /dev/null +++ b/scripts/onCreateCommand.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +# Install pyenv and Python versions here to avoid using shim. +curl https://pyenv.run | bash +echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc +echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc + +export PYENV_ROOT="$HOME/.pyenv" +command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH" +# eval "$(pyenv init -)" Comment this out and DO NOT use shim. +source ~/.bashrc + +# Install Rust and Cargo +curl https://sh.rustup.rs -sSf | bash -s -- -y +echo 'source $HOME/.cargo/env' >> ~/.bashrc + +# Install Python via pyenv . +pyenv install 3.8:latest 3.9:latest 3.10:latest 3.11:latest + +# Set default Python version to 3.8 . +pyenv global 3.8.18 + +# Create Virutal environment. +pyenv exec python3.8 -m venv .venv + +# Activate Virtual environment. +source /workspaces/lsprotocol/.venv/bin/activate + diff --git a/scripts/postCreateCommand.sh b/scripts/postCreateCommand.sh new file mode 100644 index 0000000..805c353 --- /dev/null +++ b/scripts/postCreateCommand.sh @@ -0,0 +1,3 @@ +source /workspaces/lsprotocol/.venv/bin/activate +python -m pip install nox +python -m pip install -r ./packages/python/requirements.txt -r ./requirements.txt \ No newline at end of file