Skip to content

Commit fe7693b

Browse files
author
Vidar Kongsli
committed
Add psake script for build and publish to internal nuget feed
1 parent fa17eda commit fe7693b

File tree

4 files changed

+353
-0
lines changed

4 files changed

+353
-0
lines changed

default.ps1

+198
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
}

source/IdentityServer3.Localization/IdentityServer3.Localization.csproj

+5
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@
138138
<EmbeddedResource Include="Resources\Scopes\Scopes.tr-TR.resx" />
139139
</ItemGroup>
140140
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
141+
<Import Project="..\packages\OctoPack.3.0.43\tools\OctoPack.targets" Condition="Exists('..\packages\OctoPack.3.0.43\tools\OctoPack.targets')" />
142+
<Target Name="EnsureOctoPackImported" BeforeTargets="BeforeBuild" Condition="'$(OctoPackImported)' == ''">
143+
<Error Condition="!Exists('..\packages\OctoPack.3.0.43\tools\OctoPack.targets') And ('$(RunOctoPack)' != '' And $(RunOctoPack))" Text="You are trying to build with OctoPack, but the NuGet targets file that OctoPack depends on is not available on this computer. This is probably because the OctoPack package has not been committed to source control, or NuGet Package Restore is not enabled. Please enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
144+
<Error Condition="Exists('..\packages\OctoPack.3.0.43\tools\OctoPack.targets') And ('$(RunOctoPack)' != '' And $(RunOctoPack))" Text="OctoPack cannot be run because NuGet packages were restored prior to the build running, and the targets file was unavailable when the build started. Please build the project again to include these packages in the build. You may also need to make sure that your build server does not delete packages prior to each build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
145+
</Target>
141146
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
142147
Other similar extension points exist, see Microsoft.Common.targets.
143148
<Target Name="BeforeBuild">
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="IdentityServer3" version="2.0.1" targetFramework="net45" />
4+
<package id="OctoPack" version="3.0.43" targetFramework="net45" developmentDependency="true" />
45
<package id="Owin" version="1.0" targetFramework="net45" />
56
</packages>

