Skip to content

Commit 1524842

Browse files
chore: fix setup
1 parent ae20681 commit 1524842

1 file changed

Lines changed: 57 additions & 98 deletions

File tree

setup.ps1

Lines changed: 57 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -25,136 +25,95 @@ function Get-UserInput {
2525
do {
2626
$input = Read-Host "Enter new value (or press Enter to keep current)"
2727
if ([string]::IsNullOrWhiteSpace($input)) {
28-
if ($required) {
29-
Write-Host "This value is required. Please enter a value."
30-
continue
31-
}
32-
return $default
28+
$input = $default
3329
}
34-
return $input
35-
} while ($required)
30+
if ($required -and [string]::IsNullOrWhiteSpace($input)) {
31+
Write-Host "This value is required. Please enter a value."
32+
continue
33+
}
34+
break
35+
} while ($true)
36+
37+
return $input
3638
}
3739

3840
# Welcome message
3941
Write-Host "Welcome to CodeBlock Dev Kit - SaaS Template Setup!"
4042
Write-Host "This script will help you configure your new SaaS application powered by CodeBlock Dev Kit."
4143
Write-Host "Press Ctrl+C at any time to cancel the setup.`n"
4244

45+
# Read default values from appsettings.json
46+
$defaultSettingsPath = Join-Path $PSScriptRoot "src/2-Clients/AdminPanel/appsettings.json"
47+
$defaultSettings = Get-Content $defaultSettingsPath -Raw | ConvertFrom-Json
48+
4349
# Get solution name (required)
44-
$solutionName = Get-UserInput -prompt "Enter your solution name (this will be used for namespaces, assembly names, etc.)" -default "CanBeYours" -required $true
50+
$solutionName = Get-UserInput -prompt "Enter your solution name (this will be used for namespaces, assembly names, database name, etc.)" -default $defaultSettings.MongoDB.DatabaseName -required $true
4551

4652
# Get application name
47-
$appName = Get-UserInput -prompt "Enter your application name (this will be displayed in UI and Swagger)" -default "Can Be Yours"
53+
$appName = Get-UserInput -prompt "Enter your application name (this will be displayed in UI)" -default $defaultSettings.Application.Default.Name
4854

4955
# Get application URL
50-
$appUrl = Get-UserInput -prompt "Enter your application URL (this will be used for JWT issuer and other settings)" -default "https://localhost"
56+
$appUrl = Get-UserInput -prompt "Enter your application URL" -default $defaultSettings.Application.Default.Url
5157

5258
# Get admin user details
53-
$adminMobile = Get-UserInput -prompt "Enter admin user mobile number" -default "+1234567890"
54-
$adminEmail = Get-UserInput -prompt "Enter admin user email" -default "admin@example.com"
55-
$adminPassword = Get-UserInput -prompt "Enter admin user password" -default "Admin123!"
59+
$adminMobile = Get-UserInput -prompt "Enter admin user mobile number" -default $defaultSettings.Identity.AdminUser.Mobile
60+
$adminEmail = Get-UserInput -prompt "Enter admin user email" -default $defaultSettings.Identity.AdminUser.Email
61+
$adminPassword = Get-UserInput -prompt "Enter admin user password" -default $defaultSettings.Identity.AdminUser.Password
5662

5763
# Generate random keys
5864
$encryptionKey = Get-RandomKey -length 32
5965
$apiKey = Get-RandomKey -length 32
6066
$jwtKey = Get-RandomKey -length 32
6167

68+
# Update all appsettings.json files
69+
Write-Host "Updating appsettings.json files..."
70+
Get-ChildItem -Recurse -Filter "appsettings.json" | ForEach-Object {
71+
$content = Get-Content $_.FullName -Raw
72+
73+
# Update values using string replacement to preserve JSON structure
74+
$content = $content -replace '"Name":\s*"[^"]*"', "`"Name`": `"$appName`""
75+
$content = $content -replace '"Url":\s*"[^"]*"', "`"Url`": `"$appUrl`""
76+
$content = $content -replace '"Title":\s*"[^"]*"', "`"Title`": `"$appName`""
77+
$content = $content -replace '"Issuer":\s*"[^"]*"', "`"Issuer`": `"$appUrl`""
78+
$content = $content -replace '"Mobile":\s*"[^"]*"', "`"Mobile`": `"$adminMobile`""
79+
$content = $content -replace '"Email":\s*"[^"]*"', "`"Email`": `"$adminEmail`""
80+
$content = $content -replace '"Password":\s*"[^"]*"', "`"Password`": `"$adminPassword`""
81+
$content = $content -replace '"EncryptionSymmetricKey":\s*"[^"]*"', "`"EncryptionSymmetricKey`": `"$encryptionKey`""
82+
$content = $content -replace '"ApiKey":\s*"[^"]*"', "`"ApiKey`": `"$apiKey`""
83+
$content = $content -replace '"Key":\s*"[^"]*"', "`"Key`": `"$jwtKey`""
84+
$content = $content -replace '"DatabaseName":\s*"[^"]*"', "`"DatabaseName`": `"$solutionName`""
85+
$content = $content -replace '"Name":\s*"CanBeYours\.[^"]*"', "`"Name`": `"$solutionName.$1`""
86+
$content = $content -replace '"CookieName":\s*"CanBeYours\.[^"]*"', "`"CookieName`": `"$solutionName.$1`""
87+
88+
Set-Content $_.FullName $content
89+
}
90+
6291
# Update solution file
63-
Write-Host "`nUpdating solution file..."
64-
(Get-Content "CanBeYours.sln") -replace "CanBeYours", $solutionName | Set-Content "$solutionName.sln"
65-
Remove-Item "CanBeYours.sln"
92+
Write-Host "Updating solution file..."
93+
Get-ChildItem -Filter "*.sln" | ForEach-Object {
94+
$content = Get-Content $_.FullName
95+
$content = $content -replace "CanBeYours", $solutionName
96+
Set-Content $_.FullName $content
97+
}
6698

