Skip to content

Commit

Permalink
🪲 [Fix]: Fix an issue where the ModuleManifest was not removed if it …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
MariusStorhaug authored Mar 30, 2024
1 parent 31b5bd5 commit 3b374b0
Show file tree
Hide file tree
Showing 69 changed files with 1,081 additions and 52 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/Action-Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,39 @@ jobs:
Path: tests/src
ModulesOutputPath: tests/outputs/modules
DocsOutputPath: tests/outputs/docs

ActionTestUnnamedFolder:
name: Action-Test - [UnnamedFolder]
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Initialize environment
uses: PSModule/Initialize-PSModule@main

- name: Action-Test
uses: ./
with:
Name: PSModuleTest
Path: tests/srcNo
ModulesOutputPath: tests/outputs/modules
DocsOutputPath: tests/outputs/docs

ActionTestUnnamedFolderWithManifest:
name: Action-Test - [UnnamedFolderWithManifest]
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Initialize environment
uses: PSModule/Initialize-PSModule@main

- name: Action-Test
uses: ./
with:
Name: PSModuleTest
Path: tests/srcNoWithManifest
ModulesOutputPath: tests/outputs/modules
DocsOutputPath: tests/outputs/docs
8 changes: 4 additions & 4 deletions scripts/helpers/Build-PSModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ function Build-PSModule {
Write-Verbose "Docs output folder: [$DocsOutputFolderPath]"
Stop-LogGroup

Build-PSModuleBase -ModuleSourceFolder $moduleSourceFolder -ModuleOutputFolder $moduleOutputFolder
Build-PSModuleManifest -ModuleOutputFolder $moduleOutputFolder
Build-PSModuleRootModule -ModuleOutputFolder $moduleOutputFolder
Build-PSModuleDocumentation -ModuleOutputFolder $moduleOutputFolder -DocsOutputFolder $docsOutputFolder
Build-PSModuleBase -ModuleName $ModuleName -ModuleSourceFolder $moduleSourceFolder -ModuleOutputFolder $moduleOutputFolder
Build-PSModuleManifest -ModuleName $ModuleName -ModuleOutputFolder $moduleOutputFolder
Build-PSModuleRootModule -ModuleName $ModuleName -ModuleOutputFolder $moduleOutputFolder
Build-PSModuleDocumentation -ModuleName $ModuleName -DocsOutputFolder $docsOutputFolder
}
22 changes: 16 additions & 6 deletions scripts/helpers/Build/Add-ContentFromItem.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@
[Parameter(Mandatory)]
[string] $RootPath
)
$relativeFolderPath = $Path.Replace($RootPath, '').TrimStart($pathSeparator)
# Get the path separator for the current OS
$pathSeparator = [System.IO.Path]::DirectorySeparatorChar

$relativeFolderPath = $Path -Replace $RootPath, ''
$relativeFolderPath = $relativeFolderPath.TrimStart($pathSeparator)
$relativeFolderPath = $relativeFolderPath -Split $pathSeparator | ForEach-Object { "[$_]" }
$relativeFolderPath = $relativeFolderPath -Join ' - '

Add-Content -Path $RootModuleFilePath -Force -Value @"
#region - From $relativeFolderPath
Write-Verbose "[`$scriptName] - [$relativeFolderPath] - Processing folder"
Write-Verbose "[`$scriptName] - $relativeFolderPath - Processing folder"
"@

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

