@@ -12,34 +12,40 @@ function Build-PSModuleRootModule {
12
12
13
13
1. Module header from header.ps1 file. Usually to suppress code analysis warnings/errors and to add [CmdletBinding()] to the module.
14
14
2. Data files are added from source files. These are also tracked based on visibility/exportability based on folder location:
15
- 1. private
16
- 2. public
15
+ 1. private
16
+ 2. public
17
17
3. Combines *.ps1 files from the following folders in alphabetical order from each folder:
18
- 1. init
19
- 2. classes
20
- 3. private
21
- 4. public
22
- 5. Any remaining *.ps1 on module root.
18
+ 1. init
19
+ 2. classes
20
+ 3. private
21
+ 4. public
22
+ 5. Any remaining *.ps1 on module root.
23
23
3. Export-ModuleMember by using the functions, cmdlets, variables and aliases found in the source files.
24
- - `Functions` will only contain functions that are from the `public` folder.
25
- - `Cmdlets` will only contain cmdlets that are from the `public` folder.
26
- - `Variables` will only contain variables that are from the `public` folder.
27
- - `Aliases` will only contain aliases that are from the functions from the `public` folder.
24
+ - `Functions` will only contain functions that are from the `public` folder.
25
+ - `Cmdlets` will only contain cmdlets that are from the `public` folder.
26
+ - `Variables` will only contain variables that are from the `public` folder.
27
+ - `Aliases` will only contain aliases that are from the functions from the `public` folder.
28
28
29
29
. EXAMPLE
30
30
Build-PSModuleRootModule -SourceFolderPath 'C:\MyModule\src\MyModule' -OutputFolderPath 'C:\MyModule\build\MyModule'
31
31
#>
32
32
[CmdletBinding ()]
33
33
param (
34
+ # Name of the module.
35
+ [Parameter (Mandatory )]
36
+ [string ] $ModuleName ,
37
+
34
38
# Folder where the built modules are outputted. 'outputs/modules/MyModule'
35
39
[Parameter (Mandatory )]
36
40
[System.IO.DirectoryInfo ] $ModuleOutputFolder
37
41
)
38
42
43
+ # Get the path separator for the current OS
44
+ $pathSeparator = [System.IO.Path ]::DirectorySeparatorChar
45
+
39
46
# region Build root module
40
47
Start-LogGroup ' Build root module'
41
- $moduleName = Split-Path - Path $ModuleOutputFolder - Leaf
42
- $rootModuleFile = New-Item - Path $ModuleOutputFolder - Name " $moduleName .psm1" - Force
48
+ $rootModuleFile = New-Item - Path $ModuleOutputFolder - Name " $ModuleName .psm1" - Force
43
49
44
50
# region - Analyze source files
45
51
@@ -158,16 +164,20 @@ Write-Verbose "[$scriptName] - [data] - Done"
158
164
# region - Add content from *.ps1 files on module root
159
165
$files = $ModuleOutputFolder | Get-ChildItem - File - Force - Filter ' *.ps1'
160
166
foreach ($file in $files ) {
161
- $relativePath = $file.FullName.Replace ($ModuleOutputFolder , ' ' ).TrimStart($pathSeparator )
167
+ $relativePath = $file.FullName -Replace $ModuleOutputFolder , ' '
168
+ $relativePath = $relativePath.TrimStart ($pathSeparator )
169
+ $relativePath = $relativePath -Split $pathSeparator | ForEach-Object { " [$_ ]" }
170
+ $relativePath = $relativePath -Join ' - '
171
+
162
172
Add-Content - Path $rootModuleFile - Force - Value @"
163
173
#region - From $relativePath
164
- Write-Verbose "[`$ scriptName] - [ $relativePath ] - Importing"
174
+ Write-Verbose "[`$ scriptName] - $relativePath - Importing"
165
175
166
176
"@
167
177
Get-Content - Path $file.FullName | Add-Content - Path $rootModuleFile - Force
168
178
169
179
Add-Content - Path $rootModuleFile - Force - Value @"
170
- Write-Verbose "[`$ scriptName] - [ $relativePath ] - Done"
180
+ Write-Verbose "[`$ scriptName] - $relativePath - Done"
171
181
#endregion - From $relativePath
172
182
173
183
"@
@@ -213,7 +223,14 @@ Export-ModuleMember @exports
213
223
Stop-LogGroup
214
224
# endregion Format root module
215
225
216
- Start-LogGroup ' Build module - Result - File list'
226
+ # region Validate root module
227
+ Start-LogGroup ' Build root module - Validate - Import'
228
+ Add-PSModulePath - Path (Split-Path - Path $ModuleOutputFolder - Parent)
229
+ Import-PSModule - Path $ModuleOutputFolder - ModuleName $ModuleName
230
+ Stop-LogGroup
231
+
232
+ Start-LogGroup ' Build root module - Validate - File list'
217
233
(Get-ChildItem - Path $ModuleOutputFolder - Recurse - Force).FullName | Sort-Object
218
234
Stop-LogGroup
235
+ # endregion Validate root module
219
236
}
0 commit comments