-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinit.sh
More file actions
executable file
·171 lines (139 loc) · 5.99 KB
/
init.sh
File metadata and controls
executable file
·171 lines (139 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
# Exit on error
set -e
echo "--------------------------------------------------"
echo " Web API Template Initialization Script "
echo "--------------------------------------------------"
# 1. Get Project Name
read -p "Enter new project name (e.g. MyAwesomeApi): " NEW_NAME
if [ -z "$NEW_NAME" ]; then
echo "Error: Project name cannot be empty."
exit 1
fi
# 2. Get Base Port
DEFAULT_BASE_PORT=13000
read -p "Enter base port for Docker services (default $DEFAULT_BASE_PORT): " BASE_PORT
BASE_PORT=${BASE_PORT:-$DEFAULT_BASE_PORT}
API_PORT=$((BASE_PORT + 2))
DB_PORT=$((BASE_PORT + 4))
echo "--------------------------------------------------"
echo "Configuration:"
echo " Project Name: $NEW_NAME"
echo " API Port: $API_PORT"
echo " DB Port: $DB_PORT"
echo "--------------------------------------------------"
read -p "Proceed with initialization? (y/n): " CONFIRM
if [[ "$CONFIRM" != "y" && "$CONFIRM" != "Y" ]]; then
echo "Aborted."
exit 0
fi
# 3. Update Docker Ports
echo "Updating Docker ports..."
# We use a simple sed here assuming the structure of docker-compose.local.yml is known and consistent
# API Port: "13002:8080" -> "$API_PORT:8080"
# DB Port: "13004:5432" -> "$DB_PORT:5432"
OS=$(uname)
if [ "$OS" = "Darwin" ]; then
sed -i '' "s/13002:8080/$API_PORT:8080/g" docker-compose.local.yml
sed -i '' "s/13004:5432/$DB_PORT:5432/g" docker-compose.local.yml
# Update appsettings.Development.json (DB Port)
sed -i '' "s/Port=13004/Port=$DB_PORT/g" src/MyProject.WebApi/appsettings.Development.json
# Update http-client.env.json (API Port)
sed -i '' "s/localhost:13002/localhost:$API_PORT/g" src/MyProject.WebApi/http-client.env.json
else
sed -i "s/13002:8080/$API_PORT:8080/g" docker-compose.local.yml
sed -i "s/13004:5432/$DB_PORT:5432/g" docker-compose.local.yml
# Update appsettings.Development.json (DB Port)
sed -i "s/Port=13004/Port=$DB_PORT/g" src/MyProject.WebApi/appsettings.Development.json
# Update http-client.env.json (API Port)
sed -i "s/localhost:13002/localhost:$API_PORT/g" src/MyProject.WebApi/http-client.env.json
fi
# 4. Rename Project
OLD_NAME="MyProject"
OLD_NAME_LOWER="myproject"
NEW_NAME_LOWER=$(echo "$NEW_NAME" | tr '[:upper:]' '[:lower:]')
echo "Renaming project from '$OLD_NAME' to '$NEW_NAME'..."
# Function to replace text in files
replace_text() {
local search=$1
local replace=$2
if [ "$OS" = "Darwin" ]; then
grep -rIl --null "$search" . --exclude-dir=.git --exclude-dir=bin --exclude-dir=obj | xargs -0 sed -i '' "s/$search/$replace/g"
else
grep -rIl --null "$search" . --exclude-dir=.git --exclude-dir=bin --exclude-dir=obj | xargs -0 sed -i "s/$search/$replace/g"
fi
}
echo "Replacing text content..."
replace_text "$OLD_NAME" "$NEW_NAME"
replace_text "$OLD_NAME_LOWER" "$NEW_NAME_LOWER"
echo "Renaming files and directories..."
find . -depth -name "*$OLD_NAME*" -not -path "./.git/*" -not -path "./bin/*" -not -path "./obj/*" | while IFS= read -r path; do
dir=$(dirname "$path")
filename=$(basename "$path")
new_filename=$(echo "$filename" | sed "s/$OLD_NAME/$NEW_NAME/g")
mv "$path" "$dir/$new_filename"
echo "Renamed: $path -> $dir/$new_filename"
done
find . -depth -name "*$OLD_NAME_LOWER*" -not -path "./.git/*" -not -path "./bin/*" -not -path "./obj/*" | while IFS= read -r path; do
dir=$(dirname "$path")
filename=$(basename "$path")
new_filename=$(echo "$filename" | sed "s/$OLD_NAME_LOWER/$NEW_NAME_LOWER/g")
mv "$path" "$dir/$new_filename"
echo "Renamed: $path -> $dir/$new_filename"
done
# 5. Git Commit (Rename)
echo "--------------------------------------------------"
read -p "Do you want to commit the project rename changes? (y/n): " GIT_RENAME_CONFIRM
if [[ "$GIT_RENAME_CONFIRM" == "y" || "$GIT_RENAME_CONFIRM" == "Y" ]]; then
git add .
git commit -m "Renamed project from $OLD_NAME to $NEW_NAME"
echo "Changes committed."
fi
# 6. Migrations
echo "--------------------------------------------------"
read -p "Do you want to reset and create a fresh Initial Migration? (y/n): " MIGRATION_CONFIRM
if [[ "$MIGRATION_CONFIRM" == "y" || "$MIGRATION_CONFIRM" == "Y" ]]; then
echo "Resetting migrations..."
MIGRATION_DIR="src/$NEW_NAME.Infrastructure/Features/Postgres/Migrations"
if [ -d "$MIGRATION_DIR" ]; then
echo "Removing existing migrations in $MIGRATION_DIR..."
rm -rf "$MIGRATION_DIR"/*
else
echo "Migration directory not found, creating..."
mkdir -p "$MIGRATION_DIR"
fi
echo "Building project and adding Initial migration..."
# Restore local tools
echo "Restoring local tools..."
dotnet tool restore
# Restore and build explicitly
echo "Restoring dependencies..."
dotnet restore "src/$NEW_NAME.WebApi"
echo "Building project..."
dotnet build "src/$NEW_NAME.WebApi" --no-restore
echo "Running migrations..."
dotnet ef migrations add Initial \
--project "src/$NEW_NAME.Infrastructure" \
--startup-project "src/$NEW_NAME.WebApi" \
--output-dir Features/Postgres/Migrations \
--no-build
echo "Migration 'Initial' created successfully."
# 7. Git Commit (Migration)
echo "--------------------------------------------------"
read -p "Do you want to commit the initial migration? (y/n): " GIT_MIGRATION_CONFIRM
if [[ "$GIT_MIGRATION_CONFIRM" == "y" || "$GIT_MIGRATION_CONFIRM" == "Y" ]]; then
git add .
git commit -m "Add initial migration"
echo "Migration changes committed."
fi
fi
echo "--------------------------------------------------"
echo "Initialization complete!"
read -p "Do you want to start the application now (docker compose up)? (y/n): " DOCKER_CONFIRM
if [[ "$DOCKER_CONFIRM" == "y" || "$DOCKER_CONFIRM" == "Y" ]]; then
echo "Starting application..."
docker compose -f docker-compose.local.yml up -d --build
else
echo "You can run the application later with:"
echo "docker compose -f docker-compose.local.yml up -d --build"
fi