214 lines
4.3 KiB
Bash
214 lines
4.3 KiB
Bash
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
|
|
}
|
|
|
|
OPERATING_SYSTEM=$(get_os)
|
|
|
|
# For a given file, return its corresponding project root.
|
|
function project_root() {
|
|
set +u
|
|
if [[ ! -z "${PROJECT_ROOT}" ]]; then
|
|
echo "$PROJECT_ROOT"
|
|
return 0
|
|
fi
|
|
set -u
|
|
|
|
pushd "$(dirname "$1")" >/dev/null 2>&1 || exit 101
|
|
out=$(git rev-parse --show-toplevel)
|
|
|
|
ret="$?"
|
|
popd >/dev/null 2>&1 || exit 1
|
|
|
|
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() {
|
|
set +u
|
|
if [[ ! -z "${WF_ROOT}" ]]; then
|
|
echo "$WF_ROOT"
|
|
return 0
|
|
fi
|
|
set -u
|
|
|
|
arg=$(pwd)/bin/would-reformat.sh
|
|
PROJECT_ROOT=$(project_root "$arg")
|
|
|
|
pushd "$PROJECT_ROOT" 2>&1 >/dev/null
|
|
|
|
pr="$PROJECT_ROOT"
|
|
|
|
link="$pr/bin/would-reformat.sh"
|
|
|
|
original0=$(resolve_symlink "$link")
|
|
original=$(normalize_dir_of_file "$original0")
|
|
|
|
out="$original"
|
|
|
|
popd 2>&1 >/dev/null
|
|
|
|
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_sniff() {
|
|
file="$1"
|
|
|
|
pr=$(project_root "$file")
|
|
|
|
if [[ -f "$pr/.would-reformat/custom-sniffer" ]]; then
|
|
custom=$("$pr/.would-reformat/custom-sniffer" "$1")
|
|
ret="$?"
|
|
|
|
echo "$custom"
|
|
|
|
return 0
|
|
fi
|
|
}
|
|
|
|
## WOULD_FORMAT_PROJECT_ROOT=$(project_root $1)
|
|
|
|
# FIXME: This needs to be made customizable
|
|
function sniff_file_type() {
|
|
ff=$1
|
|
|
|
maybe_custom=$(custom_sniff "$ff")
|
|
if [[ "$maybe_custom" != "" ]]; then
|
|
echo "$maybe_custom"
|
|
return 0
|
|
fi
|
|
|
|
file_type=
|
|
|
|
if [[ $ff == *.py ]]; then
|
|
file_type="python"
|
|
elif [[ $ff == *.js ]]; then
|
|
file_type="javascript"
|
|
elif [[ $ff == *.ts ]]; then
|
|
file_type="typescript"
|
|
elif [[ $ff == *.vue ]]; then
|
|
file_type="vue"
|
|
elif [[ $ff == *.css ]]; then
|
|
file_type="css"
|
|
elif [[ $ff == *.php ]]; then
|
|
file_type="php"
|
|
elif [[ $ff == *.html ]]; then
|
|
file_type="html"
|
|
elif [[ $ff == *.jsx ]]; then
|
|
file_type="jsx"
|
|
elif [[ $ff == *.tsx ]]; then
|
|
file_type="tsx"
|
|
elif [[ $ff == *.css ]]; then
|
|
file_type="css"
|
|
elif [[ $ff == *.scss ]]; then
|
|
file_type="scss"
|
|
elif [[ $ff = *.sh ]]; then
|
|
file_type="sh"
|
|
elif [[ $ff = *.go ]]; then
|
|
file_type="golang"
|
|
elif [[ $ff = *.dart ]]; then
|
|
file_type="dart"
|
|
elif [[ $ff = *.pl ]]; then
|
|
file_type="perl"
|
|
elif [[ $ff = *.rs ]] ; then
|
|
file_type="rust"
|
|
fi
|
|
|
|
echo $file_type
|
|
}
|
|
|
|
function custom_wrapper() {
|
|
root="$1"
|
|
file_type="$2"
|
|
|
|
maybe="$root/.would-reformat/$file_type"
|
|
if [[ -x "$maybe" ]]; then
|
|
echo -n "$maybe"
|
|
return
|
|
fi
|
|
|
|
echo ""
|
|
}
|
|
|
|
function choose_wrapper() {
|
|
file_type="$1"
|
|
|
|
custom=$(custom_wrapper $PROJECT_ROOT $file_type)
|
|
|
|
if [[ ! -z "${custom}" ]]; then
|
|
echo -n "$custom"
|
|
return
|
|
fi
|
|
|
|
if [[ $file_type = "javascript" ||
|
|
$file_type = "vue" ||
|
|
$file_type = "typescript" ||
|
|
$file_type = "css" ||
|
|
$file_type = "php" ||
|
|
$file_type = "html" ||
|
|
$file_type = "jsx" ||
|
|
$file_type = "tsx" ||
|
|
$file_type == "css" ||
|
|
$file_type == "scss" ||
|
|
$file_type = "sh" ]]; then
|
|
out="$WF_ROOT"/./prettier.sh
|
|
elif [[ $file_type == "python" ]]; then
|
|
out="$WF_ROOT"/./isort-and-black.sh
|
|
elif [[ $file_type == "golang" ]]; then
|
|
out="$WF_ROOT"/./gofmt.sh
|
|
elif [[ $file_type == "rust" ]] ; then
|
|
out="$WF_ROOT"/./rustfmt.sh
|
|
elif [[ $file_type == "dart" ]]; then
|
|
out="$WF_ROOT"/./dart_format.sh
|
|
else
|
|
out="$WF_ROOT"/./default.sh
|
|
fi
|
|
|
|
echo "$out"
|
|
}
|
|
|
|
function wrflog() {
|
|
echo "$@" >>/tmp/wrflog
|
|
}
|
|
|
|
# WOULD_FORMAT_INSTALLATION_ROOT=$(wfroot)
|
|
|
|
export WOULD_FORMAT_PROJECT_ROOT
|