Skip to content

Instantly share code, notes, and snippets.

@warpfork
Created March 10, 2015 23:19
Show Gist options
  • Save warpfork/27db671839a21f5e936c to your computer and use it in GitHub Desktop.
Save warpfork/27db671839a21f5e936c to your computer and use it in GitHub Desktop.
a wrapper script for teaching liteide how to workspaces
#!/bin/bash
set -euo pipefail
IFS=$'\0'
#
# config
# (you'll want to fix these to your environment, or change the project auto-detect to fit.)
#
litespace="~/liteideSpace/"
sourcespace="~/repos/"
#
# script begins
#
function recognizeProject {
searchDir="${1:-}"
[[ "$searchDir" = "$sourcespace"* ]] && echo "${searchDir/#"$sourcespace"/}"
}
function recognizeGopath {
searchDir="${1:-}"
(cd "$searchDir"
if [ -d ".gopath" ]; then echo ".gopath";
elif [ -d "Godeps/_workspace/" ]; then echo "Godeps/_workspace/";
fi
)
}
# if a project specified, open that;
# if in a project dir, open that;
# otherwise just open plain liteide.
project="${1:-}"
[ -z "$project" ] && {
project="$(recognizeProject "$PWD")"
projectDir="$PWD"
} || {
projectDir="$sourcespace/$project"
[ ! -d "$projectDir" ] && {
printf "no dir \"%q\"\n" "$projectDir" 2>&1
exit 4
}
}
[ -z "$project" ] && {
liteide
exit
}
printf "launching project \"%q\"...\n" "$project" 2>&1
gopath="$(recognizeGopath "$projectDir")"
[ -z "$gopath" ] && {
echo "no gopath recognized" 2>&1
exit 5
}
# okay, now really move.
projConf="${project//\//-}"
fakeHome="$litespace/$projConf"
mkdir -p "$fakeHome/.config/liteide/"
iniFile="$fakeHome/.config/liteide/liteide.ini"
# if the file doesn't exist yet, try to start with the one in the homedir.
# if it does, we'll just rewrite it; don't want to through away open files and such state.
[ -f "$iniFile" ] || cp "$HOME/.config/liteide/liteide.ini" "$iniFile" || touch "$iniFile"
# this rewrite is a hair tricky since the line of interest may not exist yet
# first we get rid of the line of interest if it does
# then we append the section header if it doesn't exist yet
# then we put the desired value right after the section header
sed -i '/^gopath=/d' "$iniFile"
grep '^\[liteide\]$' "$iniFile" >/dev/null || echo -e "\n[liteide]" >> "$iniFile"
sed -i 's/^\[liteide\]$/\[liteide\]\n'"gopath=${projectDir//\//\\/}\/${gopath//\//\\/}/" "$iniFile"
# yes, yes I did just template inputs to sed with bash. WHAT COULD GO RONG
HOME="$fakeHome" liteide
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment