install_stylua.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env bash
  2. set -eu pipefall
  3. declare -r INSTALL_DIR="$PWD/utils"
  4. declare -r RELEASE="0.10.0"
  5. declare -r OS="linux"
  6. # declare -r OS="$(uname -s)"
  7. declare -r FILENAME="stylua-$RELEASE-$OS"
  8. declare -a __deps=("curl" "unzip")
  9. function check_deps() {
  10. for dep in "${__deps[@]}"; do
  11. if ! command -v "$dep" >/dev/null; then
  12. echo "Missing depdendecy!"
  13. echo "The \"$dep\" command was not found!. Please install and try again."
  14. fi
  15. done
  16. }
  17. function download_stylua() {
  18. local DOWNLOAD_DIR
  19. local URL="https://github.com/JohnnyMorganz/StyLua/releases/download/v$RELEASE/$FILENAME.zip"
  20. DOWNLOAD_DIR="$(mktemp -d)"
  21. echo "Initiating download for Stylua v$RELEASE"
  22. if ! curl --progress-bar --fail -L "$URL" -o "$DOWNLOAD_DIR/$FILENAME.zip"; then
  23. echo "Download failed. Check that the release/filename are correct."
  24. exit 1
  25. fi
  26. echo "Installation in progress.."
  27. unzip -q "$DOWNLOAD_DIR/$FILENAME.zip" -d "$DOWNLOAD_DIR"
  28. if [ -f "$DOWNLOAD_DIR/stylua" ]; then
  29. mv "$DOWNLOAD_DIR/stylua" "$INSTALL_DIR/stylua"
  30. else
  31. mv "$DOWNLOAD_DIR/$FILENAME/stylua" "$INSTALL_DIR/."
  32. fi
  33. chmod u+x "$INSTALL_DIR/stylua"
  34. }
  35. function verify_install() {
  36. echo "Verifying installation.."
  37. local DOWNLOADED_VER
  38. DOWNLOADED_VER="$("$INSTALL_DIR/stylua" -V | awk '{ print $2 }')"
  39. if [ "$DOWNLOADED_VER" != "$RELEASE" ]; then
  40. echo "Mismatched version!"
  41. echo "Expected: v$RELEASE but got v$DOWNLOADED_VER"
  42. exit 1
  43. fi
  44. echo "Verification complete!"
  45. }
  46. function main() {
  47. check_deps
  48. download_stylua
  49. verify_install
  50. }
  51. main "$@"