aboutsummaryrefslogtreecommitdiff
path: root/.devcontainer/node.sh
diff options
context:
space:
mode:
authorKevin J Hoerr <kjhoerr@protonmail.com>2021-09-20 23:08:50 +0000
committerKevin J Hoerr <kjhoerr@protonmail.com>2021-09-20 23:08:50 +0000
commit49373b9b516fbb4aaa69376d4c8705fdfa633fa0 (patch)
tree1812a66836cfaea85420f15d236e7bc0dafdf332 /.devcontainer/node.sh
parent22af0d845e56615b216457111524e3a268d2a526 (diff)
downloadao-coverage-49373b9b516fbb4aaa69376d4c8705fdfa633fa0.tar.gz
ao-coverage-49373b9b516fbb4aaa69376d4c8705fdfa633fa0.tar.bz2
ao-coverage-49373b9b516fbb4aaa69376d4c8705fdfa633fa0.zip
Last .devcontainer changes
- Copy over more RC changes - Copy over Microsoft's license and copyright for brevity - Change container names
Diffstat (limited to '.devcontainer/node.sh')
-rw-r--r--.devcontainer/node.sh213
1 files changed, 212 insertions, 1 deletions
diff --git a/.devcontainer/node.sh b/.devcontainer/node.sh
index 0b47058..3306fbe 100644
--- a/.devcontainer/node.sh
+++ b/.devcontainer/node.sh
@@ -1,9 +1,24 @@
#!/bin/bash
+#-------------------------------------------------------------------------------------------------------------
+# Copyright (c) Microsoft Corporation. All rights reserved.
+# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
+#-------------------------------------------------------------------------------------------------------------
+#
+# Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/common.md
+#
+# This is file is derived from multiple of the image scripts provided by Microsoft (common-debian.sh and
+# node-debian.sh). The reasoning for the separate maintenance is to provide support for both Node and Mongo
+# tools on the arm64 architecture. Mongo's own repository does not provide those tools for arm64 on Debian,
+# on which Microsoft bases all of their Node devcontainer images. The scripts have thus been adapted to work
+# with one of the official Ubuntu Docker images. The mentioned MongoDB tools are installed via the Dockerfile
+# that calls this script.
export NVM_DIR="/usr/local/share/nvm"
export NODE_VERSION="lts"
export NVM_VERSION="0.38.0"
USERNAME="node"
+INSTALL_ZSH=${1:-"true"}
+MARKER_FILE="/usr/local/etc/vscode-dev-containers/common"
updaterc() {
echo "Updating /etc/bash.bashrc and /etc/zsh/zshrc..."
@@ -28,7 +43,7 @@ apt_get_update_if_needed()
check_packages() {
if ! dpkg -s "$@" > /dev/null 2>&1; then
apt_get_update_if_needed
- apt-get -y install --no-install-recommends "$@"
+ apt-get -y install --no-install-recommends "$@" 2> >( grep -v 'debconf: delaying package configuration, since apt-utils is not installed' >&2 )
fi
}
@@ -84,6 +99,14 @@ check_packages apt-utils \
echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME
+# Ensure at least the en_US.UTF-8 UTF-8 locale is available.
+# Common need for both applications and things like the agnoster ZSH theme.
+if [ "${LOCALE_ALREADY_SET}" != "true" ] && ! grep -o -E '^\s*en_US.UTF-8\s+UTF-8' /etc/locale.gen > /dev/null; then
+ echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
+ locale-gen
+ LOCALE_ALREADY_SET="true"
+fi
+
# Install yarn
if type yarn > /dev/null 2>&1; then
echo "Yarn already installed."
@@ -141,4 +164,192 @@ export NVM_DIR="${NVM_DIR}"
EOF
)"
+# ** Shell customization section **
+if [ "${USERNAME}" = "root" ]; then
+ user_rc_path="/root"
+else
+ user_rc_path="/home/${USERNAME}"
+fi
+
+# Restore user .bashrc defaults from skeleton file if it doesn't exist or is empty
+if [ ! -f "${user_rc_path}/.bashrc" ] || [ ! -s "${user_rc_path}/.bashrc" ] ; then
+ cp /etc/skel/.bashrc "${user_rc_path}/.bashrc"
+fi
+
+# Restore user .profile defaults from skeleton file if it doesn't exist or is empty
+if [ ! -f "${user_rc_path}/.profile" ] || [ ! -s "${user_rc_path}/.profile" ] ; then
+ cp /etc/skel/.profile "${user_rc_path}/.profile"
+fi
+
+# .bashrc/.zshrc snippet
+rc_snippet="$(cat << 'EOF'
+if [ -z "${USER}" ]; then export USER=$(whoami); fi
+if [[ "${PATH}" != *"$HOME/.local/bin"* ]]; then export PATH="${PATH}:$HOME/.local/bin"; fi
+# Display optional first run image specific notice if configured and terminal is interactive
+if [ -t 1 ] && [[ "${TERM_PROGRAM}" = "vscode" || "${TERM_PROGRAM}" = "codespaces" ]] && [ ! -f "$HOME/.config/vscode-dev-containers/first-run-notice-already-displayed" ]; then
+ if [ -f "/usr/local/etc/vscode-dev-containers/first-run-notice.txt" ]; then
+ cat "/usr/local/etc/vscode-dev-containers/first-run-notice.txt"
+ elif [ -f "/workspaces/.codespaces/shared/first-run-notice.txt" ]; then
+ cat "/workspaces/.codespaces/shared/first-run-notice.txt"
+ fi
+ mkdir -p "$HOME/.config/vscode-dev-containers"
+ # Mark first run notice as displayed after 10s to avoid problems with fast terminal refreshes hiding it
+ ((sleep 10s; touch "$HOME/.config/vscode-dev-containers/first-run-notice-already-displayed") &)
+fi
+# Set the default git editor if not already set
+if [ -z "$(git config --get core.editor)" ] && [ -z "${GIT_EDITOR}" ]; then
+ if [ "${TERM_PROGRAM}" = "vscode" ]; then
+ if [[ -n $(command -v code-insiders) && -z $(command -v code) ]]; then
+ export GIT_EDITOR="code-insiders --wait"
+ else
+ export GIT_EDITOR="code --wait"
+ fi
+ fi
+fi
+EOF
+)"
+
+# code shim, it fallbacks to code-insiders if code is not available
+cat << 'EOF' > /usr/local/bin/code
+#!/bin/sh
+get_in_path_except_current() {
+ which -a "$1" | grep -A1 "$0" | grep -v "$0"
+}
+code="$(get_in_path_except_current code)"
+if [ -n "$code" ]; then
+ exec "$code" "$@"
+elif [ "$(command -v code-insiders)" ]; then
+ exec code-insiders "$@"
+else
+ echo "code or code-insiders is not installed" >&2
+ exit 127
+fi
+EOF
+chmod +x /usr/local/bin/code
+
+# systemctl shim - tells people to use 'service' if systemd is not running
+cat << 'EOF' > /usr/local/bin/systemctl
+#!/bin/sh
+set -e
+if [ -d "/run/systemd/system" ]; then
+ exec /bin/systemctl/systemctl "$@"
+else
+ echo '\n"systemd" is not running in this container due to its overhead.\nUse the "service" command to start services intead. e.g.: \n\nservice --status-all'
+fi
+EOF
+chmod +x /usr/local/bin/systemctl
+
+# Codespaces bash and OMZ themes - partly inspired by https://github.com/ohmyzsh/ohmyzsh/blob/master/themes/robbyrussell.zsh-theme
+codespaces_bash="$(cat \
+<<'EOF'
+# Codespaces bash prompt theme
+__bash_prompt() {
+ local userpart='`export XIT=$? \
+ && [ ! -z "${GITHUB_USER}" ] && echo -n "\[\033[0;32m\]@${GITHUB_USER} " || echo -n "\[\033[0;32m\]\u " \
+ && [ "$XIT" -ne "0" ] && echo -n "\[\033[1;31m\]➜" || echo -n "\[\033[0m\]➜"`'
+ local gitbranch='`\
+ export BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --short HEAD 2>/dev/null); \
+ if [ "${BRANCH}" != "" ]; then \
+ echo -n "\[\033[0;36m\](\[\033[1;31m\]${BRANCH}" \
+ && if git ls-files --error-unmatch -m --directory --no-empty-directory -o --exclude-standard ":/*" > /dev/null 2>&1; then \
+ echo -n " \[\033[1;33m\]✗"; \
+ fi \
+ && echo -n "\[\033[0;36m\]) "; \
+ fi`'
+ local lightblue='\[\033[1;34m\]'
+ local removecolor='\[\033[0m\]'
+ PS1="${userpart} ${lightblue}\w ${gitbranch}${removecolor}\$ "
+ unset -f __bash_prompt
+}
+__bash_prompt
+EOF
+)"
+
+codespaces_zsh="$(cat \
+<<'EOF'
+# Codespaces zsh prompt theme
+__zsh_prompt() {
+ local prompt_username
+ if [ ! -z "${GITHUB_USER}" ]; then
+ prompt_username="@${GITHUB_USER}"
+ else
+ prompt_username="%n"
+ fi
+ PROMPT="%{$fg[green]%}${prompt_username} %(?:%{$reset_color%}➜ :%{$fg_bold[red]%}➜ )" # User/exit code arrow
+ PROMPT+='%{$fg_bold[blue]%}%(5~|%-1~/…/%3~|%4~)%{$reset_color%} ' # cwd
+ PROMPT+='$(git_prompt_info)%{$fg[white]%}$ %{$reset_color%}' # Git status
+ unset -f __zsh_prompt
+}
+ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[cyan]%}(%{$fg_bold[red]%}"
+ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
+ZSH_THEME_GIT_PROMPT_DIRTY=" %{$fg_bold[yellow]%}✗%{$fg_bold[cyan]%})"
+ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg_bold[cyan]%})"
+__zsh_prompt
+EOF
+)"
+
+# Add RC snippet and custom bash prompt
+if [ "${RC_SNIPPET_ALREADY_ADDED}" != "true" ]; then
+ echo "${rc_snippet}" >> /etc/bash.bashrc
+ echo "${codespaces_bash}" >> "${user_rc_path}/.bashrc"
+ echo 'export PROMPT_DIRTRIM=4' >> "${user_rc_path}/.bashrc"
+ if [ "${USERNAME}" != "root" ]; then
+ echo "${codespaces_bash}" >> "/root/.bashrc"
+ echo 'export PROMPT_DIRTRIM=4' >> "/root/.bashrc"
+ fi
+ chown ${USERNAME}:${USERNAME} "${user_rc_path}/.bashrc"
+ RC_SNIPPET_ALREADY_ADDED="true"
+fi
+
+# Optionally install and configure zsh and Oh My Zsh!
+if [ "${INSTALL_ZSH}" = "true" ]; then
+ if ! type zsh > /dev/null 2>&1; then
+ apt_get_update_if_needed
+ apt-get install -y zsh
+ fi
+ if [ "${ZSH_ALREADY_INSTALLED}" != "true" ]; then
+ echo "${rc_snippet}" >> /etc/zsh/zshrc
+ ZSH_ALREADY_INSTALLED="true"
+ fi
+
+ # Adapted, simplified inline Oh My Zsh! install steps that adds, defaults to a codespaces theme.
+ # See https://github.com/ohmyzsh/ohmyzsh/blob/master/tools/install.sh for official script.
+ oh_my_install_dir="${user_rc_path}/.oh-my-zsh"
+ if [ ! -d "${oh_my_install_dir}" ]; then
+ template_path="${oh_my_install_dir}/templates/zshrc.zsh-template"
+ user_rc_file="${user_rc_path}/.zshrc"
+ umask g-w,o-w
+ mkdir -p ${oh_my_install_dir}
+ git clone --depth=1 \
+ -c core.eol=lf \
+ -c core.autocrlf=false \
+ -c fsck.zeroPaddedFilemode=ignore \
+ -c fetch.fsck.zeroPaddedFilemode=ignore \
+ -c receive.fsck.zeroPaddedFilemode=ignore \
+ "https://github.com/ohmyzsh/ohmyzsh" "${oh_my_install_dir}" 2>&1
+ echo -e "$(cat "${template_path}")\nDISABLE_AUTO_UPDATE=true\nDISABLE_UPDATE_PROMPT=true" > ${user_rc_file}
+ sed -i -e 's/ZSH_THEME=.*/ZSH_THEME="codespaces"/g' ${user_rc_file}
+
+ mkdir -p ${oh_my_install_dir}/custom/themes
+ echo "${codespaces_zsh}" > "${oh_my_install_dir}/custom/themes/codespaces.zsh-theme"
+ # Shrink git while still enabling updates
+ cd "${oh_my_install_dir}"
+ git repack -a -d -f --depth=1 --window=1
+ # Copy to non-root user if one is specified
+ if [ "${USERNAME}" != "root" ]; then
+ cp -rf "${user_rc_file}" "${oh_my_install_dir}" /root
+ chown -R ${USERNAME}:${USERNAME} "${user_rc_path}"
+ fi
+ fi
+fi
+
+# Write marker file
+mkdir -p "$(dirname "${MARKER_FILE}")"
+echo -e "\
+ PACKAGES_ALREADY_INSTALLED=${PACKAGES_ALREADY_INSTALLED}\n\
+ LOCALE_ALREADY_SET=${LOCALE_ALREADY_SET}\n\
+ EXISTING_NON_ROOT_USER=${EXISTING_NON_ROOT_USER}\n\
+ RC_SNIPPET_ALREADY_ADDED=${RC_SNIPPET_ALREADY_ADDED}\n\
+ ZSH_ALREADY_INSTALLED=${ZSH_ALREADY_INSTALLED}" > "${MARKER_FILE}"
+
echo "Done!" \ No newline at end of file