From 36f2334af0bc4c0da8634902d84c4205878f0ea2 Mon Sep 17 00:00:00 2001 From: Thomas Nieto <38873752+tnieto88@users.noreply.github.com> Date: Thu, 30 May 2019 12:10:05 -0500 Subject: [PATCH] Add PSPath alias to LiteralPath parameter --- .../public/psgetfunctions/Publish-Script.ps1 | 18 +- .../public/psgetfunctions/Save-Module.ps1 | 22 +- .../public/psgetfunctions/Save-Script.ps1 | 5 +- .../psgetfunctions/Test-ScriptFileInfo.ps1 | 261 ++++++------ .../psgetfunctions/Update-ScriptFileInfo.ps1 | 370 ++++++++---------- 5 files changed, 296 insertions(+), 380 deletions(-) diff --git a/src/PowerShellGet/public/psgetfunctions/Publish-Script.ps1 b/src/PowerShellGet/public/psgetfunctions/Publish-Script.ps1 index 3a2ac7ad..ccd4f998 100644 --- a/src/PowerShellGet/public/psgetfunctions/Publish-Script.ps1 +++ b/src/PowerShellGet/public/psgetfunctions/Publish-Script.ps1 @@ -18,6 +18,7 @@ function Publish-Script { [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'LiteralPathParameterSet')] + [Alias('PSPath')] [ValidateNotNullOrEmpty()] [string] $LiteralPath, @@ -49,7 +50,7 @@ function Publish-Script { $scriptFilePath = $null if ($Path) { $scriptFilePath = Resolve-PathHelper -Path $Path -CallerPSCmdlet $PSCmdlet | - Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore + Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore if (-not $scriptFilePath -or -not (Microsoft.PowerShell.Management\Test-Path -Path $scriptFilePath -PathType Leaf)) { @@ -64,7 +65,7 @@ function Publish-Script { } else { $scriptFilePath = Resolve-PathHelper -Path $LiteralPath -IsLiteralPath -CallerPSCmdlet $PSCmdlet | - Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore + Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore if (-not $scriptFilePath -or -not (Microsoft.PowerShell.Management\Test-Path -LiteralPath $scriptFilePath -PathType Leaf)) { @@ -117,8 +118,7 @@ function Publish-Script { if (-not $DestinationLocation -or (-not (Microsoft.PowerShell.Management\Test-Path -Path $DestinationLocation) -and - -not (Test-WebUri -uri $DestinationLocation))) - { + -not (Test-WebUri -uri $DestinationLocation))) { $message = $LocalizedData.PSRepositoryScriptPublishLocationIsMissing -f ($Repository, $Repository) ThrowError -ExceptionName "System.ArgumentException" ` -ExceptionMessage $message ` @@ -182,7 +182,7 @@ function Publish-Script { # Copy the source script file to temp location to publish $tempScriptPath = Microsoft.PowerShell.Management\Join-Path -Path $script:TempPath -ChildPath "$(Get-Random)" | - Microsoft.PowerShell.Management\Join-Path -ChildPath $scriptName + Microsoft.PowerShell.Management\Join-Path -ChildPath $scriptName $null = Microsoft.PowerShell.Management\New-Item -Path $tempScriptPath -ItemType Directory -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -Confirm:$false -WhatIf:$false if ($Path) { @@ -211,8 +211,8 @@ function Publish-Script { # Check if the specified script name is already used for a module on the specified repository # Use Find-Module to check if that name is already used as module name $modulePSGetItemInfo = Find-Module @FindParameters | - Microsoft.PowerShell.Core\Where-Object {$_.Name -eq $scriptName} | - Microsoft.PowerShell.Utility\Select-Object -Last 1 -ErrorAction Ignore + Microsoft.PowerShell.Core\Where-Object { $_.Name -eq $scriptName } | + Microsoft.PowerShell.Utility\Select-Object -Last 1 -ErrorAction Ignore if ($modulePSGetItemInfo) { $message = $LocalizedData.SpecifiedNameIsAlearyUsed -f ($scriptName, $Repository, 'Find-Module') ThrowError -ExceptionName "System.InvalidOperationException" ` @@ -227,8 +227,8 @@ function Publish-Script { $currentPSGetItemInfo = $null $currentPSGetItemInfo = Find-Script @FindParameters | - Microsoft.PowerShell.Core\Where-Object {$_.Name -eq $scriptName} | - Microsoft.PowerShell.Utility\Select-Object -Last 1 -ErrorAction Ignore + Microsoft.PowerShell.Core\Where-Object { $_.Name -eq $scriptName } | + Microsoft.PowerShell.Utility\Select-Object -Last 1 -ErrorAction Ignore if ($currentPSGetItemInfo) { $result = ValidateAndGet-VersionPrereleaseStrings -Version $currentPSGetItemInfo.Version -CallerPSCmdlet $PSCmdlet diff --git a/src/PowerShellGet/public/psgetfunctions/Save-Module.ps1 b/src/PowerShellGet/public/psgetfunctions/Save-Module.ps1 index 4055b1b5..3225f153 100644 --- a/src/PowerShellGet/public/psgetfunctions/Save-Module.ps1 +++ b/src/PowerShellGet/public/psgetfunctions/Save-Module.ps1 @@ -65,13 +65,24 @@ function Save-Module { [string[]] $Repository, - [Parameter(Mandatory = $true, Position = 1, ParameterSetName = 'NameAndPathParameterSet')] - [Parameter(Mandatory = $true, Position = 1, ParameterSetName = 'InputObjectAndPathParameterSet')] + [Parameter(Mandatory = $true, + Position = 1, + ValueFromPipelineByPropertyName = $true, + ParameterSetName = 'NameAndPathParameterSet')] + [Parameter(Mandatory = $true, + Position = 1, + ValueFromPipelineByPropertyName = $true, + ParameterSetName = 'InputObjectAndPathParameterSet')] [string] $Path, - [Parameter(Mandatory = $true, ParameterSetName = 'NameAndLiteralPathParameterSet')] - [Parameter(Mandatory = $true, ParameterSetName = 'InputObjectAndLiteralPathParameterSet')] + [Parameter(Mandatory = $true, + ValueFromPipelineByPropertyName = $true, + ParameterSetName = 'NameAndLiteralPathParameterSet')] + [Parameter(Mandatory = $true, + ValueFromPipelineByPropertyName = $true, + ParameterSetName = 'InputObjectAndLiteralPathParameterSet')] + [Alias('PSPath')] [string] $LiteralPath, @@ -189,8 +200,7 @@ function Save-Module { ($inputValue.PSTypeNames -notcontains "Microsoft.PowerShell.Commands.PSGetDscResourceInfo") -and ($inputValue.PSTypeNames -notcontains "Deserialized.Microsoft.PowerShell.Commands.PSGetDscResourceInfo") -and ($inputValue.PSTypeNames -notcontains "Microsoft.PowerShell.Commands.PSGetRoleCapabilityInfo") -and - ($inputValue.PSTypeNames -notcontains "Deserialized.Microsoft.PowerShell.Commands.PSGetRoleCapabilityInfo")) - { + ($inputValue.PSTypeNames -notcontains "Deserialized.Microsoft.PowerShell.Commands.PSGetRoleCapabilityInfo")) { ThrowError -ExceptionName "System.ArgumentException" ` -ExceptionMessage $LocalizedData.InvalidInputObjectValue ` -ErrorId "InvalidInputObjectValue" ` diff --git a/src/PowerShellGet/public/psgetfunctions/Save-Script.ps1 b/src/PowerShellGet/public/psgetfunctions/Save-Script.ps1 index fbf0fb9b..1a6c6328 100644 --- a/src/PowerShellGet/public/psgetfunctions/Save-Script.ps1 +++ b/src/PowerShellGet/public/psgetfunctions/Save-Script.ps1 @@ -84,6 +84,7 @@ function Save-Script { [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'InputObjectAndLiteralPathParameterSet')] + [Alias('PSPath')] [string] $LiteralPath, @@ -134,7 +135,7 @@ function Save-Script { if (-not $Force) { if ($Path) { $destinationPath = Resolve-PathHelper -Path $Path -CallerPSCmdlet $PSCmdlet | - Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore + Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore if (-not $destinationPath -or -not (Microsoft.PowerShell.Management\Test-path $destinationPath)) { $errorMessage = ($LocalizedData.PathNotFound -f $Path) @@ -150,7 +151,7 @@ function Save-Script { } else { $destinationPath = Resolve-PathHelper -Path $LiteralPath -IsLiteralPath -CallerPSCmdlet $PSCmdlet | - Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore + Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore if (-not $destinationPath -or -not (Microsoft.PowerShell.Management\Test-Path -LiteralPath $destinationPath)) { $errorMessage = ($LocalizedData.PathNotFound -f $LiteralPath) diff --git a/src/PowerShellGet/public/psgetfunctions/Test-ScriptFileInfo.ps1 b/src/PowerShellGet/public/psgetfunctions/Test-ScriptFileInfo.ps1 index fac918a3..cdc3ea9c 100644 --- a/src/PowerShellGet/public/psgetfunctions/Test-ScriptFileInfo.ps1 +++ b/src/PowerShellGet/public/psgetfunctions/Test-ScriptFileInfo.ps1 @@ -1,74 +1,68 @@ -function Test-ScriptFileInfo -{ +function Test-ScriptFileInfo { <# .ExternalHelp PSModule-help.xml #> - [CmdletBinding(PositionalBinding=$false, - DefaultParameterSetName='PathParameterSet', - HelpUri='https://go.microsoft.com/fwlink/?LinkId=619791')] + [CmdletBinding(PositionalBinding = $false, + DefaultParameterSetName = 'PathParameterSet', + HelpUri = 'https://go.microsoft.com/fwlink/?LinkId=619791')] Param ( - [Parameter(Mandatory=$true, - Position=0, - ValueFromPipelineByPropertyName=$true, - ParameterSetName='PathParameterSet')] + [Parameter(Mandatory = $true, + Position = 0, + ValueFromPipelineByPropertyName = $true, + ParameterSetName = 'PathParameterSet')] [ValidateNotNullOrEmpty()] [string] $Path, - [Parameter(Mandatory=$true, - ValueFromPipelineByPropertyName=$true, - ParameterSetName='LiteralPathParameterSet')] + [Parameter(Mandatory = $true, + ValueFromPipelineByPropertyName = $true, + ParameterSetName = 'LiteralPathParameterSet')] + [Alias('PSPath')] [ValidateNotNullOrEmpty()] [string] $LiteralPath ) - Process - { + Process { $scriptFilePath = $null - if($Path) - { + if ($Path) { $scriptFilePath = Resolve-PathHelper -Path $Path -CallerPSCmdlet $PSCmdlet | Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore - if(-not $scriptFilePath -or -not (Microsoft.PowerShell.Management\Test-Path -Path $scriptFilePath -PathType Leaf)) - { + if (-not $scriptFilePath -or -not (Microsoft.PowerShell.Management\Test-Path -Path $scriptFilePath -PathType Leaf)) { $errorMessage = ($LocalizedData.PathNotFound -f $Path) ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage $errorMessage ` - -ErrorId "PathNotFound" ` - -CallerPSCmdlet $PSCmdlet ` - -ExceptionObject $Path ` - -ErrorCategory InvalidArgument + -ExceptionMessage $errorMessage ` + -ErrorId "PathNotFound" ` + -CallerPSCmdlet $PSCmdlet ` + -ExceptionObject $Path ` + -ErrorCategory InvalidArgument return } } - else - { + else { $scriptFilePath = Resolve-PathHelper -Path $LiteralPath -IsLiteralPath -CallerPSCmdlet $PSCmdlet | Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore - if(-not $scriptFilePath -or -not (Microsoft.PowerShell.Management\Test-Path -LiteralPath $scriptFilePath -PathType Leaf)) - { + if (-not $scriptFilePath -or -not (Microsoft.PowerShell.Management\Test-Path -LiteralPath $scriptFilePath -PathType Leaf)) { $errorMessage = ($LocalizedData.PathNotFound -f $LiteralPath) ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage $errorMessage ` - -ErrorId "PathNotFound" ` - -CallerPSCmdlet $PSCmdlet ` - -ExceptionObject $LiteralPath ` - -ErrorCategory InvalidArgument + -ExceptionMessage $errorMessage ` + -ErrorId "PathNotFound" ` + -CallerPSCmdlet $PSCmdlet ` + -ExceptionObject $LiteralPath ` + -ErrorCategory InvalidArgument return } } - if(-not $scriptFilePath.EndsWith('.ps1', [System.StringComparison]::OrdinalIgnoreCase)) - { + if (-not $scriptFilePath.EndsWith('.ps1', [System.StringComparison]::OrdinalIgnoreCase)) { $errorMessage = ($LocalizedData.InvalidScriptFilePath -f $scriptFilePath) ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage $errorMessage ` - -ErrorId "InvalidScriptFilePath" ` - -CallerPSCmdlet $PSCmdlet ` - -ExceptionObject $scriptFilePath ` - -ErrorCategory InvalidArgument + -ExceptionMessage $errorMessage ` + -ErrorId "InvalidScriptFilePath" ` + -CallerPSCmdlet $PSCmdlet ` + -ExceptionObject $scriptFilePath ` + -ErrorCategory InvalidArgument return } @@ -80,39 +74,36 @@ function Test-ScriptFileInfo $notSupportedOnNanoErrorIds = @('WorkflowNotSupportedInPowerShellCore', - 'ConfigurationNotSupportedInPowerShellCore') - $errorsAfterSkippingOneCoreErrors = $errors | Microsoft.PowerShell.Core\Where-Object { $notSupportedOnNanoErrorIds -notcontains $_.ErrorId} + 'ConfigurationNotSupportedInPowerShellCore') + $errorsAfterSkippingOneCoreErrors = $errors | Microsoft.PowerShell.Core\Where-Object { $notSupportedOnNanoErrorIds -notcontains $_.ErrorId } - if($errorsAfterSkippingOneCoreErrors) - { + if ($errorsAfterSkippingOneCoreErrors) { $errorMessage = ($LocalizedData.ScriptParseError -f $scriptFilePath) ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage $errorMessage ` - -ErrorId "ScriptParseError" ` - -CallerPSCmdlet $PSCmdlet ` - -ExceptionObject $errorsAfterSkippingOneCoreErrors ` - -ErrorCategory InvalidArgument + -ExceptionMessage $errorMessage ` + -ErrorId "ScriptParseError" ` + -CallerPSCmdlet $PSCmdlet ` + -ExceptionObject $errorsAfterSkippingOneCoreErrors ` + -ErrorCategory InvalidArgument return } - if($ast) - { + if ($ast) { # Get the block/group comment beginning with <#PSScriptInfo - $CommentTokens = $tokens | Microsoft.PowerShell.Core\Where-Object {$_.Kind -eq 'Comment'} + $CommentTokens = $tokens | Microsoft.PowerShell.Core\Where-Object { $_.Kind -eq 'Comment' } $psscriptInfoComments = $CommentTokens | - Microsoft.PowerShell.Core\Where-Object { $_.Extent.Text -match "<#PSScriptInfo" } | - Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore + Microsoft.PowerShell.Core\Where-Object { $_.Extent.Text -match "<#PSScriptInfo" } | + Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore - if(-not $psscriptInfoComments) - { + if (-not $psscriptInfoComments) { $errorMessage = ($LocalizedData.MissingPSScriptInfo -f $scriptFilePath) ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage $errorMessage ` - -ErrorId "MissingPSScriptInfo" ` - -CallerPSCmdlet $PSCmdlet ` - -ExceptionObject $scriptFilePath ` - -ErrorCategory InvalidArgument + -ExceptionMessage $errorMessage ` + -ErrorId "MissingPSScriptInfo" ` + -CallerPSCmdlet $PSCmdlet ` + -ExceptionObject $scriptFilePath ` + -ErrorCategory InvalidArgument return } @@ -124,7 +115,7 @@ function Test-ScriptFileInfo $Value = "" # PSScriptInfo comment will be in following format: - <#PSScriptInfo + <#PSScriptInfo .VERSION 1.0 @@ -163,68 +154,55 @@ function Test-ScriptFileInfo # First line is <#PSScriptInfo # Last line #> # - if($commentLines.Count -gt 2) - { - for($i = 1; $i -lt ($commentLines.count - 1); $i++) - { + if ($commentLines.Count -gt 2) { + for ($i = 1; $i -lt ($commentLines.count - 1); $i++) { $line = $commentLines[$i] - if(-not $line) - { + if (-not $line) { continue } # A line is starting with . conveys a new metadata property # __NEWLINE__ is used for replacing the value lines while adding the value to $PSScriptInfo object # - if($line.trim().StartsWith('.')) - { - $parts = $line.trim() -split '[.\s+]',3 | Microsoft.PowerShell.Core\Where-Object {$_} - - if($KeyName -and $Value) - { - if($keyName -eq $script:ReleaseNotes) - { + if ($line.trim().StartsWith('.')) { + $parts = $line.trim() -split '[.\s+]', 3 | Microsoft.PowerShell.Core\Where-Object { $_ } + + if ($KeyName -and $Value) { + if ($keyName -eq $script:ReleaseNotes) { $Value = $Value.Trim() -split '__NEWLINE__' } - elseif($keyName -eq $script:DESCRIPTION) - { + elseif ($keyName -eq $script:DESCRIPTION) { $Value = $Value -split '__NEWLINE__' $Value = ($Value -join "`r`n").Trim() } - else - { - $Value = $Value -split '__NEWLINE__' | Microsoft.PowerShell.Core\Where-Object { $_ } + else { + $Value = $Value -split '__NEWLINE__' | Microsoft.PowerShell.Core\Where-Object { $_ } - if($Value -and $Value.GetType().ToString() -eq "System.String") - { + if ($Value -and $Value.GetType().ToString() -eq "System.String") { $Value = $Value.Trim() } } ValidateAndAdd-PSScriptInfoEntry -PSScriptInfo $PSScriptInfo ` - -PropertyName $KeyName ` - -PropertyValue $Value ` - -CallerPSCmdlet $PSCmdlet + -PropertyName $KeyName ` + -PropertyValue $Value ` + -CallerPSCmdlet $PSCmdlet } $KeyName = $null $Value = "" - if($parts.GetType().ToString() -eq "System.String") - { + if ($parts.GetType().ToString() -eq "System.String") { $KeyName = $parts } - else - { + else { $KeyName = $parts[0]; $Value = $parts[1] } } - else - { - if($Value) - { + else { + if ($Value) { # __NEWLINE__ is used for replacing the value lines while adding the value to $PSScriptInfo object $Value += '__NEWLINE__' } @@ -233,31 +211,26 @@ function Test-ScriptFileInfo } } - if($KeyName -and $Value) - { - if($keyName -eq $script:ReleaseNotes) - { + if ($KeyName -and $Value) { + if ($keyName -eq $script:ReleaseNotes) { $Value = $Value.Trim() -split '__NEWLINE__' } - elseif($keyName -eq $script:DESCRIPTION) - { + elseif ($keyName -eq $script:DESCRIPTION) { $Value = $Value -split '__NEWLINE__' $Value = ($Value -join "`r`n").Trim() } - else - { - $Value = $Value -split '__NEWLINE__' | Microsoft.PowerShell.Core\Where-Object { $_ } + else { + $Value = $Value -split '__NEWLINE__' | Microsoft.PowerShell.Core\Where-Object { $_ } - if($Value -and $Value.GetType().ToString() -eq "System.String") - { + if ($Value -and $Value.GetType().ToString() -eq "System.String") { $Value = $Value.Trim() } } ValidateAndAdd-PSScriptInfoEntry -PSScriptInfo $PSScriptInfo ` - -PropertyName $KeyName ` - -PropertyValue $Value ` - -CallerPSCmdlet $PSCmdlet + -PropertyName $KeyName ` + -PropertyValue $Value ` + -CallerPSCmdlet $PSCmdlet $KeyName = $null $Value = "" @@ -265,71 +238,65 @@ function Test-ScriptFileInfo } $helpContent = $ast.GetHelpContent() - if($helpContent -and $helpContent.Description) - { + if ($helpContent -and $helpContent.Description) { ValidateAndAdd-PSScriptInfoEntry -PSScriptInfo $PSScriptInfo ` - -PropertyName $script:DESCRIPTION ` - -PropertyValue $helpContent.Description.Trim() ` - -CallerPSCmdlet $PSCmdlet + -PropertyName $script:DESCRIPTION ` + -PropertyValue $helpContent.Description.Trim() ` + -CallerPSCmdlet $PSCmdlet } # Handle RequiredModules - if((Microsoft.PowerShell.Utility\Get-Member -InputObject $ast -Name 'ScriptRequirements') -and - $ast.ScriptRequirements -and - (Microsoft.PowerShell.Utility\Get-Member -InputObject $ast.ScriptRequirements -Name 'RequiredModules') -and - $ast.ScriptRequirements.RequiredModules) - { + if ((Microsoft.PowerShell.Utility\Get-Member -InputObject $ast -Name 'ScriptRequirements') -and + $ast.ScriptRequirements -and + (Microsoft.PowerShell.Utility\Get-Member -InputObject $ast.ScriptRequirements -Name 'RequiredModules') -and + $ast.ScriptRequirements.RequiredModules) { ValidateAndAdd-PSScriptInfoEntry -PSScriptInfo $PSScriptInfo ` - -PropertyName $script:RequiredModules ` - -PropertyValue $ast.ScriptRequirements.RequiredModules ` - -CallerPSCmdlet $PSCmdlet + -PropertyName $script:RequiredModules ` + -PropertyValue $ast.ScriptRequirements.RequiredModules ` + -CallerPSCmdlet $PSCmdlet } # Get all defined functions and populate DefinedCommands, DefinedFunctions and DefinedWorkflows - $allCommands = $ast.FindAll({param($i) return ($i.GetType().Name -eq 'FunctionDefinitionAst')}, $true) + $allCommands = $ast.FindAll( { param($i) return ($i.GetType().Name -eq 'FunctionDefinitionAst') }, $true) - if($allCommands) - { - $allCommandNames = $allCommands | ForEach-Object {$_.Name} | Select-Object -Unique -ErrorAction Ignore + if ($allCommands) { + $allCommandNames = $allCommands | ForEach-Object { $_.Name } | Select-Object -Unique -ErrorAction Ignore ValidateAndAdd-PSScriptInfoEntry -PSScriptInfo $PSScriptInfo ` - -PropertyName $script:DefinedCommands ` - -PropertyValue $allCommandNames ` - -CallerPSCmdlet $PSCmdlet + -PropertyName $script:DefinedCommands ` + -PropertyValue $allCommandNames ` + -CallerPSCmdlet $PSCmdlet - $allFunctionNames = $allCommands | Where-Object {-not $_.IsWorkflow} | ForEach-Object {$_.Name} | Select-Object -Unique -ErrorAction Ignore + $allFunctionNames = $allCommands | Where-Object { -not $_.IsWorkflow } | ForEach-Object { $_.Name } | Select-Object -Unique -ErrorAction Ignore ValidateAndAdd-PSScriptInfoEntry -PSScriptInfo $PSScriptInfo ` - -PropertyName $script:DefinedFunctions ` - -PropertyValue $allFunctionNames ` - -CallerPSCmdlet $PSCmdlet + -PropertyName $script:DefinedFunctions ` + -PropertyValue $allFunctionNames ` + -CallerPSCmdlet $PSCmdlet - $allWorkflowNames = $allCommands | Where-Object {$_.IsWorkflow} | ForEach-Object {$_.Name} | Select-Object -Unique -ErrorAction Ignore + $allWorkflowNames = $allCommands | Where-Object { $_.IsWorkflow } | ForEach-Object { $_.Name } | Select-Object -Unique -ErrorAction Ignore ValidateAndAdd-PSScriptInfoEntry -PSScriptInfo $PSScriptInfo ` - -PropertyName $script:DefinedWorkflows ` - -PropertyValue $allWorkflowNames ` - -CallerPSCmdlet $PSCmdlet + -PropertyName $script:DefinedWorkflows ` + -PropertyValue $allWorkflowNames ` + -CallerPSCmdlet $PSCmdlet } } # Ensure that the script file has the required metadata properties. - if(-not $PSScriptInfo.Version -or -not $PSScriptInfo.Guid -or -not $PSScriptInfo.Author -or -not $PSScriptInfo.Description) - { + if (-not $PSScriptInfo.Version -or -not $PSScriptInfo.Guid -or -not $PSScriptInfo.Author -or -not $PSScriptInfo.Description) { $errorMessage = ($LocalizedData.MissingRequiredPSScriptInfoProperties -f $scriptFilePath) ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage $errorMessage ` - -ErrorId "MissingRequiredPSScriptInfoProperties" ` - -CallerPSCmdlet $PSCmdlet ` - -ExceptionObject $Path ` - -ErrorCategory InvalidArgument + -ExceptionMessage $errorMessage ` + -ErrorId "MissingRequiredPSScriptInfoProperties" ` + -CallerPSCmdlet $PSCmdlet ` + -ExceptionObject $Path ` + -ErrorCategory InvalidArgument return } - if ($PSScriptInfo.Version -match '-') - { + if ($PSScriptInfo.Version -match '-') { $result = ValidateAndGet-VersionPrereleaseStrings -Version $PSScriptInfo.Version -CallerPSCmdlet $PSCmdlet - if (-not $result) - { + if (-not $result) { # ValidateAndGet-VersionPrereleaseStrings throws the error. # returning to avoid further execution when different values are specified for -ErrorAction parameter return @@ -340,4 +307,4 @@ function Test-ScriptFileInfo return $PSScriptInfo } -} \ No newline at end of file +} diff --git a/src/PowerShellGet/public/psgetfunctions/Update-ScriptFileInfo.ps1 b/src/PowerShellGet/public/psgetfunctions/Update-ScriptFileInfo.ps1 index 3851e3bb..860dd556 100644 --- a/src/PowerShellGet/public/psgetfunctions/Update-ScriptFileInfo.ps1 +++ b/src/PowerShellGet/public/psgetfunctions/Update-ScriptFileInfo.ps1 @@ -1,26 +1,26 @@ -function Update-ScriptFileInfo -{ +function Update-ScriptFileInfo { <# .ExternalHelp PSModule-help.xml #> - [CmdletBinding(PositionalBinding=$false, - DefaultParameterSetName='PathParameterSet', - SupportsShouldProcess=$true, - HelpUri='https://go.microsoft.com/fwlink/?LinkId=619793')] + [CmdletBinding(PositionalBinding = $false, + DefaultParameterSetName = 'PathParameterSet', + SupportsShouldProcess = $true, + HelpUri = 'https://go.microsoft.com/fwlink/?LinkId=619793')] Param ( - [Parameter(Mandatory=$true, - Position=0, - ParameterSetName='PathParameterSet', - ValueFromPipelineByPropertyName=$true)] + [Parameter(Mandatory = $true, + Position = 0, + ParameterSetName = 'PathParameterSet', + ValueFromPipelineByPropertyName = $true)] [ValidateNotNullOrEmpty()] [string] $Path, - [Parameter(Mandatory=$true, - Position=0, - ParameterSetName='LiteralPathParameterSet', - ValueFromPipelineByPropertyName=$true)] + [Parameter(Mandatory = $true, + Position = 0, + ParameterSetName = 'LiteralPathParameterSet', + ValueFromPipelineByPropertyName = $true)] + [Alias('PSPath')] [ValidateNotNullOrEmpty()] [string] $LiteralPath, @@ -99,7 +99,7 @@ function Update-ScriptFileInfo [string[]] $ReleaseNotes, - [Parameter()] + [Parameter()] [ValidateNotNullOrEmpty()] [string] $PrivateData, @@ -113,220 +113,184 @@ function Update-ScriptFileInfo $Force ) - Process - { + Process { # Resolve the script path $scriptFilePath = $null - if($Path) - { + if ($Path) { $scriptFilePath = Resolve-PathHelper -Path $Path -CallerPSCmdlet $PSCmdlet | - Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore + Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore - if(-not $scriptFilePath -or - -not (Microsoft.PowerShell.Management\Test-Path -Path $scriptFilePath -PathType Leaf)) - { + if (-not $scriptFilePath -or + -not (Microsoft.PowerShell.Management\Test-Path -Path $scriptFilePath -PathType Leaf)) { $errorMessage = ($LocalizedData.PathNotFound -f $Path) ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage $errorMessage ` - -ErrorId "PathNotFound" ` - -CallerPSCmdlet $PSCmdlet ` - -ExceptionObject $Path ` - -ErrorCategory InvalidArgument + -ExceptionMessage $errorMessage ` + -ErrorId "PathNotFound" ` + -CallerPSCmdlet $PSCmdlet ` + -ExceptionObject $Path ` + -ErrorCategory InvalidArgument } } - else - { + else { $scriptFilePath = Resolve-PathHelper -Path $LiteralPath -IsLiteralPath -CallerPSCmdlet $PSCmdlet | - Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore + Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore - if(-not $scriptFilePath -or - -not (Microsoft.PowerShell.Management\Test-Path -LiteralPath $scriptFilePath -PathType Leaf)) - { + if (-not $scriptFilePath -or + -not (Microsoft.PowerShell.Management\Test-Path -LiteralPath $scriptFilePath -PathType Leaf)) { $errorMessage = ($LocalizedData.PathNotFound -f $LiteralPath) ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage $errorMessage ` - -ErrorId "PathNotFound" ` - -CallerPSCmdlet $PSCmdlet ` - -ExceptionObject $LiteralPath ` - -ErrorCategory InvalidArgument + -ExceptionMessage $errorMessage ` + -ErrorId "PathNotFound" ` + -CallerPSCmdlet $PSCmdlet ` + -ExceptionObject $LiteralPath ` + -ErrorCategory InvalidArgument } } - if(-not $scriptFilePath.EndsWith('.ps1', [System.StringComparison]::OrdinalIgnoreCase)) - { + if (-not $scriptFilePath.EndsWith('.ps1', [System.StringComparison]::OrdinalIgnoreCase)) { $errorMessage = ($LocalizedData.InvalidScriptFilePath -f $scriptFilePath) ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage $errorMessage ` - -ErrorId "InvalidScriptFilePath" ` - -CallerPSCmdlet $PSCmdlet ` - -ExceptionObject $scriptFilePath ` - -ErrorCategory InvalidArgument + -ExceptionMessage $errorMessage ` + -ErrorId "InvalidScriptFilePath" ` + -CallerPSCmdlet $PSCmdlet ` + -ExceptionObject $scriptFilePath ` + -ErrorCategory InvalidArgument return } # Obtain script info $psscriptInfo = $null - try - { + try { $psscriptInfo = Test-ScriptFileInfo -LiteralPath $scriptFilePath } - catch - { - if(-not $Force) - { + catch { + if (-not $Force) { throw $_ return } } - if(-not $psscriptInfo) - { - if(-not $Description) - { + if (-not $psscriptInfo) { + if (-not $Description) { ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage $LocalizedData.DescriptionParameterIsMissingForAddingTheScriptFileInfo ` - -ErrorId 'DescriptionParameterIsMissingForAddingTheScriptFileInfo' ` - -CallerPSCmdlet $PSCmdlet ` - -ErrorCategory InvalidArgument + -ExceptionMessage $LocalizedData.DescriptionParameterIsMissingForAddingTheScriptFileInfo ` + -ErrorId 'DescriptionParameterIsMissingForAddingTheScriptFileInfo' ` + -CallerPSCmdlet $PSCmdlet ` + -ErrorCategory InvalidArgument return } - if(-not $Version) - { + if (-not $Version) { $Version = '1.0' } - else - { + else { $result = ValidateAndGet-VersionPrereleaseStrings -Version $Version -CallerPSCmdlet $PSCmdlet - if (-not $result) - { + if (-not $result) { # ValidateAndGet-VersionPrereleaseStrings throws the error. # returning to avoid further execution when different values are specified for -ErrorAction parameter return } } - if(-not $Author) - { - if($script:IsWindows) - { + if (-not $Author) { + if ($script:IsWindows) { $Author = (Get-EnvironmentVariable -Name 'USERNAME' -Target $script:EnvironmentVariableTarget.Process -ErrorAction SilentlyContinue) } - else - { + else { $Author = $env:USER } } - if(-not $Guid) - { + if (-not $Guid) { $Guid = [System.Guid]::NewGuid() } } - else - { + else { # Use existing values if any of the parameters are not specified during Update-ScriptFileInfo - if(-not $Version -and $psscriptInfo.Version) - { + if (-not $Version -and $psscriptInfo.Version) { $Version = $psscriptInfo.Version } - if(-not $Guid -and $psscriptInfo.Guid) - { + if (-not $Guid -and $psscriptInfo.Guid) { $Guid = $psscriptInfo.Guid } - if(-not $Author -and $psscriptInfo.Author) - { + if (-not $Author -and $psscriptInfo.Author) { $Author = $psscriptInfo.Author } - if(-not $CompanyName -and $psscriptInfo.CompanyName) - { + if (-not $CompanyName -and $psscriptInfo.CompanyName) { $CompanyName = $psscriptInfo.CompanyName } - if(-not $Copyright -and $psscriptInfo.Copyright) - { + if (-not $Copyright -and $psscriptInfo.Copyright) { $Copyright = $psscriptInfo.Copyright } - if(-not $RequiredModules -and $psscriptInfo.RequiredModules) - { + if (-not $RequiredModules -and $psscriptInfo.RequiredModules) { $RequiredModules = $psscriptInfo.RequiredModules } - if(-not $ExternalModuleDependencies -and $psscriptInfo.ExternalModuleDependencies) - { + if (-not $ExternalModuleDependencies -and $psscriptInfo.ExternalModuleDependencies) { $ExternalModuleDependencies = $psscriptInfo.ExternalModuleDependencies } - if(-not $RequiredScripts -and $psscriptInfo.RequiredScripts) - { + if (-not $RequiredScripts -and $psscriptInfo.RequiredScripts) { $RequiredScripts = $psscriptInfo.RequiredScripts } - if(-not $ExternalScriptDependencies -and $psscriptInfo.ExternalScriptDependencies) - { + if (-not $ExternalScriptDependencies -and $psscriptInfo.ExternalScriptDependencies) { $ExternalScriptDependencies = $psscriptInfo.ExternalScriptDependencies } - if(-not $Tags -and $psscriptInfo.Tags) - { + if (-not $Tags -and $psscriptInfo.Tags) { $Tags = $psscriptInfo.Tags } - if(-not $ProjectUri -and $psscriptInfo.ProjectUri) - { + if (-not $ProjectUri -and $psscriptInfo.ProjectUri) { $ProjectUri = $psscriptInfo.ProjectUri } - if(-not $LicenseUri -and $psscriptInfo.LicenseUri) - { + if (-not $LicenseUri -and $psscriptInfo.LicenseUri) { $LicenseUri = $psscriptInfo.LicenseUri } - if(-not $IconUri -and $psscriptInfo.IconUri) - { + if (-not $IconUri -and $psscriptInfo.IconUri) { $IconUri = $psscriptInfo.IconUri } - if(-not $ReleaseNotes -and $psscriptInfo.ReleaseNotes) - { + if (-not $ReleaseNotes -and $psscriptInfo.ReleaseNotes) { $ReleaseNotes = $psscriptInfo.ReleaseNotes } - - if(-not $PrivateData -and $psscriptInfo.PrivateData) - { + + if (-not $PrivateData -and $psscriptInfo.PrivateData) { $PrivateData = $psscriptInfo.PrivateData } } $params = @{ - Version = $Version - Author = $Author - Guid = $Guid - CompanyName = $CompanyName - Copyright = $Copyright + Version = $Version + Author = $Author + Guid = $Guid + CompanyName = $CompanyName + Copyright = $Copyright ExternalModuleDependencies = $ExternalModuleDependencies - RequiredScripts = $RequiredScripts + RequiredScripts = $RequiredScripts ExternalScriptDependencies = $ExternalScriptDependencies - Tags = $Tags - ProjectUri = $ProjectUri - LicenseUri = $LicenseUri - IconUri = $IconUri - ReleaseNotes = $ReleaseNotes - PrivateData = $PrivateData + Tags = $Tags + ProjectUri = $ProjectUri + LicenseUri = $LicenseUri + IconUri = $IconUri + ReleaseNotes = $ReleaseNotes + PrivateData = $PrivateData } # Ensure no fields contain '<#' or '#>' (would break comment section) - if(-not (Validate-ScriptFileInfoParameters -parameters $params)) - { + if (-not (Validate-ScriptFileInfoParameters -parameters $params)) { return } - if("$Description" -match '<#' -or "$Description" -match '#>') - { + if ("$Description" -match '<#' -or "$Description" -match '#>') { $message = $LocalizedData.InvalidParameterValue -f ($Description, 'Description') Write-Error -Message $message -ErrorId 'InvalidParameterValue' -Category InvalidArgument @@ -337,14 +301,13 @@ function Update-ScriptFileInfo $requiresStrings = Get-RequiresString -RequiredModules $RequiredModules - $DescriptionValue = if($Description) {$Description} else {$psscriptInfo.Description} + $DescriptionValue = if ($Description) { $Description } else { $psscriptInfo.Description } $ScriptCommentHelpInfoString = Get-ScriptCommentHelpInfoString -Description $DescriptionValue $ScriptMetadataString = $PSScriptInfoString $ScriptMetadataString += "`r`n" - if("$requiresStrings".Trim()) - { + if ("$requiresStrings".Trim()) { $ScriptMetadataString += "`r`n" $ScriptMetadataString += $requiresStrings -join "`r`n" $ScriptMetadataString += "`r`n" @@ -356,15 +319,13 @@ function Update-ScriptFileInfo $tempScriptFilePath = Microsoft.PowerShell.Management\Join-Path -Path $script:TempPath -ChildPath "$(Get-Random).ps1" - try - { + try { # First create a new script file with new script metadata to ensure that updated values are valid. Microsoft.PowerShell.Management\Set-Content -Value $ScriptMetadataString -Path $tempScriptFilePath -Force -WhatIf:$false -Confirm:$false $scriptInfo = Test-ScriptFileInfo -Path $tempScriptFilePath - if(-not $scriptInfo) - { + if (-not $scriptInfo) { # Above Test-ScriptFileInfo cmdlet writes the error return } @@ -373,54 +334,49 @@ function Update-ScriptFileInfo # If -Force is specified and script file doesnt have a valid PSScriptInfo # Prepend the PSScriptInfo and Check if the Test-ScriptFileInfo returns a valid script info without any errors - if($Force -and -not $psscriptInfo) - { + if ($Force -and -not $psscriptInfo) { # Add the script file contents to the temp file with script metadata Microsoft.PowerShell.Management\Set-Content -LiteralPath $tempScriptFilePath ` - -Value $ScriptMetadataString,$scriptFileContents ` - -Force ` - -WhatIf:$false ` - -Confirm:$false + -Value $ScriptMetadataString, $scriptFileContents ` + -Force ` + -WhatIf:$false ` + -Confirm:$false $tempScriptInfo = $null - try - { + try { $tempScriptInfo = Test-ScriptFileInfo -LiteralPath $tempScriptFilePath } - catch - { + catch { $errorMessage = ($LocalizedData.UnableToAddPSScriptInfo -f $scriptFilePath) ThrowError -ExceptionName 'System.InvalidOperationException' ` - -ExceptionMessage $errorMessage ` - -ErrorId 'UnableToAddPSScriptInfo' ` - -CallerPSCmdlet $PSCmdlet ` - -ExceptionObject $scriptFilePath ` - -ErrorCategory InvalidOperation + -ExceptionMessage $errorMessage ` + -ErrorId 'UnableToAddPSScriptInfo' ` + -CallerPSCmdlet $PSCmdlet ` + -ExceptionObject $scriptFilePath ` + -ErrorCategory InvalidOperation return } } - else - { + else { [System.Management.Automation.Language.Token[]]$tokens = $null; [System.Management.Automation.Language.ParseError[]]$errors = $null; $ast = [System.Management.Automation.Language.Parser]::ParseFile($scriptFilePath, ([ref]$tokens), ([ref]$errors)) # Update PSScriptInfo and #Requires - $CommentTokens = $tokens | Microsoft.PowerShell.Core\Where-Object {$_.Kind -eq 'Comment'} + $CommentTokens = $tokens | Microsoft.PowerShell.Core\Where-Object { $_.Kind -eq 'Comment' } $psscriptInfoComments = $CommentTokens | - Microsoft.PowerShell.Core\Where-Object { $_.Extent.Text -match "<#PSScriptInfo" } | - Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore + Microsoft.PowerShell.Core\Where-Object { $_.Extent.Text -match "<#PSScriptInfo" } | + Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore - if(-not $psscriptInfoComments) - { + if (-not $psscriptInfoComments) { $errorMessage = ($LocalizedData.MissingPSScriptInfo -f $scriptFilePath) ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage $errorMessage ` - -ErrorId "MissingPSScriptInfo" ` - -CallerPSCmdlet $PSCmdlet ` - -ExceptionObject $scriptFilePath ` - -ErrorCategory InvalidArgument + -ExceptionMessage $errorMessage ` + -ErrorId "MissingPSScriptInfo" ` + -CallerPSCmdlet $PSCmdlet ` + -ExceptionObject $scriptFilePath ` + -ErrorCategory InvalidArgument return } @@ -432,109 +388,91 @@ function Update-ScriptFileInfo $tempContents = @() $IsNewPScriptInfoAdded = $false - for($i = 0; $i -lt $scriptFileContents.Count; $i++) - { - $line = $scriptFileContents[$i] - if(($i -ge ($psscriptInfoComments.Extent.StartLineNumber - 1)) -and - ($i -le ($psscriptInfoComments.Extent.EndLineNumber - 1))) - { - if(-not $IsNewPScriptInfoAdded) - { - $PSScriptInfoString = $PSScriptInfoString.TrimStart() - $requiresStrings = $requiresStrings.TrimEnd() - - $tempContents += "$PSScriptInfoString `r`n`r`n$($requiresStrings -join "`r`n")" - $IsNewPScriptInfoAdded = $true - } - } - elseif($line -notmatch "\s*#Requires\s+-Module") - { - # Add the existing lines if they are not part of PSScriptInfo comment or not containing #Requires -Module statements. - $tempContents += $line - } + for ($i = 0; $i -lt $scriptFileContents.Count; $i++) { + $line = $scriptFileContents[$i] + if (($i -ge ($psscriptInfoComments.Extent.StartLineNumber - 1)) -and + ($i -le ($psscriptInfoComments.Extent.EndLineNumber - 1))) { + if (-not $IsNewPScriptInfoAdded) { + $PSScriptInfoString = $PSScriptInfoString.TrimStart() + $requiresStrings = $requiresStrings.TrimEnd() + + $tempContents += "$PSScriptInfoString `r`n`r`n$($requiresStrings -join "`r`n")" + $IsNewPScriptInfoAdded = $true + } + } + elseif ($line -notmatch "\s*#Requires\s+-Module") { + # Add the existing lines if they are not part of PSScriptInfo comment or not containing #Requires -Module statements. + $tempContents += $line + } } Microsoft.PowerShell.Management\Set-Content -Value $tempContents -Path $tempScriptFilePath -Force -WhatIf:$false -Confirm:$false $scriptInfo = Test-ScriptFileInfo -Path $tempScriptFilePath - if(-not $scriptInfo) - { + if (-not $scriptInfo) { # Above Test-ScriptFileInfo cmdlet writes the error return } # Now update the Description value if a new is specified. - if($Description) - { + if ($Description) { $tempContents = @() $IsDescriptionAdded = $false $IsDescriptionBeginFound = $false $scriptFileContents = Microsoft.PowerShell.Management\Get-Content -Path $tempScriptFilePath - for($i = 0; $i -lt $scriptFileContents.Count; $i++) - { - $line = $scriptFileContents[$i] - - if(-not $IsDescriptionAdded) - { - if(-not $IsDescriptionBeginFound) - { - if($line.Trim().StartsWith(".DESCRIPTION", [System.StringComparison]::OrdinalIgnoreCase)) - { - $IsDescriptionBeginFound = $true + for ($i = 0; $i -lt $scriptFileContents.Count; $i++) { + $line = $scriptFileContents[$i] + + if (-not $IsDescriptionAdded) { + if (-not $IsDescriptionBeginFound) { + if ($line.Trim().StartsWith(".DESCRIPTION", [System.StringComparison]::OrdinalIgnoreCase)) { + $IsDescriptionBeginFound = $true } - else - { + else { $tempContents += $line } } - else - { + else { # Description begin has found # Skip the old description lines until description end is found - if($line.Trim().StartsWith("#>", [System.StringComparison]::OrdinalIgnoreCase) -or - $line.Trim().StartsWith(".", [System.StringComparison]::OrdinalIgnoreCase)) - { - $tempContents += ".DESCRIPTION `r`n$($Description -join "`r`n")`r`n" - $IsDescriptionAdded = $true - $tempContents += $line + if ($line.Trim().StartsWith("#>", [System.StringComparison]::OrdinalIgnoreCase) -or + $line.Trim().StartsWith(".", [System.StringComparison]::OrdinalIgnoreCase)) { + $tempContents += ".DESCRIPTION `r`n$($Description -join "`r`n")`r`n" + $IsDescriptionAdded = $true + $tempContents += $line } } - } - else - { - $tempContents += $line - } + } + else { + $tempContents += $line + } } Microsoft.PowerShell.Management\Set-Content -Value $tempContents -Path $tempScriptFilePath -Force -WhatIf:$false -Confirm:$false $scriptInfo = Test-ScriptFileInfo -Path $tempScriptFilePath - if(-not $scriptInfo) - { + if (-not $scriptInfo) { # Above Test-ScriptFileInfo cmdlet writes the error return } } } - if($Force -or $PSCmdlet.ShouldProcess($scriptFilePath, ($LocalizedData.UpdateScriptFileInfowhatIfMessage -f $Path) )) - { + if ($Force -or $PSCmdlet.ShouldProcess($scriptFilePath, ($LocalizedData.UpdateScriptFileInfowhatIfMessage -f $Path) )) { Microsoft.PowerShell.Management\Copy-Item -Path $tempScriptFilePath -Destination $scriptFilePath -Force -WhatIf:$false -Confirm:$false - if($PassThru) - { + if ($PassThru) { $ScriptMetadataString } } } - finally - { + finally { Microsoft.PowerShell.Management\Remove-Item -Path $tempScriptFilePath -Force -WhatIf:$false -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue } } -} \ No newline at end of file +}