Skip to content

Instantly share code, notes, and snippets.

@deconstructionalism
Created November 6, 2018 20:28
Show Gist options
  • Save deconstructionalism/c8d36bef9f93a006a25fc2aa63f3b048 to your computer and use it in GitHub Desktop.
Save deconstructionalism/c8d36bef9f93a006a25fc2aa63f3b048 to your computer and use it in GitHub Desktop.
#!/bin/sh
print_header () {
echo $1
echo "------------------------------------"
}
run_print_error () {
print_header "$2"
echo $1
# error_msg=$($1 2>&1 >/dev/null)
if error_msg=$($1 2>&1 >/dev/null); then
echo "SUCCESS!\n\n"
else
echo "\nERROR!\n$error_msg\n"
exit 1
fi
}
# install gem packages
run_print_error "gem install rubocop solargraph" "Installing Global Rubocop, Solargraph"
# install vscode extensions
run_print_error "code --install-extension misogi.ruby-rubocop" "Installing VSCode extension ruby-rubocop"
run_print_error "code --install-extension castwide.solargraph" "Installing VSCode extension ruby-solargraph"
if grep -q Microsoft /proc/version 1>/dev/null 2>/dev/null; then
# -------------------- #
# W I N D O W S 1 0 #
# -------------------- #
# -------------------------------------------- #
# BATCH SCRIPTS TO BIND WSL BINARIES TO VSCODE #
# -------------------------------------------- #
# VSCode will use binaries installed in WSL to run linting and other
# functionality. In order for VSCode, a Windows 10 program to use WSL
# binaries, we need simple batch scripts to allow calling WSL binaries
# from VSCode.
print_header "Generating BATCH files to bind WSL Binaries for rubocop, solargraph"
mkdir -p /mnt/c/vscode_helpers
# WSL rubocop batch file
cat <<EOF > /mnt/c/vscode_helpers/rubocop.bat
@echo off
bash.exe -c "$(rbenv which rubocop) %*"
@echo on
EOF
# WSL solargraph batch file
cat <<EOF > /mnt/c/vscode_helpers/solargraph.bat
@echo off
bash.exe -c "$(rbenv which solargraph) %*"
@echo on
EOF
echo "SUCCESS!\n\n"
# ----------------------------------- #
# SETTINGS TO ADD TO VSCODE (WINDOWS) #
# ----------------------------------- #
print_header "Generating new VSCode settings file"
cat <<EOF > settings.json
{
"editor.formatOnPaste": true,
"editor.formatOnType": true,
"files.autoSave": "afterDelay",
"git.ignoreMissingGitWarning": true,
"solargraph.commandPath": "C:\\\vscode_helpers\\\solargraph.bat",
"ruby.rubocop.executePath": "C:\\\vscode_helpers\\\",
"terminal.integrated.shell.windows": "C:\\\Windows\\\Sysnative\\\bash.exe",
"files.eol": "\n",
"files.associations": {
"*.pgsql": "sql",
"*.psql": "sql"
}
}
EOF
echo "SUCCESS!\n\n"
# WINDOWS settings.json path
settings_json="`readlink -f ~/winhome`/AppData/Roaming/Code/User/settings.json"
# install jq to allow JSON file merge
run_print_error "sudo apt-get install jq" "Installing jq JSON tool"
else
# ---------------------------- #
# M A C O S / L I N U X #
# ---------------------------- #
# ------------------------------------- #
# SETTINGS TO ADD TO VSCODE (MAC/LINUX) #
# ------------------------------------- #
rubocop_path=`rbenv which rubocop`
cat <<EOF > settings.json
{
"editor.formatOnPaste": true,
"editor.formatOnType": true,
"files.autoSave": "afterDelay",
"ruby.rubocop.executePath": "${rubocop_path%rubocop}",
"solargraph.commandPath": "$(rbenv which solargraph)",
"files.associations": {
"*.pgsql": "sql",
"*.psql": "sql"
}
}
EOF
if [[ $(uname -s) = 'Darwin' ]]; then
# install jq to allow JSON file merge
run_print_error "brew install jq" "Installing jq JSON tool"
# MAC settings.json path
settings_json="$HOME/Library/Application Support/Code/User/settings.json"
else
# install jq to allow JSON file merge
run_print_error "sudo apt-get install jq" "Installing jq JSON tool"
# LINUX settings.json path
settings_json="$HOME/.config/Code/User/settings.json"
fi
fi
# merge settings.json into VSCode settings
merge_settings () {
jq -s '.[0] * .[1]' "$settings_json" settings.json > temp.json
}
write_settings () {
cat temp.json > "$settings_json"
}
clean_up () {
rm temp.json ; rm settings.json
}
run_print_error "merge_settings" "Merging settings into VSCode settings.json"
run_print_error "write_settings" "Writing to settings.json"
run_print_error "clean_up" "Cleaning up temporary files"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment