From 4e6ad6c6ca4a1f6ecba2aaa993cbbcb55305b010 Mon Sep 17 00:00:00 2001 From: Gilbert Sanchez Date: Wed, 25 Nov 2026 22:17:36 +0000 Subject: [PATCH] feat: per-DependencyType default options under PSDependOptions - Adds a 3-tier option cascade: a Dependency's own value overrides a per-type default (a nested hashtable under PSDependOptions keyed by DependencyType) overrides the existing global default. - Renames the private Get-GlobalOption helper to Resolve-DependencyOption to reflect its widened responsibility. - Warns (non-fatal) when a per-type block names a DependencyType no Dependency in the file uses. - No lock-format change needed: resolution happens before a parsed Dependency reaches Resolve-PSDependLock/Merge-PSDependLock. - Adds docs/adr/0003-per-type-dependency-options.md, merges the new Global default / Per-type default terms into CONTEXT.md, updates comment-based help and about_PSDepend_Definitions.help.txt, and adds a CHANGELOG entry. - 6 new Pester tests covering the cascade, whole-value override (not a deep merge), the typo warning, and per-type Credential resolution. Fixes #42 --- CHANGELOG.md | 8 + CONTEXT.md | 14 +- PSDepend/Public/Get-Dependency.ps1 | 148 ++++++++++++------ .../en-US/about_PSDepend_Definitions.help.txt | 16 ++ Tests/PSDepend.Tests.ps1 | 80 ++++++++++ docs/adr/0003-per-type-dependency-options.md | 45 ++++++ 6 files changed, 256 insertions(+), 55 deletions(-) create mode 100644 docs/adr/0003-per-type-dependency-options.md diff --git a/CHANGELOG.md b/CHANGELOG.md index b3455b5..d70f2d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Per-type defaults under `PSDependOptions`: a nested hashtable keyed by + `DependencyType` (e.g. `PSDependOptions.Git = @{ Target = '...' }`) sets a + default scoped to that type, between the global `PSDependOptions` default + and a value set directly on a Dependency. Warns when a per-type block names + a `DependencyType` no Dependency in the file uses. (#42) + ## [0.7.0-beta1] - 2026-10-07 ### Added diff --git a/CONTEXT.md b/CONTEXT.md index 8020bba..4d67a67 100644 --- a/CONTEXT.md +++ b/CONTEXT.md @@ -14,7 +14,7 @@ _Avoid_: requirements file, depend file, manifest **DependencyType**: The registered identifier that selects which DependencyScript handles a Dependency (e.g., `PSGalleryModule`, `Git`, `Chocolatey`). -_Avoid_: type, provider, handler type +_Avoid_: type, provider, handler type (bare "type"; the compound "Per-type default" below names a different concept and is fine) **DependencyScript**: A `PSDependScripts/.ps1` file that implements a DependencyType. Receives a typed Dependency object and a set of PSDependAction flags. @@ -29,8 +29,16 @@ A Dependency field with dual semantics: a scope keyword (`AllUsers`, `CurrentUse _Avoid_: destination, scope, path (use Target regardless of which semantic applies) **PSDependOptions**: -A reserved key in a DependencyFile that sets default field values applied to all Dependencies in that file. -_Avoid_: defaults, global options, file options +A reserved key in a DependencyFile holding default field values for its Dependencies: a Global default applies to every Dependency in the file, and a Per-type default narrows that to one DependencyType. Either is overridden by a value set directly on a Dependency. +_Avoid_: defaults, global options, file options (as a name for this key; "Global default" below names a narrower, different concept and is fine) + +**Global default**: +A value set directly under `PSDependOptions` for an option field (e.g. `PSDependOptions.Target`), applied to every Dependency in the file unless a Per-type default or the Dependency itself overrides it. +_Avoid_: global option + +**Per-type default**: +A default scoped to one DependencyType, set as a nested hashtable under `PSDependOptions` keyed by that type's name (e.g. `PSDependOptions.Git = @{ Target = '...' }`). Overrides the Global default for Dependencies of that type; overridden by a value set directly on a Dependency. A `PSDependOptions` key is read as a Per-type default only when its value is itself a hashtable and its name isn't one of the fixed option names (`Source`, `Target`, `Parameters`, `AddToPath`, `Tags`, `DependsOn`, `PreScripts`, `PostScripts`, `Credential`) — those names are reserved and therefore unavailable as DependencyType names. +_Avoid_: per-dependency-type option, type-level option **Prerequisite**: A declared ordering constraint between Dependencies. Expressed via the `DependsOn` field; resolved into topological execution order before dispatch. diff --git a/PSDepend/Public/Get-Dependency.ps1 b/PSDepend/Public/Get-Dependency.ps1 index 1ef5d5a..41c23e2 100644 --- a/PSDepend/Public/Get-Dependency.ps1 +++ b/PSDepend/Public/Get-Dependency.ps1 @@ -83,8 +83,18 @@ function Get-Dependency { # DependsOn # PreScripts # PostScripts + # Credential # Dependencies use these values as a default, unless you specify them explicitly for a dependency + + # A nested hashtable keyed by DependencyType sets a default for just that type, + # overriding the global default above and overridden by a value set on the dependency itself: + # PSDependOptions = @{ + # Target = 'C:\temp' # global default + # Git = @{ Target = 'C:\temp\git' } # default for Git dependencies only + # } + # A DependencyType name can't also be one of the option names above (Target, Source, etc.) - + # those are reserved as scalar option keys. } Note that you can mix these syntax together in the same psd1. @@ -162,23 +172,39 @@ function Get-Dependency { [switch]$IgnoreLock ) - # Helper to pick from global PSDependOptions, or return a default - function Get-GlobalOption { + # Option names that can be set directly under PSDependOptions; any other + # hashtable-valued key there is read as a per-type default block. + $ReservedOptionKeys = 'Source', 'Target', 'Parameters', 'AddToPath', 'Tags', 'DependsOn', 'PreScripts', 'PostScripts', 'Credential' + + # Helper to pick a value: the Dependency's own value (Prefer) wins, then a + # per-type default (a hashtable under PSDependOptions keyed by + # DependencyType), then the global default, then $Default. + function Resolve-DependencyOption { param( $Options = $PSDependOptions, $Name, + $DependencyType, $Prefer, $Default = $null ) - # Check for preferred value, otherwise try to get value from key, otherwise use default.... $Output = $Default if ($Prefer) { $Output = $Prefer } else { - try { - $Output = $Options[$Name] - } catch { - $Output = $Default + $TypeOptions = $null + if ($DependencyType -and $ReservedOptionKeys -notcontains $DependencyType -and + $Options -is [System.Collections.IDictionary] -and + $Options.Contains($DependencyType) -and $Options[$DependencyType] -is [System.Collections.IDictionary]) { + $TypeOptions = $Options[$DependencyType] + } + if ($TypeOptions -and $TypeOptions.Contains($Name)) { + $Output = $TypeOptions[$Name] + } else { + try { + $Output = $Options[$Name] + } catch { + $Output = $Default + } } } @@ -240,32 +266,34 @@ function Get-Dependency { $Dependencies.Remove('PSDependOptions') } + $UsedDependencyTypes = @{} + foreach ($Dependency in $Dependencies.keys) { $DependencyHash = $Dependencies.$Dependency - $DependencyType = Get-GlobalOption -Name DependencyType - - $CredentialName = Get-GlobalOption -Name Credential + $DependencyType = Resolve-DependencyOption -Name DependencyType # Look simple syntax with helpers in the key first if ( $DependencyHash -is [string] -and $Dependency -match '::' -and ($Dependency -split '::').count -eq 2 ) { + $SplitDependencyType = ($Dependency -split '::')[0] + $UsedDependencyTypes[$SplitDependencyType] = $true [PSCustomObject]@{ PSTypeName = 'PSDepend.Dependency' DependencyFile = $DependencyFile DependencyName = ($Dependency -split '::')[1] - DependencyType = ($Dependency -split '::')[0] + DependencyType = $SplitDependencyType Name = ($Dependency -split '::')[1] Version = $DependencyHash - Parameters = Get-GlobalOption -Name Parameters - Source = Get-GlobalOption -Name Source - Target = Get-GlobalOption -Name Target - AddToPath = Get-GlobalOption -Name AddToPath - Tags = Get-GlobalOption -Name Tags - DependsOn = Get-GlobalOption -Name DependsOn - PreScripts = Get-GlobalOption -Name PreScripts - PostScripts = Get-GlobalOption -Name PostScripts + Parameters = Resolve-DependencyOption -Name Parameters -DependencyType $SplitDependencyType + Source = Resolve-DependencyOption -Name Source -DependencyType $SplitDependencyType + Target = Resolve-DependencyOption -Name Target -DependencyType $SplitDependencyType + AddToPath = Resolve-DependencyOption -Name AddToPath -DependencyType $SplitDependencyType + Tags = Resolve-DependencyOption -Name Tags -DependencyType $SplitDependencyType + DependsOn = Resolve-DependencyOption -Name DependsOn -DependencyType $SplitDependencyType + PreScripts = Resolve-DependencyOption -Name PreScripts -DependencyType $SplitDependencyType + PostScripts = Resolve-DependencyOption -Name PostScripts -DependencyType $SplitDependencyType PSDependOptions = $PSDependOptions Raw = $null } @@ -276,6 +304,8 @@ function Get-Dependency { $Dependency -notmatch '/' -and (-not $DependencyType -or $DependencyType -eq 'PSGalleryModule')) { + $UsedDependencyTypes['PSGalleryModule'] = $true + $CredentialName = Resolve-DependencyOption -Name Credential -DependencyType 'PSGalleryModule' [PSCustomObject]@{ PSTypeName = 'PSDepend.Dependency' DependencyFile = $DependencyFile @@ -283,14 +313,14 @@ function Get-Dependency { DependencyType = 'PSGalleryModule' Name = $Dependency Version = $DependencyHash - Parameters = Get-GlobalOption -Name Parameters - Source = Get-GlobalOption -Name Source - Target = Get-GlobalOption -Name Target - AddToPath = Get-GlobalOption -Name AddToPath - Tags = Get-GlobalOption -Name Tags - DependsOn = Get-GlobalOption -Name DependsOn - PreScripts = Get-GlobalOption -Name PreScripts - PostScripts = Get-GlobalOption -Name PostScripts + Parameters = Resolve-DependencyOption -Name Parameters -DependencyType 'PSGalleryModule' + Source = Resolve-DependencyOption -Name Source -DependencyType 'PSGalleryModule' + Target = Resolve-DependencyOption -Name Target -DependencyType 'PSGalleryModule' + AddToPath = Resolve-DependencyOption -Name AddToPath -DependencyType 'PSGalleryModule' + Tags = Resolve-DependencyOption -Name Tags -DependencyType 'PSGalleryModule' + DependsOn = Resolve-DependencyOption -Name DependsOn -DependencyType 'PSGalleryModule' + PreScripts = Resolve-DependencyOption -Name PreScripts -DependencyType 'PSGalleryModule' + PostScripts = Resolve-DependencyOption -Name PostScripts -DependencyType 'PSGalleryModule' Credential = Resolve-Credential -Name $CredentialName PSDependOptions = $PSDependOptions Raw = $null @@ -302,6 +332,7 @@ function Get-Dependency { $Dependency.split('/').count -eq 2 -and (-not $DependencyType -or $DependencyType -eq 'GitHub')) { + $UsedDependencyTypes['GitHub'] = $true [PSCustomObject]@{ PSTypeName = 'PSDepend.Dependency' DependencyFile = $DependencyFile @@ -309,14 +340,14 @@ function Get-Dependency { DependencyType = 'GitHub' Name = $Dependency Version = $DependencyHash - Parameters = Get-GlobalOption -Name Parameters - Source = Get-GlobalOption -Name Source - Target = Get-GlobalOption -Name Target - AddToPath = Get-GlobalOption -Name AddToPath - Tags = Get-GlobalOption -Name Tags - DependsOn = Get-GlobalOption -Name DependsOn - PreScripts = Get-GlobalOption -Name PreScripts - PostScripts = Get-GlobalOption -Name PostScripts + Parameters = Resolve-DependencyOption -Name Parameters -DependencyType 'GitHub' + Source = Resolve-DependencyOption -Name Source -DependencyType 'GitHub' + Target = Resolve-DependencyOption -Name Target -DependencyType 'GitHub' + AddToPath = Resolve-DependencyOption -Name AddToPath -DependencyType 'GitHub' + Tags = Resolve-DependencyOption -Name Tags -DependencyType 'GitHub' + DependsOn = Resolve-DependencyOption -Name DependsOn -DependencyType 'GitHub' + PreScripts = Resolve-DependencyOption -Name PreScripts -DependencyType 'GitHub' + PostScripts = Resolve-DependencyOption -Name PostScripts -DependencyType 'GitHub' PSDependOptions = $PSDependOptions Raw = $null } @@ -326,6 +357,7 @@ function Get-Dependency { $Dependency -match '/' -and (-not $DependencyType -or $DependencyType -eq 'Git')) { + $UsedDependencyTypes['Git'] = $true [PSCustomObject]@{ PSTypeName = 'PSDepend.Dependency' DependencyFile = $DependencyFile @@ -333,14 +365,14 @@ function Get-Dependency { DependencyType = 'Git' Name = $Dependency Version = $DependencyHash - Parameters = Get-GlobalOption -Name Parameters - Source = Get-GlobalOption -Name Source - Target = Get-GlobalOption -Name Target - AddToPath = Get-GlobalOption -Name AddToPath - Tags = Get-GlobalOption -Name Tags - DependsOn = Get-GlobalOption -Name DependsOn - PreScripts = Get-GlobalOption -Name PreScripts - PostScripts = Get-GlobalOption -Name PostScripts + Parameters = Resolve-DependencyOption -Name Parameters -DependencyType 'Git' + Source = Resolve-DependencyOption -Name Source -DependencyType 'Git' + Target = Resolve-DependencyOption -Name Target -DependencyType 'Git' + AddToPath = Resolve-DependencyOption -Name AddToPath -DependencyType 'Git' + Tags = Resolve-DependencyOption -Name Tags -DependencyType 'Git' + DependsOn = Resolve-DependencyOption -Name DependsOn -DependencyType 'Git' + PreScripts = Resolve-DependencyOption -Name PreScripts -DependencyType 'Git' + PostScripts = Resolve-DependencyOption -Name PostScripts -DependencyType 'Git' PSDependOptions = $PSDependOptions Raw = $null } @@ -377,7 +409,8 @@ function Get-Dependency { $DependencyType = $DependencyHash.DependencyType } - $CredentialName = Get-GlobalOption -Name Credential -Prefer $DependencyHash.Credential + $UsedDependencyTypes[$DependencyType] = $true + $CredentialName = Resolve-DependencyOption -Name Credential -DependencyType $DependencyType -Prefer $DependencyHash.Credential [PSCustomObject]@{ PSTypeName = 'PSDepend.Dependency' DependencyFile = $DependencyFile @@ -385,20 +418,31 @@ function Get-Dependency { DependencyType = $DependencyType Name = $DependencyHash.Name Version = $DependencyHash.Version - Parameters = Get-GlobalOption -Name Parameters -Prefer $DependencyHash.Parameters - Source = Get-GlobalOption -Name Source -Prefer $DependencyHash.Source - Target = Get-GlobalOption -Name Target -Prefer $DependencyHash.Target - AddToPath = Get-GlobalOption -Name AddToPath -Prefer $DependencyHash.AddToPath - Tags = Get-GlobalOption -Name Tags -Prefer $DependencyHash.Tags - DependsOn = Get-GlobalOption -Name DependsOn -Prefer $DependencyHash.DependsOn - PreScripts = Get-GlobalOption -Name PreScripts -Prefer $DependencyHash.PreScripts - PostScripts = Get-GlobalOption -Name PostScripts -Prefer $DependencyHash.PostScripts + Parameters = Resolve-DependencyOption -Name Parameters -DependencyType $DependencyType -Prefer $DependencyHash.Parameters + Source = Resolve-DependencyOption -Name Source -DependencyType $DependencyType -Prefer $DependencyHash.Source + Target = Resolve-DependencyOption -Name Target -DependencyType $DependencyType -Prefer $DependencyHash.Target + AddToPath = Resolve-DependencyOption -Name AddToPath -DependencyType $DependencyType -Prefer $DependencyHash.AddToPath + Tags = Resolve-DependencyOption -Name Tags -DependencyType $DependencyType -Prefer $DependencyHash.Tags + DependsOn = Resolve-DependencyOption -Name DependsOn -DependencyType $DependencyType -Prefer $DependencyHash.DependsOn + PreScripts = Resolve-DependencyOption -Name PreScripts -DependencyType $DependencyType -Prefer $DependencyHash.PreScripts + PostScripts = Resolve-DependencyOption -Name PostScripts -DependencyType $DependencyType -Prefer $DependencyHash.PostScripts Credential = Resolve-Credential -Name $CredentialName PSDependOptions = $PSDependOptions Raw = $DependencyHash } } } + + # Warn about per-type default blocks that don't match any DependencyType used in this file + if ($PSDependOptions -is [System.Collections.IDictionary]) { + foreach ($OptionKey in $PSDependOptions.Keys) { + if ($ReservedOptionKeys -contains $OptionKey) { continue } + if ($PSDependOptions[$OptionKey] -isnot [System.Collections.IDictionary]) { continue } + if (-not $UsedDependencyTypes.ContainsKey($OptionKey)) { + Write-Warning "PSDependOptions has a per-type default for DependencyType '$OptionKey', but no dependency in this file uses that type. Possible typo?" + } + } + } } # Helper to retrieve the credential for a dependency diff --git a/PSDepend/en-US/about_PSDepend_Definitions.help.txt b/PSDepend/en-US/about_PSDepend_Definitions.help.txt index 101a0c7..bcdb4db 100644 --- a/PSDepend/en-US/about_PSDepend_Definitions.help.txt +++ b/PSDepend/en-US/about_PSDepend_Definitions.help.txt @@ -120,6 +120,22 @@ DETAILED DESCRIPTION DependsOn PreScripts PostScripts + Credential + + A nested hashtable keyed by DependencyType sets a default for just that type, + overriding the global default and overridden by a value set on the dependency itself: + + @{ + PSDependOptions = @{ + Target = 'C:\MyProject' # global default + Git = @{ Target = 'C:\git' } # default for Git dependencies only + } + pester = 'latest' + 'Git::buildhelpers/buildhelpers' = 'latest' # a Git dependency, uses 'C:\git' + } + + A DependencyType name can't also be one of the attribute names above (Target, + Source, etc.) - those are reserved as scalar option keys. PSDepend configuration: PSDepend.Config ======================================= diff --git a/Tests/PSDepend.Tests.ps1 b/Tests/PSDepend.Tests.ps1 index 6fc4443..2a34d84 100644 --- a/Tests/PSDepend.Tests.ps1 +++ b/Tests/PSDepend.Tests.ps1 @@ -148,6 +148,86 @@ Describe "Get-Dependency PS$PSVersion" -Tag 'Unit' { $Dependencies.DependencyType | Should -Be 'PSGalleryModule' $Dependencies.Version | Should -Be 'latest' } + + It 'Applies a per-type default under PSDependOptions, overriding the global default' { + $inputObj = @{ + PSDependOptions = @{ + Target = 'GlobalTarget' + Git = @{ Target = 'GitTarget' } + } + 'Git::org/repo' = 'latest' + Pester = 'latest' + } + $Dependencies = Get-Dependency -InputObject $inputObj + ($Dependencies | Where-Object DependencyType -EQ 'Git').Target | Should -Be 'GitTarget' + ($Dependencies | Where-Object DependencyType -EQ 'PSGalleryModule').Target | Should -Be 'GlobalTarget' + } + + It 'Lets a Dependency''s own value win over a per-type default' { + $inputObj = @{ + PSDependOptions = @{ + Git = @{ Target = 'GitTarget' } + } + Dep = @{ + DependencyType = 'Git' + Name = 'org/repo' + Target = 'DependencyTarget' + } + } + $Dependencies = Get-Dependency -InputObject $inputObj + $Dependencies.Target | Should -Be 'DependencyTarget' + } + + It 'Overrides a per-type Parameters hashtable by whole value, not a deep merge' { + $inputObj = @{ + PSDependOptions = @{ + Parameters = @{ Global = 'g' } + Git = @{ Parameters = @{ PerType = 'p' } } + } + Dep = @{ + DependencyType = 'Git' + Name = 'org/repo' + } + } + $Dependencies = Get-Dependency -InputObject $inputObj + $Dependencies.Parameters.ContainsKey('PerType') | Should -Be $True + $Dependencies.Parameters.ContainsKey('Global') | Should -Be $False + } + + It 'Warns when a per-type default names a type no Dependency in the file uses' { + $inputObj = @{ + PSDependOptions = @{ + Gti = @{ Target = 'Typo' } + } + Pester = 'latest' + } + $Warnings = @() + $null = Get-Dependency -InputObject $inputObj -WarningVariable Warnings -WarningAction SilentlyContinue + $Warnings.Message -join '' | Should -Match "'Gti'" + } + + It 'Does not warn when a per-type default names a type a Dependency in the file uses' { + $inputObj = @{ + PSDependOptions = @{ + PSGalleryModule = @{ Target = 'Used' } + } + Pester = 'latest' + } + $Warnings = @() + $null = Get-Dependency -InputObject $inputObj -WarningVariable Warnings -WarningAction SilentlyContinue + $Warnings.Count | Should -Be 0 + } + + It 'Applies a per-type Credential default for the bare PSGalleryModule syntax' { + $inputObj = @{ + PSDependOptions = @{ + PSGalleryModule = @{ Credential = 'MyCredKey' } + } + Pester = 'latest' + } + $Dependencies = Get-Dependency -InputObject $inputObj -Credentials @{ MyCredKey = 'TheCredentialObject' } + $Dependencies.Credential | Should -Be 'TheCredentialObject' + } } } diff --git a/docs/adr/0003-per-type-dependency-options.md b/docs/adr/0003-per-type-dependency-options.md new file mode 100644 index 0000000..a7ae15c --- /dev/null +++ b/docs/adr/0003-per-type-dependency-options.md @@ -0,0 +1,45 @@ +# Per-type defaults nest inside PSDependOptions, override by whole value + +## Context + +Issue #42 asks for a middle tier between the global `PSDependOptions` block and a +Dependency's own values: a default scoped to one `DependencyType` (e.g. a +`Source` shared by every `Nuget` dependency but not by `Git` ones). Several +shapes were possible: a sibling top-level key, a deep-merged nested block, or +a whole-value-override nested block. + +## Decision + +Resolution is a three-tier cascade: per-Dependency value overrides per-type +default overrides global default. Per-type defaults live as nested hashtables +**inside** `PSDependOptions`, keyed by the `DependencyType` name, rather than a +new sibling top-level key — one place to look for a Dependency's effective +options. A `PSDependOptions` key is read as a per-type block only when its +value is a hashtable and its name is not one of the fixed scalar option names +(`Source`, `Target`, `Parameters`, `AddToPath`, `Tags`, `DependsOn`, +`PreScripts`, `PostScripts`, `Credential`); those names are reserved and +therefore unavailable as custom `DependencyType` names. + +Override is whole-value, matching the existing `-Prefer` resolution semantics +in `Resolve-DependencyOption` (`Get-Dependency.ps1`): a type default for +`Parameters` replaces the global `Parameters` hashtable entirely rather than +deep-merging keys, so the new tier behaves exactly like the existing +per-Dependency override already does. + +`Parse-Dependency` warns (non-fatal) when a per-type block names a +`DependencyType` no Dependency in the file uses — the typo/shared-name risk +the original issue raised. + +Because this resolves inside `Get-Dependency`, before a parsed Dependency ever +reaches `Resolve-PSDependLock` or `Merge-PSDependLock`, no lock format change +is needed (legacy `adr/0002-lock-resolution-model.md` fingerprints the +already-resolved `Source`/`Parameters`). + +## Consequences + +- A `DependencyType` can never be named `Source`, `Target`, `Parameters`, + `AddToPath`, `Tags`, `DependsOn`, `PreScripts`, `PostScripts`, or + `Credential`. +- A type default for a hashtable-valued option (`Parameters`) is all-or- + nothing per Dependency, consistent with today's global-vs-Dependency + override but worth calling out since it reads like it could merge.