Skip to content

Commit 3240e90

Browse files
authored
Flexible nuget package versioning
1 parent 74240b0 commit 3240e90

11 files changed

+190
-91
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ version: 0.1.{build}
66
#images
77

88
image:
9-
- Ubuntu1804
109
- Visual Studio 2017
10+
- Ubuntu1804
1111

1212
# Set build info
1313
environment:

dbops.psm1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ Set-PSFConfig -FullName dbops.security.usecustomencryptionkey -Value ($PSVersion
173173
Set-PSFConfig -FullName dbops.rdbms.type -Value 'SqlServer' -Validation connectionType -Initialize -Description "Assumes a certain RDBMS as a default one for each command. SQLServer by default"
174174
Set-PSFConfig -FullName dbops.package.slim -Value $false -Validation bool -Initialize -Description "Decides whether to make the packages 'slim' and omit module files when creating the package. Default: `$false"
175175
Set-PSFConfig -FullName dbops.config.variabletoken -Value "\#\{(token)\}" -Validation tokenRegex -Initialize -Description "Variable replacement token. Regex string that will be replaced with values from -Variables parameters. Default: \#\{(token)\}"
176+
Set-PSFConfig -FullName dbops.runtime.dotnetversion -Value ((dotnet --version) -as [version]) -Description "Current dotnet runtime." -Hidden
176177

177178
# extensions for SMO
178179
$typeData = Get-TypeData -TypeName 'Microsoft.SqlServer.Management.Smo.Database'

functions/Install-DBOSupportLibrary.ps1

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Function Install-DBOSupportLibrary {
1818
.PARAMETER SkipDependencies
1919
Skips dependencies of the package with the connectivity libraries, only downloading a single package.
2020
21+
.PARAMETER SkipPreRelease
22+
Skip pre-release versions of the packages to be downloaded.
23+
2124
.PARAMETER Confirm
2225
Prompts to confirm certain actions
2326
@@ -41,6 +44,7 @@ Function Install-DBOSupportLibrary {
4144
[ValidateSet('CurrentUser', 'AllUsers')]
4245
[string]$Scope = 'AllUsers',
4346
#[switch]$SkipDependencies, # disabling for now, dependencies are not supported anyways
47+
[switch]$SkipPreRelease,
4448
[switch]$Force
4549
)
4650
begin {
@@ -52,15 +56,21 @@ Function Install-DBOSupportLibrary {
5256
foreach ($t in $Type) {
5357
# Check existance
5458
foreach ($package in $dependencies.$t) {
55-
$p = Get-Package -name $package.Name -RequiredVersion $package.Version -ProviderName nuget -ErrorAction SilentlyContinue
56-
if (-Not $p -or $Force) { $packagesToUpdate += $package }
59+
$packageSplat = @{
60+
Name = $package.Name
61+
MinimumVersion = $package.MinimumVersion
62+
MaximumVersion = $package.MaximumVersion
63+
RequiredVersion = $package.RequiredVersion
64+
}
65+
$p = Get-Package @packageSplat -ProviderName nuget -ErrorAction SilentlyContinue
66+
if (-Not $p -or $Force) { $packagesToUpdate += $packageSplat }
5767
}
5868
}
5969
if ($packagesToUpdate -and $PSCmdlet.ShouldProcess("Scope: $Scope", "Installing dependent package(s) $($packagesToUpdate.Name -join ', ') from nuget.org")) {
6070
# Install dependencies
61-
foreach ($package in $packagesToUpdate) {
62-
Write-PSFMessage -Level Verbose -Message "Installing package $($package.Name)($($package.Version))"
63-
$null = Install-NugetPackage -Name $package.Name -RequiredVersion $package.Version -Force:$Force -Scope $Scope
71+
foreach ($packageSplat in $packagesToUpdate) {
72+
Write-PSFMessage -Level Verbose -Message "Installing package`: $($packageSplat | ConvertTo-Json -Compress)"
73+
$null = Install-NugetPackage @packageSplat -Force:$Force -Scope $Scope -SkipPreRelease:$SkipPreRelease
6474
}
6575
}
6676
}

functions/Test-DBOSupportedSystem.ps1

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,23 @@ Function Test-DBOSupportedSystem {
2222
[Alias('System', 'Database')]
2323
[DBOps.ConnectionType]$Type
2424
)
25-
begin {}
25+
begin { }
2626
process {
2727
$dependencies = Get-ExternalLibrary -Type $Type
2828
foreach ($package in $dependencies) {
29-
$packageEntry = Get-Package $package.Name -RequiredVersion $package.Version -ProviderName nuget -ErrorAction SilentlyContinue
29+
$packageSplat = @{
30+
Name = $package.Name
31+
MinimumVersion = $package.MinimumVersion
32+
MaximumVersion = $package.MaximumVersion
33+
RequiredVersion = $package.RequiredVersion
34+
ProviderName = "nuget"
35+
}
36+
$packageEntry = Get-Package @packageSplat -ErrorAction SilentlyContinue
3037
if (!$packageEntry) {
3138
return $false
3239
}
3340
}
3441
return $true
3542
}
36-
end {}
43+
end { }
3744
}

internal/functions/Get-ExternalLibrary.ps1

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,37 @@ function Get-ExternalLibrary {
33
Param (
44
[DBOps.ConnectionType]$Type
55
)
6+
Function Get-PackageList {
7+
Param (
8+
$Node
9+
)
10+
$jsonClause = {
11+
((-Not $_.PSEdition) -or ($_.PSEdition -eq $PSVersionTable.PSEdition)) -and
12+
((-Not $runtime -and -Not $_.DotNetCore) -or (-Not $_.DotNetCore) -or ($runtime -and $runtime -ge [version]$_.DotNetCore))
13+
}
14+
$output = @()
15+
$applicableDependencies = $Node | Where-Object $jsonClause
16+
$groupedDependencies = $applicableDependencies | Group-Object -Property Name
17+
foreach ($group in $groupedDependencies) {
18+
$selectedGroup = $group.Group | Sort-Object -Property @{ Expression = { $_.DotNetCore -as [version] }; Descending = $true } | Select-Object -First 1
19+
if ($selectedGroup.Dependencies) {
20+
$output += Get-PackageList $selectedGroup.Dependencies
21+
}
22+
$output += $selectedGroup | Select-Object Name, MinimumVersion, RequiredVersion, MaximumVersion, Path
23+
}
24+
return $output
25+
}
626
$jsonFile = Join-PSFPath -Normalize (Get-Item $PSScriptRoot).Parent.FullName "json\dbops.dependencies.json"
7-
$d = Get-Content $jsonFile -Raw | ConvertFrom-Json
8-
if ($null -ne $Type) { $d.$Type | Where-Object { -Not $_.PSEdition -or $_.PSEdition -eq $PSVersionTable.PSEdition } }
27+
$dependencies = Get-Content $jsonFile -Raw | ConvertFrom-Json
28+
# this is declared at module import
29+
$runtime = Get-PSFConfigValue dbops.runtime.dotnetversion
30+
31+
if ($null -ne $Type) { Get-PackageList $dependencies.$Type }
932
else {
10-
$rdbms = $d | Get-Member | Where-Object MemberType -eq NoteProperty | Select-Object -ExpandProperty Name
11-
$output = @{}
33+
$rdbms = $dependencies | Get-Member | Where-Object MemberType -eq NoteProperty | Select-Object -ExpandProperty Name
34+
$output = @{ }
1235
foreach ($t in $rdbms) {
13-
$output += @{
14-
$t = $d.$t | Where-Object { -Not $_.PSEdition -or $_.PSEdition -eq $PSVersionTable.PSEdition }
15-
}
36+
$output.$t = Get-PackageList $dependencies.$t
1637
}
1738
[pscustomobject]$output
1839
}

internal/functions/Initialize-ExternalLibrary.ps1

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,26 @@ function Initialize-ExternalLibrary {
99
$dependencies = Get-ExternalLibrary -Type $Type
1010
$isLoaded = $true
1111
foreach ($dPackage in $dependencies) {
12-
if ($libs.Name -notcontains $dPackage.Name) {
13-
$isLoaded = $false
14-
break
12+
if ($lib = $libs | Where-Object Name -eq $dPackage.Name) {
13+
if ($minVersion = $dPackage.MinimumVersion -as [version]) {
14+
if ($minVersion -gt $lib.Version) {
15+
$isLoaded = $false; break
16+
}
17+
}
18+
if ($maxVersion = $dPackage.MaximumVersion -as [version]) {
19+
if ($maxVersion -lt $lib.Version) {
20+
$isLoaded = $false; break
21+
}
22+
}
23+
if ($reqVersion = $dPackage.RequiredVersion -as [version]) {
24+
if ($reqVersion -eq $lib.Version) {
25+
$isLoaded = $false; break
26+
}
27+
}
28+
Write-PSFMessage -Level Verbose -Message "$($lib.Name) $($lib.Version) was found among the loaded libraries, assuming that the library is fully loaded"
1529
}
1630
else {
17-
Write-PSFMessage -Level Verbose -Message "$($dPackage.Name) was found among the loaded libraries, assuming that the library is fully loaded"
31+
$isLoaded = $false; break
1832
}
1933
}
2034
if ($isLoaded) {
@@ -34,9 +48,16 @@ function Initialize-ExternalLibrary {
3448
}
3549
}
3650
$dependencies = Get-ExternalLibrary -Type $Type
37-
foreach ($dPackage in $dependencies) {
38-
$localPackage = Get-Package -Name $dPackage.Name -RequiredVersion $dPackage.Version -ProviderName nuget -ErrorAction Stop
39-
foreach ($dPath in $dPackage.Path) {
51+
foreach ($package in $dependencies) {
52+
$packageSplat = @{
53+
Name = $package.Name
54+
MinimumVersion = $package.MinimumVersion
55+
MaximumVersion = $package.MaximumVersion
56+
RequiredVersion = $package.RequiredVersion
57+
ProviderName = "nuget"
58+
}
59+
$localPackage = Get-Package @packageSplat
60+
foreach ($dPath in $package.Path) {
4061
Write-PSFMessage -Level Debug -Message "Loading library $dPath from $($localPackage.Source)"
4162
try {
4263
$null = Add-Type -Path (Join-PSFPath -Normalize (Split-Path $localPackage.Source -Parent) $dPath) -ErrorAction SilentlyContinue

internal/functions/Install-NugetPackage.ps1

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,47 @@ function Install-NugetPackage {
2525
$packageInfoObject = $packageInfoResponse.Content | ConvertFrom-Json
2626
$packageInfo = $packageInfoObject.data | Select-Object -First 1
2727
if (-Not $packageInfo) {
28-
Write-PSFMessage -Level Critical -Message "Package $Name was not found"
28+
Stop-PSFFunction -Message "Package $Name was not found"
2929
}
3030
$packageName = $packageInfo.id
3131
$packageLowerName = $packageName.ToLower()
32-
33-
# get package versions
34-
$baseAddressUrl = $indexObject.resources | Where-Object { $_.'@type' -eq 'PackageBaseAddress/3.0.0' } | Select-Object -First 1
35-
$packageVersions = Invoke-WebRequest -Uri "$($baseAddressUrl.'@id')$packageLowerName/index.json" -ErrorAction Stop
36-
$packageVersionsObject = $packageVersions.Content | ConvertFrom-Json
37-
[array]$versionList = $packageVersionsObject.Versions
38-
Write-PSFMessage -Level Verbose -Message "Found a total of $($versionList.Count) versions of $packageName"
39-
40-
# filter out the versions we don't need based on parameters
41-
if ($SkipPreRelease) {
42-
$versionList = $versionList | Where-Object { try { [version]$_ } catch { $false } }
43-
}
44-
if ($MinimumVersion) {
45-
$versionList = $versionList | Where-Object { $_ -ge $MinimumVersion }
46-
}
47-
if ($MaximumVersion) {
48-
$versionList = $versionList | Where-Object { $_ -le $MaximumVersion }
49-
}
5032
if ($RequiredVersion) {
51-
$versionList = $versionList | Where-Object { $_ -eq $RequiredVersion }
33+
$versionList = @($RequiredVersion)
34+
Write-PSFMessage -Level Verbose -Message "Using RequiredVersion $RequiredVersion of $packageName"
35+
}
36+
else {
37+
[array]$versionList = $packageInfo.versions.version
38+
Write-PSFMessage -Level Verbose -Message "Found a total of $($versionList.Count) versions of $packageName"
39+
40+
# filter out the versions we don't need based on parameters
41+
if ($versionList -and $MinimumVersion) {
42+
$position = $versionList.IndexOf($MinimumVersion)
43+
if ($position -eq -1) {
44+
$versionList = $versionList | Where-Object { try { [version]$_ -ge $MinimumVersion } catch { $false } }
45+
}
46+
else {
47+
$versionList = $versionList[$position..($versionList.Count - 1)]
48+
}
49+
}
50+
if ($versionList -and $MaximumVersion) {
51+
$position = $versionList.IndexOf($MaximumVersion)
52+
if ($position -eq -1) {
53+
$versionList = $versionList | Where-Object { try { [version]$_ -le $MaximumVersion } catch { $false } }
54+
}
55+
else {
56+
$versionList = $versionList[0..$position]
57+
}
58+
}
59+
Write-PSFMessage -Level Verbose -Message "$($versionList.Count) versions left after applying filters"
5260
}
53-
Write-PSFMessage -Level Verbose -Message "$($versionList.Count) versions left after applying filters"
54-
$selectedVersion = $versionList | Sort-Object -Descending | Select-Object -First 1
61+
62+
$selectedVersion = $versionList | Select-Object -Last 1
5563
if (-Not $selectedVersion) {
56-
Write-PSFMessage -Level Critical -Message "Version could not be found using current parameters" -EnableException $true
64+
Stop-PSFFunction -Message "Version could not be found using current parameters" -EnableException $true
5765
}
5866

5967
# download and extract the files
60-
Write-PSFMessage -Level Verbose -Message "Downloading version $selectedVersion of $packageName"
68+
Write-PSFMessage -Level Verbose -Message "Version $selectedVersion of $packageName was selected"
6169
$fileName = "$packageName.$selectedVersion.nupkg"
6270
# Path reference: https://github.com/OneGet/oneget/blob/master/src/Microsoft.PackageManagement/Utility/Platform/OSInformation.cs
6371
$scopePath = switch ($Scope) {
@@ -75,26 +83,35 @@ function Install-NugetPackage {
7583
}
7684
}
7785
$path = Join-PSFPath $scopePath "$packageName.$selectedVersion"
78-
$folder = New-Item -ItemType Directory -Path $path -Force
7986
$packagePath = Join-PSFPath $path $fileName
80-
if ((Test-Path $packagePath) -and -Not $Force) {
81-
Write-PSFMessage -Level Critical -Message "File $packagePath already exists at destination" -EnableException $true
82-
}
83-
$downloadUrl = "$($baseAddressUrl.'@id')$packageLowerName/$selectedVersion/$fileName"
84-
Invoke-WebRequest -Uri $downloadUrl -OutFile $packagePath -ErrorAction Stop
85-
Write-PSFMessage -Level Verbose -Message "Extracting $fileName to $folder"
86-
if ($isCoreCLR) {
87-
[System.IO.Compression.ZipFile]::ExtractToDirectory($packagePath, $folder, $true)
88-
}
89-
else {
90-
[System.IO.Compression.ZipFile]::ExtractToDirectory($packagePath, $folder)
91-
}
87+
if ($PSCmdlet.ShouldProcess($fileName, "Download package")) {
88+
if (Test-Path $path) {
89+
if ($Force) {
90+
Remove-Item $path -Recurse -Force
91+
}
92+
else {
93+
Write-PSFMessage -Level Critical -Message "$packageName.$selectedVersion already exists at destination" -EnableException $true
94+
}
95+
}
96+
$folder = New-Item -ItemType Directory -Path $path -Force
97+
98+
$baseAddressUrl = $indexObject.resources | Where-Object { $_.'@type' -eq 'PackageBaseAddress/3.0.0' } | Select-Object -First 1
99+
$downloadUrl = "$($baseAddressUrl.'@id')$packageLowerName/$selectedVersion/$fileName"
100+
Invoke-WebRequest -Uri $downloadUrl -OutFile $packagePath -ErrorAction Stop
101+
Write-PSFMessage -Level Verbose -Message "Extracting $fileName to $folder"
102+
if ($isCoreCLR) {
103+
[System.IO.Compression.ZipFile]::ExtractToDirectory($packagePath, $folder, $true)
104+
}
105+
else {
106+
[System.IO.Compression.ZipFile]::ExtractToDirectory($packagePath, $folder)
107+
}
92108

93-
#return output
94-
[PSCustomObject]@{
95-
Name = $packageName
96-
Source = $packagePath
97-
Version = $selectedVersion
98-
Uri = $downloadUrl
99-
} | Select-Object *
109+
#return output
110+
[PSCustomObject]@{
111+
Name = $packageName
112+
Source = $packagePath
113+
Version = $selectedVersion
114+
Uri = $downloadUrl
115+
} | Select-Object *
116+
}
100117
}

0 commit comments

Comments
 (0)