1+ #! /usr/bin/env bash
2+ set -euo pipefail
3+ APP=feature-installer
4+
5+ RED=' \033[0;31m'
6+ GREEN=' \033[0;32m'
7+ YELLOW=' \033[1;33m'
8+ ORANGE=' \033[38;2;255;140;0m'
9+ NC=' \033[0m' # No Color
10+
11+ requested_version=${VERSION:- }
12+
13+ os=$( uname -s | tr ' [:upper:]' ' [:upper:]' )
14+ if [[ " $os " == " darwin" ]]; then
15+ os=" darwin"
16+ fi
17+ arch=$( uname -m)
18+
19+ if [[ " $arch " == " aarch64" ]]; then
20+ arch=" arm64"
21+ elif [[ " $arch " == " x86_64" ]]; then
22+ arch=" x86_64"
23+ fi
24+
25+ filename=" ${APP} _${os} _${arch} .tar.gz"
26+
27+ echo $filename
28+
29+
30+ case " $filename " in
31+ * " _Linux_" * )
32+ [[ " $arch " == " x86_64" || " $arch " == " arm64" ]] || exit 1
33+ ;;
34+ * )
35+ echo -e " ${RED} Unsupported OS/Arch: $os /$arch ${NC} "
36+ exit 1
37+ ;;
38+ esac
39+
40+ INSTALL_DIR=$HOME /.feature-installer/bin
41+ mkdir -p " $INSTALL_DIR "
42+
43+ if [ -z " $requested_version " ]; then
44+ url=" https://github.com/devcontainer-community/feature-installer/releases/latest/download/$filename "
45+ specific_version=$( curl -s https://api.github.com/repos/devcontainer-community/feature-installer/releases/latest | awk -F' "' ' /"tag_name": "/ {gsub(/^v/, "", $4); print $4}' )
46+
47+ if [[ $? -ne 0 || -z " $specific_version " ]]; then
48+ echo -e " ${RED} Failed to fetch version information${NC} "
49+ exit 1
50+ fi
51+ else
52+ url=" https://github.com/devcontainer-community/feature-installer/releases/download/v${requested_version} /$filename "
53+ specific_version=$requested_version
54+ fi
55+
56+ print_message () {
57+ local level=$1
58+ local message=$2
59+ local color=" "
60+
61+ case $level in
62+ info) color=" ${GREEN} " ;;
63+ warning) color=" ${YELLOW} " ;;
64+ error) color=" ${RED} " ;;
65+ esac
66+
67+ echo -e " ${color}${message}${NC} "
68+ }
69+
70+ check_version () {
71+ if command -v feature-installer > /dev/null 2>&1 ; then
72+ feature_installer_path=$( which feature-installer)
73+
74+
75+ # # TODO: check if version is installed
76+ # installed_version=$(feature-installer version)
77+ installed_version=" 0.0.1"
78+ installed_version=$( echo $installed_version | awk ' {print $2}' )
79+
80+ if [[ " $installed_version " != " $specific_version " ]]; then
81+ print_message info " Installed version: ${YELLOW} $installed_version ."
82+ else
83+ print_message info " Version ${YELLOW} $specific_version ${GREEN} already installed"
84+ exit 0
85+ fi
86+ fi
87+ }
88+
89+ download_and_install () {
90+ print_message info " Downloading ${ORANGE} feature-installer ${GREEN} version: ${YELLOW} $specific_version ${GREEN} ..."
91+ mkdir -p featureinstallertmp && cd featureinstallertmp
92+ curl -# -L -o " $filename " " $url "
93+ tar -xzf " $filename "
94+ mv feature-installer " $INSTALL_DIR "
95+ cd .. && rm -rf featureinstallertmp
96+ }
97+
98+ check_version
99+ download_and_install
100+
101+
102+ add_to_path () {
103+ local config_file=$1
104+ local command=$2
105+
106+ if grep -Fxq " $command " " $config_file " ; then
107+ print_message info " Command already exists in $config_file , skipping write."
108+ elif [[ -w $config_file ]]; then
109+ echo -e " \n# feature-installer" >> " $config_file "
110+ echo " $command " >> " $config_file "
111+ print_message info " Successfully added ${ORANGE} feature-installer ${GREEN} to \$ PATH in $config_file "
112+ else
113+ print_message warning " Manually add the directory to $config_file (or similar):"
114+ print_message info " $command "
115+ fi
116+ }
117+
118+ XDG_CONFIG_HOME=${XDG_CONFIG_HOME:- $HOME / .config}
119+
120+ current_shell=$( basename " $SHELL " )
121+ case $current_shell in
122+ fish)
123+ config_files=" $HOME /.config/fish/config.fish"
124+ ;;
125+ zsh)
126+ config_files=" $HOME /.zshrc $HOME /.zshenv $XDG_CONFIG_HOME /zsh/.zshrc $XDG_CONFIG_HOME /zsh/.zshenv"
127+ ;;
128+ bash)
129+ config_files=" $HOME /.bashrc $HOME /.bash_profile $HOME /.profile $XDG_CONFIG_HOME /bash/.bashrc $XDG_CONFIG_HOME /bash/.bash_profile"
130+ ;;
131+ ash)
132+ config_files=" $HOME /.ashrc $HOME /.profile /etc/profile"
133+ ;;
134+ sh)
135+ config_files=" $HOME /.ashrc $HOME /.profile /etc/profile"
136+ ;;
137+ * )
138+ # Default case if none of the above matches
139+ config_files=" $HOME /.bashrc $HOME /.bash_profile $XDG_CONFIG_HOME /bash/.bashrc $XDG_CONFIG_HOME /bash/.bash_profile"
140+ ;;
141+ esac
142+
143+ config_file=" "
144+ for file in $config_files ; do
145+ if [[ -f $file ]]; then
146+ config_file=$file
147+ break
148+ fi
149+ done
150+
151+ if [[ -z $config_file ]]; then
152+ print_message error " No config file found for $current_shell . Checked files: ${config_files[@]} "
153+ exit 1
154+ fi
155+
156+ if [[ " :$PATH :" != * " :$INSTALL_DIR :" * ]]; then
157+ case $current_shell in
158+ fish)
159+ add_to_path " $config_file " " fish_add_path $INSTALL_DIR "
160+ ;;
161+ zsh)
162+ add_to_path " $config_file " " export PATH=$INSTALL_DIR :\$ PATH"
163+ ;;
164+ bash)
165+ add_to_path " $config_file " " export PATH=$INSTALL_DIR :\$ PATH"
166+ ;;
167+ ash)
168+ add_to_path " $config_file " " export PATH=$INSTALL_DIR :\$ PATH"
169+ ;;
170+ sh)
171+ add_to_path " $config_file " " export PATH=$INSTALL_DIR :\$ PATH"
172+ ;;
173+ * )
174+ export PATH=$INSTALL_DIR :$PATH
175+ print_message warning " Manually add the directory to $config_file (or similar):"
176+ print_message info " export PATH=$INSTALL_DIR :\$ PATH"
177+ ;;
178+ esac
179+ fi
180+
181+ if [ -n " ${GITHUB_ACTIONS-} " ] && [ " ${GITHUB_ACTIONS} " == " true" ]; then
182+ echo " $INSTALL_DIR " >> $GITHUB_PATH
183+ print_message info " Added $INSTALL_DIR to \$ GITHUB_PATH"
184+ fi
0 commit comments