Last active
September 23, 2021 19:09
-
-
Save TomMD/5efd796aa19a8a59ca268dbe00904148 to your computer and use it in GitHub Desktop.
Psalm Integration to Lift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
PSALM=/tmp/psalm.phar | |
SUDO= | |
if [[ -x $(command -v sudo) ]] ; then | |
SUDO=sudo | |
fi | |
# Psalm isn't part of the Lift tooling. Install it. | |
function install_psalm() { | |
curl -sL https://github.com/vimeo/psalm/releases/latest/download/psalm.phar -o $PSALM 1>&2 | |
chmod +x $PSALM | |
} | |
# PHP and composer are not part of the Lift tooling. Install them. | |
function install_php() { | |
$SUDO apt update 2>/dev/null 1>&2 && $SUDO apt install -y php php-intl php-gd php-curl php-mbstring php-zip php-xml composer 2>/dev/null 1>&2 | |
} | |
function initialize_composer() { | |
if [[ ! ( -f "composer.json" ) ]] ; then | |
composer init -q -n 1>&2 | |
fi | |
composer install 1>&2 | |
} | |
function initialize_psalm() { | |
if [[ ! ( -f "psalm-baseline.xml" ) ]] ; then | |
$PSALM --init 1>&2 | |
fi | |
} | |
function run_psalm() { | |
output=/tmp/psalm-output.json | |
$PSALM --report "$output" --no-progress 1>&2 | |
if [[ ! ( -f "$output" ) ]] ; then | |
# psalm has a bug (feature?) where it doesn't emit file when no results exist | |
echo "[]" > "$output" | |
fi | |
jq '[ .[] | select(.severity == "error") | |
| .line = .line_from | |
| .file = .file_name | |
| .detailsUrl = .link | |
| del(.snippet) | |
| del(.file_path) | |
| del(.line_from) | |
| del(.line_to) | |
| del(.other_references) | |
| del(.taint_trace) | |
| del(.link) ]' < "$output" | |
} | |
function tellName() { | |
printf "Psalm" | |
} | |
function tellVersion() { | |
echo "1" | |
} | |
function tellApplicable() { | |
files=$(git ls-files | grep -E '.php$' | head) | |
res="broken" | |
if [[ -z "$files" ]] ; then | |
res="false" | |
else | |
res="true" | |
fi | |
printf "%s\n" "$res" | |
} | |
case "$3" in | |
run) | |
install_php | |
install_psalm | |
initialize_composer | |
initialize_psalm | |
run_psalm | |
;; | |
applicable) | |
tellApplicable | |
;; | |
name) | |
tellName | |
;; | |
*) | |
tellVersion | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment