Skip to content

Commit 9ace0f0

Browse files
SCAN4NET-94 Fix Choco packaging scripts (#2211)
1 parent 8483352 commit 9ace0f0

File tree

3 files changed

+159
-48
lines changed

3 files changed

+159
-48
lines changed

azure-pipelines.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,17 @@ stages:
219219
Add-Content build/version.txt $(SONAR_PROJECT_VERSION)
220220
displayName: 'Write project version in file'
221221
222-
223222
- task: DownloadSecureFile@1
224223
displayName: 'Download Maven settings'
225224
name: mavenSettings
226225
inputs:
227226
secureFile: 'maven-settings.xml'
228227

229-
- powershell: .\scripts\generate-packages.ps1 -sourcesDirectory "$(Build.SourcesDirectory)"
228+
- powershell: |
229+
. .\scripts\generate-packages.ps1
230+
Run
230231
displayName: 'Generate packages'
231232
232-
233233
- task: Maven@3
234234
displayName: Promote new version in pom
235235
inputs:

scripts/generate-packages.ps1

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,65 @@
1-
param ($sourcesDirectory)
2-
3-
[xml]$versionProps = Get-Content "$env:BUILD_SOURCESDIRECTORY\scripts\version\Version.props"
4-
$version= $versionProps.Project.PropertyGroup.MainVersion + $versionProps.Project.PropertyGroup.PrereleaseSuffix
5-
6-
$artifactsFolder = "$env:BUILD_SOURCESDIRECTORY\\build"
7-
8-
$netFrameworkScannerZipPath = Get-Item "$artifactsFolder\\sonarscanner-net-framework.zip"
9-
$netScannerZipPath = Get-Item "$artifactsFolder\\sonarscanner-net.zip"
10-
$netScannerGlobalToolPath = Get-Item "$artifactsFolder\\dotnet-sonarscanner.$version.nupkg"
11-
$sbomJsonPath = Get-Item "$sourcesDirectory\build\bom.json"
12-
13-
Write-Host "Generating the chocolatey packages"
14-
$netFrameworkZipPath = (Get-FileHash $netFrameworkScannerZipPath -Algorithm SHA256).hash
15-
$netFrameworkPs1 = "nuspec\chocolatey\chocolateyInstall-net-framework.ps1"
16-
(Get-Content $netFrameworkPs1) `
17-
-Replace '-Checksum "not-set"', "-Checksum $netFrameworkZipPath" `
18-
-Replace "__PackageVersion__", "$version" `
19-
| Set-Content $netFrameworkPs1
20-
21-
$netZipHash = (Get-FileHash $netScannerZipPath -Algorithm SHA256).hash
22-
$netPs1 = "nuspec\chocolatey\chocolateyInstall-net.ps1"
23-
(Get-Content $netPs1) `
24-
-Replace '-Checksum "not-set"', "-Checksum $netZipHash" `
25-
-Replace "__PackageVersion__", "$version" `
26-
| Set-Content $netPs1
27-
28-
choco pack nuspec\chocolatey\sonarscanner-net-framework.nuspec `
29-
--outputdirectory $artifactsFolder `
30-
--version $version
31-
32-
choco pack nuspec\chocolatey\sonarscanner-net.nuspec `
33-
--outputdirectory $artifactsFolder `
34-
--version $version
35-
36-
Write-Host "Update artifacts locations in pom.xml"
37-
$pomFile = ".\pom.xml"
38-
(Get-Content $pomFile) `
39-
-Replace 'netFrameworkScannerZipPath', "$netFrameworkScannerZipPath" `
40-
-Replace 'netScannerZipPath', "$netScannerZipPath" `
41-
-Replace 'netScannerGlobalToolPath', "$netScannerGlobalToolPath" `
42-
-Replace 'netFrameworkScannerChocoPath', "$artifactsFolder\\sonarscanner-net-framework.$version.nupkg" `
43-
-Replace 'netScannerChocoPath', "$artifactsFolder\\sonarscanner-net.$version.nupkg" `
44-
-Replace 'sbomPath', "$sbomJsonPath" `
45-
| Set-Content $pomFile
1+
# This script generates the chocolatey packages for the .NET Scanner and the .NET Framework Scanner and updates the pom.xml file with the new artifacts locations.
2+
3+
# The chocolatey packages are generated from the nuspec files located in the nuspec\chocolatey folder and they point to GitHub artifacts that have the following format:
4+
# Release candidates:
5+
# - https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/9.0.0-rc.99116/sonar-scanner-9.0.0-rc.99116-net.zip
6+
# - https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/9.0.0-rc.99116/sonar-scanner-9.0.0-rc.99116-net-framework.zip
7+
# Normal releases:
8+
# - https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/8.0.3.99785/sonar-scanner-8.0.3.99785-net.zip
9+
# - https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/8.0.3.99785/sonar-scanner-8.0.3.99785-net-framework.zip
10+
11+
# In the case of pre-releases, the full version is following the Sem 2 versioning format: 9.0.0-rc.99116.
12+
# Unfortunatelly Choco only supports Sem 1 versioning format, which does not allow anything except for [0-9A-Za-z-] after the dash in `-rc`.
13+
# Due to this, when calling `choco pack` the version should not contain the build number (9.0.0-rc).
14+
# At the same time the the url inside the ps1 file that downloads the scanner should be correct and contain the build number.
15+
16+
param (
17+
[string] $sourcesDirectory = $env:BUILD_SOURCESDIRECTORY,
18+
[string] $buildId = $env:BUILD_BUILDID
19+
)
20+
21+
function Update-Choco-Package([string] $scannerZipFileName, [string] $runtimeSuffix) {
22+
Write-Host "Generating the chocolatey package from $scannerZipFileName"
23+
24+
$hash = (Get-FileHash $scannerZipFileName -Algorithm SHA256).hash
25+
$powershellScriptPath = "nuspec/chocolatey/chocolateyInstall-$runtimeSuffix.ps1"
26+
Write-Host (Get-Item $powershellScriptPath).FullName
27+
(Get-Content $powershellScriptPath) `
28+
-Replace '-Checksum "not-set"', "-Checksum $hash" `
29+
-Replace '__PackageVersion__', "$fullVersion" `
30+
| Set-Content $powershellScriptPath
31+
32+
choco pack "nuspec/chocolatey/sonarscanner-$runtimeSuffix.nuspec" --outputdirectory $artifactsFolder --version $shortVersion
33+
}
34+
35+
function Update-Pom-File() {
36+
Write-Host 'Update artifacts locations in pom.xml'
37+
$sbomJsonPath = Get-Item "$sourcesDirectory/build/bom.json"
38+
$netScannerGlobalToolPath = Get-Item "$artifactsFolder/dotnet-sonarscanner.$shortVersion.nupkg" # dotnet-sonarscanner.9.0.0-rc.nupkg or dotnet-sonarscanner.9.0.0.nupkg
39+
$pomFile = './pom.xml'
40+
(Get-Content $pomFile) `
41+
-Replace 'netFrameworkScannerZipPath', "$netFrameworkScannerZipPath" `
42+
-Replace 'netScannerZipPath', "$netScannerZipPath" `
43+
-Replace 'netScannerGlobalToolPath', "$netScannerGlobalToolPath" `
44+
-Replace 'netFrameworkScannerChocoPath', "$artifactsFolder/sonarscanner-net-framework.$shortVersion.nupkg" `
45+
-Replace 'netScannerChocoPath', "$artifactsFolder/sonarscanner-net.$shortVersion.nupkg" `
46+
-Replace 'sbomPath', "$sbomJsonPath" `
47+
| Set-Content $pomFile
48+
}
49+
50+
function Run() {
51+
Update-Choco-Package $netFrameworkScannerZipPath 'net-framework'
52+
Update-Choco-Package $netScannerZipPath 'net'
53+
Update-Pom-File
54+
}
55+
56+
# Read the version from the Version.props file and initialize the global variables.
57+
$artifactsFolder = "$sourcesDirectory/build"
58+
$netFrameworkScannerZipPath = Get-Item "$artifactsFolder/sonarscanner-net-framework.zip"
59+
$netScannerZipPath = Get-Item "$artifactsFolder/sonarscanner-net.zip"
60+
61+
Write-Host 'Reading version information from Version.props file'
62+
[xml] $versionProps = Get-Content "$sourcesDirectory/scripts/version/Version.props"
63+
$shortVersion = $versionProps.Project.PropertyGroup.MainVersion + $versionProps.Project.PropertyGroup.PrereleaseSuffix # 9.0.0-rc for release candidates or 9.0.0 for normal releases.
64+
$fullVersion = $shortVersion + '.' + $buildId # 9.0.0-rc.99116 for release candidates or 9.0.0.99116 for normal releases.
65+
Write-Host "Short version is $shortVersion, Full version is $fullVersion"
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Unit tests for the generate-packages.ps1 script.
2+
# Preconditions:
3+
# - Install chocolatey: https://docs.chocolatey.org/en-us/choco/setup/#install-with-powershellexe
4+
# - Install Pester: Install-Module -Name Pester -Force -SkipPublisherCheck (https://pester.dev/docs/introduction/installation#windows)
5+
# Running:
6+
# - Open a PowerShell terminal.
7+
# - Generate binaries: .\scripts\its-build.ps1
8+
# - Package the scanner: nuget pack nuspec\netcoreglobaltool\dotnet-sonarscanner.nuspec
9+
# - Run the tests: Invoke-Pester -Output Detailed .\scripts\generate-packages.tests.ps1
10+
11+
BeforeAll {
12+
New-Item -Force -Path "$PSScriptRoot" -Name "testcontext" -ItemType Directory
13+
New-Item -Force -Path "$PSScriptRoot/testcontext" -Name "nuspec" -ItemType Directory
14+
New-Item -Force -Path "$PSScriptRoot/testcontext" -Name "build" -ItemType Directory
15+
New-Item -Force -Path "$PSScriptRoot/testcontext/build" -Name "sonarscanner-net.zip" -type File
16+
New-Item -Force -Path "$PSScriptRoot/testcontext/build" -Name "sonarscanner-net-framework.zip" -type File
17+
New-Item -Force -Path "$PSScriptRoot/testcontext/scripts/version" -Name "Version.props" -type File
18+
19+
Set-Location "$PSScriptRoot/testcontext"
20+
21+
function CheckVersion([string] $packageFileName, [string] $nuspecFileName, [string] $expectedVersion) {
22+
Rename-Item -Path "build/$packageFileName" -NewName 'temp.zip'
23+
Expand-Archive 'build/temp.zip' -DestinationPath 'build/temp'
24+
25+
Get-Content -Raw "build/temp/$nuspecFileName" | Should -Match $expectedVersion
26+
27+
Remove-Item -Path 'build/temp.zip'
28+
Remove-Item -Path 'build/temp' -Recurse
29+
}
30+
31+
function Set-Version([string] $version, [string] $prereleaseSuffix) {
32+
Set-Content -Path 'scripts/version/Version.props' -Value "<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
33+
<PropertyGroup>
34+
<MainVersion>$version</MainVersion>
35+
<BuildNumber>123456789</BuildNumber>
36+
<PrereleaseSuffix>$prereleaseSuffix</PrereleaseSuffix>
37+
</PropertyGroup>
38+
</Project>"
39+
}
40+
}
41+
42+
AfterAll {
43+
Set-Location $PSScriptRoot
44+
Remove-Item -Path "$PSScriptRoot/testcontext" -Recurse
45+
}
46+
47+
Describe 'Choco package generation' {
48+
Describe 'Main' {
49+
It 'Sets short and long versions' -TestCases @(
50+
@{ Version = '1.2.3'; PreReleaseSuffix = '-rc'; ExpectedShortVersion = '1.2.3-rc'; ExpectedFullVersion = '1.2.3-rc.99116' }
51+
@{ Version = '1.2.3'; PreReleaseSuffix = ''; ExpectedShortVersion = '1.2.3'; ExpectedFullVersion = '1.2.3.99116' }
52+
) {
53+
Set-Version $Version $PreReleaseSuffix
54+
55+
. $PSScriptRoot/generate-packages.ps1 -sourcesDirectory . -buildId 99116
56+
57+
$shortVersion | Should -Be $ExpectedShortVersion
58+
$fullVersion | Should -Be $ExpectedFullVersion
59+
}
60+
}
61+
62+
Describe 'Update-Choco-Package' {
63+
BeforeEach {
64+
Remove-Item -Path 'nuspec' -Recurse
65+
Copy-Item -Path "../../nuspec/chocolatey/" -Destination "nuspec/chocolatey" -Recurse
66+
}
67+
68+
It 'Sets the package name, version and url' -TestCases @(
69+
@{ Version = '1.2.3'; PreReleaseSuffix = '-rc'; ExpectedShortVersion = '1.2.3-rc'; ExpectedFullVersion = '1.2.3-rc.99116' }
70+
@{ Version = '1.2.3'; PreReleaseSuffix = ''; ExpectedShortVersion = '1.2.3'; ExpectedFullVersion = '1.2.3.99116' }
71+
) {
72+
Set-Version $Version $PreReleaseSuffix
73+
74+
. $PSScriptRoot/generate-packages.ps1 -sourcesDirectory . -buildId 99116
75+
76+
Update-Choco-Package $netFrameworkScannerZipPath 'net-framework'
77+
78+
$unzipLocation = '$(Split-Path -parent $MyInvocation.MyCommand.Definition)'
79+
Get-Content -Raw 'nuspec/chocolatey/chocolateyInstall-net-framework.ps1' | Should -BeExactly "Install-ChocolateyZipPackage ""sonarscanner-net-framework"" ``
80+
-Url ""https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/$ExpectedFullVersion/sonar-scanner-$ExpectedFullVersion-net-framework.zip"" ``
81+
-UnzipLocation ""$unzipLocation"" ``
82+
-ChecksumType 'sha256' ``
83+
-Checksum E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
84+
"
85+
86+
"build/sonarscanner-net-framework.$ExpectedShortVersion.nupkg" | Should -Exist
87+
88+
CheckVersion "sonarscanner-net-framework.$ExpectedShortVersion.nupkg" 'sonarscanner-net-framework.nuspec' "<version>$ExpectedShortVersion</version>"
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)