#!/usr/bin/env bash
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# Runs a Python sample by name.
# Usage: py/bin/run_sample <sample-name> [args...]
# Example: py/bin/run_sample google-genai-hello
# py/bin/run_sample evaluator-demo
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PY_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
SAMPLES_DIR="$PY_DIR/samples"
# List available samples using find for robustness
list_samples() {
# Only list directories that contain a run.sh script
find "$SAMPLES_DIR" -mindepth 1 -maxdepth 1 -type d -not -name '.*' -not -name '__pycache__' | while read -r dir; do
if [[ -f "$dir/run.sh" ]]; then
basename "$dir"
fi
done | sort
}
# Check if gum is available or install it
GUM_CMD=""
if command -v gum &> /dev/null; then
GUM_CMD="gum"
elif [[ -f "$HOME/go/bin/gum" ]]; then
GUM_CMD="$HOME/go/bin/gum"
elif command -v go &> /dev/null; then
read -p "gum not found. Would you like to install it using 'go install github.com/charmbracelet/gum@latest'? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Installing gum..."
if go install github.com/charmbracelet/gum@latest; then
GUM_CMD="$HOME/go/bin/gum"
fi
fi
fi
# Check if fzf is available or install it
FZF_CMD=""
if command -v fzf &> /dev/null; then
FZF_CMD="fzf"
else
read -p "fzf not found. Would you like to install it for a better experience (includes preview)? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
if command -v brew &> /dev/null; then
echo "Installing fzf via brew..."
brew install fzf
FZF_CMD="fzf"
elif command -v go &> /dev/null; then
echo "Installing fzf via go..."
if go install github.com/junegunn/fzf@latest; then
FZF_CMD="$HOME/go/bin/fzf"
fi
else
echo "Neither brew nor go found. Skipping fzf installation."
fi
fi
fi
if [[ $# -lt 1 ]]; then
SAMPLES=$(list_samples)
if [[ -n "$FZF_CMD" ]]; then
# Use fzf with preview window if available
SAMPLE_NAME=$(echo "$SAMPLES" | $FZF_CMD --preview "if [ -f $SAMPLES_DIR/{}/README.md ]; then cat $SAMPLES_DIR/{}/README.md; else echo 'No README found.'; fi" --preview-window=right:60%:wrap --height=100% --reverse --header="Select a sample to run")
if [[ -z "$SAMPLE_NAME" ]]; then
exit 0
fi
elif [[ -n "$GUM_CMD" ]]; then
SAMPLE_NAME=$($GUM_CMD filter --placeholder "Select a sample to run..." <<< "$SAMPLES")
if [[ -z "$SAMPLE_NAME" ]]; then
exit 0
fi
else
echo "Usage: $0 <sample-name> [args...]"
echo ""
echo "Available samples:"
echo "$SAMPLES"
exit 1
fi
else
SAMPLE_NAME="$1"
shift
fi
SAMPLE_DIR="$SAMPLES_DIR/$SAMPLE_NAME"
if [[ ! -d "$SAMPLE_DIR" ]]; then
echo "Error: Sample '$SAMPLE_NAME' not found in $SAMPLES_DIR"
echo ""
echo "Available samples:"
list_samples
exit 1
fi
cd "$SAMPLE_DIR"
if [[ -f "./README.md" ]]; then
if [[ -n "$GUM_CMD" ]]; then
$GUM_CMD format -t markdown < ./README.md
else
cat ./README.md
fi
echo ""
fi
if [[ -f "./run.sh" ]]; then
exec ./run.sh "$@"
else
echo "Error: Sample '$SAMPLE_NAME' does not have a 'run.sh' script."
exit 1
fi