Repository navigation
feat: add Add-PSDepend command to append dependencies to a DependencyFile #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HeyItsGilbert
wants to merge
3
commits into
main
Choose a base branch
from
feat/add-psdepend-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,385
−0
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| function ConvertTo-PSDependLiteral { | ||
| <# | ||
| .SYNOPSIS | ||
| Render a value as PowerShell source text suitable for a DependencyFile entry. | ||
|
|
||
| .DESCRIPTION | ||
| Used by Add-PSDepend to serialize field values (Tags, Parameters, ...) when | ||
| writing a new hashtable-form entry. Handles strings, booleans, numbers, | ||
| arrays, and nested hashtables. | ||
|
|
||
| .PARAMETER Value | ||
| The value to render. | ||
|
|
||
| .PARAMETER IndentLevel | ||
| Indentation depth, in 4-space units, for a nested hashtable's members and | ||
| its closing brace. | ||
|
|
||
| .EXAMPLE | ||
| ConvertTo-PSDependLiteral -Value @{ SkipPublisherCheck = $true } | ||
|
|
||
| Returns "@{`r`n SkipPublisherCheck = `$true`r`n }" | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([string])] | ||
| param( | ||
| $Value, | ||
|
|
||
| [int]$IndentLevel = 2 | ||
| ) | ||
|
|
||
| if ($null -eq $Value) { | ||
| return '$null' | ||
| } | ||
| if ($Value -is [bool]) { | ||
| return $(if ($Value) { '$true' } else { '$false' }) | ||
| } | ||
| if ($Value -is [System.Collections.IDictionary]) { | ||
| $ChildIndent = ' ' * ($IndentLevel + 1) | ||
| $ClosingIndent = ' ' * $IndentLevel | ||
| $Entries = foreach ($Key in $Value.Keys) { | ||
| $QuotedKey = "'$($Key.ToString() -replace "'", "''")'" | ||
| "$ChildIndent$QuotedKey = $(ConvertTo-PSDependLiteral -Value $Value[$Key] -IndentLevel ($IndentLevel + 1))" | ||
| } | ||
| return "@{`r`n$($Entries -join "`r`n")`r`n$ClosingIndent}" | ||
| } | ||
| if ($Value -is [string]) { | ||
| return "'$($Value -replace "'", "''")'" | ||
| } | ||
| if ($Value -is [int] -or $Value -is [long] -or $Value -is [double]) { | ||
| return "$Value" | ||
| } | ||
| if ($Value -is [System.Collections.IEnumerable]) { | ||
| $Items = @($Value | ForEach-Object { ConvertTo-PSDependLiteral -Value $_ -IndentLevel $IndentLevel }) | ||
| if ($Items.Count -eq 0) { | ||
| return '@()' | ||
| } | ||
| return ($Items -join ', ') | ||
| } | ||
|
HeyItsGilbert marked this conversation as resolved.
|
||
| return "'$($Value.ToString() -replace "'", "''")'" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| function Resolve-PSDependFileTarget { | ||
| <# | ||
| .SYNOPSIS | ||
| Resolve the DependencyFile that Add-PSDepend should write to. | ||
|
|
||
| .DESCRIPTION | ||
| If Path points to an existing file, it is used as-is. If Path points to a | ||
| nonexistent path with a .psd1 extension, it is treated as the file to | ||
| create. Otherwise Path is treated as a directory: it is searched | ||
| (optionally recursively) for *.depend.psd1 and requirements.psd1 files, | ||
| the same way Invoke-PSDepend and Update-PSDependLock discover | ||
| DependencyFiles. Exactly one match is returned as-is. No matches falls | ||
| back to <Path>\requirements.psd1, to be created by the caller. More than | ||
| one match is ambiguous and the caller must narrow it with -Path. | ||
|
|
||
| .PARAMETER Path | ||
| A DependencyFile, a new DependencyFile to create, or a directory to search. | ||
|
|
||
| .PARAMETER Recurse | ||
| Whether to recurse into subdirectories when Path is a directory. | ||
|
|
||
| .EXAMPLE | ||
| Resolve-PSDependFileTarget -Path . -Recurse $true | ||
|
|
||
| Finds the single DependencyFile under the current directory tree, or | ||
| falls back to .\requirements.psd1 if none exist yet. | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([string])] | ||
| param( | ||
| [Parameter(Mandatory)] | ||
| [string]$Path, | ||
|
|
||
| [bool]$Recurse = $true | ||
| ) | ||
|
|
||
| if (Test-Path -LiteralPath $Path -PathType Leaf) { | ||
| $Resolved = (Resolve-Path -LiteralPath $Path).ProviderPath | ||
| if ([System.IO.Path]::GetExtension($Resolved) -ine '.psd1') { | ||
| throw "Path '$Resolved' is not a .psd1 file" | ||
| } | ||
| return $Resolved | ||
| } | ||
|
HeyItsGilbert marked this conversation as resolved.
|
||
|
|
||
| if (-not (Test-Path -LiteralPath $Path)) { | ||
| if ([System.IO.Path]::GetExtension($Path) -eq '.psd1') { | ||
| return $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) | ||
| } | ||
| throw "Path '$Path' does not exist" | ||
| } | ||
|
|
||
| $DirectoryPath = (Resolve-Path -LiteralPath $Path).ProviderPath | ||
| $Found = @(Resolve-DependScripts -Path $DirectoryPath -Recurse $Recurse) | ||
| switch ($Found.Count) { | ||
| 0 { Join-Path $DirectoryPath 'requirements.psd1' } | ||
| 1 { $Found[0] } | ||
| default { | ||
| throw "Multiple DependencyFiles found under '$DirectoryPath': $($Found -join ', '). Specify -Path to target one of them." | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.