Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions CONTEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<Type>.ps1` file that implements a DependencyType. Receives a typed Dependency object and a set of PSDependAction flags.
Expand All @@ -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.
Expand Down
148 changes: 96 additions & 52 deletions PSDepend/Public/Get-Dependency.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}
}

Expand Down Expand Up @@ -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
}
Expand All @@ -276,21 +304,23 @@ 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
DependencyName = $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
Expand All @@ -302,21 +332,22 @@ function Get-Dependency {
$Dependency.split('/').count -eq 2 -and
(-not $DependencyType -or
$DependencyType -eq 'GitHub')) {
$UsedDependencyTypes['GitHub'] = $true
[PSCustomObject]@{
PSTypeName = 'PSDepend.Dependency'
DependencyFile = $DependencyFile
DependencyName = $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
}
Expand All @@ -326,21 +357,22 @@ function Get-Dependency {
$Dependency -match '/' -and
(-not $DependencyType -or
$DependencyType -eq 'Git')) {
$UsedDependencyTypes['Git'] = $true
[PSCustomObject]@{
PSTypeName = 'PSDepend.Dependency'
DependencyFile = $DependencyFile
DependencyName = $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
}
Expand Down Expand Up @@ -377,28 +409,40 @@ 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
DependencyName = $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
Expand Down
16 changes: 16 additions & 0 deletions PSDepend/en-US/about_PSDepend_Definitions.help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======================================
Expand Down
Loading
Loading