|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +INDEX_FILE="static/index.html" |
| 4 | +ORIGINAL_SPAN='<span id="version-display"></span>' |
| 5 | + |
| 6 | +# Function to display help message |
| 7 | +display_help() { |
| 8 | + echo "Usage: $0 [OPTION]" |
| 9 | + echo "" |
| 10 | + echo "Update the version string in index.html with the current Git commit hash or tag." |
| 11 | + echo "" |
| 12 | + echo "Options:" |
| 13 | + echo " reset Revert index.html to its original state (no version displayed)." |
| 14 | + echo " -h, --help Display this help message." |
| 15 | + echo "" |
| 16 | + echo "If no option is provided, the script will update the version string." |
| 17 | + exit 0 |
| 18 | +} |
| 19 | + |
| 20 | +# Argument parsing |
| 21 | +if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then |
| 22 | + display_help |
| 23 | +fi |
| 24 | + |
| 25 | +# Reset mode: if the first argument is 'reset', revert the span to its original state |
| 26 | +if [ "$1" == "reset" ]; then |
| 27 | + # This sed command finds the version span (regardless of its content or class) |
| 28 | + # and replaces it with the original, empty, class-less span. |
| 29 | + # Using '#' as a delimiter to avoid issues with '/' or '>' in HTML tags |
| 30 | + sed -i -E 's#<span id="version-display"[^>]*>.*</span>#<span id="version-display"></span>#g' "$INDEX_FILE" |
| 31 | + echo "Version display has been reset in $INDEX_FILE" |
| 32 | + exit 0 |
| 33 | +fi |
| 34 | + |
| 35 | +# Default mode: Update version |
| 36 | +# Get the git version string using describe for a more readable version |
| 37 | +GIT_VERSION=$(git describe --tags --always) |
| 38 | +if [ -z "$GIT_VERSION" ]; then |
| 39 | + echo "Error: Not a git repository or no commits yet." |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +# The version string is the git version itself, no 'v' prefix needed here |
| 44 | +VERSION_STRING="$GIT_VERSION" |
| 45 | + |
| 46 | +# Check if the index file exists |
| 47 | +if [ ! -f "$INDEX_FILE" ]; then |
| 48 | + echo "Error: $INDEX_FILE not found." |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +# Use sed to update the version and add the visibility class. |
| 53 | +# This works by finding the empty version-display span and replacing its outer HTML |
| 54 | +# with the version string and the class attribute. |
| 55 | +sed -i -E 's#<span id="version-display"></span>#<span id="version-display" class="version-visible">'${VERSION_STRING}'</span>#g' "$INDEX_FILE" |
| 56 | + |
| 57 | +echo "Successfully updated version in $INDEX_FILE to $VERSION_STRING" |
| 58 | + |
| 59 | +# A small check to see if it worked. |
| 60 | +if ! grep -q "version-visible" "$INDEX_FILE"; then |
| 61 | + echo "Warning: Failed to make version visible. The span tag might not have been found correctly." |
| 62 | +fi |
0 commit comments