$files = $Path | Get-ChildItem -File -Force -Filter '*.ps1' | Sort-Object -Property FullName
foreach ($file in $files) {
$relativeFilePath = $file.FullName.Replace($RootPath, '').TrimStart($pathSeparator)
$relativeFilePath = $file.FullName -Replace $RootPath, ''
$relativeFilePath = $relativeFilePath.TrimStart($pathSeparator)
$relativeFilePath = $relativeFilePath -Split $pathSeparator | ForEach-Object { "[$_]" }
$relativeFilePath = $relativeFilePath -Join ' - '

Add-Content -Path $RootModuleFilePath -Force -Value @"
#region - From $relativeFilePath
Write-Verbose "[`$scriptName] - [$relativeFilePath] - Importing"
Write-Verbose "[`$scriptName] - $relativeFilePath - Importing"
"@
Get-Content -Path $file.FullName | Add-Content -Path $RootModuleFilePath -Force
Add-Content -Path $RootModuleFilePath -Value @"
Write-Verbose "[`$scriptName] - [$relativeFilePath] - Done"
Write-Verbose "[`$scriptName] - $relativeFilePath - Done"
#endregion - From $relativeFilePath
"@
}
Add-Content -Path $RootModuleFilePath -Force -Value @"
Write-Verbose "[`$scriptName] - [$relativeFolderPath] - Done"
Write-Verbose "[`$scriptName] - $relativeFolderPath - Done"
#endregion - From $relativeFolderPath
"@
Expand Down
8 changes: 6 additions & 2 deletions scripts/helpers/Build/Build-PSModuleBase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function Build-PSModuleBase {
#>
[CmdletBinding()]
param(
# Name of the module.
[Parameter(Mandatory)]
[string] $ModuleName,

# Path to the folder where the module source code is located.
[Parameter(Mandatory)]
[System.IO.DirectoryInfo] $ModuleSourceFolder,
Expand All @@ -24,9 +28,9 @@ function Build-PSModuleBase {
)

Start-LogGroup 'Build base'

Write-Verbose "Copying files from [$ModuleSourceFolder] to [$ModuleOutputFolder]"
Copy-Item -Path "$ModuleSourceFolder\*" -Destination $ModuleOutputFolder -Recurse -Force -Verbose
Copy-Item -Path "$ModuleSourceFolder\*" -Destination $ModuleOutputFolder -Recurse -Force -Verbose -Exclude "$ModuleName.psm1"
New-Item -Path $ModuleOutputFolder -Name "$ModuleName.psm1" -ItemType File -Force -Verbose
Stop-LogGroup

Start-LogGroup 'Build base - Result'
Expand Down
12 changes: 3 additions & 9 deletions scripts/helpers/Build/Build-PSModuleDocumentation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,17 @@ function Build-PSModuleDocumentation {
#>
[CmdletBinding()]
param(
# Folder where the module source code is located. 'outputs/modules/MyModule'
# Name of the module.
[Parameter(Mandatory)]
[System.IO.DirectoryInfo] $ModuleOutputFolder,
[string] $ModuleName,

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

Start-LogGroup 'Build docs - Dependencies'
$moduleName = Split-Path -Path $ModuleOutputFolder -Leaf

Add-PSModulePath -Path (Split-Path -Path $ModuleOutputFolder -Parent)
Import-PSModule -Path $ModuleOutputFolder -ModuleName $moduleName

Start-LogGroup 'Build docs - Generate markdown help'
$null = New-MarkdownHelp -Module $moduleName -OutputFolder $DocsOutputFolder -Force -Verbose
$null = New-MarkdownHelp -Module $ModuleName -OutputFolder $DocsOutputFolder -Force -Verbose
Get-ChildItem -Path $DocsOutputFolder -Recurse -Force -Include '*.md' | ForEach-Object {
$content = Get-Content -Path $_.FullName
$fixedOpening = $false
Expand Down
33 changes: 25 additions & 8 deletions scripts/helpers/Build/Build-PSModuleManifest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,40 @@ function Build-PSModuleManifest {
Justification = 'No real reason. Just to get going.'
)]
param(
# Name of the module.
[Parameter(Mandatory)]
[string] $ModuleName,

# Folder where the built modules are outputted. 'outputs/modules/MyModule'
[Parameter(Mandatory)]
[System.IO.DirectoryInfo] $ModuleOutputFolder
)

#region Build manifest file
Start-LogGroup 'Build manifest file'
$moduleName = Split-Path -Path $ModuleOutputFolder -Leaf
$sourceManifestFilePath = Join-Path -Path $ModuleOutputFolder -ChildPath "$moduleName.psd1"
$sourceManifestFilePath = Join-Path -Path $ModuleOutputFolder -ChildPath "$ModuleName.psd1"
Write-Verbose "[SourceManifestFilePath] - [$sourceManifestFilePath]"
if (-not (Test-Path -Path $sourceManifestFilePath)) {
Write-Verbose "[SourceManifestFilePath] - [$sourceManifestFilePath] - Not found"
$sourceManifestFilePath = Join-Path -Path $ModuleOutputFolder -ChildPath 'manifest.psd1'
}
$manifest = if (-not (Test-Path -Path $sourceManifestFilePath)) {
@{}
if (-not (Test-Path -Path $sourceManifestFilePath)) {
Write-Verbose "[SourceManifestFilePath] - [$sourceManifestFilePath] - Not found"
$manifest = @{}
Write-Verbose '[Manifest] - Loading empty manifest'
} else {
Get-ModuleManifest -Path $sourceManifestFilePath -Verbose:$false
Write-Verbose "[SourceManifestFilePath] - [$sourceManifestFilePath] - Found"
$manifest = Get-ModuleManifest -Path $sourceManifestFilePath -Verbose:$false
Write-Verbose '[Manifest] - Loading from file'
Remove-Item -Path $sourceManifestFilePath -Force -Verbose:$false
}

$manifest.RootModule = "$moduleName.psm1"
$rootModule = "$ModuleName.psm1"
$manifest.RootModule = $rootModule
Write-Verbose "[RootModule] - [$($manifest.RootModule)]"

$manifest.ModuleVersion = '999.0.0'
Write-Verbose "[ModuleVersion] - [$($manifest.ModuleVersion)]"

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

Write-Verbose 'Creating new manifest file in outputs folder'
$outputManifestPath = Join-Path -Path $ModuleOutputFolder -ChildPath "$moduleName.psd1"
$outputManifestPath = Join-Path -Path $ModuleOutputFolder -ChildPath "$ModuleName.psd1"
Write-Verbose "OutputManifestPath - [$outputManifestPath]"
New-ModuleManifest -Path $outputManifestPath @manifest
Stop-LogGroup
Expand All @@ -352,6 +366,9 @@ function Build-PSModuleManifest {
Start-LogGroup 'Build manifest file - Result - After format'
Show-FileContent -Path $outputManifestPath
Stop-LogGroup

#endregion Format manifest file

Start-LogGroup 'Build manifest file - Validate'
Test-ModuleManifest -Path $outputManifestPath
Stop-LogGroup
}
51 changes: 34 additions & 17 deletions scripts/helpers/Build/Build-PSModuleRootModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,40 @@ function Build-PSModuleRootModule {
1. Module header from header.ps1 file. Usually to suppress code analysis warnings/errors and to add [CmdletBinding()] to the module.
2. Data files are added from source files. These are also tracked based on visibility/exportability based on folder location:
1. private
2. public
1. private
2. public
3. Combines *.ps1 files from the following folders in alphabetical order from each folder:
1. init
2. classes
3. private
4. public
5. Any remaining *.ps1 on module root.
1. init
2. classes
3. private
4. public
5. Any remaining *.ps1 on module root.
3. Export-ModuleMember by using the functions, cmdlets, variables and aliases found in the source files.
- `Functions` will only contain functions that are from the `public` folder.
- `Cmdlets` will only contain cmdlets that are from the `public` folder.
- `Variables` will only contain variables that are from the `public` folder.
- `Aliases` will only contain aliases that are from the functions from the `public` folder.
- `Functions` will only contain functions that are from the `public` folder.
- `Cmdlets` will only contain cmdlets that are from the `public` folder.
- `Variables` will only contain variables that are from the `public` folder.
- `Aliases` will only contain aliases that are from the functions from the `public` folder.
.EXAMPLE
Build-PSModuleRootModule -SourceFolderPath 'C:\MyModule\src\MyModule' -OutputFolderPath 'C:\MyModule\build\MyModule'
#>
[CmdletBinding()]
param(
# Name of the module.
[Parameter(Mandatory)]
[string] $ModuleName,

# Folder where the built modules are outputted. 'outputs/modules/MyModule'
[Parameter(Mandatory)]
[System.IO.DirectoryInfo] $ModuleOutputFolder
)

# Get the path separator for the current OS
$pathSeparator = [System.IO.Path]::DirectorySeparatorChar

#region Build root module
Start-LogGroup 'Build root module'
$moduleName = Split-Path -Path $ModuleOutputFolder -Leaf
$rootModuleFile = New-Item -Path $ModuleOutputFolder -Name "$moduleName.psm1" -Force
$rootModuleFile = New-Item -Path $ModuleOutputFolder -Name "$ModuleName.psm1" -Force

#region - Analyze source files

Expand Down Expand Up @@ -158,16 +164,20 @@ Write-Verbose "[$scriptName] - [data] - Done"
#region - Add content from *.ps1 files on module root
$files = $ModuleOutputFolder | Get-ChildItem -File -Force -Filter '*.ps1'
foreach ($file in $files) {
$relativePath = $file.FullName.Replace($ModuleOutputFolder, '').TrimStart($pathSeparator)
$relativePath = $file.FullName -Replace $ModuleOutputFolder, ''
$relativePath = $relativePath.TrimStart($pathSeparator)
$relativePath = $relativePath -Split $pathSeparator | ForEach-Object { "[$_]" }
$relativePath = $relativePath -Join ' - '

Add-Content -Path $rootModuleFile -Force -Value @"
#region - From $relativePath
Write-Verbose "[`$scriptName] - [$relativePath] - Importing"
Write-Verbose "[`$scriptName] - $relativePath - Importing"
"@
Get-Content -Path $file.FullName | Add-Content -Path $rootModuleFile -Force

Add-Content -Path $rootModuleFile -Force -Value @"
Write-Verbose "[`$scriptName] - [$relativePath] - Done"
Write-Verbose "[`$scriptName] - $relativePath - Done"
#endregion - From $relativePath
"@
Expand Down Expand Up @@ -213,7 +223,14 @@ Export-ModuleMember @exports
Stop-LogGroup
#endregion Format root module

Start-LogGroup 'Build module - Result - File list'
#region Validate root module
Start-LogGroup 'Build root module - Validate - Import'
Add-PSModulePath -Path (Split-Path -Path $ModuleOutputFolder -Parent)
Import-PSModule -Path $ModuleOutputFolder -ModuleName $ModuleName
Stop-LogGroup

Start-LogGroup 'Build root module - Validate - File list'
(Get-ChildItem -Path $ModuleOutputFolder -Recurse -Force).FullName | Sort-Object
Stop-LogGroup
#endregion Validate root module
}
3 changes: 0 additions & 3 deletions scripts/helpers/Build/Import-PSModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
[string] $ModuleName
)

Start-LogGroup "Importing module [$ModuleName]"

$moduleName = Split-Path -Path $Path -Leaf
$manifestFileName = "$moduleName.psd1"
$manifestFilePath = Join-Path -Path $Path $manifestFileName
Expand All @@ -44,5 +42,4 @@
if ($ModuleName -notin $availableModules.Name) {
throw 'Module not found'
}
Stop-LogGroup
}
4 changes: 4 additions & 0 deletions tests/src/PSModuleTest/PSModuleTest.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@{
ModuleVersion = '0.0.0'
RootModule = 'PSModuleTest.psm1'
}
Loading

0 comments on commit 3b374b0

Please sign in to comment.