Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Not all functions with a Path parameter have a LiteralPath parameter #488 #539

Open
wants to merge 10 commits into
base: development
Choose a base branch
from
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
157 changes: 79 additions & 78 deletions src/PowerShellGet/public/psgetfunctions/New-ScriptFileInfo.ps1
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
function New-ScriptFileInfo
{
function New-ScriptFileInfo {
<#
.ExternalHelp PSModule-help.xml
#>
[CmdletBinding(PositionalBinding=$false,
SupportsShouldProcess=$true,
HelpUri='https://go.microsoft.com/fwlink/?LinkId=619792')]
[CmdletBinding(PositionalBinding = $false,
SupportsShouldProcess = $true,
HelpUri = 'https://go.microsoft.com/fwlink/?LinkId=619792')]
Param
(
[Parameter(Mandatory=$false,
Position=0,
ValueFromPipelineByPropertyName=$true)]
[Parameter(Mandatory = $false,
Position = 0,
ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[string]
$Path,

[Parameter(Mandatory = $false,
Position = 0,
ValueFromPipelineByPropertyName = $true)]
[Alias('PSPath')]
[ValidateNotNullOrEmpty()]
[string]
$LiteralPath,

[Parameter()]
[ValidateNotNullOrEmpty()]
[string]
Expand All @@ -25,7 +32,7 @@ function New-ScriptFileInfo
[string]
$Author,

[Parameter(Mandatory=$true)]
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]
$Description,
Expand Down Expand Up @@ -89,8 +96,8 @@ function New-ScriptFileInfo
[string[]]
$ReleaseNotes,

[Parameter()]
[ValidateNotNullOrEmpty()]
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]
$PrivateData,

Expand All @@ -103,100 +110,100 @@ function New-ScriptFileInfo
$Force
)

Process
{
if($Path)
{
if(-not $Path.EndsWith('.ps1', [System.StringComparison]::OrdinalIgnoreCase))
{
Process {
if ($Path) {
if (!$Path) {
#echo "Path is not null, Do nothing"
$Path = $LiteralPath;
}
}
Elseif ($LiteralPath) {
#echo "LiteralPath is not null, assign the value to Path"
$Path = $LiteralPath
}
else {
#echo "Both are null, do nothing"
}

if ($Path) {
if (-not $Path.EndsWith('.ps1', [System.StringComparison]::OrdinalIgnoreCase)) {
$errorMessage = ($LocalizedData.InvalidScriptFilePath -f $Path)
ThrowError -ExceptionName 'System.ArgumentException' `
-ExceptionMessage $errorMessage `
-ErrorId 'InvalidScriptFilePath' `
-CallerPSCmdlet $PSCmdlet `
-ExceptionObject $Path `
-ErrorCategory InvalidArgument
-ExceptionMessage $errorMessage `
-ErrorId 'InvalidScriptFilePath' `
-CallerPSCmdlet $PSCmdlet `
-ExceptionObject $Path `
-ErrorCategory InvalidArgument
return
}

if(-not $Force -and (Microsoft.PowerShell.Management\Test-Path -Path $Path))
{
if (-not $Force -and (Microsoft.PowerShell.Management\Test-Path -Path $Path)) {
$errorMessage = ($LocalizedData.ScriptFileExist -f $Path)
ThrowError -ExceptionName 'System.ArgumentException' `
-ExceptionMessage $errorMessage `
-ErrorId 'ScriptFileExist' `
-CallerPSCmdlet $PSCmdlet `
-ExceptionObject $Path `
-ErrorCategory InvalidArgument
-ExceptionMessage $errorMessage `
-ErrorId 'ScriptFileExist' `
-CallerPSCmdlet $PSCmdlet `
-ExceptionObject $Path `
-ErrorCategory InvalidArgument
return
}
}
elseif(-not $PassThru)
{
elseif (-not $PassThru) {
ThrowError -ExceptionName 'System.ArgumentException' `
-ExceptionMessage $LocalizedData.MissingTheRequiredPathOrPassThruParameter `
-ErrorId 'MissingTheRequiredPathOrPassThruParameter' `
-CallerPSCmdlet $PSCmdlet `
-ErrorCategory InvalidArgument
-ExceptionMessage $LocalizedData.MissingTheRequiredPathOrPassThruParameter `
-ErrorId 'MissingTheRequiredPathOrPassThruParameter' `
-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()
}

$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
}

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

Expand All @@ -212,8 +219,7 @@ function New-ScriptFileInfo
$ScriptMetadataString = $PSScriptInfoString
$ScriptMetadataString += "`r`n"

if("$requiresStrings".Trim())
{
if ("$requiresStrings".Trim()) {
$ScriptMetadataString += "`r`n"
$ScriptMetadataString += $requiresStrings -join "`r`n"
$ScriptMetadataString += "`r`n"
Expand All @@ -225,31 +231,26 @@ function New-ScriptFileInfo

$tempScriptFilePath = Microsoft.PowerShell.Management\Join-Path -Path $script:TempPath -ChildPath "$(Get-Random).ps1"

try
{
try {
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 errors
return
}

if($Path -and ($Force -or $PSCmdlet.ShouldProcess($Path, ($LocalizedData.NewScriptFileInfowhatIfMessage -f $Path) )))
{
if ($Path -and ($Force -or $PSCmdlet.ShouldProcess($Path, ($LocalizedData.NewScriptFileInfowhatIfMessage -f $Path) ))) {
Microsoft.PowerShell.Management\Copy-Item -Path $tempScriptFilePath -Destination $Path -Force -WhatIf:$false -Confirm:$false
}

if($PassThru)
{
if ($PassThru) {
Write-Output -InputObject $ScriptMetadataString
}
}
finally
{
finally {
Microsoft.PowerShell.Management\Remove-Item -Path $tempScriptFilePath -Force -WhatIf:$false -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
}
}
}
}
57 changes: 40 additions & 17 deletions src/PowerShellGet/public/psgetfunctions/Publish-Module.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ function Publish-Module {
[string]
$Path,

[Parameter(Mandatory = $true,
ParameterSetName = "ModuleLiteralPathParameterSet",
ValueFromPipelineByPropertyName = $true)]
[Alias('PSPath')]
[ValidateNotNullOrEmpty()]
[string]
$LiteralPath,

[Parameter(ParameterSetName = "ModuleNameParameterSet")]
[ValidateNotNullOrEmpty()]
[string]
Expand Down Expand Up @@ -123,6 +131,21 @@ function Publish-Module {
}

Process {

if ($Path) {
if (!$Path) {
#echo "Path is not null, Do nothing"
$Path = $LiteralPath;
}
}
Elseif ($LiteralPath) {
#echo "LiteralPath is not null, assign the value to Path"
$Path = $LiteralPath
}
else {
#echo "Both are null, do nothing"
}

if ($Repository -eq $Script:PSGalleryModuleSource) {
$moduleSource = Get-PSRepository -Name $Repository -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if (-not $moduleSource) {
Expand Down Expand Up @@ -215,16 +238,16 @@ function Publish-Module {
# Find the module to be published locally, search by name and RequiredVersion
$module = Microsoft.PowerShell.Core\Get-Module -ListAvailable -Name $Name -Verbose:$false |
Microsoft.PowerShell.Core\Where-Object {
$modInfoPrerelease = $null
if ($_.PrivateData -and
$_.PrivateData.GetType().ToString() -eq "System.Collections.Hashtable" -and
$_.PrivateData["PSData"] -and
$_.PrivateData.PSData.GetType().ToString() -eq "System.Collections.Hashtable" -and
$_.PrivateData.PSData["Prerelease"]) {
$modInfoPrerelease = $_.PrivateData.PSData.Prerelease
$modInfoPrerelease = $null
if ($_.PrivateData -and
$_.PrivateData.GetType().ToString() -eq "System.Collections.Hashtable" -and
$_.PrivateData["PSData"] -and
$_.PrivateData.PSData.GetType().ToString() -eq "System.Collections.Hashtable" -and
$_.PrivateData.PSData["Prerelease"]) {
$modInfoPrerelease = $_.PrivateData.PSData.Prerelease
}
(-not $RequiredVersion) -or ( ($reqVersion -eq $_.Version) -and ($reqPrerelease -match $modInfoPrerelease) )
}
(-not $RequiredVersion) -or ( ($reqVersion -eq $_.Version) -and ($reqPrerelease -match $modInfoPrerelease) )
}

if (-not $module) {
if ($RequiredVersion) {
Expand Down Expand Up @@ -378,13 +401,13 @@ function Publish-Module {
# This finds all the items without force (leaving out hidden files and dirs) then copies them
Microsoft.PowerShell.Management\Get-ChildItem $Path -recurse |
Microsoft.PowerShell.Management\Copy-Item -Force -Confirm:$false -WhatIf:$false -Destination {
if ($_.PSIsContainer) {
Join-Path $tempModulePathForFormatVersion $_.Parent.FullName.substring($path.length)
}
else {
join-path $tempModulePathForFormatVersion $_.FullName.Substring($path.Length)
if ($_.PSIsContainer) {
Join-Path $tempModulePathForFormatVersion $_.Parent.FullName.substring($path.length)
}
else {
join-path $tempModulePathForFormatVersion $_.FullName.Substring($path.Length)
}
}
}

try {
$manifestPath = Join-PathUtility -Path $tempModulePathForFormatVersion -ChildPath "$moduleName.psd1" -PathType File
Expand Down Expand Up @@ -458,7 +481,7 @@ function Publish-Module {
# Check if the specified module name is already used for a script on the specified repository
# Use Find-Script to check if that name is already used as scriptname
$scriptPSGetItemInfo = Find-Script @FindParameters |
Microsoft.PowerShell.Core\Where-Object {$_.Name -eq $moduleName} |
Microsoft.PowerShell.Core\Where-Object { $_.Name -eq $moduleName } |
Microsoft.PowerShell.Utility\Select-Object -Last 1 -ErrorAction Ignore
if ($scriptPSGetItemInfo) {
$message = $LocalizedData.SpecifiedNameIsAlearyUsed -f ($moduleName, $Repository, 'Find-Script')
Expand All @@ -472,7 +495,7 @@ function Publish-Module {

$null = $FindParameters.Remove('Tag')
$currentPSGetItemInfo = Find-Module @FindParameters |
Microsoft.PowerShell.Core\Where-Object {$_.Name -eq $moduleInfo.Name} |
Microsoft.PowerShell.Core\Where-Object { $_.Name -eq $moduleInfo.Name } |
Microsoft.PowerShell.Utility\Select-Object -Last 1 -ErrorAction Ignore

if ($currentPSGetItemInfo) {
Expand Down
Loading