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

Add SkipAutomaticTags Parameter to Publish-Module #452

Merged
merged 4 commits into from
May 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
77 changes: 77 additions & 0 deletions Tests/PSGetPublishModule.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,83 @@ Describe PowerShell.PSGet.PublishModuleTests -Tags 'BVT','InnerLoop' {
} `
-Skip:$($PSVersionTable.PSVersion -lt '5.0.0')

# Purpose: Test Publish-Module cmdlet warns when tag length is greater than 4000
#
# Action: Create a module manifest with PrivateData\PSData hashtable, try to publish
#
# Expected Result: Publish operation should succeed and should have warned about tag length.
#
It PublishModuleFunctionsAsTagsWarnWithoutEnforceMaximumTagLength {
$version = "1.0.0"
$Description = "$script:PublishModuleName module"
$Tags = "PSGet","DSC"
$Author = "AuthorName"
$CompanyName = "CompanyName"
$CopyRight = "CopyRight"

$functionNames = 1..250 | Foreach-Object { "Get-TestFunction$($_)" }
$tempFunctions = 1..250 | Foreach-Object { "function Get-TestFunction$($_) { Get-Date }" + [Environment]::NewLine }
Set-Content (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psm1") -Value $tempFunctions

New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") `
-ModuleVersion $version `
-Description "$script:PublishModuleName module" `
-NestedModules "$script:PublishModuleName.psm1" `
-Author $Author `
-CompanyName $CompanyName `
-Copyright $CopyRight `
-Tags $Tags `
-FunctionsToExport $functionNames

Publish-Module -Path $script:PublishModuleBase `
-NuGetApiKey $script:ApiKey `
-Repository "PSGallery" `
-WarningAction SilentlyContinue `
-WarningVariable wa

Assert ("$wa".Contains('4000 characters')) "Warning messages should include 'Tag list exceeded 4000 characters and may not be accepted by some Nuget feeds.'"
} `
-Skip:$($PSVersionTable.PSVersion -lt '5.0.0')

# Purpose: Test Publish-Module cmdlet excludes functions from tags when using EnforceMaximumTagLength parameter
#
# Action: Create a module manifest with PrivateData\PSData hashtable, try to publish with EnforceMaxmimumTagLength parameter
#
# Expected Result: Publish operation should succeed and should not have warned about tag length
#
It PublishModuleFunctionsAsTagsWithEnforceMaximumTagLength {
$version = "1.0.0"
$Description = "$script:PublishModuleName module"
$Tags = "PSGet","DSC"
$Author = "AuthorName"
$CompanyName = "CompanyName"
$CopyRight = "CopyRight"

$functionNames = 1..250 | Foreach-Object { "Get-TestFunction$($_)" }
$tempFunctions = 1..250 | Foreach-Object { "function Get-TestFunction$($_) { Get-Date }" + [Environment]::NewLine }
Set-Content (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psm1") -Value $tempFunctions

New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") `
-ModuleVersion $version `
-Description "$script:PublishModuleName module" `
-NestedModules "$script:PublishModuleName.psm1" `
-Author $Author `
-CompanyName $CompanyName `
-Copyright $CopyRight `
-Tags $Tags `
-FunctionsToExport $functionNames

Publish-Module -Path $script:PublishModuleBase `
-NuGetApiKey $script:ApiKey `
-Repository "PSGallery" `
-EnforceMaximumTagLength `
-WarningAction SilentlyContinue `
-WarningVariable wa

Assert (-not "$wa".Contains('4000 characters')) "Warning messages should not include 'Tag list exceeded 4000 characters and may not be accepted by some Nuget feeds.'"
} `
-Skip:$($PSVersionTable.PSVersion -lt '5.0.0')

# Purpose: Test Publish-Module cmdlet gets the PSData properties from the module manifest file and also with Uri objects specified to the cmdlet
#
# Action: Create a module manifest with PrivateData\PSData hashtable, try to publish it with Uri objects to ProjectUri, IconUri and LicenseUri parameters
Expand Down
7 changes: 3 additions & 4 deletions src/PowerShellGet/private/functions/New-NuspecFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ function New-NuspecFile {
$packageElement = $xml.CreateElement("package", $nameSpaceUri)
$metaDataElement = $xml.CreateElement("metadata", $nameSpaceUri)

#truncate tags if they exceed nuspec specifications for size.
$Tags = $Tags -Join " "

if ($Tags.Length -gt 4000) {
# warn we're over 4000 characters for standard nuget servers
$tagsString = $Tags -Join " "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need to assign to another variable here? Tags is now left as an array and when we write it out to the nuspec file it may just appear as System.Object[] or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During my testing it seems that since Tags was a parameter defined as a string array it was always kept as an array during the execution. Tags.Length always ended up with a length of 1 (1 element in the array).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point though. I'm going to change this PR to WIP and make sure to validate that further.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ($tagsString.Length -gt 4000) {
Write-Warning -Message "Tag list exceeded 4000 characters and may not be accepted by some Nuget feeds."
}

Expand Down
76 changes: 41 additions & 35 deletions src/PowerShellGet/private/functions/Publish-PSArtifactUtility.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ function Publish-PSArtifactUtility {
[string[]]
$Tags,

[Parameter(ParameterSetName = 'PublishModule')]
[switch]
$EnforceMaximumTagLength,

[Parameter(ParameterSetName = 'PublishModule')]
[Uri]
$LicenseUri,
Expand Down Expand Up @@ -218,7 +222,7 @@ function Publish-PSArtifactUtility {
if ($PSScriptInfo) {
$Tags += "PSScript"

if ($PSScriptInfo.DefinedCommands) {
if ($PSScriptInfo.DefinedCommands -and -not $EnforceMaximumTagLength) {
if ($PSScriptInfo.DefinedFunctions) {
$Tags += "$($script:Includes)_Function"
$Tags += $PSScriptInfo.DefinedFunctions | Microsoft.PowerShell.Core\ForEach-Object { "$($script:Function)_$_" }
Expand Down Expand Up @@ -251,52 +255,54 @@ function Publish-PSArtifactUtility {

$ModuleManifestHashTable = Get-ManifestHashTable -Path $ManifestPath

if ($PSModuleInfo.ExportedCommands.Count) {
if ($PSModuleInfo.ExportedCmdlets.Count) {
$Tags += "$($script:Includes)_Cmdlet"
$Tags += $PSModuleInfo.ExportedCmdlets.Keys | Microsoft.PowerShell.Core\ForEach-Object { "$($script:Cmdlet)_$_" }
if (-not $EnforceMaximumTagLength) {
if ($PSModuleInfo.ExportedCommands.Count) {
if ($PSModuleInfo.ExportedCmdlets.Count) {
$Tags += "$($script:Includes)_Cmdlet"
$Tags += $PSModuleInfo.ExportedCmdlets.Keys | Microsoft.PowerShell.Core\ForEach-Object { "$($script:Cmdlet)_$_" }

#if CmdletsToExport field in manifest file is "*", we suggest the user to include all those cmdlets for best practice
if ($ModuleManifestHashTable -and $ModuleManifestHashTable.ContainsKey('CmdletsToExport') -and ($ModuleManifestHashTable.CmdletsToExport -eq "*")) {
$WarningMessage = $LocalizedData.ShouldIncludeCmdletsToExport -f ($ManifestPath)
Write-Warning -Message $WarningMessage
#if CmdletsToExport field in manifest file is "*", we suggest the user to include all those cmdlets for best practice
if ($ModuleManifestHashTable -and $ModuleManifestHashTable.ContainsKey('CmdletsToExport') -and ($ModuleManifestHashTable.CmdletsToExport -eq "*")) {
$WarningMessage = $LocalizedData.ShouldIncludeCmdletsToExport -f ($ManifestPath)
Write-Warning -Message $WarningMessage
}
}
}

if ($PSModuleInfo.ExportedFunctions.Count) {
$Tags += "$($script:Includes)_Function"
$Tags += $PSModuleInfo.ExportedFunctions.Keys | Microsoft.PowerShell.Core\ForEach-Object { "$($script:Function)_$_" }
if ($PSModuleInfo.ExportedFunctions.Count) {
$Tags += "$($script:Includes)_Function"
$Tags += $PSModuleInfo.ExportedFunctions.Keys | Microsoft.PowerShell.Core\ForEach-Object { "$($script:Function)_$_" }

if ($ModuleManifestHashTable -and $ModuleManifestHashTable.ContainsKey('FunctionsToExport') -and ($ModuleManifestHashTable.FunctionsToExport -eq "*")) {
$WarningMessage = $LocalizedData.ShouldIncludeFunctionsToExport -f ($ManifestPath)
Write-Warning -Message $WarningMessage
if ($ModuleManifestHashTable -and $ModuleManifestHashTable.ContainsKey('FunctionsToExport') -and ($ModuleManifestHashTable.FunctionsToExport -eq "*")) {
$WarningMessage = $LocalizedData.ShouldIncludeFunctionsToExport -f ($ManifestPath)
Write-Warning -Message $WarningMessage
}
}
}

$Tags += $PSModuleInfo.ExportedCommands.Keys | Microsoft.PowerShell.Core\ForEach-Object { "$($script:Command)_$_" }
}
$Tags += $PSModuleInfo.ExportedCommands.Keys | Microsoft.PowerShell.Core\ForEach-Object { "$($script:Command)_$_" }
}

$dscResourceNames = Get-ExportedDscResources -PSModuleInfo $PSModuleInfo
if ($dscResourceNames) {
$Tags += "$($script:Includes)_DscResource"
$dscResourceNames = Get-ExportedDscResources -PSModuleInfo $PSModuleInfo
if ($dscResourceNames) {
$Tags += "$($script:Includes)_DscResource"

$Tags += $dscResourceNames | Microsoft.PowerShell.Core\ForEach-Object { "$($script:DscResource)_$_" }
$Tags += $dscResourceNames | Microsoft.PowerShell.Core\ForEach-Object { "$($script:DscResource)_$_" }

#If DscResourcesToExport is commented out or "*" is used, we will write-warning
if ($ModuleManifestHashTable -and
($ModuleManifestHashTable.ContainsKey("DscResourcesToExport") -and
$ModuleManifestHashTable.DscResourcesToExport -eq "*") -or
-not $ModuleManifestHashTable.ContainsKey("DscResourcesToExport")) {
$WarningMessage = $LocalizedData.ShouldIncludeDscResourcesToExport -f ($ManifestPath)
Write-Warning -Message $WarningMessage
#If DscResourcesToExport is commented out or "*" is used, we will write-warning
if ($ModuleManifestHashTable -and
($ModuleManifestHashTable.ContainsKey("DscResourcesToExport") -and
$ModuleManifestHashTable.DscResourcesToExport -eq "*") -or
-not $ModuleManifestHashTable.ContainsKey("DscResourcesToExport")) {
$WarningMessage = $LocalizedData.ShouldIncludeDscResourcesToExport -f ($ManifestPath)
Write-Warning -Message $WarningMessage
}
}
}

$RoleCapabilityNames = Get-AvailableRoleCapabilityName -PSModuleInfo $PSModuleInfo
if ($RoleCapabilityNames) {
$Tags += "$($script:Includes)_RoleCapability"
$RoleCapabilityNames = Get-AvailableRoleCapabilityName -PSModuleInfo $PSModuleInfo
if ($RoleCapabilityNames) {
$Tags += "$($script:Includes)_RoleCapability"

$Tags += $RoleCapabilityNames | Microsoft.PowerShell.Core\ForEach-Object { "$($script:RoleCapability)_$_" }
$Tags += $RoleCapabilityNames | Microsoft.PowerShell.Core\ForEach-Object { "$($script:RoleCapability)_$_" }
}
}

# Populate the module dependencies elements from RequiredModules and
Expand Down
39 changes: 22 additions & 17 deletions src/PowerShellGet/public/psgetfunctions/Publish-Module.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ function Publish-Module {

[Parameter(ParameterSetName = "ModuleNameParameterSet")]
[switch]
$AllowPrerelease
$AllowPrerelease,

[Parameter()]
[switch]
$EnforceMaximumTagLength
)

Begin {
Expand Down Expand Up @@ -541,22 +545,23 @@ function Publish-Module {
$shouldProcessMessage = $LocalizedData.PublishModulewhatIfMessage -f ($moduleInfo.Version, $moduleInfo.Name)
if ($Force -or $PSCmdlet.ShouldProcess($shouldProcessMessage, "Publish-Module")) {
$PublishPSArtifactUtility_Params = @{
PSModuleInfo = $moduleInfo
ManifestPath = $manifestPath
NugetApiKey = $NuGetApiKey
Destination = $DestinationLocation
Repository = $Repository
NugetPackageRoot = $tempModulePath
FormatVersion = $FormatVersion
ReleaseNotes = $($ReleaseNotes -join "`r`n")
Tags = $Tags
LicenseUri = $LicenseUri
IconUri = $IconUri
ProjectUri = $ProjectUri
Verbose = $VerbosePreference
WarningAction = $WarningPreference
ErrorAction = $ErrorActionPreference
Debug = $DebugPreference
PSModuleInfo = $moduleInfo
ManifestPath = $manifestPath
NugetApiKey = $NuGetApiKey
Destination = $DestinationLocation
Repository = $Repository
NugetPackageRoot = $tempModulePath
FormatVersion = $FormatVersion
ReleaseNotes = $($ReleaseNotes -join "`r`n")
Tags = $Tags
EnforceMaximumTagLength = $EnforceMaximumTagLength
LicenseUri = $LicenseUri
IconUri = $IconUri
ProjectUri = $ProjectUri
Verbose = $VerbosePreference
WarningAction = $WarningPreference
ErrorAction = $ErrorActionPreference
Debug = $DebugPreference
}
if ($PSBoundParameters.Containskey('Credential')) {
$PublishPSArtifactUtility_Params.Add('Credential', $Credential)
Expand Down