1- # Install PowerShell for your platform :
1+ # Install PowerShell Core :
22# https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell
33# Install Invoke-Build:
44# https://github.com/nightroman/Invoke-Build
77# Invoke-Build build
88# Invoke-Build ? # this lists available tasks
99
10+ param (
11+ $NuGetApiPushKey = ( property NuGetApiPushKey ' MISSING' ),
12+ $LocalPackageDir = ( property LocalPackageDir ' C:\code\LocalPackages' )
13+ )
14+
15+ $baseProjectName = " BinaryToTextEncoding"
16+ $basePackageName = " Zanaptak.$baseProjectName "
17+
18+ task . Build
19+
1020task Clean {
1121 exec { dotnet clean .\src - c Release }
1222}
@@ -15,12 +25,9 @@ task Build {
1525 exec { dotnet build .\src - c Release }
1626}
1727
18- task Pack Clean , Build, {
19- exec { dotnet pack .\src - c Release }
20- }
21-
2228task TestJs {
2329 Set-Location .\test
30+ if ( -not ( Test-Path node_modules ) ) { exec { npm install } }
2431 remove bld
2532 exec { npm test }
2633}
@@ -34,7 +41,97 @@ task Test TestJs, TestNet
3441
3542task Benchmark Clean , Build, {
3643 Set-Location .\benchmark
37- exec { dotnet run - p BinaryToTextEncoding.Benchmark.fsproj - c Release }
44+ exec { dotnet run - c Release }
3845}
3946
40- task . Build
47+ task Pack Clean , Build, {
48+ exec { dotnet pack .\src - c Release }
49+ }
50+
51+ task PackInternal Clean , Build, GetVersion, {
52+ $yearStart = Get-Date - Year ( ( Get-Date ).Year ) - Month 1 - Day 1 - Hour 0 - Minute 0 - Second 0 - Millisecond 0
53+ $now = Get-Date
54+ $buildSuffix = [ int ] ( ( $now - $yearStart ).TotalSeconds )
55+ $internalVersion = " $Version .$buildSuffix "
56+ exec { dotnet pack .\src - c Release - p:PackageVersion= $internalVersion }
57+ $filename = " $basePackageName .$internalVersion .nupkg"
58+ Copy-Item .\src\bin\Release\$filename $LocalPackageDir
59+ Write-Build Green " Copied $filename to $LocalPackageDir "
60+ }
61+
62+ function UpdateProjectFile (
63+ [ string ] $Filename ,
64+ [ string ] $XPath ,
65+ [ string ] $Value
66+ ) {
67+
68+ $xml = New-Object System.Xml.XmlDocument
69+ $xml.PreserveWhitespace = $true
70+ $xml.Load ( $Filename )
71+
72+ $node = $xml.SelectSingleNode ( $XPath )
73+ if ( -not ( $node ) ) { throw " xpath not found" }
74+ $node.InnerText = $Value
75+
76+ $settings = New-Object System.Xml.XmlWriterSettings
77+ $settings.OmitXmlDeclaration = $true
78+ $settings.Encoding = New-Object System.Text.UTF8Encoding( $true )
79+
80+ $writer = [ System.Xml.XmlWriter ]::Create( $Filename , $settings )
81+ try {
82+ $xml.Save ( $writer )
83+ } finally {
84+ $writer.Dispose ()
85+ }
86+ }
87+
88+ task IncrementMinor GetVersion, {
89+ if ( $Version -match " ^(\d+)\.(\d+)\.(\d+)$" ) {
90+ $projectFile = " $BuildRoot \src\$baseProjectName .fsproj"
91+ $major = $Matches [ 1 ]
92+ $minor = $Matches [ 2 ]
93+ $patch = $Matches [ 3 ]
94+ $newMinor = ( [ int ] $minor ) + 1
95+ $newVersion = " $major .$newMinor .0"
96+ UpdateProjectFile $projectFile ' /Project/PropertyGroup/Version' $newVersion
97+ Write-Build Green " Updated version to $newVersion "
98+ }
99+ else {
100+ throw " invalid version: $Version "
101+ }
102+ }
103+
104+ task IncrementPatch GetVersion, {
105+ if ( $Version -match " ^(\d+)\.(\d+)\.(\d+)$" ) {
106+ $projectFile = " $BuildRoot \src\$baseProjectName .fsproj"
107+ $major = $Matches [ 1 ]
108+ $minor = $Matches [ 2 ]
109+ $patch = $Matches [ 3 ]
110+ $newPatch = ( [ int ] $patch ) + 1
111+ $newVersion = " $major .$minor .$newPatch "
112+ UpdateProjectFile $projectFile ' /Project/PropertyGroup/Version' $newVersion
113+ Write-Build Green " Updated version to $newVersion "
114+ }
115+ else {
116+ throw " invalid version: $Version "
117+ }
118+ }
119+
120+ task GetVersion {
121+ $script :Version = Select-Xml - Path " .\src\$baseProjectName .fsproj" - XPath / Project/ PropertyGroup/ Version | % { $_.Node.InnerXml.Trim () }
122+ }
123+
124+ task UploadNuGet EnsureCommitted, GetVersion, {
125+ if ( $NuGetApiPushKey -eq " MISSING" ) { throw " NuGet key not provided" }
126+ Set-Location ./ src/ bin/ Release
127+ $filename = " $basePackageName .$Version .nupkg"
128+ if ( -not ( Test-Path $filename ) ) { throw " nupkg file not found" }
129+ $lastHour = ( Get-Date ).AddHours( -1 )
130+ if ( ( Get-ChildItem $filename ).LastWriteTime -lt $lastHour ) { throw " nupkg file too old" }
131+ exec { dotnet nuget push $filename - k $NuGetApiPushKey - s https:// api.nuget.org/ v3/ index.json }
132+ }
133+
134+ task EnsureCommitted {
135+ $gitoutput = exec { git status - s - uall }
136+ if ( $gitoutput ) { throw " uncommitted changes exist in working directory" }
137+ }
0 commit comments