Skip to content

Instantly share code, notes, and snippets.

@LaurentDardenne
Created September 12, 2023 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LaurentDardenne/156ef18ea6b34adc6df5c0f8dab6e834 to your computer and use it in GitHub Desktop.
Save LaurentDardenne/156ef18ea6b34adc6df5c0f8dab6e834 to your computer and use it in GitHub Desktop.
Tests, only after a call of Save-Module, the case of the file names of a published module (.nupkg).
function Test-ModuleNaming {
#We test the naming of a module.
#see : https://github.com/PowerShell/PowerShell/issues/17342
# https://github.com/Splaxi/PSNotification/issues/15
param(
#Name of module directory to check.The name come from the nuget package ( case sensitive)
$ModuleName,
$Version,
#Path where the module was saved by Save-ModuleCache.
$SavePath
)
if ($null -eq $Version)
{ throw "Assert: Test-ModuleNaming '-Version' parameter must be filled in." }
$ModuleRegex = [RegEx]::Escape($ModuleName) + '\.(psd1|psm1|ni\.dll|dll|exe)$'
$Files = Get-ChildItem "$SavePath\$ModuleName\$Version\$ModuleName.*" -Include *.psd1, *.psm1, *.ni.dll, *.dll, *.exe
If ($Files.Count -eq 0) {
Write-Warning "The module directory must contain at least one file of one of these types .psd1 or .psm1 or .ni.dll or .dll or .exe : '$SavePath\$ModuleName\$Version'"
#This is not a naming error
return $true
}
Foreach ($File in $Files) {
$isNamingCorrect = $File -cMatch $ModuleRegex
switch ($File.Extension) {
#If a correctly named manifest exists we quit without warning otherwise we warn and then we quit.
#If no manifest exists we continue the search.
'.psd1' { if (-not $isNamingCorrect) { Write-Warning "Module manifest name is invalid under Ubuntu : '$file'" }; return }
'.psm1' { if (-not $isNamingCorrect) { Write-Warning "Script module name is invalid under Ubuntu : '$file'" }; return }
#'.ni.dll' see PS Core source 'PowerShellNgenAssemblyExtension'
# Note : "Test.no.dll"' match '.dll'
{ '.ni.dll', '.dll' -eq $_ } { if (-not $isNamingCorrect) { Write-Warning "Binary module name is invalid under Ubuntu : '$file'" }; return }
#See use case :https://github.com/PowerShell/PowerShell/issues/6741#issuecomment-385746538
'.exe' { if (-not $isNamingCorrect) { Write-Warning "Executable name is invalid under Ubuntu : '$file'" }; return }
# Windows Powershell only : CDXML (WMI) or .xaml (Workflow).
}
}
return $isNamingCorrect
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment