Files
wolf_dart/package.sh

141 lines
5.2 KiB
Bash
Executable File

#!/bin/bash
# Exit immediately if a command exits with a non-zero status
set -e
# ==========================================
# 1. Identify Candidate Packages & Dependents
# ==========================================
PACKAGES=("wolf_3d_data" "wolf_3d_data_types" "wolf_3d_engine" "wolf_3d_entities" "wolf_3d_input" "wolf_3d_synth")
DEPENDENTS=("apps/wolf_3d_cli" "apps/wolf_3d_gui" "packages/wolf_3d_flutter" "packages/wolf_3d_renderer")
NEW_PKG="wolf_3d_dart"
echo "Starting migration to $NEW_PKG..."
# ==========================================
# 2. Create the New Package wolf_3d_dart
# ==========================================
echo "Creating new package structure..."
mkdir -p "packages/$NEW_PKG/lib/src"
# Initialize a base pubspec.yaml
cat <<EOF > "packages/$NEW_PKG/pubspec.yaml"
name: $NEW_PKG
description: A combined package for all non-Flutter components of wolf 3D
version: 0.0.1
resolution: workspace
environment:
sdk: ^3.11.1
dependencies:
EOF
# Carefully extract dependencies from source packages (ignoring the wolf_3d packages themselves)
touch temp_deps.txt
for pkg in "${PACKAGES[@]}"; do
if [ -f "packages/$pkg/pubspec.yaml" ]; then
# Use awk to extract lines under 'dependencies:' until a new unindented section starts
awk '/^dependencies:/{flag=1; next} /^[a-zA-Z]/{flag=0} flag && /^[ \t]+[a-zA-Z0-9_]+:/ {print}' "packages/$pkg/pubspec.yaml" >> temp_deps.txt
fi
done
# Deduplicate dependencies, filter out the old packages, and append to pubspec.yaml
sort -u temp_deps.txt | grep -v "wolf_3d_" >> "packages/$NEW_PKG/pubspec.yaml" || true
rm temp_deps.txt
cat <<EOF >> "packages/$NEW_PKG/pubspec.yaml"
dev_dependencies:
lints: ^3.0.0
test: ^1.24.0
EOF
# ==========================================
# 3 & 4. Migrate Code, Entry Points & Refactor Internal Imports
# ==========================================
echo "Migrating code and updating imports..."
for pkg in "${PACKAGES[@]}"; do
SUFFIX="${pkg#wolf_3d_}" # Removes the 'wolf_3d_' prefix
# 3.1 & 3.2 Move Internal Code and Entry Point
mkdir -p "packages/$NEW_PKG/lib/src/$SUFFIX"
# Copy instead of move for safety during script execution (we delete originals at the end)
if [ -d "packages/$pkg/lib/src" ] && [ "$(ls -A "packages/$pkg/lib/src")" ]; then
cp -R "packages/$pkg/lib/src/"* "packages/$NEW_PKG/lib/src/$SUFFIX/"
fi
if [ -f "packages/$pkg/lib/$pkg.dart" ]; then
cp "packages/$pkg/lib/$pkg.dart" "packages/$NEW_PKG/lib/$pkg.dart"
# 3.3 Update Entry Point Paths (export 'src/... -> export 'src/suffix/...)
sed -i "s|export 'src/|export 'src/$SUFFIX/|g" "packages/$NEW_PKG/lib/$pkg.dart"
sed -i "s|export \"src/|export \"src/$SUFFIX/|g" "packages/$NEW_PKG/lib/$pkg.dart"
fi
done
# 4.1 & 4.2 Refactor Internal Imports (Run this globally on the new package)
for pkg in "${PACKAGES[@]}"; do
SUFFIX="${pkg#wolf_3d_}"
# Find all Dart files in the new package
find "packages/$NEW_PKG/lib" -name "*.dart" -type f | while read -r file; do
# 1st Pass: Fix deep paths (package:wolf_3d_data/src/... -> package:wolf_3d_dart/src/data/...)
sed -i "s|package:$pkg/src/|package:$NEW_PKG/src/$SUFFIX/|g" "$file"
# 2nd Pass: Fix root imports (package:wolf_3d_data/... -> package:wolf_3d_dart/...)
sed -i "s|package:$pkg/|package:$NEW_PKG/|g" "$file"
done
done
# ==========================================
# 5. Update Dependents (Apps and Other Packages)
# ==========================================
echo "Updating dependent apps and packages..."
for target in "${DEPENDENTS[@]}"; do
if [ -d "$target" ]; then
# 5.1 Update pubspec.yaml Files
if [ -f "$target/pubspec.yaml" ]; then
# Remove old dependencies
for pkg in "${PACKAGES[@]}"; do
sed -i "/^[[:space:]]*$pkg:/d" "$target/pubspec.yaml"
done
# Add new dependency under dependencies block if not present
if ! grep -q "$NEW_PKG:" "$target/pubspec.yaml"; then
sed -i "/^dependencies:/a\\ $NEW_PKG:" "$target/pubspec.yaml"
fi
fi
# 5.2 Update Imports in dependent dart files
for pkg in "${PACKAGES[@]}"; do
SUFFIX="${pkg#wolf_3d_}"
find "$target" -name "*.dart" -type f | while read -r file; do
# Match user requirement: package:wolf_3d_<suffix>/wolf_3d_<suffix>.dart -> package:wolf_3d_dart/wolf_3d_<suffix>.dart
sed -i "s|package:$pkg/$pkg.dart|package:$NEW_PKG/$pkg.dart|g" "$file"
# Catch any stray deep imports just in case
sed -i "s|package:$pkg/src/|package:$NEW_PKG/src/$SUFFIX/|g" "$file"
sed -i "s|package:$pkg/|package:$NEW_PKG/|g" "$file"
done
done
fi
done
# ==========================================
# 6. Cleanup and Validation
# ==========================================
echo "Cleaning up old packages..."
for pkg in "${PACKAGES[@]}"; do
rm -rf "packages/$pkg"
done
echo "Running workspace refresh and tests..."
dart pub get || echo "⚠️ 'dart pub get' failed. Check your new pubspec.yaml."
dart test || echo "⚠️ Tests failed. Please review the output."
echo "Migration complete!"