From 543f189fe6ac192bae74f3af511217b842ba1eab Mon Sep 17 00:00:00 2001 From: MAMIDI RAVI PRAVEEN <121561557+RAVI-PRAVEEN@users.noreply.github.com> Date: Tue, 8 Oct 2024 21:34:43 +0530 Subject: [PATCH] Update generate_openapi_code.sh Key Changes: Modular Functions: The script is now organized into functions for logging actions, compiling TypeSpec, and running Poetry tasks. Logging: Introduced log_action for logging actions and errors with timestamps. This helps track what the script is doing in real-time. Error Handling: Added || { ... } constructs to gracefully handle errors and log them. Optional Poetry Update: The --update-poetry flag is available to update Poetry dependencies before running tasks. If not passed, Poetry runs without updating. Execution Time: Logs the total time taken for the script to execute, making it easier to track performance. --- scripts/generate_openapi_code.sh | 92 ++++++++++++++++++++++++++++---- 1 file changed, 82 insertions(+), 10 deletions(-) diff --git a/scripts/generate_openapi_code.sh b/scripts/generate_openapi_code.sh index f3cfe70b1..8944f47dc 100755 --- a/scripts/generate_openapi_code.sh +++ b/scripts/generate_openapi_code.sh @@ -1,17 +1,89 @@ #!/usr/bin/env bash -# Turn on echo command -set -x +# Turn on command echo and exit on error +set -xe # Exit on error set -e +# Define log file for output +LOG_FILE="script.log" +TIME_FORMAT="+%Y-%m-%d %H:%M:%S" -cd typespec/ && \ - tsp compile . -cd - +# Function to log actions with timestamps +log_action() { + echo "$(date "$TIME_FORMAT") - $1" | tee -a "$LOG_FILE" +} -cd agents-api && \ - # poetry update && \ - poetry run poe codegen && \ - poetry run poe format -cd - +# Function to compile TypeSpec project +compile_typespec() { + log_action "Starting TypeSpec compilation" + pushd typespec/ || { log_action "Error: Failed to navigate to typespec/ directory"; exit 1; } + tsp compile . || { log_action "Error: TypeSpec compilation failed"; exit 1; } + popd > /dev/null + log_action "Finished TypeSpec compilation" +} + +# Function to run Poetry tasks (codegen and format) +run_poetry_tasks() { + log_action "Navigating to agents-api directory" + pushd agents-api || { log_action "Error: Failed to navigate to agents-api/ directory"; exit 1; } + + # Update dependencies if the flag is set + if [[ "$UPDATE_POETRY" == "true" ]]; then + log_action "Updating Poetry dependencies" + poetry update || { log_action "Error: Poetry update failed"; exit 1; } + fi + + log_action "Running code generation" + poetry run poe codegen || { log_action "Error: Code generation failed"; exit 1; } + + log_action "Running code formatting" + poetry run poe format || { log_action "Error: Code formatting failed"; exit 1; } + + popd > /dev/null + log_action "Finished Poetry tasks in agents-api" +} + +# Function to display script usage +usage() { + echo "Usage: $0 [options]" + echo "Options:" + echo " --update-poetry Update Poetry dependencies before running tasks" + echo " -h, --help Show this help message" +} + +# Parse arguments +while [[ "$#" -gt 0 ]]; do + case "$1" in + --update-poetry) + UPDATE_POETRY="true" + shift + ;; + -h|--help) + usage + exit 0 + ;; + *) + echo "Unknown parameter passed: $1" + usage + exit 1 + ;; + esac +done + +# Start time logging +START_TIME=$(date +%s) + +# Main execution +log_action "Script execution started" +compile_typespec +run_poetry_tasks + +# End time logging +END_TIME=$(date +%s) +ELAPSED_TIME=$((END_TIME - START_TIME)) + +log_action "Script execution completed in $ELAPSED_TIME seconds" + +# Success exit +exit 0