- 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")
$outputExecutionDirectory = "$outputDirectory\$executionTimestamp"
### 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"
### Out Files and Directories
$outFilePrefix = "$($repositoryName)_$($version_assembly)"
$htmlLogFileName = "$($outFilePrefix).tests.html"
$trxLogFileName = "$($outFilePrefix).tests.trx"
$coverageLogFileName = "$($outFilePrefix).cobertura.xml"
$coverageDirectoryName = "$($outFilePrefix).coverage"
### 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"
$testProjectPath = Join-Path (Join-Path $testsPath "$productName - Test.Unit") "$productName - Test.Unit.csproj"
### Tools
$msbuildPath = vswhere -products * -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe | Select-Object -first 1
$msbuildDirectory = Split-Path $msbuildPath
$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
}
}
$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 -PostCondition $exitCodeCheck -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 `
{
& $msbuildPath $sourceSolutionPath -p:Version_Assembly=$version_assembly -p:Version_File=$version_file -p:NuGetExePath=$nugetPath
}
task Build-ProductTests -PostCondition $exitCodeCheck -Action `
{
if (Test-Path $testProjectPath)
{
& $msbuildPath $testsSolutionPath -p:Version_Assembly=$version_assembly -p:Version_File=$version_file -p:NuGetExePath=$nugetPath
}
}
task Restore-ProductSource -PostCondition $exitCodeCheck -Action `
{
if(Test-Path $sourcePackagesConfig)
{
nuget restore $sourcePackagesConfig -PackagesDirectory $libraryPath
}
else
{
nuget restore $sourceProjectPath -PackagesDirectory "$libraryPath" -MSBuildPath "$msbuildDirectory"
}
}
task Restore-ProductTests -PostCondition $exitCodeCheck -Action `
{
if(Test-Path $testsPackagesConfig)
{
nuget restore $testsPackagesConfig -PackagesDirectory $libraryPath
}
elseif (Test-Path $testProjectPath)
{
nuget restore $testProjectPath -PackagesDirectory "$libraryPath" -MSBuildPath "$msbuildDirectory"
}
}
task Package-Product -PostCondition $exitCodeCheck -Action `
{
nuget pack $sourceProjectPath -Symbols -OutputDirectory $outputExecutionPath -Version $version_file
}
task Publish-Product -action `
{
$nugetPackage = Get-Item "$outputExecutionPath\*.nupkg"
dotnet nuget push $nugetPackage.FullName --api-key $nugetApiKey --source $nugetRepositoryUri
}
task Save-TestCoverageReport -Action `
{
reportgenerator -reports:"$outputExecutionPath\$coverageLogFileName" `
-targetdir:"$outputExecutionPath\$coverageDirectoryName.coverage" `
-reporttypes:Html `
-assemblyfilters:"-*Test.Unit;"
}
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, Package-Product
task CICD -Depends CI, Publish-Product