tools/teamcity.psm1

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
if ($env:TEAMCITY_VERSION) {
2+
# When PowerShell is started through TeamCity's Command Runner, the standard
3+
# output will be wrapped at column 80 (a default). This has a negative impact
4+
# on service messages, as TeamCity quite naturally fails parsing a wrapped
5+
# message. The solution is to set a new, much wider output width. It will
6+
# only be set if TEAMCITY_VERSION exists, i.e., if started by TeamCity.
7+
$host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size(8192,50)
8+
}
9+
10+
function TeamCity-TestSuiteStarted([string]$name) {
11+
Write-Output "##teamcity[testSuiteStarted name='$name']"
12+
}
13+
14+
function TeamCity-TestSuiteFinished([string]$name) {
15+
Write-Output "##teamcity[testSuiteFinished name='$name']"
16+
}
17+
18+
function TeamCity-TestStarted([string]$name) {
19+
Write-Output "##teamcity[testStarted name='$name']"
20+
}
21+
22+
function TeamCity-TestFinished([string]$name) {
23+
Write-Output "##teamcity[testFinished name='$name']"
24+
}
25+
26+
function TeamCity-TestIgnored([string]$name, [string]$message='') {
27+
Write-Output "##teamcity[testIgnored name='$name' message='$message']"
28+
}
29+
30+
function TeamCity-TestOutput([string]$name, [string]$output) {
31+
Write-Output "##teamcity[testStdOut name='$name' out='$output']"
32+
}
33+
34+
function TeamCity-TestError([string]$name, [string]$output) {
35+
Write-Output "##teamcity[testStdErr name='$name' out='$output']"
36+
}
37+
38+
function TeamCity-TestFailed([string]$name, [string]$message, [string]$details='', [string]$type='', [string]$expected='', [string]$actual='') {
39+
$output="##teamcity[testFailed ";
40+
if (![string]::IsNullOrEmpty($type)) {
41+
$output += " type='$type'"
42+
}
43+
44+
$output += " name='$name' message='$message' details='$details'"
45+
46+
if (![string]::IsNullOrEmpty($expected)) {
47+
$output += " expected='$expected'"
48+
}
49+
if (![string]::IsNullOrEmpty($actual)) {
50+
$output += " actual='$actual'"
51+
}
52+
53+
$output += ']'
54+
Write-Output $output
55+
}
56+
57+
# See http://confluence.jetbrains.net/display/TCD5/Manually+Configuring+Reporting+Coverage
58+
function TeamCity-ConfigureDotNetCoverage([string]$key, [string]$value) {
59+
Write-Output "##teamcity[dotNetCoverage $key='$value']"
60+
}
61+
62+
function TeamCity-ImportDotNetCoverageResult([string]$tool, [string]$path) {
63+
Write-Output "##teamcity[importData type='dotNetCoverage' tool='$tool' path='$path']"
64+
}
65+
66+
# See http://confluence.jetbrains.net/display/TCD5/FxCop_#FxCop_-UsingServiceMessages
67+
function TeamCity-ImportFxCopResult([string]$path) {
68+
Write-Output "##teamcity[importData type='FxCop' path='$path']"
69+
}
70+
71+
function TeamCity-PublishArtifact([string]$path) {
72+
Write-Output "##teamcity[publishArtifacts '$path']"
73+
}
74+
75+
function TeamCity-ReportBuildStart([string]$message) {
76+
Write-Output "##teamcity[progressStart '$message']"
77+
}
78+
79+
function TeamCity-ReportBuildProgress([string]$message) {
80+
Write-Output "##teamcity[progessMessage '$message']"
81+
}
82+
83+
function TeamCity-ReportBuildFinish([string]$message) {
84+
Write-Output "##teamcity[progessFinish '$message']"
85+
}
86+
87+
function TeamCity-ReportBuildStatus([string]$status, [string]$text='') {
88+
Write-Output "##teamcity[buildStatus '$status' text='$text']"
89+
}
90+
91+
function TeamCity-SetBuildNumber([string]$buildNumber) {
92+
Write-Output "##teamcity[buildNumber '$buildNumber']"
93+
}
94+
95+
function TeamCity-SetBuildStatistic([string]$key, [string]$value) {
96+
Write-Output "##teamcity[buildStatisticValue key='$key' value='$value']"
97+
}
98+
99+
function TeamCity-CreateInfoDocument([string]$buildNumber='', [boolean]$status=$true, [string[]]$statusText=$null, [System.Collections.IDictionary]$statistics=$null) {
100+
$doc=New-Object xml;
101+
$buildEl=$doc.CreateElement('build');
102+
103+
if (![string]::IsNullOrEmpty($buildNumber)) {
104+
$buildEl.SetAttribute('number', $buildNumber);
105+
}
106+
107+
$buildEl=$doc.AppendChild($buildEl);
108+
109+
$statusEl=$doc.CreateElement('statusInfo');
110+
if ($status) {
111+
$statusEl.SetAttribute('status', 'SUCCESS');
112+
} else {
113+
$statusEl.SetAttribute('status', 'FAILURE');
114+
}
115+
116+
if ($statusText -ne $null) {
117+
foreach ($text in $statusText) {
118+
$textEl=$doc.CreateElement('text');
119+
$textEl.SetAttribute('action', 'append');
120+
$textEl.set_InnerText($text);
121+
$textEl=$statusEl.AppendChild($textEl);
122+
}
123+
}
124+
125+
$statusEl=$buildEl.AppendChild($statusEl);
126+
127+
if ($statistics -ne $null) {
128+
foreach ($key in $statistics.Keys) {
129+
$val=$statistics.$key
130+
if ($val -eq $null) {
131+
$val=''
132+
}
133+
134+
$statEl=$doc.CreateElement('statisticsValue');
135+
$statEl.SetAttribute('key', $key);
136+
$statEl.SetAttribute('value', $val.ToString());
137+
$statEl=$buildEl.AppendChild($statEl);
138+
}
139+
}
140+
141+
return $doc;
142+
}
143+
144+
function TeamCity-WriteInfoDocument([xml]$doc) {
145+
$dir=(Split-Path $buildFile)
146+
$path=(Join-Path $dir 'teamcity-info.xml')
147+
148+
$doc.Save($path);
149+
}

0 commit comments

Comments
 (0)