Skip to content

Commit 74d07fc

Browse files
committed
feat: ✨ Add localization support and improve error handling
* Introduced `Messages.psd1` for localized messages. * Updated functions to utilize localized messages for warnings and errors. * Improved formatting and readability in several functions. * Ensured consistent use of `[CmdletBinding()]` across all functions.
1 parent abd30f9 commit 74d07fc

11 files changed

+231
-116
lines changed

PowerShellBuild/PowerShellBuild.psm1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Dot source public functions
22
$private = @(Get-ChildItem -Path ([IO.Path]::Combine($PSScriptRoot, 'Private/*.ps1')) -Recurse)
3-
$public = @(Get-ChildItem -Path ([IO.Path]::Combine($PSScriptRoot, 'Public/*.ps1')) -Recurse)
3+
$public = @(Get-ChildItem -Path ([IO.Path]::Combine($PSScriptRoot, 'Public/*.ps1')) -Recurse)
44
foreach ($import in $public + $private) {
55
try {
66
. $import.FullName
@@ -9,6 +9,14 @@ foreach ($import in $public + $private) {
99
}
1010
}
1111

12+
$importLocalizedDataSplat = @{
13+
BindingVariable = 'LocalizedData'
14+
FileName = 'Messages.psd1'
15+
ErrorAction = 'SilentlyContinue'
16+
}
17+
Import-LocalizedData @importLocalizedDataSplat
18+
19+
1220
Export-ModuleMember -Function $public.Basename
1321

1422
# $psakeTaskAlias = 'PowerShellBuild.psake.tasks'

PowerShellBuild/Public/Build-PSBuildMAMLHelp.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function Build-PSBuildMAMLHelp {
1414
Uses PlatyPS to generate MAML XML help from markdown files in ./docs
1515
and saves the XML file to a directory under ./output/MyModule
1616
#>
17-
[cmdletbinding()]
17+
[CmdletBinding()]
1818
param(
1919
[parameter(Mandatory)]
2020
[string]$Path,
@@ -23,7 +23,7 @@ function Build-PSBuildMAMLHelp {
2323
[string]$DestinationPath
2424
)
2525

26-
$helpLocales = (Get-ChildItem -Path $Path -Directory).Name
26+
$helpLoc2ales = (Get-ChildItem -Path $Path -Directory).Name
2727

2828
# Generate the module's primary MAML help file
2929
foreach ($locale in $helpLocales) {

PowerShellBuild/Public/Build-PSBuildMarkdown.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function Build-PSBuildMarkdown {
2525
2626
Analysis the comment-based help of the MyModule module and create markdown documents under ./docs/en-US.
2727
#>
28-
[cmdletbinding()]
28+
[CmdletBinding()]
2929
param(
3030
[parameter(Mandatory)]
3131
[string]$ModulePath,
@@ -56,7 +56,7 @@ function Build-PSBuildMarkdown {
5656

5757
try {
5858
if ($moduleInfo.ExportedCommands.Count -eq 0) {
59-
Write-Warning 'No commands have been exported. Skipping markdown generation.'
59+
Write-Warning $LocalizedData.NoCommandsExported
6060
return
6161
}
6262

@@ -93,7 +93,7 @@ function Build-PSBuildMarkdown {
9393
}
9494
New-MarkdownHelp @newMDParams > $null
9595
} catch {
96-
Write-Error "Failed to generate markdown help. : $_"
96+
Write-Error ($LocalizedData.FailedToGenerateMarkdownHelp -f $_)
9797
} finally {
9898
Remove-Module $moduleName
9999
}

PowerShellBuild/Public/Build-PSBuildModule.ps1

Lines changed: 83 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,58 @@
1+
# spell-checker:ignore modulename
12
function Build-PSBuildModule {
23
<#
34
.SYNOPSIS
45
Builds a PowerShell module based on source directory
56
.DESCRIPTION
6-
Builds a PowerShell module based on source directory and optionally concatenates
7-
public/private functions from separete files into monolithic .PSM1 file.
7+
Builds a PowerShell module based on source directory and optionally
8+
concatenates public/private functions from separate files into
9+
monolithic .PSM1 file.
810
.PARAMETER Path
911
The source module path.
1012
.PARAMETER DestinationPath
1113
Destination path to write "built" module to.
1214
.PARAMETER ModuleName
1315
The name of the module.
1416
.PARAMETER Compile
15-
Switch to indicate if separete function files should be concatenated into monolithic .PSM1 file.
17+
Switch to indicate if separate function files should be concatenated
18+
into monolithic .PSM1 file.
1619
.PARAMETER CompileHeader
1720
String that will be at the top of your PSM1 file.
1821
.PARAMETER CompileFooter
1922
String that will be added to the bottom of your PSM1 file.
2023
.PARAMETER CompileScriptHeader
2124
String that will be added to your PSM1 file before each script file.
2225
.PARAMETER CompileScriptFooter
23-
String that will be added to your PSM1 file beforeafter each script file.
26+
String that will be added to your PSM1 file after each script file.
2427
.PARAMETER ReadMePath
25-
Path to project README. If present, this will become the "about_<ModuleName>.help.txt" file in the build module.
28+
Path to project README. If present, this will become the
29+
"about_<ModuleName>.help.txt" file in the build module.
2630
.PARAMETER CompileDirectories
27-
List of directories containing .ps1 files that will also be compiled into the PSM1.
31+
List of directories containing .ps1 files that will also be compiled
32+
into the PSM1.
2833
.PARAMETER CopyDirectories
2934
List of directories that will copying "as-is" into the build module.
3035
.PARAMETER Exclude
31-
Array of files (regular expressions) to exclude from copying into built module.
36+
Array of files (regular expressions) to exclude from copying into built
37+
module.
3238
.PARAMETER Culture
33-
UI Culture. This is used to determine what culture directory to store "about_<ModuleName>.help.txt" in.
39+
UI Culture. This is used to determine what culture directory to store
40+
"about_<ModuleName>.help.txt" in.
3441
.EXAMPLE
35-
PS> $buildParams = @{
42+
$buildParams = @{
3643
Path = ./MyModule
3744
DestinationPath = ./Output/MoModule/0.1.0
3845
ModuleName = MyModule
3946
Exclude = @()
4047
Compile = $false
4148
Culture = (Get-UICulture).Name
4249
}
43-
PS> Build-PSBuildModule @buildParams
50+
Build-PSBuildModule @buildParams
4451
45-
Build module from source directory './MyModule' and save to '/Output/MoModule/0.1.0'
52+
Build module from source directory './MyModule' and save to
53+
'/Output/MoModule/0.1.0'
4654
#>
47-
[cmdletbinding()]
55+
[CmdletBinding()]
4856
param(
4957
[parameter(Mandatory)]
5058
[string]$Path,
@@ -77,49 +85,80 @@ function Build-PSBuildModule {
7785
)
7886

7987
if (-not (Test-Path -LiteralPath $DestinationPath)) {
80-
New-Item -Path $DestinationPath -ItemType Directory -Verbose:$VerbosePreference > $null
88+
$newItemSplat = @{
89+
Path = $DestinationPath
90+
ItemType = 'Directory'
91+
Verbose = $VerbosePreference
92+
}
93+
New-Item @newItemSplat > $null
8194
}
8295

8396
# Copy "non-processed files"
84-
Get-ChildItem -Path $Path -Include '*.psm1', '*.psd1', '*.ps1xml' -Depth 1 | Copy-Item -Destination $DestinationPath -Force
97+
$getChildItemSplat = @{
98+
Path = $Path
99+
Include = '*.psm1', '*.psd1', '*.ps1xml'
100+
Depth = 1
101+
}
102+
Get-ChildItem @getChildItemSplat |
103+
Copy-Item -Destination $DestinationPath -Force
85104
foreach ($dir in $CopyDirectories) {
86105
$copyPath = [IO.Path]::Combine($Path, $dir)
87106
Copy-Item -Path $copyPath -Destination $DestinationPath -Recurse -Force
88107
}
89108

90109
# Copy README as about_<modulename>.help.txt
91110
if (-not [string]::IsNullOrEmpty($ReadMePath)) {
92-
$culturePath = [IO.Path]::Combine($DestinationPath, $Culture)
93-
$aboutModulePath = [IO.Path]::Combine($culturePath, "about_$($ModuleName).help.txt")
94-
if(-not (Test-Path $culturePath -PathType Container)) {
111+
$culturePath = [IO.Path]::Combine($DestinationPath, $Culture)
112+
$aboutModulePath = [IO.Path]::Combine(
113+
$culturePath,
114+
"about_$($ModuleName).help.txt"
115+
)
116+
if (-not (Test-Path $culturePath -PathType Container)) {
95117
New-Item $culturePath -Type Directory -Force > $null
96-
Copy-Item -LiteralPath $ReadMePath -Destination $aboutModulePath -Force
118+
$copyItemSplat = @{
119+
LiteralPath = $ReadMePath
120+
Destination = $aboutModulePath
121+
Force = $true
122+
}
123+
Copy-Item @copyItemSplat
97124
}
98125
}
99126

100-
# Copy source files to destination and optionally combine *.ps1 files into the PSM1
127+
# Copy source files to destination and optionally combine *.ps1 files
128+
# into the PSM1
101129
if ($Compile.IsPresent) {
102130
$rootModule = [IO.Path]::Combine($DestinationPath, "$ModuleName.psm1")
103131

104132
# Grab the contents of the copied over PSM1
105133
# This will be appended to the end of the finished PSM1
106134
$psm1Contents = Get-Content -Path $rootModule -Raw
107-
'' | Out-File -FilePath $rootModule -Encoding utf8
135+
'' | Out-File -FilePath $rootModule -Encoding 'utf8'
108136

109137
if ($CompileHeader) {
110-
$CompileHeader | Add-Content -Path $rootModule -Encoding utf8
138+
$CompileHeader | Add-Content -Path $rootModule -Encoding 'utf8'
111139
}
112140

113141
$resolvedCompileDirectories = $CompileDirectories | ForEach-Object {
114142
[IO.Path]::Combine($Path, $_)
115143
}
116-
$allScripts = Get-ChildItem -Path $resolvedCompileDirectories -Filter '*.ps1' -File -Recurse -ErrorAction SilentlyContinue
144+
$getChildItemSplat = @{
145+
Path = $resolvedCompileDirectories
146+
Filter = '*.ps1'
147+
File = $true
148+
Recurse = $true
149+
ErrorAction = 'SilentlyContinue'
150+
}
151+
$allScripts = Get-ChildItem @getChildItemSplat
117152

118153
$allScripts = $allScripts | Remove-ExcludedItem -Exclude $Exclude
119154

155+
$addContentSplat = @{
156+
Path = $rootModule
157+
Encoding = 'utf8'
158+
}
120159
$allScripts | ForEach-Object {
121160
$srcFile = Resolve-Path $_.FullName -Relative
122-
Write-Verbose "Adding [$srcFile] to PSM1"
161+
Write-Verbose ($LocalizedData.AddingFileToPsm1 -f $srcFile)
123162

124163
if ($CompileScriptHeader) {
125164
Write-Output $CompileScriptHeader
@@ -130,15 +169,14 @@ function Build-PSBuildModule {
130169
if ($CompileScriptFooter) {
131170
Write-Output $CompileScriptFooter
132171
}
172+
} | Add-Content @addContentSplat
133173

134-
} | Add-Content -Path $rootModule -Encoding utf8
135-
136-
$psm1Contents | Add-Content -Path $rootModule -Encoding utf8
174+
$psm1Contents | Add-Content @addContentSplat
137175

138176
if ($CompileFooter) {
139-
$CompileFooter | Add-Content -Path $rootModule -Encoding utf8
177+
$CompileFooter | Add-Content @addContentSplat
140178
}
141-
} else{
179+
} else {
142180
# Copy everything over, then remove stuff that should have been excluded
143181
# It's just easier this way
144182
$copyParams = @{
@@ -157,13 +195,26 @@ function Build-PSBuildModule {
157195
}
158196
}
159197
}
160-
$toRemove | Remove-Item -Recurse -Force -ErrorAction Ignore
198+
$toRemove | Remove-Item -Recurse -Force -ErrorAction 'Ignore'
161199
}
162200

163201
# Export public functions in manifest if there are any public functions
164-
$publicFunctions = Get-ChildItem $Path/Public/*.ps1 -Recurse -ErrorAction SilentlyContinue
202+
$getChildItemSplat = @{
203+
Recurse = $true
204+
ErrorAction = 'SilentlyContinue'
205+
Path = "$Path/Public/*.ps1"
206+
}
207+
$publicFunctions = Get-ChildItem @getChildItemSplat
165208
if ($publicFunctions) {
166-
$outputManifest = [IO.Path]::Combine($DestinationPath, "$ModuleName.psd1")
167-
Update-Metadata -Path $OutputManifest -PropertyName FunctionsToExport -Value $publicFunctions.BaseName
209+
$outputManifest = [IO.Path]::Combine(
210+
$DestinationPath,
211+
"$ModuleName.psd1"
212+
)
213+
$updateMetadataSplat = @{
214+
Path = $OutputManifest
215+
PropertyName = 'FunctionsToExport'
216+
Value = $publicFunctions.BaseName
217+
}
218+
Update-Metadata @updateMetadataSplat
168219
}
169220
}

PowerShellBuild/Public/Build-PSBuildUpdatableHelp.ps1

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ function Build-PSBuildUpdatableHelp {
99
.PARAMETER OutputPath
1010
Path to create updatable help .cab file in.
1111
.PARAMETER Module
12-
Name of the module to create a .cab file for. Defaults to the $ModuleName variable from the parent scope.
12+
Name of the module to create a .cab file for. Defaults to the
13+
$ModuleName variable from the parent scope.
1314
.EXAMPLE
1415
PS> Build-PSBuildUpdatableHelp -DocsPath ./docs -OutputPath ./Output/UpdatableHelp
1516
1617
Create help .cab file based on PlatyPS markdown help.
1718
#>
18-
[cmdletbinding()]
19+
[CmdletBinding()]
1920
param(
2021
[parameter(Mandatory)]
2122
[string]$DocsPath,
@@ -27,26 +28,40 @@ function Build-PSBuildUpdatableHelp {
2728
)
2829

2930
if ($null -ne $IsWindows -and -not $IsWindows) {
30-
Write-Warning 'MakeCab.exe is only available on Windows. Cannot create help cab.'
31+
Write-Warning $LocalizedData.MakeCabNotAvailable
3132
return
3233
}
3334

3435
$helpLocales = (Get-ChildItem -Path $DocsPath -Directory).Name
3536

3637
# Create updatable help output directory
3738
if (-not (Test-Path -LiteralPath $OutputPath)) {
38-
New-Item $OutputPath -ItemType Directory -Verbose:$VerbosePreference > $null
39+
$newItemSplat = @{
40+
ItemType = 'Directory'
41+
Verbose = $VerbosePreference
42+
Path = $OutputPath
43+
}
44+
New-Item @newItemSplat > $null
3945
} else {
40-
Write-Verbose "Directory already exists [$OutputPath]."
41-
Get-ChildItem $OutputPath | Remove-Item -Recurse -Force -Verbose:$VerbosePreference
46+
Write-Verbose ($LocalizedData.DirectoryAlreadyExists -f $OutputPath)
47+
$removeItemSplat = @{
48+
Recurse = $true
49+
Force = $true
50+
Verbose = $VerbosePreference
51+
}
52+
Get-ChildItem $OutputPath | Remove-Item @removeItemSplat
4253
}
4354

44-
# Generate updatable help files. Note: this will currently update the version number in the module's MD
45-
# file in the metadata.
55+
# Generate updatable help files. Note: this will currently update the
56+
# version number in the module's MD file in the metadata.
4657
foreach ($locale in $helpLocales) {
4758
$cabParams = @{
4859
CabFilesFolder = [IO.Path]::Combine($moduleOutDir, $locale)
49-
LandingPagePath = [IO.Path]::Combine($DocsPath, $locale, "$Module.md")
60+
LandingPagePath = [IO.Path]::Combine(
61+
$DocsPath,
62+
$locale,
63+
"$Module.md"
64+
)
5065
OutputFolder = $OutputPath
5166
Verbose = $VerbosePreference
5267
}

PowerShellBuild/Public/Clear-PSBuildOutputFolder.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ function Clear-PSBuildOutputFolder {
1616
# Maybe a bit paranoid but this task nuked \ on my laptop. Good thing I was not running as admin.
1717
[parameter(Mandatory)]
1818
[ValidateScript({
19-
if ($_.Length -le 3) {
20-
throw "`$Path [$_] must be longer than 3 characters."
21-
}
22-
$true
23-
})]
19+
if ($_.Length -le 3) {
20+
throw ($LocalizedData.PathLongerThan3Chars -f $_)
21+
}
22+
$true
23+
})]
2424
[string]$Path
2525
)
2626

0 commit comments

Comments
 (0)