-
Notifications
You must be signed in to change notification settings - Fork 3
/
install-neovim.sh
executable file
·287 lines (244 loc) · 10.3 KB
/
install-neovim.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash
# Ensure DEBUG is set to 0 if unset or null
: "${DEBUG:=0}"
[[ $DEBUG == 1 ]] && echo "DEBUG mode on with strict -euo pipefail error handling" && set -euo pipefail
SCRIPT_PATH=$(realpath "$0")
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
# Change to the script directory
cd "$SCRIPT_DIR" || { echo "Failed to change directory to $SCRIPT_DIR" >&2; exit 1; }
echo "Working directory: $(pwd)"
# Define icons for better readability
INFO="ℹ️"
SUCCESS="✅"
WARNING="⚠️"
ERROR="❌"
TOOLS="🛠️"
REMOVE="🗑️"
REVOCATION="🔒"
printf "${SUCCESS} Successfully called %s\n" "$(basename "$0")"
# Source the functions file
source "./functions.sh"
# Initialize environment variables
func_initialize_env_vars
echo "${INFO} DARWIN: $DARWIN"
echo "${INFO} HOMEBREW_PATH: $HOMEBREW_PATH"
echo "${INFO} USERNAME: $USERNAME"
echo "${INFO} USERGROUP: $USERGROUP"
echo "${INFO} ROOTGROUP: $ROOTGROUP"
# Assign the first argument to temp_file, default to empty if not provided
temp_file=${1:-}
# Determine if the script is called by the main script or run directly
if [[ -n "$temp_file" && -f "$temp_file" ]]; then
echo "${INFO} install-neovim.sh has been called by the main script with temp file: $temp_file"
CALLED_BY_MAIN=1
else
echo "${INFO} Script is being run directly by the user."
CALLED_BY_MAIN=0
fi
# Main logic of install-neovim.sh
echo "${TOOLS} Running Neovim configuration tasks..."
# Validate and read YAML from the temporary file or config.yaml
if [[ "$CALLED_BY_MAIN" -eq 1 ]]; then
if ! CONFIG_YAML=$(<"$temp_file"); then
printf "${ERROR} Failed to read YAML file at %s\n" "$temp_file" >&2
exit 1
fi
echo "${INFO} Tempfile parsed correctly: $temp_file"
else
CONFIG_YAML_PATH="./config.yaml"
# Ensure config.yaml exists
if [[ ! -f "$CONFIG_YAML_PATH" ]]; then
printf "${ERROR} config.yaml not found in this directory\n" >&2
exit 1
fi
if ! CONFIG_YAML=$(<"$CONFIG_YAML_PATH"); then
printf "${ERROR} Failed to read YAML file at %s\n" "$CONFIG_YAML_PATH" >&2
exit 1
fi
echo -e "-----------------------------------------------------------------\n"
read -p "Would you like to check and install Neovim dependencies? (y/n): " answer
if [[ "$answer" == "y" ]]; then
CONFIG_YAML=$(printf '%s\n' "$CONFIG_YAML" | yq e '.packages.neovim.action = "install"')
else
CONFIG_YAML=$(printf '%s\n' "$CONFIG_YAML" | yq e '.packages.neovim.action = "skip"')
fi
read -p "Would you like to check and install kickstart.nvim? (y/n): " answer
if [[ "$answer" == "y" ]]; then
CONFIG_YAML=$(printf '%s\n' "$CONFIG_YAML" | yq e '.plugins."kickstart.nvim".action = "install"')
else
CONFIG_YAML=$(printf '%s\n' "$CONFIG_YAML" | yq e '.plugins."kickstart.nvim".action = "skip"')
fi
func_sudoers
fi
# Function to update the action status in the YAML
update_action_status() {
local plugin="$1"
CONFIG_YAML=$(printf '%s\n' "$CONFIG_YAML" | yq e ".plugins[\"$plugin\"].action = \"handled\"" -)
}
# Install kickstart.nvim based on the action in config.yaml
kickstart_action=$(printf '%s\n' "$CONFIG_YAML" | yq eval -r '.plugins."kickstart.nvim".action')
if [[ "$kickstart_action" == "install" ]]; then
kickstart_dir=$(printf '%s\n' "$CONFIG_YAML" | yq eval -r '.plugins."kickstart.nvim".directory')
# Expand variables in the directory path
eval kickstart_dir="$kickstart_dir"
if [[ ! -d "$kickstart_dir" ]]; then
echo "${TOOLS} Installing kickstart.nvim..."
git clone "$(printf '%s\n' "$CONFIG_YAML" | yq eval -r '.plugins."kickstart.nvim".url')" "$kickstart_dir"
update_action_status "kickstart.nvim"
else
echo "${SUCCESS} kickstart.nvim is already installed."
update_action_status "kickstart.nvim"
fi
elif [[ "$kickstart_action" == "uninstall" ]]; then
kickstart_dir=$(printf '%s\n' "$CONFIG_YAML" | yq eval -r '.plugins."kickstart.nvim".directory')
eval kickstart_dir="$kickstart_dir"
if [[ -d "$kickstart_dir" ]]; then
echo "${REMOVE} Uninstalling kickstart.nvim..."
rm -rf "$kickstart_dir"
update_action_status "kickstart.nvim"
else
echo "${INFO} kickstart.nvim is not installed. Nothing to uninstall."
update_action_status "kickstart.nvim"
fi
elif [[ "$kickstart_action" == "skip" ]]; then
echo "${INFO} Skipping kickstart.nvim as per config.yaml."
else
echo "${WARNING} Invalid action for kickstart.nvim: '$kickstart_action'. Skipping."
fi
# Install Neovim dependencies based on the action in config.yaml
neovim_action=$(printf '%s\n' "$CONFIG_YAML" | yq eval -r '.packages.neovim.action')
if [[ "$neovim_action" == "install" ]]; then
echo "${TOOLS} Installing Neovim dependencies..."
if [[ $DARWIN == 0 ]]; then
brew install --quiet neovim 2> /dev/null || true
else
brew install --quiet neovim 2> /dev/null || true
fi
echo "${SUCCESS} Neovim dependencies installed."
elif [[ "$neovim_action" == "uninstall" ]]; then
echo "${REMOVE} Uninstalling Neovim and its dependencies..."
brew uninstall --quiet neovim 2> /dev/null || true
echo "${SUCCESS} Neovim dependencies uninstalled."
elif [[ "$neovim_action" == "skip" ]]; then
echo "${INFO} Skipping Neovim dependencies as per config.yaml."
else
echo "${WARNING} Invalid action for Neovim: '$neovim_action'. Skipping."
fi
# Additional configuration and logic
# Configure OSC52 clipboard support if Neovim is installed
if [[ "$neovim_action" == "install" ]]; then
config_files=$(find -L ~/.config -type f -exec grep -l 'unnamedplus' {} +)
echo "----------------------------------------"
echo "FOUND files with 'unnamedplus':"
printf "%s\n" "$config_files"
echo "----------------------------------------"
# Use a heredoc to store the code block in a variable which is used to activate clipboard over SSH
code_to_add=$(cat <<'EOF'
-- Added by Synology-Homebrew OSC52
vim.g.clipboard = {
name = 'OSC52',
copy = {
['+'] = require('vim.ui.clipboard.osc52').copy('+'),
['*'] = require('vim.ui.clipboard.osc52').copy('*'),
},
paste = {
['+'] = require('vim.ui.clipboard.osc52').paste('+'),
['*'] = require('vim.ui.clipboard.osc52').paste('*'),
},
}
EOF
)
# Check if any files are found
if [[ -n "$config_files" ]]; then
echo "Processing files:"
while IFS= read -r config_file; do
echo "Checking: $config_file"
if ! grep -q "Added by Synology-Homebrew OSC52" "$config_file"; then
echo "Adding OSC52 code to $config_file"
if func_sed "/unnamedplus/ r /dev/stdin" "$config_file" <<<"$code_to_add"; then
echo "OSC52 code successfully added to $config_file"
else
echo "Error: Failed to apply sed to $config_file" >&2
continue
fi
else
echo "OSC52 code already exists in $config_file"
fi
done <<< "$config_files"
else
echo "No file containing 'unnamedplus' found in ~/.config folder."
fi
# Install additional packages for Neovim
echo "----------------------------------------"
if [[ -n "$CONFIG_YAML" ]]; then
if [[ "$neovim_action" == "install" ]]; then
echo "Installing additional Neovim components..."
# Install or upgrade pynvim
if ! pip3 show pynvim &> /dev/null; then
echo "pynvim is not installed. Installing pynvim..."
pip3 install pynvim --break-system-packages
else
echo "pynvim is already installed. Upgrading pynvim..."
pip3 install --upgrade pynvim --break-system-packages
fi
# Upgrade pip
python3 -m pip install --upgrade pip --break-system-packages
echo "npm check:"
# Check if npm is installed, if not, install it
if ! brew list npm &> /dev/null; then
echo "npm is not installed. Installing npm..."
brew install --quiet npm
fi
# Ensure icu4c is installed
if ! brew list icu4c &>/dev/null; then
echo "icu4c is not installed. Installing icu4c..."
brew install icu4c
else
echo "icu4c is already installed."
fi
# Check if icu4c is already linked
if ! brew list --versions icu4c &>/dev/null; then
echo "Linking icu4c libraries..."
brew link --force icu4c
else
echo "icu4c is already linked."
fi
# Remove existing global node_modules directory
sudo rm -rf "$HOMEBREW_PATH/lib/node_modules"
# Run the postinstall step for Node.js
brew postinstall node
# Disable npm funding messages globally
sudo npm config set fund false --location=global
# Install neovim globally using npm
echo "installing neovim with npm."
[[ ! -d ~/.npm ]] && mkdir ~/.npm
sudo chown -R $USERNAME:$USERGROUP ~/.npm
sudo npm install -g neovim@latest
echo "checking neovim gem..."
if ! gem list -i neovim; then
gem install neovim --no-document
fi
# Check if a compatible version of Bundler is installed
echo "checking gem bundler..."
if ! gem list -i bundler; then
gem install bundler -v '< 2.5' --no-document
fi
# Clone fzf-git.sh into scripts directory for fzf git keybindings. This will be sourced in .profile
echo "Cloning fzf-git.sh into ~/.scripts directory"
mkdir -p ~/.scripts && curl -o ~/.scripts/fzf-git.sh https://raw.githubusercontent.com/junegunn/fzf-git.sh/main/fzf-git.sh
else
echo "SKIPPING: Neovim components as config.yaml action is not set to 'install'."
fi
else
echo "SKIPPING: Neovim components installation. This is expected when running this script independently."
fi
fi
# Write updated YAML back to the temporary file
[[ -n $temp_file ]] && printf '%s\n' "$CONFIG_YAML" > "$temp_file"
# Perform cleanup only if run directly
if [[ "$CALLED_BY_MAIN" -eq 0 ]]; then
echo "${REMOVE} Performing cleanup since script was run directly."
func_cleanup_exit 0
else
echo "${INFO} Skipping cleanup because the main script is managing it."
fi