67-
# Update all .csproj files
99+
# Update all project files
68100
Write-Host "Updating project files..."
69101
Get-ChildItem -Recurse -Filter "*.csproj" | ForEach-Object {
70102
$content = Get-Content $_.FullName
71-
$content = $content -replace "CanBeYours\.", "$solutionName."
72-
$content | Set-Content $_.FullName
103+
$content = $content -replace "<AssemblyName>CanBeYours\.", "<AssemblyName>$solutionName."
104+
$content = $content -replace "<RootNamespace>CanBeYours\.", "<RootNamespace>$solutionName."
105+
Set-Content $_.FullName $content
73106
}
74107

75-
# Update all .cs, .razor, and .cshtml files
76-
Write-Host "Updating C#, Razor, and Blazor files..."
108+
# Update all source files
109+
Write-Host "Updating source files..."
77110
Get-ChildItem -Recurse -Include "*.cs","*.razor","*.cshtml" | ForEach-Object {
78111
$content = Get-Content $_.FullName
79112
$content = $content -replace "namespace CanBeYours\.", "namespace $solutionName."
80113
$content = $content -replace "using CanBeYours\.", "using $solutionName."
81-
$content = $content -replace "@using CanBeYours\.", "@using $solutionName."
82114
$content = $content -replace "@namespace CanBeYours\.", "@namespace $solutionName."
83-
$content | Set-Content $_.FullName
84-
}
85-
86-
# Update appsettings.json files
87-
Write-Host "Updating appsettings.json files..."
88-
Get-ChildItem -Recurse -Filter "appsettings.json" | ForEach-Object {
89-
$jsonContent = Get-Content $_.FullName -Raw
90-
91-
# Convert string to JSON object (compatible with older PowerShell versions)
92-
$json = $jsonContent | ConvertFrom-Json
93-
94-
# Create a template if the JSON is empty
95-
if ($null -eq $json) {
96-
$json = @{
97-
Application = @{
98-
Default = @{
99-
Name = ""
100-
Url = ""
101-
}
102-
}
103-
Swagger = @{
104-
Title = ""
105-
}
106-
Identity = @{
107-
AdminUser = @{
108-
Mobile = ""
109-
Email = ""
110-
Password = ""
111-
}
112-
}
113-
Security = @{
114-
EncryptionSymmetricKey = ""
115-
}
116-
ApiKey = ""
117-
JwtAuthentication = @{
118-
Key = ""
119-
Issuer = ""
120-
}
121-
Hangfire = @{
122-
MongoStorage = "mongodb://localhost:27017/CanBeYours-Hangfire"
123-
}
124-
Monitoring = @{
125-
Service = @{
126-
Name = "CanBeYours-Monitoring"
127-
}
128-
}
129-
CookieAuthentication = @{
130-
CookieName = "CanBeYours-Auth"
131-
}
132-
Localization = @{
133-
CookieName = "CanBeYours-Language"
134-
}
135-
MongoDB = @{
136-
DatabaseName = "CanBeYours-DB"
137-
}
138-
}
139-
}
140-
141-
# Update values using string replacement to preserve the JSON structure
142-
$jsonContent = $jsonContent -replace '"Name":\s*"[^"]*"', "`"Name`": `"$appName`""
143-
$jsonContent = $jsonContent -replace '"Url":\s*"[^"]*"', "`"Url`": `"$appUrl`""
144-
$jsonContent = $jsonContent -replace '"Title":\s*"[^"]*"', "`"Title`": `"$appName`""
145-
$jsonContent = $jsonContent -replace '"Mobile":\s*"[^"]*"', "`"Mobile`": `"$adminMobile`""
146-
$jsonContent = $jsonContent -replace '"Email":\s*"[^"]*"', "`"Email`": `"$adminEmail`""
147-
$jsonContent = $jsonContent -replace '"Password":\s*"[^"]*"', "`"Password`": `"$adminPassword`""
148-
$jsonContent = $jsonContent -replace '"EncryptionSymmetricKey":\s*"[^"]*"', "`"EncryptionSymmetricKey`": `"$encryptionKey`""
149-
$jsonContent = $jsonContent -replace '"ApiKey":\s*"[^"]*"', "`"ApiKey`": `"$apiKey`""
150-
$jsonContent = $jsonContent -replace '"Key":\s*"[^"]*"', "`"Key`": `"$jwtKey`""
151-
$jsonContent = $jsonContent -replace '"Issuer":\s*"[^"]*"', "`"Issuer`": `"$appUrl`""
152-
153-
# Replace solution name in various settings
154-
$jsonContent = $jsonContent -replace "CanBeYours-", "$solutionName-"
155-
156-
# Save the updated JSON
157-
$jsonContent | Set-Content $_.FullName -NoNewline
115+
$content = $content -replace "@using CanBeYours\.", "@using $solutionName."
116+
Set-Content $_.FullName $content
158117
}
159118

160119
Write-Host "`nSetup completed successfully!"
@@ -163,4 +122,4 @@ Write-Host "Solution Name: $solutionName"
163122
Write-Host "Application Name: $appName"
164123
Write-Host "Application URL: $appUrl"
165124
Write-Host "Admin Email: $adminEmail"
166-
Write-Host "`nYou can now open the solution in your IDE and start developing with CodeBlock Dev Kit!"
125+
Write-Host "`nYou can now build and run your application."

0 commit comments

Comments
 (0)