generate-swagger-ui.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/bin/sh
  2. set -e
  3. [ -z "$SWAGGER_UI_VERSION" ] && echo "missing \$SWAGGER_UI_VERSION" && exit 1
  4. SWAGGER_UI_GIT="https://github.com/swagger-api/swagger-ui.git"
  5. CACHE_DIR="./.cache/swagger-ui/$SWAGGER_UI_VERSION"
  6. GEN_DIR="./third_party/OpenAPI"
  7. escape_str() {
  8. echo "$1" | sed -e 's/[]\/$*.^[]/\\&/g'
  9. }
  10. # do caching if there's no cache yet
  11. if [ ! -d "$CACHE_DIR" ]; then
  12. mkdir -p "$CACHE_DIR"
  13. tmp="$(mktemp -d)"
  14. git clone --depth 1 --branch "$SWAGGER_UI_VERSION" "$SWAGGER_UI_GIT" "$tmp"
  15. cp -r "$tmp/dist/"* "$CACHE_DIR"
  16. cp -r "$tmp/LICENSE" "$CACHE_DIR"
  17. rm -rf "$tmp"
  18. fi
  19. # populate swagger.json
  20. tmp=" urls: ["
  21. for i in $(find "$GEN_DIR" -name "*.swagger.json"); do
  22. escaped_gen_dir="$(escape_str "$GEN_DIR/")"
  23. path="$(echo $i | sed -e "s/$escaped_gen_dir//g")"
  24. tmp="$tmp{\"url\":\"$path\",\"name\":\"$path\"},"
  25. done
  26. # delete last characters from $tmp
  27. tmp=$(echo "$tmp" | sed 's/.$//')
  28. tmp="$tmp],"
  29. # recreate swagger-ui, delete all except swagger.json
  30. find "$GEN_DIR" -type f -not -name "*.swagger.json" -delete
  31. mkdir -p "$GEN_DIR"
  32. cp -r "$CACHE_DIR/"* "$GEN_DIR"
  33. # replace the default URL
  34. line="$(cat "$GEN_DIR/swagger-initializer.js" | grep -n "url" | cut -f1 -d:)"
  35. escaped_tmp="$(escape_str "$tmp")"
  36. sed -i'' -e "$line s/^.*$/$escaped_tmp/" "$GEN_DIR/swagger-initializer.js"
  37. rm -f "$GEN_DIR/swagger-initializer.js-e"