Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 139 additions & 5 deletions scripts/fhir_scripts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,95 @@ function igtools_version() {
fi
}

function check_pytool_version() {
local package_name="$1"
local repo_url="$2"
local current_version=""
local latest_version=""
local response=""

# Handle missing parameters gracefully
if [[ -z "$package_name" || -z "$repo_url" ]]; then
echo "error:missing_parameters"
return 0
fi

# Get current version from pipx
current_version=$(sudo pipx list --global --short 2>/dev/null | awk -v pkg="$package_name" '$1 == pkg {print $2}') || true

if [[ -z "$current_version" ]]; then
echo "not_installed"
return 0
fi

# Try to fetch latest version from GitHub with 1s timeout
response=$(curl -s --connect-timeout 1 --max-time 1 "$repo_url" 2>/dev/null || true)

if [[ -z "$response" ]]; then
echo "error:fetch_failed_or_timeout:${current_version}"
return 0
fi

# extract from __VERSION__ in source code listing
if [[ -z "$latest_version" ]]; then
latest_version=$(echo "$response" \
| grep -Eo "__VERSION__\s*=\s*(Version\()?['\"][0-9]+(\.[0-9]+){1,2}['\"]\)?" \
| sed -E "s/.*__VERSION__\s*=\s*(Version\()?['\"]([0-9]+(\.[0-9]+){1,2})['\"]\)?/\2/" \
| sort -V \
| tail -n 1 || true)
fi

if [[ -z "$latest_version" ]]; then
echo "error:could_not_parse_latest_version:${current_version}"
return 0
fi

# Compare installed vs. latest
if [ "$current_version" = "$latest_version" ]; then
echo "up_to_date:${current_version}"
else
echo "update_available:${current_version}:${latest_version}"
fi

return 0
}

function maintain_pytool() {
local package_name=$1
local check_url=$2
local update_url=$3

echo "Checking $package_name version"
result=$(check_pytool_version "$package_name" "$check_url")
IFS=':' read -r status current latest <<< "$result"

case "$status" in
update_available)
echo "⬆️ $package_name: $current → $latest"
update_pytool "$update_url" "Updated" "Failed to update $package_name"
;;
up_to_date)
echo "✅ $package_name up to date ($current)"
;;
error*)
echo "⚠️ $package_name check failed ($current)"
;;
not_installed)
echo "📦 $package_name not installed"
;;
esac
}

function maintain_pytools() {
maintain_pytool "epatools" \
"https://raw.githubusercontent.com/onyg/epa-tools/refs/heads/main/src/epatools/version.py" \
"git+https://github.com/onyg/epa-tools.git"

maintain_pytool "igtools" \
"https://raw.githubusercontent.com/onyg/req-tooling/refs/heads/main/src/igtools/versioning.py" \
"git+https://github.com/onyg/req-tooling.git"
}

function update_pytools() {
update_pytool "git+https://github.com/onyg/epa-tools.git" "Updated epa-tools to $(epatools_version)" "Failed to update epa-tools"
update_pytool "git+https://github.com/onyg/req-tooling.git" "Upated reqtooling to $(igtools_version)" "Failed to update req-tooling"
Expand All @@ -215,17 +304,24 @@ function update_pytools() {
function build() {
case "$1" in
pytools)
[[ "$UPDATE_PYTOOLS" == true ]] && maintain_pytools
run_igtools
merge_capabilities
;;
sushi) run_sushi ;;
defs) build_definitions ;;
ig) build_ig ;;
defs)
[[ "$UPDATE_PYTOOLS" == true ]] && maintain_pytools
build_definitions
;;
ig)
[[ "$UPDATE_PYTOOLS" == true ]] && maintain_pytools
build_ig
;;
*)
[[ "$UPDATE_PYTOOLS" == true ]] && maintain_pytools
build_definitions
build_ig
;;

esac
}

Expand Down Expand Up @@ -474,9 +570,25 @@ function gcloud_cp() {
gsutil -m -h "Cache-Control:no-cache" cp -r ./output/* gs://$1
}


###
# Parse global flags
###
UPDATE_PYTOOLS=false
for arg in "$@"; do
if [[ "$arg" == "-updatepytools" || "$arg" == "-u" ]]; then
UPDATE_PYTOOLS=true
# remove flag from arguments
set -- "${@/-updatepytools/}"
set -- "${@/-u/}"
break
fi
done

###
# Handle command-line argument or menu
###

case "$1" in
update) update $2 ;;
fhircache) rebuild_fhir_cache $2 ;;
Expand Down Expand Up @@ -512,6 +624,23 @@ case "$1" in
4) rebuild_fhir_cache ;;
5) delete_build_cache ;;
6)

if [[ "$UPDATE_PYTOOLS" == false && -t 0 && $# -eq 0 ]]; then

default_choice="n"
echo
echo "Would you like to check for and install new pytool verions on the go?"
echo -n "Enter y or n [default: n]: "
read -t 15 choice || choice="$default_choice"
choice="${choice:-$default_choice}"
echo
echo "You selected: $choice"
echo
if [[ "$choice" =~ ^[Yy]$ ]]; then
UPDATE_PYTOOLS=true
fi
fi

default_choice=0 # Exit on default

echo "Please select an option:"
Expand All @@ -529,11 +658,16 @@ case "$1" in

case "$choice" in
1)
[[ "$UPDATE_PYTOOLS" == true ]] && maintain_pytools
build_definitions
build_ig
;;
2) build_definitions ;;
3) build_ig ;;
2)
[[ "$UPDATE_PYTOOLS" == true ]] && maintain_pytools
build_definitions ;;
3)
[[ "$UPDATE_PYTOOLS" == true ]] && maintain_pytools
build_ig ;;
0) exit 0 ;;
*) echo "Invalid option." ;;
esac
Expand Down