Skip to content

Instantly share code, notes, and snippets.

@luutuankiet
Last active July 19, 2024 03:11
Show Gist options
  • Save luutuankiet/5c88f9640c596ec5a23a96337304756b to your computer and use it in GitHub Desktop.
Save luutuankiet/5c88f9640c596ec5a23a96337304756b to your computer and use it in GitHub Desktop.
boilerplate devcontainer script
#!/bin/bash
# usage : `source <(curl -s https://gist.githubusercontent.com/luutuankiet/5c88f9640c596ec5a23a96337304756b/raw/boilerplate_devcontainer.sh)`
# Set working directory
ENV_WORK_DIR="$(pwd)"
# URLs for devcontainer.json and postCreateCommand.sh
URL_DEVCONTAINER="https://gist.githubusercontent.com/luutuankiet/23a7c5bdf896896f8b9d1fa1b95ddb08/raw/devcontainer.json"
URL_POSTCREATESCRIPT="https://gist.githubusercontent.com/luutuankiet/a29c065133be245f17786c81f48c4f09/raw/postCreateCommand.sh"
# Function to print section headers
print_section() {
echo
echo "================================="
echo "================================="
echo "================================="
echo " $1"
echo "================================="
echo "================================="
echo "================================="
echo
}
# Function to download devcontainer files
download_devcontainer_files() {
print_section "DOWNLOADING DEVCONTAINER FILES..."
mkdir -p "$ENV_WORK_DIR/.devcontainer"
if ! curl -sSfL "$URL_DEVCONTAINER" -o "$ENV_WORK_DIR/.devcontainer/devcontainer.json"; then
echo "Failed to download devcontainer.json from $URL_DEVCONTAINER"
exit 1
fi
if ! curl -sSfL "$URL_POSTCREATESCRIPT" -o "$ENV_WORK_DIR/.devcontainer/postCreateCommand.sh"; then
echo "Failed to download postCreateCommand.sh from $URL_POSTCREATESCRIPT"
exit 1
fi
}
# Function to install devcontainer configuration
install_devcontainer() {
download_devcontainer_files
print_section "INSTALLING DEVCONTAINER STANDARD CONFIGS..."
# Ask for container name
echo "Enter the name to add to runArgs (default: devcontainer): "
read container_name
name=${container_name:-devcontainer} # Use default value "devcontainer" if no name is provided
# Append the runArgs entry to devcontainer.json
sed -i '' -e '$s/}/,\n "runArgs": ["--name", "'"$name"'"]\n}/' "$ENV_WORK_DIR/.devcontainer/devcontainer.json"
# Scaffold requirements.txt
echo "pandas" >> "$ENV_WORK_DIR/requirements.txt"
echo "ipykernel" >> "$ENV_WORK_DIR/requirements.txt"
echo ".venv" >> "$ENV_WORK_DIR/.gitignore"
echo "Devcontainer configs installation completed."
}
# Function to install env.sh script
install_env_sh() {
print_section "INSTALLING ENV SCRIPT..."
mkdir -p "$ENV_WORK_DIR/src"
cat > "$ENV_WORK_DIR/env.sh" <<EOF
#!/bin/bash
ENV_WORK_DIR="\$(pwd)"
# Function to initialize .env file
init_env() {
# Add more environment variables here if needed
cat > ".env" <<EOENV
VIRTUAL_ENV="\$ENV_WORK_DIR/.venv"
PYTHONPATH="\$ENV_WORK_DIR/src"
EOENV
}
# Function to source .env file
source_env() {
set -a # Automatically export all variables
source .env
set +a # Stop automatically exporting variables
}
# Invoke init_env and source_env functions
init_env
source_env
EOF
chmod +x "$ENV_WORK_DIR/env.sh"
echo "env.sh installation completed."
}
# Function to invoke env.sh script
invoke_env_sh() {
print_section "INIT ENV..."
"$ENV_WORK_DIR/env.sh"
echo "Script completed."
}
# Main script logic
install_devcontainer
install_env_sh
invoke_env_sh
echo "Next action : go to .devcontainer folder and change the configurations to your liking; it is recommended to keep the blocks marked with default."
echo "Press any key to exit..."
read -n 1
echo
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment