Skip to content

Commit 3b374b0

Browse files
🪲 [Fix]: Fix an issue where the ModuleManifest was not removed if it exists. (#45)
## Description - Fix an issue where the ModuleManifest was not removed if it exists. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [x] 🪲 [Fix] - [ ] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent 31b5bd5 commit 3b374b0

File tree

69 files changed

+1081
-52
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1081
-52
lines changed

.github/workflows/Action-Test.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,39 @@ jobs:
3131
Path: tests/src
3232
ModulesOutputPath: tests/outputs/modules
3333
DocsOutputPath: tests/outputs/docs
34+
35+
ActionTestUnnamedFolder:
36+
name: Action-Test - [UnnamedFolder]
37+
runs-on: ubuntu-latest
38+
steps:
39+
- name: Checkout repo
40+
uses: actions/checkout@v4
41+
42+
- name: Initialize environment
43+
uses: PSModule/Initialize-PSModule@main
44+
45+
- name: Action-Test
46+
uses: ./
47+
with:
48+
Name: PSModuleTest
49+
Path: tests/srcNo
50+
ModulesOutputPath: tests/outputs/modules
51+
DocsOutputPath: tests/outputs/docs
52+
53+
ActionTestUnnamedFolderWithManifest:
54+
name: Action-Test - [UnnamedFolderWithManifest]
55+
runs-on: ubuntu-latest
56+
steps:
57+
- name: Checkout repo
58+
uses: actions/checkout@v4
59+
60+
- name: Initialize environment
61+
uses: PSModule/Initialize-PSModule@main
62+
63+
- name: Action-Test
64+
uses: ./
65+
with:
66+
Name: PSModuleTest
67+
Path: tests/srcNoWithManifest
68+
ModulesOutputPath: tests/outputs/modules
69+
DocsOutputPath: tests/outputs/docs

scripts/helpers/Build-PSModule.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ function Build-PSModule {
4242
Write-Verbose "Docs output folder: [$DocsOutputFolderPath]"
4343
Stop-LogGroup
4444

45-
Build-PSModuleBase -ModuleSourceFolder $moduleSourceFolder -ModuleOutputFolder $moduleOutputFolder
46-
Build-PSModuleManifest -ModuleOutputFolder $moduleOutputFolder
47-
Build-PSModuleRootModule -ModuleOutputFolder $moduleOutputFolder
48-
Build-PSModuleDocumentation -ModuleOutputFolder $moduleOutputFolder -DocsOutputFolder $docsOutputFolder
45+
Build-PSModuleBase -ModuleName $ModuleName -ModuleSourceFolder $moduleSourceFolder -ModuleOutputFolder $moduleOutputFolder
46+
Build-PSModuleManifest -ModuleName $ModuleName -ModuleOutputFolder $moduleOutputFolder
47+
Build-PSModuleRootModule -ModuleName $ModuleName -ModuleOutputFolder $moduleOutputFolder
48+
Build-PSModuleDocumentation -ModuleName $ModuleName -DocsOutputFolder $docsOutputFolder
4949
}

scripts/helpers/Build/Add-ContentFromItem.ps1

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@
2222
[Parameter(Mandatory)]
2323
[string] $RootPath
2424
)
25-
$relativeFolderPath = $Path.Replace($RootPath, '').TrimStart($pathSeparator)
25+
# Get the path separator for the current OS
26+
$pathSeparator = [System.IO.Path]::DirectorySeparatorChar
27+
28+
$relativeFolderPath = $Path -Replace $RootPath, ''
29+
$relativeFolderPath = $relativeFolderPath.TrimStart($pathSeparator)
30+
$relativeFolderPath = $relativeFolderPath -Split $pathSeparator | ForEach-Object { "[$_]" }
31+
$relativeFolderPath = $relativeFolderPath -Join ' - '
2632

2733
Add-Content -Path $RootModuleFilePath -Force -Value @"
2834
#region - From $relativeFolderPath
29-
Write-Verbose "[`$scriptName] - [$relativeFolderPath] - Processing folder"
35+
Write-Verbose "[`$scriptName] - $relativeFolderPath - Processing folder"
3036
3137
"@
3238

