-
Notifications
You must be signed in to change notification settings - Fork 5
/
Utilities.psm1
78 lines (58 loc) · 2.05 KB
/
Utilities.psm1
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
# Utilities
<#
.DESCRIPTION
Returns the web application folder path from the workspace root
#>
function Get-WebProjectPath {
param(
[string] $WorkspaceFolder = ".."
)
return Join-Path $WorkspaceFolder "src/Kentico.Community.Portal.Web"
}
<#
.DESCRIPTION
Gets the database connection string from the user secrets or appsettings.json file
#>
function Get-ConnectionString {
param(
[string] $WorkspaceFolder = ".."
)
$projectPath = Get-WebProjectPath ($WorkspaceFolder)
# Try to get the connection string from user secrets first
Write-Host "Checking for a connection string user secrets for project: $projectPath"
$connectionString = dotnet user-secrets list --project $projectPath `
| Select-String -Pattern "ConnectionStrings:" `
| ForEach-Object { $_.Line -replace '^ConnectionStrings:CMSConnectionString \= ', '' }
if (-not [string]::IsNullOrEmpty($connectionString)) {
Write-Host 'Using ConnectionString from user-secrets'
return $connectionString
}
$appSettingFileName = $Env:ASPNETCORE_ENVIRONMENT -eq "CI" ? 'appsettings.CI.json' : 'appsettings.json'
$jsonFilePath = Join-Path $projectPath $appSettingFileName
Write-Host "Using settings from $jsonFilePath"
if (!(Test-Path $jsonFilePath)) {
throw "Could not find file $jsonFilePath"
}
$appSettingsJson = Get-Content $jsonFilePath | Out-String | ConvertFrom-Json
$connectionString = $appSettingsJson.ConnectionStrings.CMSConnectionString;
if (!$connectionString) {
throw "Connection string not found in $jsonFilePath"
}
return $connectionString;
}
<#
.DESCRIPTION
Ensures the expression successfully exits and throws an exception
with the failed expression if it does not.
#>
function Invoke-ExpressionWithException {
param(
[string]$expression
)
Write-Host "$expression"
Invoke-Expression -Command $expression
if ($LASTEXITCODE -ne 0) {
$errorMessage = "[ $expression ] failed`n`n"
throw $errorMessage
}
}