1+ #
2+ # Creates the Chocolatey package version based on assembly version,
3+ # and whether there is already a CI revision set.
4+ #
5+ param ([String ]$pat , [String ]$targetPackageName = ' typewriter' )
6+
7+ # Get the assembly version from the built library.
8+ $pathToTypewriter = $env: Build_SourcesDirectory + ' \src\Typewriter\bin\Release\Typewriter.exe'
9+ $fileInfo = [Diagnostics.FileVersionInfo ]::GetVersionInfo($pathToTypewriter )
10+ $assemblySemver = $fileInfo.FileVersion.ToString ()
11+
12+ Write-Host " Target package: $targetPackageName " - ForegroundColor Green
13+
14+ Write-Host " Assembly version: $assemblySemver " - ForegroundColor Green
15+
16+ $finalPackageVersion = " "
17+ $ciToken = " -ci"
18+
19+ # Get information about the targetPackage from the Feed/Packages Azure DevOps REST API
20+ # https://docs.microsoft.com/en-us/rest/api/vsts/packaging/packages/list?view=vsts-rest-5.0
21+ # f887743a-88c6-4716-b4aa-50761b543a15 is the MicrosoftGraph package feed.
22+ $feedQuery = ' https://feeds.dev.azure.com/o365exchange/_apis/packaging/Feeds/f887743a-88c6-4716-b4aa-50761b543a15/packages?api-version=5.0-preview.1'
23+ $headerValue = " :" + $pat
24+ $encodedPat = [Convert ]::ToBase64String([Text.Encoding ]::UTF8.GetBytes($headerValue ))
25+ $webclient = new-object System.Net.WebClient
26+ $webclient.Headers.Add (" Authorization" , " Basic " + $encodedPat )
27+
28+ $jsonObject = $webclient.DownloadString ($feedQuery ) | ConvertFrom-Json
29+
30+ # Query the JSON object and get the package identifier for the target package name.
31+ # $packageID = ($jsonObject.value | ? normalizedName -eq $targetPackageName).id
32+
33+ # Query the results of the Feed/Packages call for our target package.
34+ $packageFeedNormalizedVersion = ($jsonObject.value | ? normalizedName -eq $targetPackageName ).versions.normalizedVersion
35+
36+ Write-Host " Package feed version: $packageFeedNormalizedVersion " - ForegroundColor Green
37+
38+ # The string to split could be in the following two forms: "n.n.n" or "n.n.n-ci*"
39+ $packageFeedNormalizedVersionParts = $packageFeedNormalizedVersion.Split (' -' )
40+
41+ # Check whether the assembly version and feed version match.
42+ if ($packageFeedNormalizedVersionParts [0 ] -eq $assemblySemver ) {
43+ # The assembly version and feed version match.
44+
45+ # Check whether the latest package in the feed has been updated with a ci release, eg '-ci*'
46+ if ($packageFeedNormalizedVersionParts.length -eq 1 ) {
47+ # Append the starting ci revision.
48+ $finalPackageVersion = $assemblySemver + $ciToken + " 0"
49+ }
50+ else {
51+ # increment the ci revision, eg "0.1.2-ci3" become "0.1.2-ci4"
52+ $number = $packageFeedNormalizedVersionParts [1 ].Remove(0 , 2 ) -as [int ] # removing 'ci'
53+ $finalPackageVersion = $assemblySemver + $ciToken + ($number += 1 )
54+ }
55+
56+ }
57+ # The don't match so just use the assembly version.
58+ else {
59+ $finalPackageVersion = $assemblySemver
60+ }
61+
62+ Write-Host " Final package version: $finalPackageVersion " - ForegroundColor Green
63+
64+ # Create Typewriter package
65+ choco pack Typewriter.nuspec -- version= $finalPackageVersion -- out= $env: Build_ArtifactStagingDirectory
0 commit comments