Skip to content

Commit 792d0cf

Browse files
committed
Fix renaming script
1 parent 278a44b commit 792d0cf

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
# Define the old and new package names with escaped periods
4+
OLD_PACKAGE="io.github.xilinjia.krdb"
5+
NEW_PACKAGE="io.realm.kotlin"
6+
OLD_PACKAGE_PATH="io/github/xilinjia/krdb"
7+
NEW_PACKAGE_PATH="io/realm/kotlin"
8+
9+
# Escape the periods for sed replacement
10+
ESCAPED_OLD_PACKAGE=$(echo "$OLD_PACKAGE" | sed 's/\./\\./g')
11+
ESCAPED_NEW_PACKAGE=$(echo "$NEW_PACKAGE" | sed 's/\./\\./g')
12+
13+
ESCAPED_OLD_PACKAGE_PATH=$(echo "$OLD_PACKAGE_PATH" | sed 's/\//\\\//g')
14+
ESCAPED_NEW_PACKAGE_PATH=$(echo "$NEW_PACKAGE_PATH" | sed 's/\//\\\//g')
15+
16+
echo "🔍 Searching for references to $OLD_PACKAGE and replacing with $NEW_PACKAGE in all files..."
17+
18+
# 1️⃣ Replace package references in relevant source files, including .json, .ir, .hpp, .cpp, .kts, etc.
19+
find . -type f \( -name "*.kt" -o -name "*.java" -o -name "*.xml" -o -name "*.json" -o -name "*.ir" -o -name "build.gradle*" -o -name "settings.gradle*" -o -name "*.pro" -o -name "*.hpp" -o -name "*.cpp" -o -name "*.kts" -o -name "realm.i" -o -name "collect_metrics.sh" -o -name "info.md" \) \
20+
-exec sed -i.bak "s/$ESCAPED_OLD_PACKAGE/$ESCAPED_NEW_PACKAGE/g" {} +
21+
22+
# Remove backup files created by macOS `sed`
23+
find . -type f -name "*.bak" -exec rm {} +
24+
25+
echo "✅ Package references updated."
26+
27+
# 2️⃣ Rename directories across all Kotlin Multiplatform (KMP) source sets
28+
echo "📂 Renaming package directories in all source sets..."
29+
30+
find . -type d -path "*/src/*/kotlin/$OLD_PACKAGE_PATH" | while read -r dir; do
31+
NEW_DIR=$(echo "$dir" | sed "s|$OLD_PACKAGE_PATH|$NEW_PACKAGE_PATH|g")
32+
33+
# Ensure the new directory exists
34+
mkdir -p "$NEW_DIR"
35+
36+
# Move all files from the old package directory to the new one
37+
git mv "$dir"/* "$NEW_DIR/" 2>/dev/null
38+
39+
echo "✅ Renamed: $dir$NEW_DIR"
40+
done
41+
42+
# 3️⃣ Remove any remaining empty io/realm directories
43+
echo "🗑️ Cleaning up empty directories..."
44+
find . -type d -empty -path "*/io/realm*" -exec rmdir {} +
45+
46+
# 4️⃣ (Optional) Verify and cleanup files after renaming, such as .json, .ir, .hpp, .cpp files
47+
echo "🔍 Verifying changes in .json, .ir, .hpp, .cpp, .kts files..."
48+
find . -type f \( -name "*.json" -o -name "*.ir" -o -name "*.hpp" -o -name "*.cpp" -o -name "*.kts" -o -name "realm.i" -o -name "collect_metrics.sh" -o -name "info.md" \) -exec grep -l "$OLD_PACKAGE" {} \; | while read -r file; do
49+
echo "✅ Updating $file"
50+
# Replace both old package and old path references
51+
sed -i '' "s/$ESCAPED_OLD_PACKAGE/$ESCAPED_NEW_PACKAGE/g" "$file"
52+
sed -i '' "s/$ESCAPED_OLD_PACKAGE_PATH/$ESCAPED_NEW_PACKAGE_PATH/g" "$file" # Also replace package path in .hpp/.cpp/.json/.ir/.kts etc.
53+
done
54+
55+
# 5️⃣ Running Gradle sync to make sure everything is fine
56+
#echo "🔄 Running Gradle sync..."
57+
#./gradlew clean
58+
59+
echo "🚀 Done! Your project is now using $NEW_PACKAGE, and old directories are removed."

0 commit comments

Comments
 (0)