Add various utility functions

This commit is contained in:
Michael Wolf
2023-09-30 14:54:11 -06:00
parent d4f5fbdaf8
commit 952c7eeb90

View File

@@ -1,10 +1,71 @@
function root() {
git rev-parse --show-toplevel
function get_os() {
un=$(uname)
if [[ "$un" = "Linux" ]] ; then
echo -n "linux"
return 0
fi
if [[ "$un" = "Darwin" ]] ; then
echo -n "osx"
return 0
fi
}
wfroot() {
dir="$1"
cat "$dir/.root"
OPERATING_SYSTEM=$(get_os)
# 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() {