|
| 1 | +# EXIT CODES |
| 2 | +# 1: dotnet packaging failure |
| 3 | +# 2: dotnet publishing failure |
| 4 | +# 3: Unit test failure |
| 5 | +# 4: dotnet / NuGet package restore failure |
| 6 | + |
| 7 | +<# |
| 8 | +.SYNOPSIS |
| 9 | + Gets the set of directories in which projects are available for compile/processing. |
| 10 | +
|
| 11 | +.PARAMETER RootPath |
| 12 | + Path where searching for project directories should begin. |
| 13 | +#> |
| 14 | +function Get-DotNetProjectDirectory { |
| 15 | + [CmdletBinding()] |
| 16 | + Param( |
| 17 | + [Parameter(Mandatory = $True, ValueFromPipeline = $False, ValueFromPipelineByPropertyName = $False)] |
| 18 | + [ValidateNotNullOrEmpty()] |
| 19 | + [string] |
| 20 | + $RootPath |
| 21 | + ) |
| 22 | + |
| 23 | + Get-ChildItem -Path $RootPath -Recurse -Include "*.csproj" | Select-Object @{ Name = "ParentFolder"; Expression = { $_.Directory.FullName.TrimEnd("\") } } | Select-Object -ExpandProperty ParentFolder |
| 24 | +} |
| 25 | + |
| 26 | +<# |
| 27 | +.SYNOPSIS |
| 28 | + Runs the dotnet CLI install script from GitHub to install a project-local |
| 29 | + copy of the CLI. |
| 30 | +#> |
| 31 | +function Install-DotNetCli { |
| 32 | + [CmdletBinding()] |
| 33 | + Param( |
| 34 | + [string] |
| 35 | + $Version = "Latest" |
| 36 | + ) |
| 37 | + Write-Message "Installing .NET SDK version $Version" |
| 38 | + |
| 39 | + $callerPath = Split-Path $MyInvocation.PSCommandPath |
| 40 | + $installDir = Join-Path -Path $callerPath -ChildPath ".dotnet/cli" |
| 41 | + if (!(Test-Path $installDir)) { |
| 42 | + New-Item -ItemType Directory -Path "$installDir" | Out-Null |
| 43 | + } |
| 44 | + |
| 45 | + # Download the dotnet CLI install script |
| 46 | + if ($IsWindows) { |
| 47 | + if (!(Test-Path ./.dotnet/dotnet-install.ps1)) { |
| 48 | + Invoke-WebRequest "https://dot.net/v1/dotnet-install.ps1" -OutFile "./.dotnet/dotnet-install.ps1" |
| 49 | + } |
| 50 | + |
| 51 | + & ./.dotnet/dotnet-install.ps1 -InstallDir "$installDir" -Version $Version |
| 52 | + } else { |
| 53 | + if (!(Test-Path ./.dotnet/dotnet-install.sh)) { |
| 54 | + Invoke-WebRequest "https://dot.net/v1/dotnet-install.sh" -OutFile "./.dotnet/dotnet-install.sh" |
| 55 | + } |
| 56 | + |
| 57 | + & bash ./.dotnet/dotnet-install.sh --install-dir "$installDir" --version $Version |
| 58 | + } |
| 59 | + |
| 60 | + Add-Path "$installDir" |
| 61 | +} |
| 62 | + |
| 63 | +<# |
| 64 | +.SYNOPSIS |
| 65 | + Appends a given value to the path but only if the value does not yet exist within the path. |
| 66 | +.PARAMETER Path |
| 67 | + The path to append. |
| 68 | +#> |
| 69 | +function Add-Path { |
| 70 | + [CmdletBinding()] |
| 71 | + Param( |
| 72 | + [ValidateNotNullOrEmpty()] |
| 73 | + [string] |
| 74 | + $Path |
| 75 | + ) |
| 76 | + |
| 77 | + $pathSeparator = ":"; |
| 78 | + |
| 79 | + if ($IsWindows) { |
| 80 | + $pathSeparator = ";"; |
| 81 | + } |
| 82 | + |
| 83 | + $pathValues = $env:PATH.Split($pathSeparator); |
| 84 | + if ($pathValues -Contains $Path) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + $env:PATH = "${Path}${pathSeparator}$env:PATH" |
| 89 | +} |
| 90 | + |
| 91 | +<# |
| 92 | +.SYNOPSIS |
| 93 | + Builds a project using dotnet cli. |
| 94 | +.DESCRIPTION |
| 95 | + Builds a project in a specified directory using the dotnet cli. |
| 96 | +.PARAMETER DirectoryName |
| 97 | + The path to the directory containing the project to build. |
| 98 | +#> |
| 99 | +function Invoke-DotNetBuild { |
| 100 | + [CmdletBinding()] |
| 101 | + Param( |
| 102 | + [Parameter(Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)] |
| 103 | + [ValidateNotNull()] |
| 104 | + [System.IO.DirectoryInfo[]] |
| 105 | + $ProjectDirectory |
| 106 | + ) |
| 107 | + Process { |
| 108 | + foreach ($Project in $ProjectDirectory) { |
| 109 | + & dotnet build ("""" + $Project.FullName + """") --configuration Release |
| 110 | + if ($LASTEXITCODE -ne 0) { |
| 111 | + exit 1 |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +<# |
| 118 | +.SYNOPSIS |
| 119 | + Invokes the dotnet utility to package a project. |
| 120 | +
|
| 121 | +.PARAMETER ProjectDirectory |
| 122 | + Path to the directory containing the project to package. |
| 123 | +
|
| 124 | +.PARAMETER PackagesPath |
| 125 | + Path to the "artifacts/packages" folder where packages should go. |
| 126 | +
|
| 127 | +.PARAMETER VersionSuffix |
| 128 | + The version suffix to use for the NuGet package version. |
| 129 | +#> |
| 130 | +function Invoke-DotNetPack { |
| 131 | + [CmdletBinding()] |
| 132 | + Param( |
| 133 | + [Parameter(Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)] |
| 134 | + [ValidateNotNull()] |
| 135 | + [System.IO.DirectoryInfo[]] |
| 136 | + $ProjectDirectory, |
| 137 | + |
| 138 | + [Parameter(Mandatory = $True, ValueFromPipeline = $False)] |
| 139 | + [ValidateNotNull()] |
| 140 | + [System.IO.DirectoryInfo] |
| 141 | + $PackagesPath, |
| 142 | + |
| 143 | + [Parameter(Mandatory = $True, ValueFromPipeline = $False)] |
| 144 | + [AllowEmptyString()] |
| 145 | + [string] |
| 146 | + $VersionSuffix |
| 147 | + ) |
| 148 | + Begin { |
| 149 | + New-Item -Path $PackagesPath -ItemType Directory -Force | Out-Null |
| 150 | + } |
| 151 | + Process { |
| 152 | + foreach ($Project in $ProjectDirectory) { |
| 153 | + if ($VersionSuffix -eq "") { |
| 154 | + & dotnet build ("""" + $Project.FullName + """") --configuration Release |
| 155 | + } |
| 156 | + else { |
| 157 | + & dotnet build ("""" + $Project.FullName + """") --configuration Release --version-suffix $VersionSuffix |
| 158 | + } |
| 159 | + if ($LASTEXITCODE -ne 0) { |
| 160 | + exit 1 |
| 161 | + } |
| 162 | + |
| 163 | + if ($VersionSuffix -eq "") { |
| 164 | + & dotnet pack ("""" + $Project.FullName + """") --configuration Release --output $PackagesPath |
| 165 | + } |
| 166 | + else { |
| 167 | + & dotnet pack ("""" + $Project.FullName + """") --configuration Release --version-suffix $VersionSuffix --output $PackagesPath |
| 168 | + } |
| 169 | + if ($LASTEXITCODE -ne 0) { |
| 170 | + exit 1 |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +<# |
| 177 | +.SYNOPSIS |
| 178 | + Invokes dotnet test command. |
| 179 | +
|
| 180 | +.PARAMETER ProjectDirectory |
| 181 | + Path to the directory containing the project to package. |
| 182 | +#> |
| 183 | +function Invoke-Test { |
| 184 | + [CmdletBinding()] |
| 185 | + Param( |
| 186 | + [Parameter(Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)] |
| 187 | + [ValidateNotNull()] |
| 188 | + [System.IO.DirectoryInfo[]] |
| 189 | + $ProjectDirectory |
| 190 | + ) |
| 191 | + Process { |
| 192 | + foreach ($Project in $ProjectDirectory) { |
| 193 | + Push-Location $Project |
| 194 | + |
| 195 | + & dotnet test ` |
| 196 | + --configuration Release ` |
| 197 | + --logger:trx ` |
| 198 | + /p:CollectCoverage=true ` |
| 199 | + /p:CoverletOutput="../../artifacts/coverage/$($Project.Name)/" ` |
| 200 | + /p:CoverletOutputFormat="json%2clcov" ` |
| 201 | + /p:ExcludeByAttribute=CompilerGeneratedAttribute ` |
| 202 | + /p:ExcludeByAttribute=GeneratedCodeAttribute ` |
| 203 | + /p:Exclude="[Autofac.Test.Scenarios.ScannedAssembly]*" |
| 204 | + |
| 205 | + if ($LASTEXITCODE -ne 0) { |
| 206 | + Pop-Location |
| 207 | + exit 3 |
| 208 | + } |
| 209 | + |
| 210 | + Pop-Location |
| 211 | + } |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +<# |
| 216 | +.SYNOPSIS |
| 217 | + Restores dependencies using the dotnet utility. |
| 218 | +
|
| 219 | +.PARAMETER ProjectDirectory |
| 220 | + Path to the directory containing the project with dependencies to restore. |
| 221 | +#> |
| 222 | +function Restore-DependencyPackages { |
| 223 | + [CmdletBinding()] |
| 224 | + Param( |
| 225 | + [Parameter(Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)] |
| 226 | + [ValidateNotNull()] |
| 227 | + [System.IO.DirectoryInfo[]] |
| 228 | + $ProjectDirectory |
| 229 | + ) |
| 230 | + Process { |
| 231 | + foreach ($Project in $ProjectDirectory) { |
| 232 | + & dotnet restore ("""" + $Project.FullName + """") --no-cache |
| 233 | + if ($LASTEXITCODE -ne 0) { |
| 234 | + exit 4 |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +<# |
| 241 | +.SYNOPSIS |
| 242 | + Writes a build progress message to the host. |
| 243 | +
|
| 244 | +.PARAMETER Message |
| 245 | + The message to write. |
| 246 | +#> |
| 247 | +function Write-Message { |
| 248 | + [CmdletBinding()] |
| 249 | + Param( |
| 250 | + [Parameter(Mandatory = $True, ValueFromPipeline = $False, ValueFromPipelineByPropertyName = $False)] |
| 251 | + [ValidateNotNullOrEmpty()] |
| 252 | + [string] |
| 253 | + $Message |
| 254 | + ) |
| 255 | + |
| 256 | + Write-Host "[BUILD] $Message" -ForegroundColor Cyan |
| 257 | +} |
0 commit comments