Skip to content

Commit 6b3705c

Browse files
chore: add validations to setup
1 parent aee0428 commit 6b3705c

1 file changed

Lines changed: 112 additions & 28 deletions

File tree

setup/setup.ps1

Lines changed: 112 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,57 @@
22
# Cross-platform setup script for SaaS template. Run with: pwsh setup.ps1
33
# On Windows, double-click setup.bat. On Linux/macOS, double-click setup.sh (or run chmod +x setup.ps1 and ./setup.ps1)
44

5+
# Validation functions
6+
function Test-SolutionName {
7+
param ([string]$name)
8+
if ($name -match '^[a-zA-Z][a-zA-Z0-9_]*$') {
9+
return $true
10+
}
11+
Write-Host "Invalid solution name. It must:" -ForegroundColor Red
12+
Write-Host "- Start with a letter" -ForegroundColor Red
13+
Write-Host "- Contain only letters, numbers, and underscores" -ForegroundColor Red
14+
return $false
15+
}
16+
17+
function Test-Password {
18+
param ([string]$password)
19+
if ($password.Length -ge 8) {
20+
return $true
21+
}
22+
Write-Host "Password must be at least 8 characters long (you can use more)." -ForegroundColor Red
23+
return $false
24+
}
25+
26+
function Test-Email {
27+
param ([string]$email)
28+
if ($email -match '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$') {
29+
return $true
30+
}
31+
Write-Host "Please enter a valid email address." -ForegroundColor Red
32+
return $false
33+
}
34+
35+
function Test-Url {
36+
param ([string]$url)
37+
if ($url -match '^https?://[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}[a-zA-Z0-9./_-]*$') {
38+
return $true
39+
}
40+
Write-Host "Please enter a valid URL (e.g., https://example.com)." -ForegroundColor Red
41+
return $false
42+
}
43+
44+
function Test-Mobile {
45+
param ([string]$mobile)
46+
# Allows international format with optional + prefix, country code, and 6-15 digits
47+
if ($mobile -match '^\+?[1-9]\d{1,3}[0-9]{6,14}$') {
48+
return $true
49+
}
50+
Write-Host "Please enter a valid mobile number in international format (e.g., +1234567890)." -ForegroundColor Red
51+
Write-Host "- Country code (optional + and 1-4 digits)" -ForegroundColor Red
52+
Write-Host "- Phone number (6-15 digits)" -ForegroundColor Red
53+
return $false
54+
}
55+
556
# Function to generate random string
657
function Get-RandomKey {
758
param (
@@ -11,34 +62,62 @@ function Get-RandomKey {
1162
return -join ((1..$length) | ForEach-Object { $chars[(Get-Random -Maximum $chars.Length)] })
1263
}
1364

14-
# Function to prompt for input with default value
65+
# Function to prompt for input with default value and validation
1566
function Get-UserInput {
1667
param (
1768
[string]$prompt,
1869
[string]$default,
19-
[bool]$required = $false
70+
[bool]$required = $false,
71+
[ValidateSet('text', 'solution', 'password', 'email', 'url', 'mobile')]
72+
[string]$validationType = 'text'
2073
)
2174

22-
Write-Host "`n$prompt"
23-
Write-Host "Current value: $default"
75+
Write-Host "`n$prompt" -ForegroundColor Cyan
76+
Write-Host "Current value: " -NoNewline
77+
Write-Host $default -ForegroundColor Yellow
2478

2579
do {
26-
$input = Read-Host "Enter new value (or press Enter to keep current)"
80+
Write-Host "Enter new value (or press Enter to keep current): " -NoNewline -ForegroundColor Gray
81+
if ($validationType -eq 'password') {
82+
$input = Read-Host -AsSecureString
83+
$input = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($input))
84+
} else {
85+
$input = Read-Host
86+
}
87+
2788
if ([string]::IsNullOrWhiteSpace($input)) {
2889
$input = $default
2990
}
91+
92+
# Skip validation if using default value and not required
93+
if ($input -eq $default -and -not $required) {
94+
return $input
95+
}
96+
97+
# Validate input based on type
98+
$isValid = switch ($validationType) {
99+
'solution' { Test-SolutionName $input }
100+
'password' { Test-Password $input }
101+
'email' { Test-Email $input }
102+
'url' { Test-Url $input }
103+
'mobile' { Test-Mobile $input }
104+
default { $true }
105+
}
106+
107+
if ($isValid) {
108+
if (-not $required -or -not [string]::IsNullOrWhiteSpace($input)) {
109+
return $input
110+
}
111+
}
112+
30113
if ($required -and [string]::IsNullOrWhiteSpace($input)) {
31-
Write-Host "This value is required. Please enter a value."
32-
continue
114+
Write-Host "This value is required. Please enter a value." -ForegroundColor Red
33115
}
34-
break
35116
} while ($true)
36-
37-
return $input
38117
}
39118

40119
# Welcome message
41-
Write-Host "Welcome to CodeBlock Dev Kit - SaaS Template Setup!"
120+
Write-Host "`n=== CodeBlock Dev Kit - SaaS Template Setup ===" -ForegroundColor Green
42121
Write-Host "This script will help you configure your new SaaS application powered by CodeBlock Dev Kit."
43122
Write-Host "Press Ctrl+C at any time to cancel the setup.`n"
44123

@@ -50,18 +129,18 @@ $defaultSettingsPath = Join-Path $rootDir "src/2-Clients/AdminPanel/appsettings.
50129
$defaultSettings = Get-Content $defaultSettingsPath -Raw | ConvertFrom-Json
51130

52131
# Get solution name (required)
53-
$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
132+
$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 -validationType 'solution'
54133

55134
# Get application name
56-
$appName = Get-UserInput -prompt "Enter your application name (this will be displayed in UI)" -default $defaultSettings.Application.Default.Name
135+
$appName = Get-UserInput -prompt "Enter your application name (this will be displayed in UI)" -default $defaultSettings.Application.Default.Name -validationType 'text'
57136

58137
# Get application URL
59-
$appUrl = Get-UserInput -prompt "Enter your application URL" -default $defaultSettings.Application.Default.Url
138+
$appUrl = Get-UserInput -prompt "Enter your application URL" -default $defaultSettings.Application.Default.Url -validationType 'url'
60139

61140
# Get admin user details
62-
$adminMobile = Get-UserInput -prompt "Enter admin user mobile number" -default $defaultSettings.Identity.AdminUser.Mobile
63-
$adminEmail = Get-UserInput -prompt "Enter admin user email" -default $defaultSettings.Identity.AdminUser.Email
64-
$adminPassword = Get-UserInput -prompt "Enter admin user password" -default $defaultSettings.Identity.AdminUser.Password
141+
$adminMobile = Get-UserInput -prompt "Enter admin user mobile number" -default $defaultSettings.Identity.AdminUser.Mobile -validationType 'mobile'
142+
$adminEmail = Get-UserInput -prompt "Enter admin user email" -default $defaultSettings.Identity.AdminUser.Email -validationType 'email'
143+
$adminPassword = Get-UserInput -prompt "Enter admin user password" -default $defaultSettings.Identity.AdminUser.Password -validationType 'password'
65144

66145
# Generate random keys
67146
$encryptionKey = Get-RandomKey -length 24 # Fixed length for EncryptionSymmetricKey
@@ -73,7 +152,7 @@ Push-Location $rootDir
73152

74153
try {
75154
# Update all appsettings.json files
76-
Write-Host "Updating appsettings.json files..."
155+
Write-Host "`nUpdating appsettings.json files..." -ForegroundColor Blue
77156
Get-ChildItem -Recurse -Filter "appsettings.json" | ForEach-Object {
78157
$content = Get-Content $_.FullName -Raw
79158

@@ -105,7 +184,7 @@ try {
105184
}
106185

107186
# Update solution file
108-
Write-Host "Updating solution file..."
187+
Write-Host "`nUpdating solution file..." -ForegroundColor Blue
109188
Get-ChildItem -Filter "*.sln" | ForEach-Object {
110189
$oldName = $_.Name
111190
$newName = $_.Name -replace "CanBeYours", $solutionName
@@ -121,7 +200,7 @@ try {
121200
}
122201

123202
# Update all project files
124-
Write-Host "Updating project files..."
203+
Write-Host "`nUpdating project files..." -ForegroundColor Blue
125204
Get-ChildItem -Recurse -Filter "*.csproj" | ForEach-Object {
126205
$content = Get-Content $_.FullName
127206
$content = $content -replace "<AssemblyName>CanBeYours\.", "<AssemblyName>$solutionName."
@@ -130,7 +209,7 @@ try {
130209
}
131210

132211
# Update all source files
133-
Write-Host "Updating source files..."
212+
Write-Host "`nUpdating source files..." -ForegroundColor Blue
134213
Get-ChildItem -Recurse -Include "*.cs","*.razor","*.cshtml" | ForEach-Object {
135214
$content = Get-Content $_.FullName
136215
$content = $content -replace "namespace CanBeYours\.", "namespace $solutionName."
@@ -145,10 +224,15 @@ finally {
145224
Pop-Location
146225
}
147226

148-
Write-Host "`nSetup completed successfully!"
149-
Write-Host "Your CodeBlock Dev Kit SaaS application has been configured with the following settings:"
150-
Write-Host "Solution Name: $solutionName"
151-
Write-Host "Application Name: $appName"
152-
Write-Host "Application URL: $appUrl"
153-
Write-Host "Admin Email: $adminEmail"
154-
Write-Host "`nYou can now build and run your application."
227+
Write-Host "`nSetup completed successfully!" -ForegroundColor Green
228+
Write-Host "`nYour CodeBlock Dev Kit SaaS application has been configured with the following settings:" -ForegroundColor Blue
229+
Write-Host "Solution Name: " -NoNewline
230+
Write-Host $solutionName -ForegroundColor Yellow
231+
Write-Host "Application Name: " -NoNewline
232+
Write-Host $appName -ForegroundColor Yellow
233+
Write-Host "Application URL: " -NoNewline
234+
Write-Host $appUrl -ForegroundColor Yellow
235+
Write-Host "Admin Email: " -NoNewline
236+
Write-Host $adminEmail -ForegroundColor Yellow
237+
238+
Write-Host "`nYou can now build and run your application." -ForegroundColor Green

0 commit comments

Comments
 (0)