From ee1786edf3896aab63628d0f6f14bc47e3c7ea3f Mon Sep 17 00:00:00 2001 From: Martin Kyjac Date: Fri, 18 Oct 2024 13:04:37 +0200 Subject: [PATCH] Add Utilities.psm1 --- .github/workflows/ci.yml | 4 + scripts/Utilities/Utilities.psm1 | 139 +++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 scripts/Utilities/Utilities.psm1 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c187fee..41215c1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,6 +12,10 @@ on: - "**.targets" - "**.sln" - "**/Client/**/*.json" + # Added to check if pipeline is working + - "/scripts/**/*.psm1" + - "/scripts/**/*.ps1" + - "**/ci.yml" pull_request: branches: [main] paths: diff --git a/scripts/Utilities/Utilities.psm1 b/scripts/Utilities/Utilities.psm1 new file mode 100644 index 0000000..2af9357 --- /dev/null +++ b/scripts/Utilities/Utilities.psm1 @@ -0,0 +1,139 @@ +# Utilities + +$scriptConfig = @{} +$scriptConfig.WorkspaceFolder = ".." +$scriptConfig.SolutionFileName = "Kentico.Xperience.Shopify.sln" +$scriptConfig.AssemblyName = "DancingGoat" + +<# + .DESCRIPTION + Returns shared configuration for PowerShell scripts +#> +function Get-ScriptConfig { + return $scriptConfig +} + +<# + .DESCRIPTION + Returns the main solution file path +#> +function Get-SolutionPath { + return Resolve-Path(Join-Path $($scriptConfig.WorkspaceFolder) $($scriptConfig.SolutionFileName)) +} + +<# + .DESCRIPTION + Returns the web application folder path from the workspace root +#> +function Get-WebProjectPath { + return Resolve-Path(Join-Path $($scriptConfig.WorkspaceFolder) "examples/DancingGoat-Shopify") +} + +<# + .DESCRIPTION + Returns the admin application folder path from the workspace root +#> +<#function Get-AdminProjectPath { + return Resolve-Path(Join-Path $($scriptConfig.WorkspaceFolder) "src/Kentico.Community.Portal.Admin") +}#> + +<# + .DESCRIPTION + Returns the admin client application folder path from the workspace root +#> +<#function Get-AdminClientProjectPath { + return Resolve-Path(Join-Path $($scriptConfig.WorkspaceFolder) "src/Kentico.Community.Portal.Admin/Client") +}#> + +<# + .DESCRIPTION + Returns the Core project folder path from the workspace root +#> +<#function Get-CoreProjectPath { + return Resolve-Path(Join-Path $($scriptConfig.WorkspaceFolder) "src/Kentico.Community.Portal.Core") +}#> + + +<# + .DESCRIPTION + Gets the database connection string from the user secrets or appsettings.json file +#> +<#function Get-ConnectionString { + $projectPath = Get-WebProjectPath + + # 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 + } +} +function Write-Status { + param( + [string]$message + ) + + Write-Host $message -ForegroundColor Blue +} + +function Write-Notification { + param( + [string]$message + ) + + Write-Host $message -ForegroundColor Magenta +} + +function Write-Error { + param( + [string]$message + ) + + Write-Host $message -ForegroundColor Red +} \ No newline at end of file