- ProductName
provides the name of the product to be built within/by this psakefile.
- ProductAuthor
provides the author's name for the packaged and published product
- ProductDescription
provides the description for the packaged and published product
- Info
- Clean
- Build-ProductSource
- Build-ProductTests
- Package-Product
- Publish-Product
- Restore-ProductSource
- Restore-ProductTests
- Save-TestCoverageReport
- Save-ProductDocumentation
- Test-Product
- Test-ProductWithCoverage
- Default
- Info
- Clean
- Restore-ProductSource
- Build-ProductSource
- Restore-ProductTests
- Build-ProductTests
- CI
- Default
- Test-ProductWithCoverage
- Save-TestCoverageReport
- Save-ProductDocumentation
- CICD
- CI
- Package-Product
- Publish-Product
Properties `
{
### Variables
$repositoryName = Split-Path $PWD -Leaf
$productName = "{ProductName}"
$productAuthor = "{ProductAuthor}"
$productDescription = "{ProductDescription}"
$libraryDirectory = "lib"
$outputDirectory = "out"
$sourceDirectory = "src"
$sqlDirectory = "sql"
$testsDirectory = "test"
$utilDirectory = "util"
$executionTime = Get-Date
$executionTimestamp = $executionTime.ToString("yyyyMMddHHmmssfff")
$htmlLogFileName = "$($repositoryName).$($executionTimestamp).tests.html"
$trxLogFileName = "$($repositoryName).$($executionTimestamp).tests.trx"
$coverageLogFileName = "$($repositoryName).$($executionTimestamp).cobertura.xml"
$outputExecutionDirectory = "$outputDirectory\$executionTimestamp"
### Paths
$libraryPath = Join-Path $PWD $libraryDirectory
$outputPath = Join-Path $PWD $outputDirectory
$sourcePath = Join-Path $PWD $sourceDirectory
$sqlPath = Join-Path $PWD $sqlDirectory
$testsPath = Join-Path $PWD $testsDirectory
$utilPath = Join-Path $PWD $utilDirectory
$outputExecutionPath = Join-Path $outputPath $executionTimestamp
$outputProductPath = Join-Path $outputExecutionPath $productName
### Paths - DotNET
$sourceSolutionPath = Join-Path $sourcePath "$productName.sln"
$sourcePackagesConfig = Join-Path (Join-Path $sourcePath $productName) "packages.config"
$sourceProjectPath = Join-Path (Join-Path $sourcePath $productName) "$productName.csproj"
$testsSolutionPath = Join-Path $testsPath "$productName - Tests.sln"
$testsPackagesConfig = Join-Path (Join-Path $testsPath "$productName - Test.Unit") "packages.config"
### Tools
$msbuildPath = vswhere -products * -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe | Select-Object -first 1
$nugetPath = $ENV:NUGET_PATH
if(-not $nugetPath)
{
$nugetPath = (Get-Command nuget | Select-Object -First 1).Source
}
### Build Secrets
$buildSecrets = Get-Content ~\.build-secrets.json | Out-String | ConvertFrom-Json
if($buildSecrets)
{
$nugetRepositorySecrets = $buildSecrets.KYZOG.PKG.REPOSITORIES | Where-Object { $_.NAME -eq 'DotNet' }
}
$nugetApiKey = $ENV:KYZOG_PKG_REPOSITORY_NUGET_KEY
if(-not $nugetApiKey)
{
$nugetApiKey = $nugetRepositorySecrets.KEY
}
$nugetRepositoryUri = $ENV:KYZOG_PKG_REPOSITORY_NUGET_URI
if(-not $nugetRepositoryUri)
{
$nugetRepositoryUri = $nugetRepositorySecrets.URI
}
### Version
$version_major = $executionTime.ToString("yyyy")
$version_minor = $executionTime.ToString("MM")
$version_build = $executionTime.ToString("dd")
$version_revision = $executionTime.ToString("HHmmssfff")
$version_assembly = "$version_major.$version_minor.$version_build"
$version_file = "$version_major.$version_minor.$version_build"
$versionSuffix = ""
$currentBranch = git rev-parse --abbrev-ref HEAD
if($ENV:CI_COMMIT_BRANCH)
{
$currentBranch = "$($ENV:CI_COMMIT_BRANCH)"
}
if($currentBranch -ne "main")
{
$versionSuffix = "$currentBranch"
}
if($ENV:CI_PIPELINE_IID)
{
$version_assembly += ".$($ENV:CI_PIPELINE_ID)"
$version_file += ".$($ENV:CI_PIPELINE_ID)"
}
if($versionSuffix)
{
$version_file += "-$versionSuffix"
}
$version_file += "+$version_revision"
}
$exitCodeCheck = { $LASTEXITCODE -eq 0 }
task Info -action `
{
Write-Host "#####################################################"
Write-Host "###### PSAKE VARIABLES ##############################"
Write-Host "#####################################################"
$psake | Format-List
Write-Host "###### PSAKE Context - List #########################"
$psake.context | Format-List
Write-Host "###### PSAKE Context - Properties ###################"
$psake.context.properties | Format-List
Write-Host "#####################################################"
Write-Host "###### ENVIRONMENT VARIABLES ########################"
Write-Host "#####################################################"
Get-ChildItem ENV: | ForEach-Object { Write-Host "$($_.Key) - $($_.Value)" }
Write-Host "#####################################################"
Write-Host "###### SCRIPT VARIABLES #############################"
Write-Host "#####################################################"
Get-Variable | ForEach-Object { Write-Host "$($_.Name) - $($_.Value)" }
}
task Clean -Action `
{
dotnet clean $sourceSolutionPath
if(Test-Path $libraryPath)
{
Remove-Item $libraryPath -Force -Recurse
}
if(Test-Path $outputPath)
{
Remove-Item $outputPath -Force -Recurse
}
}
task Build-ProductSource -PostCondition $exitCodeCheck -Action `
{
dotnet build $sourceSolutionPath `
--property:version="$version_file" `
--property:Version_File=$version_file `
--property:VersionPrefix=$version_assembly `
--property:NuGetExePath=$nugetPath
}
task Build-ProductTests -PostCondition $exitCodeCheck -Action `
{
dotnet build $testsSolutionPath `
--property:VersionPrefix=$version_assembly `
--property:Version_File=$version_file `
--property:NuGetExePath=$nugetPath
}
task Package-Product -PostCondition $exitCodeCheck -Action `
{
dotnet pack $sourceSolutionPath `
--output "$outputExecutionPath" `
--property:VersionPrefix=$version_assembly `
--property:version="$version_file"
}
task Publish-Product -PostCondition $exitCodeCheck -Action `
{
$nugetPackage = Get-Item "$outputExecutionPath\*.nupkg"
Write-Host "Pushing NuGet package - $($nugetPackage.FullName)"
dotnet nuget push $nugetPackage.FullName `
--api-key $nugetApiKey `
--source $nugetRepositoryUri
}
task Restore-ProductSource -PostCondition $exitCodeCheck -Action `
{
dotnet restore $sourceSolutionPath
}
task Restore-ProductTests -PostCondition $exitCodeCheck -Action `
{
dotnet restore $testsSolutionPath
}
task Save-TestCoverageReport -Action `
{
reportgenerator -reports:"$outputExecutionPath\$coverageLogFileName" `
-targetdir:"$outputExecutionPath\$($repositoryName).$($executionTimestamp).coverage" `
-reporttypes:Html `
-assemblyfilters:"-*Test.Unit;"
}
task Save-ProductDocumentation -Action `
{
docfx build docs\docfx.json --output "$outputExecutionPath\docs"
Move-Item "$outputExecutionPath\docs" "$outputExecutionPath\$($repositoryName).$($executionTimestamp).docs"
}
task Test-Product -PostCondition $exitCodeCheck -Action `
{
dotnet test $testsSolutionPath
}
task Test-ProductWithCoverage -PostCondition $exitCodeCheck -Action `
{
dotnet-coverage collect -o ".\$outputExecutionDirectory\$coverageLogFileName" -f cobertura `
dotnet test $testsSolutionPath --no-build --results-directory "$outputExecutionPath" `
--logger "html;logfilename=$htmlLogFileName" `
--logger "trx;logfilename=$trxLogFileName" `
}
task Default -Depends Info, Clean, Restore-ProductSource, Build-ProductSource, Restore-ProductTests, Build-ProductTests
task CI -Depends Default, Test-ProductWithCoverage, Save-TestCoverageReport, Save-ProductDocumentation, Package-Product
task CICD -Depends CI, Publish-Product