Add various utility functions
This commit is contained in:
@@ -1,10 +1,71 @@
|
|||||||
function root() {
|
function get_os() {
|
||||||
git rev-parse --show-toplevel
|
un=$(uname)
|
||||||
|
|
||||||
|
if [[ "$un" = "Linux" ]] ; then
|
||||||
|
echo -n "linux"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$un" = "Darwin" ]] ; then
|
||||||
|
echo -n "osx"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
wfroot() {
|
OPERATING_SYSTEM=$(get_os)
|
||||||
dir="$1"
|
|
||||||
cat "$dir/.root"
|
# For a given file, return its corresponding project root.
|
||||||
|
function project_root() {
|
||||||
|
pushd $(dirname "$1") 2>&1 > /dev/null
|
||||||
|
out=$(git rev-parse --show-toplevel)
|
||||||
|
ret="$?"
|
||||||
|
popd 2>&1 > /dev/null
|
||||||
|
|
||||||
|
if [[ "$ret" != "0" ]] ; then
|
||||||
|
echo -n ""
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -n "$out"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# When run from a dir with would-format configured, return
|
||||||
|
# would-format's own directory
|
||||||
|
wf_root() {
|
||||||
|
arg=$(pwd)/bin/would-reformat.sh
|
||||||
|
|
||||||
|
pr=$(project_root $arg)
|
||||||
|
|
||||||
|
link="$pr/bin/would-reformat.sh"
|
||||||
|
|
||||||
|
original0=$(resolve_symlink "$link")
|
||||||
|
original=$(normalize_dir_of_file $original0)
|
||||||
|
|
||||||
|
out="$original"
|
||||||
|
|
||||||
|
echo "$out"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# FIXME: Make this bail if it fails
|
||||||
|
function normalize_dir_of_file() {
|
||||||
|
out0=$(readlink -m "$1")
|
||||||
|
|
||||||
|
out=$(dirname "$out0")
|
||||||
|
|
||||||
|
echo -n "$out"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolve_symlink() {
|
||||||
|
if [[ "$OPERATING_SYSTEM" = "osx" ]] ; then
|
||||||
|
out=$(readlink "$1")
|
||||||
|
else
|
||||||
|
out=$(readlink -f "$1")
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "$out"
|
||||||
}
|
}
|
||||||
|
|
||||||
function custom_formatter() {
|
function custom_formatter() {
|
||||||
|
|||||||
Reference in New Issue
Block a user