-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·146 lines (122 loc) · 4.84 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·146 lines (122 loc) · 4.84 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
#!/usr/bin/env bash
# GitHub Template Setup Script
# This script replaces {{NAMESPACE}} placeholders with your actual namespace
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${GREEN}=== GitHub Template Setup ===${NC}"
echo ""
# Check if setup has already been completed (NO-OP check)
# If {{NAMESPACE}} folders don't exist, check if setup was already done
if [ ! -d "{{NAMESPACE}}" ] && [ ! -d "{{NAMESPACE}}.Tests" ]; then
# Look for .csproj files that don't contain {{NAMESPACE}} (indicating setup was done)
EXISTING_PROJECTS=$(find . -maxdepth 2 -name "*.csproj" ! -path "./.git/*" ! -name "*{{NAMESPACE}}*" 2>/dev/null | head -1)
if [ -n "$EXISTING_PROJECTS" ]; then
echo -e "${GREEN}✓ Setup has already been completed (likely by GitHub Actions)${NC}"
echo -e "${YELLOW}This script is a NO-OP. Your project is ready to use!${NC}"
echo ""
echo "Found existing project files:"
echo "$EXISTING_PROJECTS" | sed 's/^/ - /'
echo ""
exit 0
fi
echo -e "${RED}Error: Template folders not found. Make sure you're running this from the repository root.${NC}"
exit 1
fi
# Prompt for namespace
if [ -z "$1" ]; then
echo -e "${YELLOW}Enter your namespace (e.g., my.service or MyService):${NC}"
read -r NAMESPACE
else
NAMESPACE="$1"
fi
if [ -z "$NAMESPACE" ]; then
echo -e "${RED}Error: Namespace cannot be empty${NC}"
exit 1
fi
# Validate namespace (basic check - should be valid C# namespace)
if [[ ! "$NAMESPACE" =~ ^[a-zA-Z][a-zA-Z0-9._]*$ ]]; then
echo -e "${RED}Error: Invalid namespace format. Use alphanumeric characters, dots, and underscores.${NC}"
exit 1
fi
echo -e "${GREEN}Using namespace: ${NAMESPACE}${NC}"
echo ""
# Function to replace in file
replace_in_file() {
local file="$1"
if [ -f "$file" ]; then
# Use sed with different syntax for macOS vs Linux
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "s/{{NAMESPACE}}/${NAMESPACE}/g" "$file"
else
sed -i "s/{{NAMESPACE}}/${NAMESPACE}/g" "$file"
fi
fi
}
# Function to replace in all files recursively
replace_in_all_files() {
local dir="$1"
if [ ! -d "$dir" ]; then
echo -e "${YELLOW}Warning: Directory $dir does not exist, skipping...${NC}"
return
fi
# Find all files and replace placeholders
find "$dir" -type f \( -name "*.cs" -o -name "*.csproj" -o -name "*.sh" -o -name "Dockerfile" -o -name "*.md" -o -name "*.yml" -o -name "*.yaml" \) | while read -r file; do
replace_in_file "$file"
done
}
echo -e "${YELLOW}Replacing placeholders in files...${NC}"
# Replace in all files BEFORE renaming folders
replace_in_all_files "{{NAMESPACE}}"
replace_in_all_files "{{NAMESPACE}}.Tests"
replace_in_file "run.sh"
replace_in_file "README.md"
replace_in_file "setup.sh"
echo -e "${GREEN}✓ Placeholders replaced${NC}"
# Verify replacements worked
REMAINING_PLACEHOLDERS=$(grep -r "{{NAMESPACE}}" "{{NAMESPACE}}" "{{NAMESPACE}}.Tests" 2>/dev/null | wc -l | tr -d ' ' || echo "0")
if [ "$REMAINING_PLACEHOLDERS" -gt 0 ]; then
echo -e "${YELLOW}Warning: Found $REMAINING_PLACEHOLDERS remaining placeholders after replacement${NC}"
fi
# Rename .csproj files first (before renaming folders)
echo -e "${YELLOW}Renaming project files...${NC}"
if [ -f "{{NAMESPACE}}/{{NAMESPACE}}.csproj" ]; then
mv "{{NAMESPACE}}/{{NAMESPACE}}.csproj" "{{NAMESPACE}}/${NAMESPACE}.csproj"
echo -e "${GREEN}✓ Renamed {{NAMESPACE}}/{{NAMESPACE}}.csproj to ${NAMESPACE}.csproj${NC}"
fi
if [ -f "{{NAMESPACE}}.Tests/{{NAMESPACE}}.Tests.csproj" ]; then
mv "{{NAMESPACE}}.Tests/{{NAMESPACE}}.Tests.csproj" "{{NAMESPACE}}.Tests/${NAMESPACE}.Tests.csproj"
echo -e "${GREEN}✓ Renamed {{NAMESPACE}}.Tests/{{NAMESPACE}}.Tests.csproj to ${NAMESPACE}.Tests.csproj${NC}"
fi
# Rename folders
echo -e "${YELLOW}Renaming folders...${NC}"
if [ -d "{{NAMESPACE}}" ]; then
mv "{{NAMESPACE}}" "${NAMESPACE}"
echo -e "${GREEN}✓ Renamed {{NAMESPACE}} to ${NAMESPACE}${NC}"
fi
if [ -d "{{NAMESPACE}}.Tests" ]; then
mv "{{NAMESPACE}}.Tests" "${NAMESPACE}.Tests"
echo -e "${GREEN}✓ Renamed {{NAMESPACE}}.Tests to ${NAMESPACE}.Tests${NC}"
fi
# Update run.sh to use correct path
if [ -f "run.sh" ]; then
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "s/cd {{NAMESPACE}}/cd ${NAMESPACE}/g" "run.sh"
else
sed -i "s/cd {{NAMESPACE}}/cd ${NAMESPACE}/g" "run.sh"
fi
fi
echo ""
echo -e "${GREEN}=== Setup Complete! ===${NC}"
echo ""
echo -e "Your project has been configured with namespace: ${GREEN}${NAMESPACE}${NC}"
echo ""
echo "Next steps:"
echo " 1. Review the changes"
echo " 2. Run 'dotnet restore' in both project folders"
echo " 3. Run 'dotnet build' to verify everything compiles"
echo " 4. Update README.md with your project-specific information"
echo ""