-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
executable file
·246 lines (200 loc) · 6.84 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/bash
# Installation commands:
# Basic:
# curl -sfLS https://raw.githubusercontent.com/ZachiNachshon/git-deps-syncer/master/install.sh | bash -
# Options:
# curl -sfLS https://raw.githubusercontent.com/ZachiNachshon/git-deps-syncer/master/install.sh | DRY_RUN=True VERSION=0.3.0 bash -
# DRY_RUN=True LOCAL_ARCHIVE_FILEPATH=/Users/zachin/codebase/github/git-deps-syncer/git-deps-syncer.tar.gz ./install.sh
# When releasing a new version, the install script must be updated as well to latest
VERSION=${VERSION="0.8.0"}
# Run the install script in dry-run mode, no file system changes
DRY_RUN=${DRY_RUN=""}
# Use to install from a local git-deps-syncer archive instead of downloading from remote
LOCAL_ARCHIVE_FILEPATH=${LOCAL_ARCHIVE_FILEPATH=""}
GIT_DEPS_SYNCER_REPOSITORY_URL="https://github.com/ZachiNachshon/git-deps-syncer"
GIT_DEPS_SYNCER_ARCHIVE_NAME="git-deps-syncer.tar.gz"
GIT_DEPS_SYNCER_INSTALL_PATH="${HOME}/.config/git-deps-syncer"
GIT_DEPS_SYNCER_EXECUTABLE_NAME=git-deps-syncer
DARWIN_BIN_DIR="$HOME/.local/bin"
LINUX_BIN_DIR="$HOME/.local/bin"
COLOR_RED='\033[0;31m'
COLOR_GREEN='\033[0;32m'
COLOR_YELLOW="\033[0;33m"
COLOR_NONE='\033[0m'
is_dry_run() {
[[ -n ${DRY_RUN} ]]
}
exit_on_error() {
exit_code=$1
message=$2
if [ $exit_code -ne 0 ]; then
# >&2 echo "\"${message}\" command failed with exit code ${exit_code}."
# >&2 echo "\"${message}\""
exit $exit_code
fi
}
new_line() {
echo -e "" >&2
}
_log_base() {
prefix=$1
shift
echo -e "${prefix}$*" >&2
}
log_info() {
local info_level_txt="INFO"
if is_dry_run; then
info_level_txt+=" (Dry Run)"
fi
_log_base "${COLOR_GREEN}${info_level_txt}${COLOR_NONE}: " "$@"
}
log_warning() {
local warn_level_txt="WARNING"
if is_dry_run; then
warn_level_txt+=" (Dry Run)"
fi
_log_base "${COLOR_YELLOW}${warn_level_txt}${COLOR_NONE}: " "$@"
}
log_error() {
local error_level_txt="ERROR"
if is_dry_run; then
error_level_txt+=" (Dry Run)"
fi
_log_base "${COLOR_RED}${error_level_txt}${COLOR_NONE}: " "$@"
}
log_fatal() {
local fatal_level_txt="ERROR"
if is_dry_run; then
fatal_level_txt+=" (Dry Run)"
fi
_log_base "${COLOR_RED}${fatal_level_txt}${COLOR_NONE}: " "$@"
message="$@"
exit_on_error 1 "${message}"
}
cmd_run() {
local cmd_string=$1
if ! is_dry_run; then
eval "${cmd_string}"
else
echo """
${cmd_string}
"""
fi
}
is_install_from_local_archive() {
[[ -n "${LOCAL_ARCHIVE_FILEPATH}" ]]
}
is_symlink() {
local abs_path=$1
[[ -L "${abs_path}" ]]
}
is_file_exist() {
local path=$1
[[ -f "${path}" || $(is_symlink "${path}") ]]
}
is_directory_exist() {
local path=$1
[[ -d "${path}" ]]
}
get_git_deps_syncer_archive_filepath() {
echo "${GIT_DEPS_SYNCER_INSTALL_PATH}/${GIT_DEPS_SYNCER_ARCHIVE_NAME}"
}
read_os_type() {
if [[ "${OSTYPE}" == "linux"* ]]; then
echo "linux"
elif [[ "${OSTYPE}" == "darwin"* ]]; then
echo "darwin"
else
log_fatal "OS type is not supported. os: ${OSTYPE}"
fi
}
calculate_git_deps_syncer_exec_symlink_path() {
local os_type=$(read_os_type)
if [[ "${os_type}" == "linux" ]]; then
# $HOME/.local/bin/git-deps-syncer
echo "${LINUX_BIN_DIR}/${GIT_DEPS_SYNCER_EXECUTABLE_NAME}"
elif [[ "${os_type}" == "darwin" ]]; then
# $HOME/.local/bin/git-deps-syncer
echo "${DARWIN_BIN_DIR}/${GIT_DEPS_SYNCER_EXECUTABLE_NAME}"
else
echo ""
fi
}
adjust_global_executable_symlink() {
local git_deps_syncer_exec_bin_path=$1
local git_deps_syncer_script_path="${GIT_DEPS_SYNCER_INSTALL_PATH}/git-deps-syncer.sh"
log_info "Elevating executable permission. path: ${git_deps_syncer_script_path}"
cmd_run "chmod +x ${git_deps_syncer_script_path}"
# macOS: ~/.local/bin/git-deps-syncer --> ~/.config/git-deps-syncer/git-deps-syncer.sh
# Linux: ~/.local/bin/git-deps-syncer --> ~/.config/git-deps-syncer/git-deps-syncer.sh
log_info "Linking executable to bin directory. symlink: ${git_deps_syncer_exec_bin_path}"
cmd_run "ln -sf ${git_deps_syncer_script_path} ${git_deps_syncer_exec_bin_path}"
}
copy_local_archive_to_config_path() {
if ! is_file_exist "${LOCAL_ARCHIVE_FILEPATH}"; then
log_fatal "Local archive file does not exist. path: ${LOCAL_ARCHIVE_FILEPATH}"
fi
clear_previous_installation
log_info "Installing git-deps-syncer CLI from local artifact. path: ${LOCAL_ARCHIVE_FILEPATH}"
log_info "Copying archive to config path"
local copy_path="${GIT_DEPS_SYNCER_INSTALL_PATH}"
cmd_run "rsync ${LOCAL_ARCHIVE_FILEPATH} ${copy_path}"
}
download_latest_archive_to_config_path() {
log_info "Installing git-deps-syncer CLI from GitHub releases"
local filename="${GIT_DEPS_SYNCER_ARCHIVE_NAME}"
local download_path="${GIT_DEPS_SYNCER_INSTALL_PATH}"
local url="${GIT_DEPS_SYNCER_REPOSITORY_URL}/releases/download/v${VERSION}/${filename}"
clear_previous_installation
cwd=$(pwd)
if is_directory_exist "${download_path}"; then
cd "${download_path}" || exit
fi
log_info "Downloading git-deps-syncer from GitHub releases. version: ${VERSION}"
cmd_run "curl --fail -s ${url} -L -o ${filename}"
cd "${cwd}" || exit
if ! is_dry_run && ! is_file_exist "${download_path}/${filename}"; then
log_fatal "Failed to download git-deps-syncer from GitHub releases. version: ${VERSION}"
fi
}
extract_git_deps_syncer_archive() {
# ${HOME}/.config/git-deps-syncer/git-deps-syncer.tar.gz
local archive_file_path=$(get_git_deps_syncer_archive_filepath)
# ${HOME}/.config/git-deps-syncer
local archive_folder_path="${GIT_DEPS_SYNCER_INSTALL_PATH}"
log_info "Extracting archive. destination: ${archive_folder_path}"
cmd_run "tar -xzf ${archive_file_path} -C ${archive_folder_path}"
if is_file_exist "${archive_file_path}"; then
log_info "Removing archive file"
cmd_run "rm -rf ${archive_file_path}"
fi
}
clear_previous_installation() {
log_info "Creating a fresh installation folder"
local git_deps_syncer_install_path="${GIT_DEPS_SYNCER_INSTALL_PATH}"
if is_directory_exist "${git_deps_syncer_install_path}"; then
cmd_run "rm -rf ${git_deps_syncer_install_path}"
fi
cmd_run "mkdir -p ${git_deps_syncer_install_path}"
}
main() {
local git_deps_syncer_exec_bin_path=$(calculate_git_deps_syncer_exec_symlink_path)
if is_install_from_local_archive; then
copy_local_archive_to_config_path
else
download_latest_archive_to_config_path
fi
extract_git_deps_syncer_archive
adjust_global_executable_symlink "${git_deps_syncer_exec_bin_path}"
new_line
log_warning """Manual installation requires a specific PATH to become available on shell session:
export PATH=\${HOME}/.local/bin:\${PATH}
"""
log_warning """To make the 'git-deps-syncer' command available, please choose one method:
• Run the above command
• Add the above command to the RC file (zshrc/bashrc/bash_profile) and open a new shell session
"""
log_info "Type 'git-deps-syncer' to print the help menu"
new_line
}
main "$@"