@@ -37,22 +43,26 @@ Write-Verbose "[`$scriptName] - [$relativeFolderPath] - Processing folder"
3743

3844
$files = $Path | Get-ChildItem -File -Force -Filter '*.ps1' | Sort-Object -Property FullName
3945
foreach ($file in $files) {
40-
$relativeFilePath = $file.FullName.Replace($RootPath, '').TrimStart($pathSeparator)
46+
$relativeFilePath = $file.FullName -Replace $RootPath, ''
47+
$relativeFilePath = $relativeFilePath.TrimStart($pathSeparator)
48+
$relativeFilePath = $relativeFilePath -Split $pathSeparator | ForEach-Object { "[$_]" }
49+
$relativeFilePath = $relativeFilePath -Join ' - '
50+
4151
Add-Content -Path $RootModuleFilePath -Force -Value @"
4252
#region - From $relativeFilePath
43-
Write-Verbose "[`$scriptName] - [$relativeFilePath] - Importing"
53+
Write-Verbose "[`$scriptName] - $relativeFilePath - Importing"
4454
4555
"@
4656
Get-Content -Path $file.FullName | Add-Content -Path $RootModuleFilePath -Force
4757
Add-Content -Path $RootModuleFilePath -Value @"
4858
49-
Write-Verbose "[`$scriptName] - [$relativeFilePath] - Done"
59+
Write-Verbose "[`$scriptName] - $relativeFilePath - Done"
5060
#endregion - From $relativeFilePath
5161
"@
5262
}
5363
Add-Content -Path $RootModuleFilePath -Force -Value @"
5464
55-
Write-Verbose "[`$scriptName] - [$relativeFolderPath] - Done"
65+
Write-Verbose "[`$scriptName] - $relativeFolderPath - Done"
5666
#endregion - From $relativeFolderPath
5767
5868
"@

