/
core.sh
167 lines (130 loc) · 3.7 KB
/
core.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
#!/bin/bash
PROJECTDIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
DATETIME=$(date +%Y%m%d%H%M%S)
function update_apt {
enable_apt_universe_repository
if [[ -v SKIP_APT_UPDATE && "$SKIP_APT_UPDATE" = true ]]; then
print_warning "Skipping updating APT"
return 0
fi
print_info "Updating APT repositories ..."
sudo apt update
}
function enable_apt_universe_repository {
local -r VERSION=$(lsb_release -a 2>/dev/null | fgrep Codename | cut -f2)
apt-cache policy | fgrep "$VERSION/universe" &> /dev/null
if [ $? -ne 0 ]; then
print_info "Enabling APT universe repository ..."
sudo add-apt-repository universe
fi
}
function __check_app_installed {
local -r APP=$1
local -r CHECK=${2:-$1}
which $CHECK &> /dev/null
if [ $? -ne 0 ]; then
print_info "App NOT found: ${APP}"
return 1
fi
print_info "App already installed: ${APP}"
return 0
}
function install_app {
__check_app_installed "$@" && return 0
__install_app "$@"
}
function install_app_with_confirmation {
__check_app_installed "$@" && return 0
ask_for_installation "$@" && __install_app "$@"
}
function __install_app {
local -r APP=$1
local -r TOOL_SCRIPT=${PROJECTDIR}/tools/${APP}.sh
if [ -f "$TOOL_SCRIPT" ]; then
print_info "Installing manual app: $APP ..."
. ${TOOL_SCRIPT}
else
print_info "Installing APT app: $APP ..."
sudo apt install -y $APP
fi
}
function create_symlink {
local -r SOURCE="${PROJECTDIR}/symlinks/${1}"
local -r DESTINATION="${HOME}/${2:-$1}"
if [ -h ${DESTINATION} ]; then
print_info "Removing existing symlink: ${DESTINATION}"
rm ${DESTINATION}
elif [ -f "${DESTINATION}" ]; then
print_info "Backing up existing file: ${DESTINATION}"
mv ${DESTINATION}{,.${DATETIME}}
elif [ -d "${DESTINATION}" ]; then
print_info "Backing up existing directory: ${DESTINATION}"
mv ${DESTINATION}{,.${DATETIME}}
fi
print_info "Creating new symlink: ${DESTINATION}"
ln -s ${SOURCE} ${DESTINATION}
}
function ask_for_installation {
local -r APP=$1
__ask_for_confirmation "Do you want to install ${APP} ?" "Skipping installing: ${APP}"
}
function ask_for_configuration {
local -r TOOL=$1
__ask_for_confirmation "Do you want to configure ${TOOL} ?" "Skipping configuring: ${TOOL}"
}
function __ask_for_confirmation {
local -r QUESTION_MESSAGE=$1
local -r SKIPPING_MESSAGE=$2
if [[ -v SKIP_CONFIRMATION && "$SKIP_CONFIRMATION" = true ]]; then
return 0
fi
print_question "${QUESTION_MESSAGE} (y/N)"
read
if [[ ! "$REPLY" =~ ^[Yy]$ ]]; then
print_warning "${SKIPPING_MESSAGE}"
return 1
fi
return 0
}
function __print_in_color {
printf "%b" "$(tput setaf $2)" "$1" "$(tput sgr0)"
}
function print_error {
__print_in_color "$1\n" 1
}
function print_warning {
__print_in_color "$1\n" 3
}
function print_info {
__print_in_color "$1\n" 2
}
function print_question {
__print_in_color "$1 " 3
}
function install_npm_package {
__check_npm_package_installed "$@" && return 0
__install_npm_package "$@"
}
function install_npm_package_with_confirmation {
__check_npm_package_installed "$@" && return 0
ask_for_installation "$@" && __install_npm_package "$@"
}
function __check_npm_package_installed {
local -r PACKAGE=$1
if [[ ! $(which npm) ]]; then
print_warning "Package ${PACKAGE} can NOT be installed because npm is missing"
return 0
fi
if [[ $(npm list -g --depth=0 | fgrep " ${PACKAGE}@") ]]; then
print_info "Package already installed: ${PACKAGE}"
return 0
fi
print_info "Package NOT found: ${PACKAGE}"
return 1
}
function __install_npm_package {
local -r PACKAGE=$1
local -r ASK=${2:-"true"}
print_info "Installing package: ${PACKAGE} ..."
sudo npm install -g ${PACKAGE}
}