Skip to content

Commit 538b0e2

Browse files
committed
feat(omz-plugin): add devcontainer feature for oh-my-zsh plugins
1 parent 94d4fee commit 538b0e2

File tree

9 files changed

+288
-0
lines changed

9 files changed

+288
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# omz-plugin devcontainer feature
2+
3+
This feature will install and active plugins for oh my zsh
4+
5+
## Example Usage
6+
7+
```json
8+
"features": {
9+
"ghcr.io/ebizbase/devcontainer-features/omz-plugin:0": {
10+
"preInstalledPlugins": ["git", "git-auto-fetch"],
11+
"customPlugins": [
12+
"zsh-syntax-highlighting:https://github.com/zsh-users/zsh-syntax-highlighting.git",
13+
"zsh-autosuggestions:https://github.com/zsh-users/zsh-autosuggestions/archive/refs/tags/v0.7.1.zip"
14+
],
15+
"deleteInactive": false, // you can delete inactive plugins for lighter image
16+
}
17+
}
18+
```
19+
20+
## Options
21+
22+
| Options Id | Description | Type | Default |
23+
| ------------------- | --------------------------------------------------------------------------------------------- | ----- | ------- |
24+
| preInstalledPlugins | The list of pre-installed oh-my-zsh plugins to active (e.g. git,git-auto-fetch) (\*) | array | ["git"] |
25+
| customPlugins | The list of custom plugin you want to install with plugin-name1:url1,plugin-name2:url1 (\*\*) | array | [] |
26+
| deleteInactive | Should delete inactive plugins or not | bool | false |
27+
28+
**(\*)** See [list of oh-my-zsh preinstalled plugins](https://github.com/ohmyzsh/ohmyzsh/wiki/plugins)
29+
**(\*\*)** We support zip release url or git url. See example [here](https://github.com/ebizbase/dev-infras/blob/fff3b6ab9dd104f899b1a67c72e65c21f6f4e310/devcontainer-images/base-devcontainer/.devcontainer/devcontainer.json#L30)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "omz-plugin",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "devcontainer-features/omz-plugin/src",
5+
"projectType": "application",
6+
"tags": [],
7+
"targets": {
8+
"test": {
9+
"executor": "nx:run-commands",
10+
"options": {
11+
"command": "devcontainer features test --skip-autogenerated -p devcontainer-features/omz-plugin -f omz-plugin -i mcr.microsoft.com/devcontainers/base:ubuntu"
12+
}
13+
},
14+
"publish": {
15+
"executor": "nx:run-commands",
16+
"options": {
17+
"command": "devcontainer features publish devcontainer-features/omz-plugin/src/omz-plugin --registry ghcr.io --namespace=ebizbase/devcontainer-features",
18+
"parallel": false
19+
}
20+
},
21+
"version": {
22+
"executor": "@jscutlery/semver:version",
23+
"options": {
24+
"preset": "angular"
25+
}
26+
}
27+
}
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"id": "omz-plugin",
3+
"version": "0.1.0",
4+
"name": "Oh My Zsh plugins activate and setup",
5+
"documentationURL": "https://github.com/ebizbase/dev-infras/tree/main/devcontainer-features/omz-plugin/README.md",
6+
"description": "Setup and activate oh my zsh plugins devcontainer feature",
7+
"options": {
8+
"preInstalledPlugins": {
9+
"type": "string",
10+
"default": "git",
11+
"description": "The list of pre-installed oh-my-zsh plugins to active (e.g. git)"
12+
},
13+
"customPlugins": {
14+
"type": "string",
15+
"default": "",
16+
"description": "The list of custom plugin you want to install with plugin-name1:url1,plugin-name2:url1 (e.g. zsh-syntax-highlighting:https://github.com/zsh-users/zsh-syntax-highlighting/archive/refs/tags/0.8.0.zip). We support zip file url or git repository url"
17+
},
18+
"deleteInactive": {
19+
"type": "boolean",
20+
"default": false,
21+
"description": "Should delete inactive plugins or not"
22+
},
23+
"debug": {
24+
"type": "boolean",
25+
"default": false,
26+
"description": "Enable debug mode (Internal use)"
27+
}
28+
},
29+
"dependsOn": {
30+
"ghcr.io/devcontainers/features/common-utils": {}
31+
}
32+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/bin/bash
2+
# trunk-ignore-all(shellcheck/SC2312)
3+
set -e
4+
5+
PRE_INSTALLED_PLUGINS=${PREINSTALLEDPLUGINS:-'git,git-auto-fetch'}
6+
CUSTOM_PLUGINS=${CUSTOMPLUGINS:-''}
7+
ACTIVE_PLUGINS=()
8+
DELETE_INACTIVE=${DELETEINACTIVE:-false}
9+
DEBUG=${DEBUG:-false}
10+
11+
debug() {
12+
if [[ ${DEBUG} == true ]]; then
13+
echo "$1" >>/tmp/omz-plugin.log
14+
fi
15+
}
16+
17+
if [[ -n ${_REMOTE_USER_HOME} ]]; then
18+
USER_HOME="${_REMOTE_USER_HOME}"
19+
elif [[ ${_REMOTE_USER} == "root" ]]; then
20+
USER_HOME="/root"
21+
# Check if user already has a home directory other than /home/${USERNAME}
22+
elif [[ "/home/${_REMOTE_USER}" != $(getent passwd "${_REMOTE_USER}" | cut -d: -f6) ]]; then
23+
USER_HOME=$(getent passwd "${_REMOTE_USER}" | cut -d: -f6)
24+
else
25+
USER_HOME="/home/${_REMOTE_USER}"
26+
fi
27+
28+
debug "==============================="
29+
debug "PRE_INSTALLED_PLUGINS: ${PRE_INSTALLED_PLUGINS}"
30+
debug "CUSTOM_PLUGINS: ${CUSTOM_PLUGINS}"
31+
debug "REMOTE_USER: ${_REMOTE_USER}"
32+
debug "USER_HOME: ${USER_HOME}"
33+
debug "==============================="
34+
35+
IFS=',' read -r -a plugins_array <<<"${CUSTOM_PLUGINS}"
36+
for plugin in "${plugins_array[@]}"; do
37+
name=$(echo "${plugin}" | cut -d':' -f1)
38+
url=$(echo "${plugin}" | cut -d':' -f2-)
39+
debug " - Installing plugin: ${name} from ${url}"
40+
plugin_dir="${USER_HOME}/.oh-my-zsh/custom/plugins/${name}"
41+
mkdir -p "${plugin_dir}"
42+
debug " - Plugin directory: ${plugin_dir}"
43+
44+
if [[ ${url} == *.git ]]; then
45+
debug " - Cloning plugin from git"
46+
git clone --depth=1 "${url}" "${plugin_dir}"
47+
elif [[ ${url} == *.zip ]]; then
48+
debug " - Downloading plugin from zip"
49+
curl -L "${url}" -o /tmp/plugin.zip
50+
zip_content=$(unzip -Z1 /tmp/plugin.zip)
51+
base_dir=$(echo "${zip_content}" | head -n 1)
52+
unzip /tmp/plugin.zip -d /tmp/plugin
53+
mv /tmp/plugin/"${base_dir}"/* "${plugin_dir}/"
54+
rm /tmp/plugin.zip
55+
rm -r /tmp/plugin
56+
else
57+
echo "Unsupported URL format: ${url}"
58+
exit 1
59+
fi
60+
61+
ACTIVE_PLUGINS+=("${name}")
62+
done
63+
64+
IFS=',' read -r -a plugins_array <<<"${PRE_INSTALLED_PLUGINS}"
65+
for plugin in "${plugins_array[@]}"; do
66+
ACTIVE_PLUGINS+=("${plugin}")
67+
done
68+
69+
ACTIVE_PLUGINS_STRING=$(printf " %s" "${ACTIVE_PLUGINS[@]}")
70+
71+
sed -i "s/plugins=(.*)/plugins=(${ACTIVE_PLUGINS_STRING:1})/g" "${USER_HOME}/.zshrc"
72+
73+
if [[ ${DELETE_INACTIVE} == false ]]; then
74+
exit 0
75+
fi
76+
77+
PLUGINS_DIRS=("${USER_HOME}/.oh-my-zsh/custom/plugins" "${USER_HOME}/.oh-my-zsh/plugins")
78+
for PLUGIN_DIR in "${PLUGINS_DIRS[@]}"; do
79+
debug "Checking for inactive plugins in ${PLUGIN_DIR}"
80+
for dir in "${PLUGIN_DIR}"/*; do
81+
debug " - Checking plugin: ${dir}"
82+
dir_name=$(basename "${dir}")
83+
debug " - Plugin name: ${dir_name}"
84+
# Kiểm tra xem dir_name có nằm trong mảng ACTIVE_PLUGINS không
85+
should_delete=true
86+
for active_plugin in "${ACTIVE_PLUGINS[@]}"; do
87+
if [[ ${active_plugin} == "${dir_name}" ]]; then
88+
should_delete=false
89+
break
90+
fi
91+
done
92+
debug " - Should delete: ${should_delete}"
93+
if [[ ${should_delete} == true ]]; then
94+
debug " - Deleting plugin: ${dir}"
95+
rm -rf "${dir}"
96+
fi
97+
done
98+
done
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
# trunk-ignore-all(shellcheck/SC2312)
3+
set -e
4+
5+
source dev-container-features-test-lib
6+
7+
check "LOG" cat /tmp/omz-plugin.log
8+
check "verify omz-plugin is active" grep -v '^#' ~/.zshrc | grep 'plugins=(.*)' | awk -F'[()]' '{print $2}' | grep -q 'git'
9+
10+
reportResults
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
# trunk-ignore-all(shellcheck/SC2312)
3+
4+
set -e
5+
6+
source dev-container-features-test-lib
7+
8+
check "LOG" cat /tmp/omz-plugin.log
9+
check "git omz plugin should be active" grep -v '^#' ~/.zshrc | grep 'plugins=(.*)' | awk -F'[()]' '{print $2}' | grep -q 'git' && true || exit 1
10+
11+
reportResults
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
# trunk-ignore-all(shellcheck/SC2312)
3+
# trunk-ignore-all(shellcheck/SC2010)
4+
5+
set -e
6+
7+
source dev-container-features-test-lib
8+
9+
check "LOG" cat /tmp/omz-plugin.log
10+
check "git omz plugin should be active" grep -v '^#' ~/.zshrc | grep 'plugins=(.*)' | awk -F'[()]' '{print $2}' | grep -q 'git' || exit 1
11+
check "all custom omz plugins should be deleted except zsh-syntax-highlighting" ls -lR ~/.oh-my-zsh/custom/plugins | grep -c ^d | grep -q '1' && ls -lR ~/.oh-my-zsh/custom/plugins | grep ^d | grep -q 'zsh-syntax-highlighting' || exit 1
12+
check "all pre installed omz plugins should be deleted except git" ls -lR ~/.oh-my-zsh/plugins | grep -c ^d | grep -q '1' && ls -lR ~/.oh-my-zsh/plugins | grep ^d | grep -q 'git' || exit 1
13+
14+
reportResults
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
# trunk-ignore-all(shellcheck/SC2312)
3+
set -e
4+
5+
source dev-container-features-test-lib
6+
7+
check "LOG" cat /tmp/omz-plugin.log
8+
check "checking active omz-plugin" grep -v '^#' ~/.zshrc | grep 'plugins=(.*)' | awk -F'[()]' '{print $2}' | grep -q 'zsh-syntax-highlighting zsh-autosuggestions' || exit 1
9+
check "plugins zsh-syntax-highlighting sbould be installed" test -f ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh || exit 1
10+
check "plugins zsh-autosuggestions sbould be installed" test -f ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh || exit 1
11+
12+
# check "plugin example sbould be removed" test -f ~/.oh-my-zsh/custom/plugins/example/example.plugin.zsh && exit 1
13+
14+
# check "plugins zsh-syntax-highlighting sbould be installed" test -f ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh || exit 1
15+
# check "plugins zsh-autosuggestions sbould be installed" ls -l ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
16+
# check "should keep git omz-plugins" ls -A ~/.oh-my-zsh/plugins | grep -q 'git' && exit 1 || true
17+
# check "should keep git-auto-fetch omz-plugins" ls -A ~/.oh-my-zsh/plugins | grep -q 'git-auto-fetch' && exit 1 || true
18+
19+
reportResults
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"default": {
3+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
4+
"remoteUser": "vscode",
5+
"features": {
6+
"omz-plugin": {
7+
"debug": true
8+
}
9+
}
10+
},
11+
"active": {
12+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
13+
"remoteUser": "root",
14+
"features": {
15+
"omz-plugin": {
16+
"preInstalledPlugins": ["git", "git-auto-fetch"],
17+
"debug": true
18+
}
19+
}
20+
},
21+
"install": {
22+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
23+
"remoteUser": "root",
24+
"features": {
25+
"omz-plugin": {
26+
"customPlugins": [
27+
"zsh-syntax-highlighting:https://github.com/zsh-users/zsh-syntax-highlighting.git",
28+
"zsh-autosuggestions:https://github.com/zsh-users/zsh-autosuggestions/archive/refs/tags/v0.7.1.zip"
29+
],
30+
"debug": true
31+
}
32+
}
33+
},
34+
"delete": {
35+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
36+
"remoteUser": "vscode",
37+
"features": {
38+
"omz-plugin": {
39+
"customPlugins": [
40+
"zsh-syntax-highlighting:https://github.com/zsh-users/zsh-syntax-highlighting.git"
41+
],
42+
"deleteInactive": true,
43+
"debug": true
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)