scripts/helpers/Build/Build-PSModuleBase.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ function Build-PSModuleBase {
1414
#>
1515
[CmdletBinding()]
1616
param(
17+
# Name of the module.
18+
[Parameter(Mandatory)]
19+
[string] $ModuleName,
20+
1721
# Path to the folder where the module source code is located.
1822
[Parameter(Mandatory)]
1923
[System.IO.DirectoryInfo] $ModuleSourceFolder,
@@ -24,9 +28,9 @@ function Build-PSModuleBase {
2428
)
2529

2630
Start-LogGroup 'Build base'
27-
2831
Write-Verbose "Copying files from [$ModuleSourceFolder] to [$ModuleOutputFolder]"
29-
Copy-Item -Path "$ModuleSourceFolder\*" -Destination $ModuleOutputFolder -Recurse -Force -Verbose
32+
Copy-Item -Path "$ModuleSourceFolder\*" -Destination $ModuleOutputFolder -Recurse -Force -Verbose -Exclude "$ModuleName.psm1"
33+
New-Item -Path $ModuleOutputFolder -Name "$ModuleName.psm1" -ItemType File -Force -Verbose
3034
Stop-LogGroup
3135

3236
Start-LogGroup 'Build base - Result'

scripts/helpers/Build/Build-PSModuleDocumentation.ps1

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,17 @@ function Build-PSModuleDocumentation {
1414
#>
1515
[CmdletBinding()]
1616
param(
17-
# Folder where the module source code is located. 'outputs/modules/MyModule'
17+
# Name of the module.
1818
[Parameter(Mandatory)]
19-
[System.IO.DirectoryInfo] $ModuleOutputFolder,
19+
[string] $ModuleName,
2020

2121
# Folder where the documentation for the modules should be outputted. 'outputs/docs/MyModule'
2222
[Parameter(Mandatory)]
2323
[System.IO.DirectoryInfo] $DocsOutputFolder
2424
)
2525

26-
Start-LogGroup 'Build docs - Dependencies'
27-
$moduleName = Split-Path -Path $ModuleOutputFolder -Leaf
28-
29-
Add-PSModulePath -Path (Split-Path -Path $ModuleOutputFolder -Parent)
30-
Import-PSModule -Path $ModuleOutputFolder -ModuleName $moduleName
31-
3226
Start-LogGroup 'Build docs - Generate markdown help'
33-
$null = New-MarkdownHelp -Module $moduleName -OutputFolder $DocsOutputFolder -Force -Verbose
27+
$null = New-MarkdownHelp -Module $ModuleName -OutputFolder $DocsOutputFolder -Force -Verbose
3428
Get-ChildItem -Path $DocsOutputFolder -Recurse -Force -Include '*.md' | ForEach-Object {
3529
$content = Get-Content -Path $_.FullName
3630
$fixedOpening = $false

scripts/helpers/Build/Build-PSModuleManifest.ps1

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,40 @@ function Build-PSModuleManifest {
1818
Justification = 'No real reason. Just to get going.'
1919
)]
2020
param(
21+
# Name of the module.
22+
[Parameter(Mandatory)]
23+
[string] $ModuleName,
24+
2125
# Folder where the built modules are outputted. 'outputs/modules/MyModule'
2226
[Parameter(Mandatory)]
2327
[System.IO.DirectoryInfo] $ModuleOutputFolder
2428
)
2529

2630
#region Build manifest file
2731
Start-LogGroup 'Build manifest file'
28-
$moduleName = Split-Path -Path $ModuleOutputFolder -Leaf
29-
$sourceManifestFilePath = Join-Path -Path $ModuleOutputFolder -ChildPath "$moduleName.psd1"
32+
$sourceManifestFilePath = Join-Path -Path $ModuleOutputFolder -ChildPath "$ModuleName.psd1"
33+
Write-Verbose "[SourceManifestFilePath] - [$sourceManifestFilePath]"
3034
if (-not (Test-Path -Path $sourceManifestFilePath)) {
35+
Write-Verbose "[SourceManifestFilePath] - [$sourceManifestFilePath] - Not found"
3136
$sourceManifestFilePath = Join-Path -Path $ModuleOutputFolder -ChildPath 'manifest.psd1'
3237
}
33-
$manifest = if (-not (Test-Path -Path $sourceManifestFilePath)) {
34-
@{}
38+
if (-not (Test-Path -Path $sourceManifestFilePath)) {
39+
Write-Verbose "[SourceManifestFilePath] - [$sourceManifestFilePath] - Not found"
40+
$manifest = @{}
41+
Write-Verbose '[Manifest] - Loading empty manifest'
3542
} else {
36-
Get-ModuleManifest -Path $sourceManifestFilePath -Verbose:$false
43+
Write-Verbose "[SourceManifestFilePath] - [$sourceManifestFilePath] - Found"
44+
$manifest = Get-ModuleManifest -Path $sourceManifestFilePath -Verbose:$false
45+
Write-Verbose '[Manifest] - Loading from file'
46+
Remove-Item -Path $sourceManifestFilePath -Force -Verbose:$false
3747
}
3848

39-
$manifest.RootModule = "$moduleName.psm1"
49+
$rootModule = "$ModuleName.psm1"
50+
$manifest.RootModule = $rootModule
51+
Write-Verbose "[RootModule] - [$($manifest.RootModule)]"
52+
4053
$manifest.ModuleVersion = '999.0.0'
54+
Write-Verbose "[ModuleVersion] - [$($manifest.ModuleVersion)]"
4155

4256
$manifest.Author = $manifest.Keys -contains 'Author' ? ($manifest.Author | IsNotNullOrEmpty) ? $manifest.Author : $env:GITHUB_REPOSITORY_OWNER : $env:GITHUB_REPOSITORY_OWNER
4357
Write-Verbose "[Author] - [$($manifest.Author)]"
@@ -334,7 +348,7 @@ function Build-PSModuleManifest {
334348
}
335349

336350
Write-Verbose 'Creating new manifest file in outputs folder'
337-
$outputManifestPath = Join-Path -Path $ModuleOutputFolder -ChildPath "$moduleName.psd1"
351+
$outputManifestPath = Join-Path -Path $ModuleOutputFolder -ChildPath "$ModuleName.psd1"
338352
Write-Verbose "OutputManifestPath - [$outputManifestPath]"
339353
New-ModuleManifest -Path $outputManifestPath @manifest
340354
Stop-LogGroup
@@ -352,6 +366,9 @@ function Build-PSModuleManifest {
352366
Start-LogGroup 'Build manifest file - Result - After format'
353367
Show-FileContent -Path $outputManifestPath
354368
Stop-LogGroup
355-
356369
#endregion Format manifest file
370+
371+
Start-LogGroup 'Build manifest file - Validate'
372+
Test-ModuleManifest -Path $outputManifestPath
373+
Stop-LogGroup
357374
}

scripts/helpers/Build/Build-PSModuleRootModule.ps1

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,40 @@ function Build-PSModuleRootModule {
1212
1313
1. Module header from header.ps1 file. Usually to suppress code analysis warnings/errors and to add [CmdletBinding()] to the module.
1414
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
1717
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.
2323
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.
2828
2929
.EXAMPLE
3030
Build-PSModuleRootModule -SourceFolderPath 'C:\MyModule\src\MyModule' -OutputFolderPath 'C:\MyModule\build\MyModule'
3131
#>
3232
[CmdletBinding()]
3333
param(
34+
# Name of the module.
35+
[Parameter(Mandatory)]
36+
[string] $ModuleName,
37+
3438
# Folder where the built modules are outputted. 'outputs/modules/MyModule'
3539
[Parameter(Mandatory)]
3640
[System.IO.DirectoryInfo] $ModuleOutputFolder
3741
)
3842

43+
# Get the path separator for the current OS
44+
$pathSeparator = [System.IO.Path]::DirectorySeparatorChar
45+
3946
#region Build root module
4047
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
4349

4450
#region - Analyze source files
4551

@@ -158,16 +164,20 @@ Write-Verbose "[$scriptName] - [data] - Done"
158164
#region - Add content from *.ps1 files on module root
159165
$files = $ModuleOutputFolder | Get-ChildItem -File -Force -Filter '*.ps1'
160166
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+
162172
Add-Content -Path $rootModuleFile -Force -Value @"
163173
#region - From $relativePath
164-
Write-Verbose "[`$scriptName] - [$relativePath] - Importing"
174+
Write-Verbose "[`$scriptName] - $relativePath - Importing"
165175
166176
"@
167177
Get-Content -Path $file.FullName | Add-Content -Path $rootModuleFile -Force
168178

169179
Add-Content -Path $rootModuleFile -Force -Value @"
170-
Write-Verbose "[`$scriptName] - [$relativePath] - Done"
180+
Write-Verbose "[`$scriptName] - $relativePath - Done"
171181
#endregion - From $relativePath
172182
173183
"@
@@ -213,7 +223,14 @@ Export-ModuleMember @exports
213223
Stop-LogGroup
214224
#endregion Format root module
215225

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'
217233
(Get-ChildItem -Path $ModuleOutputFolder -Recurse -Force).FullName | Sort-Object
218234
Stop-LogGroup
235+
#endregion Validate root module
219236
}

scripts/helpers/Build/Import-PSModule.ps1

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
[string] $ModuleName
2323
)
2424

25-
Start-LogGroup "Importing module [$ModuleName]"
26-
2725
$moduleName = Split-Path -Path $Path -Leaf
2826
$manifestFileName = "$moduleName.psd1"
2927
$manifestFilePath = Join-Path -Path $Path $manifestFileName
@@ -44,5 +42,4 @@
4442
if ($ModuleName -notin $availableModules.Name) {
4543
throw 'Module not found'
4644
}
47-
Stop-LogGroup
4845
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@{
2+
ModuleVersion = '0.0.0'
3+
RootModule = 'PSModuleTest.psm1'
4+
}

0 commit comments

Comments
 (0)