Files
FTC-Project-Gen/ftc-new-project
2026-01-24 12:39:32 -06:00

236 lines
7.4 KiB
Bash
Executable File

#!/bin/bash
# FTC Project Generator
# Copyright (c) 2026 Nexus Workshops LLC
# Licensed under MIT License
set -e
# Get script directory and load library
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/linux/lib.sh"
# Configuration
DEFAULT_FTC_VERSION="v10.1.1"
FTC_SDK_DIR="${FTC_SDK_DIR:-$HOME/ftc-sdk}"
FTC_VERSION="$DEFAULT_FTC_VERSION"
GENERATOR_VERSION="$(get_generator_version)"
TEMPLATE_DIR="$SCRIPT_DIR/linux/templates"
# Parse arguments
PROJECT_NAME=""
UPGRADE_MODE=false
SHOW_HELP=false
while [[ $# -gt 0 ]]; do
case $1 in
-v|--version)
FTC_VERSION="$2"
shift 2
;;
-d|--sdk-dir)
FTC_SDK_DIR="$2"
shift 2
;;
--upgrade)
UPGRADE_MODE=true
shift
;;
-h|--help)
SHOW_HELP=true
shift
;;
*)
if [ -z "$PROJECT_NAME" ]; then
PROJECT_NAME="$1"
else
echo "Error: Unknown argument: $1"
exit 1
fi
shift
;;
esac
done
# Show help
if [ "$SHOW_HELP" = true ]; then
cat << EOF
════════════════════════════════════════════════════════════════
FTC Project Generator v${GENERATOR_VERSION}
════════════════════════════════════════════════════════════════
Creates clean, testable FTC robot projects with shared SDK.
USAGE:
$(basename $0) <project-name> [options]
OPTIONS:
-v, --version <tag> FTC SDK version (default: $DEFAULT_FTC_VERSION)
-d, --sdk-dir <path> SDK location (default: $FTC_SDK_DIR)
--upgrade Upgrade existing project
-h, --help Show this help
EXAMPLES:
# Create new project
$(basename $0) my-robot
# Create with specific FTC version
$(basename $0) my-robot -v v10.0.0
# Upgrade existing project
$(basename $0) my-robot --upgrade
WORKFLOW:
1. Create: $(basename $0) my-robot
2. Develop: cd my-robot && ./gradlew test --continuous
3. Deploy: ./deploy-to-robot.sh
UPGRADE:
Updates build files and scripts while preserving your code:
$(basename $0) existing-project --upgrade
MORE INFO:
See README.md for detailed documentation.
EOF
exit 0
fi
# Validate project name
if [ -z "$PROJECT_NAME" ]; then
echo "Error: Project name required"
echo "Usage: $0 <project-name> [options]"
echo "Run '$0 --help' for more info"
exit 1
fi
PROJECT_DIR="$(pwd)/$PROJECT_NAME"
# Export variables for template processing
export PROJECT_NAME
export FTC_SDK_DIR
export FTC_VERSION
export GENERATOR_VERSION
# Handle upgrade mode
if [ "$UPGRADE_MODE" = true ]; then
if [ ! -d "$PROJECT_DIR" ]; then
echo "Error: Project does not exist: $PROJECT_DIR"
echo "Create it first with: $0 $PROJECT_NAME"
exit 1
fi
if ! is_generator_project "$PROJECT_DIR"; then
echo "Error: Not a generator project (missing .ftc-generator-version)"
echo "This project was not created by FTC Project Generator"
exit 1
fi
echo "════════════════════════════════════════════════════════════════"
echo " Upgrading Project: $PROJECT_NAME"
echo "════════════════════════════════════════════════════════════════"
echo ""
upgrade_project "$PROJECT_DIR" "$TEMPLATE_DIR"
echo ""
echo "✓ Upgrade complete!"
echo ""
echo "Review changes with: cd $PROJECT_NAME && git diff"
echo "Test with: ./gradlew test"
echo ""
exit 0
fi
# Normal project creation mode
if [ -d "$PROJECT_DIR" ]; then
echo "════════════════════════════════════════════════════════════════"
echo " Project Already Exists"
echo "════════════════════════════════════════════════════════════════"
echo ""
echo "Directory exists: $PROJECT_DIR"
echo ""
if is_generator_project "$PROJECT_DIR"; then
echo "This is a generator project (v$(get_project_generator_version "$PROJECT_DIR"))"
echo ""
echo "To upgrade it, run:"
echo " $0 $PROJECT_NAME --upgrade"
else
echo "Choose a different name or remove the existing directory."
fi
echo ""
exit 1
fi
echo "════════════════════════════════════════════════════════════════"
echo " FTC Project Generator v${GENERATOR_VERSION}"
echo "════════════════════════════════════════════════════════════════"
echo ""
# Check prerequisites
check_prerequisites || exit 1
echo ""
echo "Configuration:"
echo " Project: $PROJECT_NAME"
echo " Location: $PROJECT_DIR"
echo " SDK Dir: $FTC_SDK_DIR"
echo " SDK Version: $FTC_VERSION"
echo ""
# Setup FTC SDK
echo ">>> Checking FTC SDK..."
if [ -d "$FTC_SDK_DIR" ]; then
echo "SDK directory exists"
cd "$FTC_SDK_DIR"
if [ -d ".git" ]; then
CURRENT_TAG=$(git describe --tags --exact-match 2>/dev/null || echo "none")
if [ "$CURRENT_TAG" = "$FTC_VERSION" ]; then
echo "✓ SDK at version $FTC_VERSION"
else
echo "Updating to $FTC_VERSION..."
git fetch --tags
git checkout "$FTC_VERSION"
echo "✓ Updated to $FTC_VERSION"
fi
else
echo "Warning: SDK exists but not a git repo"
fi
else
echo "Cloning FTC SDK $FTC_VERSION..."
git clone --depth 1 --branch "$FTC_VERSION" \
https://github.com/FIRST-Tech-Challenge/FtcRobotController.git \
"$FTC_SDK_DIR" || {
echo "Error: Failed to clone FTC SDK"
exit 1
}
echo "✓ Cloned SDK"
fi
cd "$(dirname "$PROJECT_DIR")"
echo ""
# Create project
echo ">>> Creating project..."
create_project_structure "$PROJECT_DIR"
install_templates "$PROJECT_DIR" "$TEMPLATE_DIR"
setup_gradle_wrapper "$PROJECT_DIR"
init_git_repo "$PROJECT_DIR"
echo ""
echo "════════════════════════════════════════════════════════════════"
echo " ✓ Project Created!"
echo "════════════════════════════════════════════════════════════════"
echo ""
echo "Project: $PROJECT_NAME"
echo "Location: $PROJECT_DIR"
echo "SDK: $FTC_VERSION"
echo ""
echo "Quick Start:"
echo " cd $PROJECT_NAME"
echo " ./gradlew test --continuous"
echo ""
echo "See README.md for full documentation."
echo ""