1
+ # spell-checker:ignore modulename
1
2
function Build-PSBuildModule {
2
3
<#
3
4
. SYNOPSIS
4
5
Builds a PowerShell module based on source directory
5
6
. 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.
8
10
. PARAMETER Path
9
11
The source module path.
10
12
. PARAMETER DestinationPath
11
13
Destination path to write "built" module to.
12
14
. PARAMETER ModuleName
13
15
The name of the module.
14
16
. 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.
16
19
. PARAMETER CompileHeader
17
20
String that will be at the top of your PSM1 file.
18
21
. PARAMETER CompileFooter
19
22
String that will be added to the bottom of your PSM1 file.
20
23
. PARAMETER CompileScriptHeader
21
24
String that will be added to your PSM1 file before each script file.
22
25
. 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.
24
27
. 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.
26
30
. 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.
28
33
. PARAMETER CopyDirectories
29
34
List of directories that will copying "as-is" into the build module.
30
35
. 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.
32
38
. 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.
34
41
. EXAMPLE
35
- PS> $buildParams = @{
42
+ $buildParams = @{
36
43
Path = ./MyModule
37
44
DestinationPath = ./Output/MoModule/0.1.0
38
45
ModuleName = MyModule
39
46
Exclude = @()
40
47
Compile = $false
41
48
Culture = (Get-UICulture).Name
42
49
}
43
- PS> Build-PSBuildModule @buildParams
50
+ Build-PSBuildModule @buildParams
44
51
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'
46
54
#>
47
- [cmdletbinding ()]
55
+ [CmdletBinding ()]
48
56
param (
49
57
[parameter (Mandatory )]
50
58
[string ]$Path ,
@@ -77,49 +85,80 @@ function Build-PSBuildModule {
77
85
)
78
86
79
87
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
81
94
}
82
95
83
96
# 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
85
104
foreach ($dir in $CopyDirectories ) {
86
105
$copyPath = [IO.Path ]::Combine($Path , $dir )
87
106
Copy-Item - Path $copyPath - Destination $DestinationPath - Recurse - Force
88
107
}
89
108
90
109
# Copy README as about_<modulename>.help.txt
91
110
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)) {
95
117
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
97
124
}
98
125
}
99
126
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
101
129
if ($Compile.IsPresent ) {
102
130
$rootModule = [IO.Path ]::Combine($DestinationPath , " $ModuleName .psm1" )
103
131
104
132
# Grab the contents of the copied over PSM1
105
133
# This will be appended to the end of the finished PSM1
106
134
$psm1Contents = Get-Content - Path $rootModule - Raw
107
- ' ' | Out-File - FilePath $rootModule - Encoding utf8
135
+ ' ' | Out-File - FilePath $rootModule - Encoding ' utf8'
108
136
109
137
if ($CompileHeader ) {
110
- $CompileHeader | Add-Content - Path $rootModule - Encoding utf8
138
+ $CompileHeader | Add-Content - Path $rootModule - Encoding ' utf8'
111
139
}
112
140
113
141
$resolvedCompileDirectories = $CompileDirectories | ForEach-Object {
114
142
[IO.Path ]::Combine($Path , $_ )
115
143
}
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
117
152
118
153
$allScripts = $allScripts | Remove-ExcludedItem - Exclude $Exclude
119
154
155
+ $addContentSplat = @ {
156
+ Path = $rootModule
157
+ Encoding = ' utf8'
158
+ }
120
159
$allScripts | ForEach-Object {
121
160
$srcFile = Resolve-Path $_.FullName - Relative
122
- Write-Verbose " Adding [ $srcFile ] to PSM1 "
161
+ Write-Verbose ( $LocalizedData .AddingFileToPsm1 -f $srcFile )
123
162
124
163
if ($CompileScriptHeader ) {
125
164
Write-Output $CompileScriptHeader
@@ -130,15 +169,14 @@ function Build-PSBuildModule {
130
169
if ($CompileScriptFooter ) {
131
170
Write-Output $CompileScriptFooter
132
171
}
172
+ } | Add-Content @addContentSplat
133
173
134
- } | Add-Content - Path $rootModule - Encoding utf8
135
-
136
- $psm1Contents | Add-Content - Path $rootModule - Encoding utf8
174
+ $psm1Contents | Add-Content @addContentSplat
137
175
138
176
if ($CompileFooter ) {
139
- $CompileFooter | Add-Content - Path $rootModule - Encoding utf8
177
+ $CompileFooter | Add-Content @addContentSplat
140
178
}
141
- } else {
179
+ } else {
142
180
# Copy everything over, then remove stuff that should have been excluded
143
181
# It's just easier this way
144
182
$copyParams = @ {
@@ -157,13 +195,26 @@ function Build-PSBuildModule {
157
195
}
158
196
}
159
197
}
160
- $toRemove | Remove-Item - Recurse - Force - ErrorAction Ignore
198
+ $toRemove | Remove-Item - Recurse - Force - ErrorAction ' Ignore'
161
199
}
162
200
163
201
# 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
165
208
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
168
219
}
169
220
}
0 commit comments