|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Variables |
| 6 | +dlurl="https://github.com/HL7/fhir-ig-publisher/releases/latest/download/publisher.jar" |
| 7 | +publisher_jar="publisher.jar" |
| 8 | +input_cache_path="$(pwd)/input-cache/" |
| 9 | +skipPrompts=false |
| 10 | +upper_path="../" |
| 11 | +scriptdlroot="https://raw.githubusercontent.com/HL7/ig-publisher-scripts/main" |
| 12 | +build_bat_url="${scriptdlroot}/_build.bat" |
| 13 | +build_sh_url="${scriptdlroot}/_build.sh" |
| 14 | + |
| 15 | +function check_jar_location() { |
| 16 | + if [ -f "${input_cache_path}${publisher_jar}" ]; then |
| 17 | + jar_location="${input_cache_path}${publisher_jar}" |
| 18 | + echo "Found publisher.jar in input-cache" |
| 19 | + elif [ -f "${upper_path}${publisher_jar}" ]; then |
| 20 | + jar_location="${upper_path}${publisher_jar}" |
| 21 | + echo "Found publisher.jar in parent folder" |
| 22 | + else |
| 23 | + jar_location="not_found" |
| 24 | + echo "publisher.jar not found in input-cache or parent folder" |
| 25 | + fi |
| 26 | +} |
| 27 | + |
| 28 | +function check_internet_connection() { |
| 29 | + if ping -c 1 tx.fhir.org &>/dev/null; then |
| 30 | + online=true |
| 31 | + echo "We're online and tx.fhir.org is available." |
| 32 | + latest_version=$(curl -s https://api.github.com/repos/HL7/fhir-ig-publisher/releases/latest | grep tag_name | cut -d'"' -f4) |
| 33 | + else |
| 34 | + online=false |
| 35 | + echo "We're offline or tx.fhir.org is unavailable." |
| 36 | + fi |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +function update_publisher() { |
| 41 | + echo "Publisher jar location: ${input_cache_path}${publisher_jar}" |
| 42 | + read -p "Download or update publisher.jar? (Y/N): " confirm |
| 43 | + if [[ "$confirm" =~ ^[Yy]$ ]]; then |
| 44 | + echo "Downloading latest publisher.jar (~200 MB)..." |
| 45 | + mkdir -p "$input_cache_path" |
| 46 | + curl -L "$dlurl" -o "${input_cache_path}${publisher_jar}" |
| 47 | + else |
| 48 | + echo "Skipped downloading publisher.jar" |
| 49 | + fi |
| 50 | + |
| 51 | + update_scripts_prompt |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +function update_scripts_prompt() { |
| 56 | + read -p "Update scripts (_build.bat and _build.sh)? (Y/N): " update_confirm |
| 57 | + if [[ "$update_confirm" =~ ^[Yy]$ ]]; then |
| 58 | + echo "Updating scripts..." |
| 59 | + curl -L "$build_bat_url" -o "_build.new.bat" && mv "_build.new.bat" "_build.bat" |
| 60 | + curl -L "$build_sh_url" -o "_build.new.sh" && mv "_build.new.sh" "_build.sh" |
| 61 | + chmod +x _build.sh |
| 62 | + echo "Scripts updated." |
| 63 | + else |
| 64 | + echo "Skipped updating scripts." |
| 65 | + fi |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +function build_ig() { |
| 70 | + if [ "$jar_location" != "not_found" ]; then |
| 71 | + args=() |
| 72 | + if [ "$online" = "false" ]; then |
| 73 | + args+=("-tx" "n/a") |
| 74 | + fi |
| 75 | + java -Dfile.encoding=UTF-8 -jar "$jar_location" -ig . "${args[@]}" "$@" |
| 76 | + else |
| 77 | + echo "publisher.jar not found. Please run update." |
| 78 | + fi |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +function build_nosushi() { |
| 83 | + if [ "$jar_location" != "not_found" ]; then |
| 84 | + java -Dfile.encoding=UTF-8 -jar "$jar_location" -ig . -no-sushi "$@" |
| 85 | + else |
| 86 | + echo "publisher.jar not found. Please run update." |
| 87 | + fi |
| 88 | +} |
| 89 | + |
| 90 | +function build_notx() { |
| 91 | + if [ "$jar_location" != "not_found" ]; then |
| 92 | + java -Dfile.encoding=UTF-8 -jar "$jar_location" -ig . -tx n/a "$@" |
| 93 | + else |
| 94 | + echo "publisher.jar not found. Please run update." |
| 95 | + fi |
| 96 | +} |
| 97 | + |
| 98 | +function jekyll_build() { |
| 99 | + echo "Running Jekyll build..." |
| 100 | + jekyll build -s temp/pages -d output |
| 101 | +} |
| 102 | + |
| 103 | +function cleanup() { |
| 104 | + echo "Cleaning up temp directories..." |
| 105 | + if [ -f "${input_cache_path}${publisher_jar}" ]; then |
| 106 | + mv "${input_cache_path}${publisher_jar}" ./ |
| 107 | + rm -rf "${input_cache_path}"* |
| 108 | + mkdir -p "$input_cache_path" |
| 109 | + mv "$publisher_jar" "$input_cache_path" |
| 110 | + fi |
| 111 | + rm -rf ./output ./template ./temp |
| 112 | + echo "Cleanup complete." |
| 113 | +} |
| 114 | + |
| 115 | +check_jar_location |
| 116 | +check_internet_connection |
| 117 | + |
| 118 | +# Handle command-line argument or menu |
| 119 | +case "$1" in |
| 120 | + update) update_publisher ;; |
| 121 | + build) build_ig ;; |
| 122 | + nosushi) build_nosushi ;; |
| 123 | + notx) build_notx ;; |
| 124 | + jekyll) jekyll_build ;; |
| 125 | + clean) cleanup ;; |
| 126 | + exit) exit 0 ;; |
| 127 | + *) |
| 128 | + # Compute default choice |
| 129 | + default_choice=2 # Build by default |
| 130 | + |
| 131 | + if [ "$jar_location" = "not_found" ]; then |
| 132 | + default_choice=1 # Download if jar is missing |
| 133 | + elif [ "$online" = "false" ]; then |
| 134 | + default_choice=4 # Offline build |
| 135 | + elif [ -n "$latest_version" ]; then |
| 136 | + current_version=$(java -jar "$jar_location" -v 2>/dev/null | tr -d '\r') |
| 137 | + if [ "$current_version" != "$latest_version" ]; then |
| 138 | + default_choice=1 # Offer update if newer version exists |
| 139 | + fi |
| 140 | + fi |
| 141 | + |
| 142 | + echo "---------------------------------------------" |
| 143 | + echo "Publisher: ${current_version:-unknown}; Latest: ${latest_version:-unknown}" |
| 144 | + echo "Publisher location: $jar_location" |
| 145 | + echo "Online: $online" |
| 146 | + echo "---------------------------------------------" |
| 147 | + echo |
| 148 | + echo "Please select an option:" |
| 149 | + echo "1) Update publisher" |
| 150 | + echo "2) Build IG" |
| 151 | + echo "3) Build IG without Sushi" |
| 152 | + echo "4) Build IG without TX server" |
| 153 | + echo "5) Jekyll build" |
| 154 | + echo "6) Cleanup temp directories" |
| 155 | + echo "0) Exit" |
| 156 | + echo |
| 157 | + |
| 158 | + # Read with timeout, but default if nothing entered |
| 159 | + echo -n "Choose an option [default: $default_choice]: " |
| 160 | + read -t 5 choice || choice="$default_choice" |
| 161 | + choice="${choice:-$default_choice}" |
| 162 | + echo "You selected: $choice" |
| 163 | + |
| 164 | + case "$choice" in |
| 165 | + 1) update_publisher ;; |
| 166 | + 2) build_ig ;; |
| 167 | + 3) build_nosushi ;; |
| 168 | + 4) build_notx ;; |
| 169 | + 5) jekyll_build ;; |
| 170 | + 6) cleanup ;; |
| 171 | + 0) exit 0 ;; |
| 172 | + *) echo "Invalid option." ;; |
| 173 | + esac |
| 174 | + ;; |
| 175 | + |
| 176 | +esac |
0 commit comments