Files
learn-rust/cleanup.sh

76 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# This script assumes you're in the monorepo root directory.
# It will:
# - Remove nested .git folders
# - Remove subproject .gitignore files (if present)
# - Optionally create or update the workspace Cargo.toml
# - Add the projects to your root Git repo
set -e
echo "🔧 Flattening nested Rust projects into a monorepo..."
# Gather all Rust subprojects
projects=()
for dir in */Cargo.toml; do
project=$(dirname "$dir")
projects+=("$project")
# Remove nested Git repo
if [ -d "$project/.git" ]; then
echo "❌ Removing nested Git repo in $project"
rm -rf "$project/.git"
fi
# Optionally remove local .gitignore (you can comment this out if needed)
if [ -f "$project/.gitignore" ]; then
echo "❌ Removing $project/.gitignore"
rm -f "$project/.gitignore"
fi
done
# Create .gitignore in root if missing
if [ ! -f .gitignore ]; then
echo "📄 Creating root .gitignore"
cat > .gitignore <<EOF
**/target/
**/*.o
**/*.d
**/*.exe
**/*.log
**/*.rlib
**/*.dylib
**/*.so
**/.DS_Store
**/.vscode/
**/.idea/
EOF
fi
# Create root Cargo.toml workspace
if [ ! -f Cargo.toml ]; then
echo "📄 Creating root Cargo.toml"
echo "[workspace]" > Cargo.toml
echo "members = [" >> Cargo.toml
for project in "${projects[@]}"; do
echo " \"$project\"," >> Cargo.toml
done
echo "]" >> Cargo.toml
else
echo "⚠️ Cargo.toml already exists at root. You may need to manually add workspace members."
fi
# Init root Git repo if not already
if [ ! -d .git ]; then
echo "🔁 Initializing new root Git repo"
git init
fi
# Add and commit
git add .
git commit -m "Flatten nested Rust projects into monorepo"
echo "✅ Done! Monorepo is clean and ready."