Skip to content

Commit

Permalink
Update generate_openapi_code.sh
Browse files Browse the repository at this point in the history
The script has been enhanced by organizing the tasks into functions for improved readability and modularity. A logging system was introduced to capture and store actions in both the console and a log file for better traceability. Error handling was added to ensure that any failures are caught with custom error messages, improving the robustness of the script. Additionally, an optional feature allows updating Poetry dependencies via a command-line flag, adding flexibility. The script now tracks the total execution time, providing insights into performance. Overall, the improvements make the script more structured, flexible, and user-friendly.
  • Loading branch information
Piyushsahu99 authored Oct 2, 2024
1 parent 50a95ca commit 9f5d9c8
Showing 1 changed file with 82 additions and 12 deletions.
94 changes: 82 additions & 12 deletions scripts/generate_openapi_code.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,87 @@
#!/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
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"
cd typespec/ || { echo "Error: Failed to navigate to typespec/ directory"; exit 1; }
tsp compile . || { echo "Error: TypeSpec compilation failed"; exit 1; }
cd - > /dev/null
log_action "Finished TypeSpec compilation"
}

# Function to run Poetry tasks
run_poetry_tasks() {
log_action "Navigating to agents-api directory"
cd agents-api || { echo "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 || { echo "Error: Poetry update failed"; exit 1; }
fi

log_action "Running code generation"
poetry run poe codegen || { echo "Error: Code generation failed"; exit 1; }

log_action "Running code formatting"
poetry run poe format || { echo "Error: Code formatting failed"; exit 1; }

cd - > /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

0 comments on commit 9f5d9c8

Please sign in to comment.