Skip to content

Commit 9091992

Browse files
committed
[internal] Improve script by checking edge cases-
1 parent d48f320 commit 9091992

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

scripts/rename_project.sh

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/bin/bash
22

33
current_project_name="angular-boilerplate"
4-
current_git_provider="github.com"
5-
current_git_provider_username="juanmesa2097"
4+
current_git_provider="github.com/juanmesa2097"
5+
current_git_username="juanmesa2097"
66

77
# Function to find files recursively
88
find_files() {
@@ -13,33 +13,40 @@ find_files() {
1313

1414
# Function to prompt for new project name
1515
prompt_project_name() {
16-
read -rp "▶ Enter new project name: " project_name
16+
read -rp "▶ Enter new project name: [current: $current_project_name] " project_name
17+
project_name=${project_name:-"$current_project_name"}
1718
echo "$project_name"
1819
}
1920

20-
# Function to prompt for Git provider selection (Default: github.com)
21+
# Function to prompt for Git provider
2122
prompt_git_provider() {
22-
read -rp "▶ Enter Git provider domain : [github.com] " git_provider
23-
git_provider=${git_provider:-github.com}
23+
read -rp "▶ Enter Git provider: [current: $current_git_provider] " git_provider
24+
git_provider=${git_provider:-"$current_git_provider"}
2425
echo "$git_provider"
2526
}
2627

27-
# Function to prompt for username
28-
prompt_git_provider_username() {
29-
read -rp "▶ Enter the git provider username: " git_provider_username
30-
echo "$git_provider_username"
28+
# Function to prompt for Git username
29+
prompt_git_username() {
30+
read -rp "▶ Enter Git username: [current: $current_git_username] " git_username
31+
git_username=${git_username:-"$current_git_username"}
32+
echo "$git_username"
3133
}
3234

3335
# Function to prompt for confirmation (Default: n)
3436
prompt_confirmation() {
3537
local project_name=$1
3638
local git_provider=$2
37-
local git_provider_username=$3
39+
local git_username=$3
40+
41+
if [[ "$project_name" == "$current_project_name" && "$git_provider" == "$current_git_provider" && "$git_username" == "$current_git_username" ]]; then
42+
echo "The parameters remained unchanged. No renaming and text replacement required." >/dev/tty
43+
exit 0
44+
fi
3845

3946
echo "You are about to rename the project with the following parameters:" >/dev/tty
4047
echo "Project name:" "$project_name" >/dev/tty
4148
echo "Git provider:" "$git_provider" >/dev/tty
42-
echo "Git provider username:" "$git_provider_username" >/dev/tty
49+
echo "Git username:" "$git_username" >/dev/tty
4350
read -rp "▶ Are you sure you want to continue? (y/n): [n] " confirm
4451
confirm=${confirm:-n}
4552
echo "$confirm"
@@ -50,20 +57,20 @@ process_files() {
5057
local files=$1
5158
local project_name=$2
5259
local git_provider=$3
53-
local git_provider_username=$4
60+
local git_username=$4
5461

5562
for file in $files; do
5663
sed -i \
57-
-e "s/$current_project_name/$project_name/g" \
58-
-e "s/$current_git_provider/$git_provider/g" \
59-
-e "s/$current_git_provider_username/$git_provider_username/g" \
64+
-e "s|$current_git_provider|$git_provider|g" \
65+
-e "s|$current_git_username|$git_username|g" \
66+
-e "s|$current_project_name|$project_name|g" \
6067
"$file"
6168
done
6269

6370
echo
6471
echo "✅ Project renamed to $project_name"
6572
echo "✅ Git provider: $git_provider"
66-
echo "✅ Git provider username: $git_provider_username"
73+
echo "✅ Git username: $git_username"
6774
}
6875

6976
# Main script logic
@@ -76,12 +83,13 @@ echo
7683

7784
project_name=$(prompt_project_name)
7885
git_provider=$(prompt_git_provider)
79-
git_provider_username=$(prompt_git_provider_username)
80-
confirm=$(prompt_confirmation "$project_name" "$git_provider" "$git_provider_username")
86+
git_username=$(prompt_git_username)
87+
88+
confirm=$(prompt_confirmation "$project_name" "$git_provider" "$git_username")
8189

8290
# Process files if confirmed
8391
if [ "$confirm" == "y" ] || [ "$confirm" == "Y" ]; then
84-
process_files "$files" "$project_name" "$git_provider" "$git_provider_username"
92+
process_files "$files" "$project_name" "$git_provider" "$git_username"
8593
else
8694
echo "❌ Renaming and text replacement canceled."
8795
fi

0 commit comments

Comments
 (0)