|
| 1 | + |
| 2 | +properties { |
| 3 | + $githubRepo = 'IdentityServer3.Contrib.Localization'; |
| 4 | + $base_dir = resolve-path . |
| 5 | + $src_dir = "$base_dir\source"; |
| 6 | + $packages_dir = "$src_dir\packages"; |
| 7 | + $config = 'debug'; |
| 8 | + $sln = "$src_dir\Contrib.sln"; |
| 9 | + $build_version = "$(get-date -Format "yyyy_MM_dd_")$(get-random -Maximum 100000)"; |
| 10 | + $runsOnBuildServer = $false; |
| 11 | + $dist_dir = "$base_dir\dist"; |
| 12 | + $test_report_dir = "$base_dir\TestResult"; |
| 13 | + $publishUri = $null; |
| 14 | + $publishUsername = $null; |
| 15 | + $publishPassword = $null; |
| 16 | + $publishApiKey = $null |
| 17 | +} |
| 18 | + |
| 19 | +Import-module "$psscriptroot\tools\teamcity.psm1" -WarningAction SilentlyContinue |
| 20 | + |
| 21 | +FormatTaskName { |
| 22 | + param($taskName) |
| 23 | + write-host "Executing Task: $taskName" -foregroundcolor Magenta |
| 24 | +} |
| 25 | + |
| 26 | +task -name validate-config -depends add-teamcity-reporting -action { |
| 27 | + assert ( 'debug', 'release' -contains $config) "Invalid config: $config. Should be 'debug' or 'release'" |
| 28 | + Write-host "Build version is $build_version" |
| 29 | + Write-host "Configuration is $config" |
| 30 | + Write-host "Running on build server: $runsOnBuildServer" |
| 31 | + if (-not(test-path -pathtype container -path $dist_dir)) { md $dist_dir | out-null } |
| 32 | +} |
| 33 | + |
| 34 | +task -name rebuild -depends clean, build |
| 35 | + |
| 36 | +task -name ensure-nuget -action { |
| 37 | + exec { |
| 38 | + nuget update -self |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +task -name restore-nuget -depends ensure-nuget -action { |
| 43 | + exec { |
| 44 | + nuget restore $sln |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +task -name patch-assemblyinfo -precondition { return $runsOnBuildServer } -action { |
| 49 | + exec { |
| 50 | + Write-host "Patching assembly version, setting version to $build_version" |
| 51 | + |
| 52 | + function PatchFile ([string] $pattern, [string] $replaceString, [string] $fullPath){ |
| 53 | + (gc $fullPath) -replace $pattern, $replaceString | out-file $fullPath |
| 54 | + } |
| 55 | + |
| 56 | + find_assemblyinfo | % { |
| 57 | + $assemblyVersionPattern = 'AssemblyVersion\(".+?"\)' |
| 58 | + $assembyVersionReplacement ='AssemblyVersion("' + $build_version + '")' |
| 59 | + PatchFile $assemblyVersionPattern $assembyVersionReplacement $_ |
| 60 | + |
| 61 | + $assemblyFileVersionPattern = 'AssemblyFileVersion\(".+?"\)' |
| 62 | + $assembyFileVersionReplacement ='AssemblyFileVersion("' + $build_version + '")' |
| 63 | + PatchFile $assemblyFileVersionPattern $assembyFileVersionReplacement $_ |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +task -name build-sln -depends validate-config, restore-nuget, patch-assemblyinfo -action { |
| 69 | + exec { |
| 70 | + run-msbuild $sln 'build' $config |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +task -name clean -depends validate-config -action { |
| 75 | + exec { |
| 76 | + run-msbuild $sln 'clean' $config |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +task -name ensure-xunit -action { |
| 81 | + exec { |
| 82 | + if (-not(gci -Path $packages_dir -Filter xunit.runner.console*)) { |
| 83 | + nuget install xunit.runner.console -SolutionDirectory $src_dir |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +task -name add-teamcity-reporting -precondition { return $runsOnBuildServer } -action { |
| 89 | + exec { |
| 90 | + TaskSetup { |
| 91 | + $taskName = $psake.context.Peek().currentTaskName |
| 92 | + $global:pasPakkerBuildResult = "##teamcity[buildStatus status='FAILURE' text='Build failed on step $taskName']" |
| 93 | + TeamCity-ReportBuildProgress "Running task $taskName" |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +task -name run-unittests -depends build, ensure-xunit -action { |
| 99 | + exec { |
| 100 | + run_tests "$src_dir\Unittests\bin\$config\Unittests.dll" ` |
| 101 | + "$test_report_dir\$($build_version)_integration_TestResult.xml" |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +task -name run-tests -depends run-unittests |
| 106 | + |
| 107 | +task -name ci -depends run-tests -action { |
| 108 | + exec { |
| 109 | + $global:pasPakkerBuildResult = $null |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +task -name run-octopack -depends clean,run-tests -action { |
| 114 | + exec { |
| 115 | + run-msbuild $sln 'build' $config $true |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +task -name list-publishable-artifacts -action { |
| 120 | + exec { |
| 121 | + find-publishable-artifacts | %{ Rename-item (join-path $dist_dir $_.Name) "Local.$($_.Name)" } |
| 122 | + find-publishable-artifacts | %{ Write-host "Artifact: $($_.FullName)"} |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +task -name ensure-publish-credentials -depends ensure-nuget -action { |
| 127 | + exec { |
| 128 | + if (-not((nuget sources list|out-string).Contains('udir-nuggets-publish'))) { |
| 129 | + Write-host "Adding feed udir-nuggets-publish with value $publishUri" |
| 130 | + $sourcesCmd = 'add' |
| 131 | + } else { |
| 132 | + Write-host "Updating credentials for 'udir-nuggets-publish'" |
| 133 | + $sourcesCmd = 'update' |
| 134 | + } |
| 135 | + nuget sources $sourcesCmd -Name 'udir-nuggets-publish' -source $publishUri ` |
| 136 | + -Username $publishUsername -Password $publishPassword |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +task -name publish-to-feed -precondition { return ($publishUri -ne $null)} ` |
| 141 | + -depends ensure-publish-credentials -action { |
| 142 | + exec { |
| 143 | + find-publishable-artifacts | %{ |
| 144 | + Write-host "Publishing $($_.FullName) to $publishUri" |
| 145 | + nuget push $_.FullName $publishApiKey -Source $publishUri -Verbosity detailed |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +task -name dist -depends run-octopack,list-publishable-artifacts, publish-to-feed -action { |
| 151 | + exec { |
| 152 | + $global:pasPakkerBuildResult = $null |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +task build -depends build-sln |
| 157 | +task default -depends build-sln |
| 158 | + |
| 159 | +########### Helper functions ######################## |
| 160 | + |
| 161 | +function run-msbuild($sln_file, $t, $cfg, $runOctopack=$false) { |
| 162 | + $v = if ($runsOnBuildServer) { 'n'} else { 'q' } |
| 163 | + Framework '4.5.1' |
| 164 | + msbuild /nologo /verbosity:$v $sln_file /t:$t /p:Configuration=$cfg /p:RunOctoPack=$runOctopack ` |
| 165 | + /p:OctoPackPublishPackageToFileShare=$dist_dir |
| 166 | +} |
| 167 | + |
| 168 | +function xunit_console_runner { |
| 169 | + $xunit_dir = gci -Path "$src_dir\packages" -Filter xunit.console.runner* | sort -Property Name | select -Last 1 |
| 170 | + gci -Path $xunit_dir.fullname -Filter xunit.console.exe -Recurse | select -First 1 -ExpandProperty FullName |
| 171 | +} |
| 172 | + |
| 173 | +function run_tests($testassemblies, $reportfile, $suiteName) { |
| 174 | + if ($runsOnBuildServer) { TeamCity-TestSuiteStarted $suiteName } |
| 175 | + if (-not(test-path $test_report_dir)) { md $test_report_dir | out-null } |
| 176 | + #$devNull = [System.IO.Path]::GetTempFileName() |
| 177 | + & (xunit_console_runner) $testassemblies -nunit $reportfile |
| 178 | + if ($runsOnBuildServer) { |
| 179 | + Write-output "##teamcity[importData type='nunit' path='$reportfile']" |
| 180 | + TeamCity-TestSuiteFinished $suiteName |
| 181 | + } else { |
| 182 | + Write-output "Saved test report to $reportfile" |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +function find_assemblyinfo { |
| 187 | + Get-ChildItem -Path $src_dir ` |
| 188 | + | ?{$_.PSIsContainer} ` |
| 189 | + | %{ Get-ChildItem -Path $_.FullName -Filter Properties} ` |
| 190 | + | %{ Get-ChildItem -Path $_.FullName -Filter AssemblyInfo.cs} ` |
| 191 | + | select -ExpandProperty FullName |
| 192 | +} |
| 193 | + |
| 194 | +function find-publishable-artifacts { |
| 195 | + $filter = "*$($build_version).?.nupkg" |
| 196 | + Write-host "Searching for publishable artifacts: $filter in $dist_dir" |
| 197 | + gci -Path $dist_dir -Filter $filter |
| 198 | +} |
0 commit comments