#!/usr/bin/env bash set -euo pipefail usage() { cat <<'USAGE' Usage: ./.scripts/rename-template.sh \ -g \ -p \ -n \ -s \ -r \ -a \ -y Example: ./.scripts/rename-template.sh \ -g com.acme \ -p my-project \ -n acmeRepo \ -s https://repo.acme.com/snapshots \ -r https://repo.acme.com/releases \ -a "Acme, Inc." \ -y 2026 USAGE } GROUP_ID="" PROJECT_NAME="" REPO_NAME="" SNAPSHOTS_REPO_URL="" RELEASES_REPO_URL="" AUTHOR="" COPYRIGHT_YEAR="" while getopts ":g:p:n:s:r:a:y:h" opt; do case "$opt" in g) GROUP_ID="$OPTARG" ;; p) PROJECT_NAME="$OPTARG" ;; n) REPO_NAME="$OPTARG" ;; s) SNAPSHOTS_REPO_URL="$OPTARG" ;; r) RELEASES_REPO_URL="$OPTARG" ;; a) AUTHOR="$OPTARG" ;; y) COPYRIGHT_YEAR="$OPTARG" ;; h) usage exit 0 ;; \?) echo "Unknown option: -$OPTARG" >&2 usage exit 1 ;; :) echo "Missing argument for -$OPTARG" >&2 usage exit 1 ;; esac done if [[ -z "$GROUP_ID" || -z "$PROJECT_NAME" || -z "$REPO_NAME" || -z "$SNAPSHOTS_REPO_URL" || -z "$RELEASES_REPO_URL" || -z "$AUTHOR" || -z "$COPYRIGHT_YEAR" ]]; then usage exit 1 fi PROJECT_NAME="${PROJECT_NAME##.}" if [[ -z "$PROJECT_NAME" ]]; then echo "Project name cannot be only dots." >&2 exit 1 fi normalize_segment() { local segment="$1" local normalized normalized="$(echo "$segment" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9_]/_/g')" normalized="${normalized##_}" normalized="${normalized%%_}" if [[ -z "$normalized" ]]; then normalized="app" fi echo "$normalized" } build_package_name() { local group_id="$1" local project_name="$2" local -a package_segments local -a group_parts IFS='.' read -r -a group_parts <<<"$group_id" for part in "${group_parts[@]}"; do package_segments+=("$(normalize_segment "$part")") done package_segments+=("$(normalize_segment "$project_name")") IFS='.' echo "${package_segments[*]}" } PACKAGE_NAME="$(build_package_name "$GROUP_ID" "$PROJECT_NAME")" REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" TEMPLATE_PACKAGE="com.example.template" SOURCE_MODULE_DIR="$REPO_ROOT/template-api" FILES=( "$REPO_ROOT/build.gradle.kts" "$REPO_ROOT/settings.gradle.kts" "$REPO_ROOT/build-logic/src/main/kotlin/extensions.kt" "$REPO_ROOT/build-logic/src/main/kotlin/publishing-conventions.gradle.kts" "$REPO_ROOT/license.txt" "$REPO_ROOT/license_header.txt" "$REPO_ROOT/.checkstyle/suppressions.xml" ) if [[ -d "$SOURCE_MODULE_DIR/src/main/java" ]]; then while IFS= read -r -d '' file; do FILES+=("$file") done < <(find "$SOURCE_MODULE_DIR/src/main/java" -type f -print0) fi if [[ -d "$SOURCE_MODULE_DIR/src/test/java" ]]; then while IFS= read -r -d '' file; do FILES+=("$file") done < <(find "$SOURCE_MODULE_DIR/src/test/java" -type f -print0) fi SED_INPLACE=(-i) if [[ "$(uname)" == "Darwin" ]]; then SED_INPLACE=(-i '') fi escape_sed() { local value="$1" value="${value//\\/\\\\}" value="${value//&/\\&}" value="${value//\//\\/}" echo "$value" } replace_placeholders() { local file="$1" local safe_group safe_project safe_package local safe_repo_name local safe_snapshots_repo_url safe_releases_repo_url local safe_project_api local safe_author safe_copyright_year safe_group="$(escape_sed "$GROUP_ID")" safe_project="$(escape_sed "$PROJECT_NAME")" safe_package="$(escape_sed "$PACKAGE_NAME")" safe_repo_name="$(escape_sed "$REPO_NAME")" safe_snapshots_repo_url="$(escape_sed "$SNAPSHOTS_REPO_URL")" safe_releases_repo_url="$(escape_sed "$RELEASES_REPO_URL")" safe_project_api="$(escape_sed "${PROJECT_NAME}-api")" safe_author="$(escape_sed "$AUTHOR")" safe_copyright_year="$(escape_sed "$COPYRIGHT_YEAR")" sed "${SED_INPLACE[@]}" \ -e "s/{{GROUP_ID}}/$safe_group/g" \ -e "s/{{PROJECT_NAME}}/$safe_project/g" \ -e "s/{{PACKAGE_NAME}}/$safe_package/g" \ -e "s/{{REPO_NAME}}/$safe_repo_name/g" \ -e "s/{{SNAPSHOTS_REPO_URL}}/$safe_snapshots_repo_url/g" \ -e "s/{{RELEASES_REPO_URL}}/$safe_releases_repo_url/g" \ -e "s/{{AUTHOR}}/$safe_author/g" \ -e "s/{{COPYRIGHT_YEAR}}/$safe_copyright_year/g" \ -e "s/template-api/$safe_project_api/g" \ "$file" } for file in "${FILES[@]}"; do [[ -f "$file" ]] || continue replace_placeholders "$file" echo "Updated: $file" done move_package_dir() { local base_dir="$1" local old_dir local new_dir old_dir="$base_dir/$(echo "$TEMPLATE_PACKAGE" | tr '.' '/')" new_dir="$base_dir/$(echo "$PACKAGE_NAME" | tr '.' '/')" if [[ -d "$old_dir" ]]; then mkdir -p "$new_dir" shopt -s dotglob nullglob mv "$old_dir"/* "$new_dir"/ shopt -u dotglob nullglob rmdir "$old_dir" 2>/dev/null || true fi if [[ -d "$base_dir" ]]; then find "$base_dir" -type d -empty -delete fi } move_package_dir "$SOURCE_MODULE_DIR/src/main/java" move_package_dir "$SOURCE_MODULE_DIR/src/test/java" if [[ -d "$REPO_ROOT/template-api" ]]; then mv "$REPO_ROOT/template-api" "$REPO_ROOT/${PROJECT_NAME}-api" fi if [[ -d "$REPO_ROOT/.git" ]]; then rm -rf "$REPO_ROOT/.git" fi if command -v git >/dev/null 2>&1; then git -C "$REPO_ROOT" init else echo "Warning: git not found; skipping git init." >&2 fi echo "" echo "Done!" echo ""