1+ # spell-checker:ignore modulename
12function 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}
0 commit comments