Skip to content

Commit 25bc8e3

Browse files
🌟 [Major]: Add functionality to export variables (#57)
## Description - Fixes #58 - Restructure expected module folder structure. - variables established under `variables` with a separate `private` and `public` folders. - To comply to the same structure, adjusted `classes` and `functions` to the same structure. - items under the `public` folders will be exported, the others are kept in the module scope. - Swap to use `GitHub-Script@v1`, instead of pure inline scripts. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [ ] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [x] 🌟 [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 d449f68 commit 25bc8e3

39 files changed

+824
-734
lines changed

.github/workflows/Linter.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ jobs:
2727
uses: super-linter/super-linter@latest
2828
env:
2929
GITHUB_TOKEN: ${{ github.token }}
30+
VALIDATE_JSCPD: false
31+
VALIDATE_MARKDOWN_PRETTIER: false
32+
VALIDATE_YAML_PRETTIER: false

README.md

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ This GitHub Action is a part of the [PSModule framework](https://github.com/PSMo
1818
During the build process the following steps are performed:
1919

2020
1. Copies the source code of the module to an output folder.
21-
1. Builds the module manifest file based of info on the GitHub repository and source code. Read the [Module Manifest](#module-manifest) section for more information.
22-
1. Builds the root module (.psm1) file by combining source code and adding automation into the root module file. Read the [Root module](#root-module) section for more information.
23-
1. Builds the module documentation using platyPS and comment based help in the source code. Read the [Module documentation](#module-documentation) section for more information.
21+
1. Builds the module manifest file based of info on the GitHub repository and source code. For more information, please read the [Module Manifest](#module-manifest) section.
22+
1. Builds the root module (.psm1) file by combining source code and adding automation into the root module file. For more information, please read the [Root module](#root-module) section.
23+
1. Builds the module documentation using platyPS and comment based help in the source code. For more information, please read the [Module documentation](#module-documentation) section.
2424

2525
## Usage
2626

@@ -33,28 +33,34 @@ During the build process the following steps are performed:
3333

3434
## Root module
3535

36-
The `src` folder may contain a 'root module' file. If present, the build function will disregard this file
37-
and build a new root module file based on the source code in the module folder.
36+
The `src` folder may contain a 'root module' file. If present, the build function will disregard this file and build a new root module file based on
37+
the source code in the module folder.
3838

39-
The root module file is the main file that is loaded when the module is imported.
40-
It is built from the source code files in the module folder in the following order:
39+
The root module file is the main file that is loaded when the module is imported. It is built from the source code files in the module folder in the
40+
following order:
4141

42-
1. Adds module headers from `header.ps1` if it exists and removes the file from the module folder.
43-
1. Adds data loader automation that loads files from the `data` folder as variables in the module scope, if it exists. The variables are available using the ´$script:<filename>´ syntax.
44-
1. Adds content from subfolders, if they exists, and removes them from the module folder in the following order:
42+
1. Adds a module header from `header.ps1` if it exists and removes the file from the module folder.
43+
1. Adds a data loader that loads files from the `data` folder as variables in the module scope, if the folder exists. The variables are available
44+
using the `$script:<filename>` syntax.
45+
1. Adds content from subfolders into the root module file and removes them from the module folder in the following order:
4546
- `init`
46-
- `classes`
47-
- `private`
48-
- `public`
47+
- `classes/private`
48+
- `classes/public`
49+
- `functions/private`
50+
- `functions/public`
51+
- `variables/private`
52+
- `variables/public`
4953
- `*.ps1` on module root
50-
1. Adds a `class` and `enum` exporter that exports all classes and enums in the module to the caller session, using TypeAccelerators.
51-
1. Adds the `Export-ModuleMember` function to the end of the file, to make sure that only the functions, cmdlets, variables and aliases that are defined in the module are exported.
54+
1. Adds a `class` and `enum` exporter that exports the ones from `classes/public` folder to the caller session, using [TypeAccelerators](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_classes?view=powershell-7.4#exporting-classes-with-type-accelerators).
55+
1. Adds the `Export-ModuleMember` function to the end of the file, to make sure that only the functions, cmdlets, variables and aliases that are
56+
defined in the `public` folders are exported.
5257

5358
## Module manifest
5459

5560
The module manifest file is the file that describes the module and its content. It is used by PowerShell to load the module and its prerequisites.
56-
The file also contains important metadata that is used by the PowerShell Gallery. If a file exists in the source code folder `src` it will be used as a base for the module manifest file.
57-
Most of the values in the module manifest file are calculated during the build process however some of these will not be touched if specified in the source manifest file.
61+
The file also contains important metadata that is used by the PowerShell Gallery. If a file exists in the source code folder `src` it will be used as
62+
a base for the module manifest file. Most of the values in the module manifest file will be calculated during the build process however some of these
63+
will not be touched if specified in the source manifest file.
5864

5965
During the module manifest build process the following steps are performed:
6066

@@ -72,7 +78,7 @@ During the module manifest build process the following steps are performed:
7278
1. Get the list of types to process by searching for `*.Types.ps1xml` files in the entire module source folder and set the `TypesToProcess` property in the manifest.
7379
1. Get the list of formats to process by searching for `*.Format.ps1xml` files in the entire module source folder and set the `FormatsToProcess` property in the manifest.
7480
1. Get the list of DSC resources to export by searching for `*.psm1` files in the `resources` folder and set the `DscResourcesToExport` property in the manifest.
75-
1. Get the list of functions, cmdlets, aliases, and variables to export and set the respective properties in the manifest.
81+
1. Get the list of functions, cmdlets, aliases, and variables from the respective `<type>\public` folder set the respective properties in the manifest.
7682
1. Get the list of modules by searching for all `*.psm1` files in the entire module source folder, excluding the root module and set the `ModuleList` property in the manifest.
7783
1. Gather information from source files to update `RequiredModules`, `PowerShellVersion`, and `CompatiblePSEditions` properties.
7884
1. The following values are gathered from the GitHub repository:
@@ -114,8 +120,8 @@ Linking the description to the module manifest file might show more how this wor
114120
NestedModules = @() # Get from modules\*.psm1.
115121
FunctionsToExport = @() # Get from public\*.ps1.
116122
CmdletsToExport = @() # Get from manifest file, @() if not provided.
117-
VariablesToExport = @() # To be automated, currently adds '@()' to the manifest file.
118-
AliasesToExport = '*' # To be automated, currently adds '*' to the manifest file.
123+
VariablesToExport = @() # Get from variables\public\*.ps1.
124+
AliasesToExport = '*' # Get from functions\public\*.ps1.
119125
DscResourcesToExport = @() # Get from resources\*.psm1.
120126
ModuleList = @() # Get from listing all .\*.psm1 files - Informational only.
121127
FileList = @() # Get from listing all .\* files - Informational only.

action.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ runs:
2626
using: composite
2727
steps:
2828
- name: Run Build-PSModule
29-
shell: pwsh
29+
uses: PSModule/GitHub-Script@v1
3030
env:
3131
GITHUB_ACTION_INPUT_Name: ${{ inputs.Name }}
3232
GITHUB_ACTION_INPUT_Path: ${{ inputs.Path }}
3333
GITHUB_ACTION_INPUT_ModulesOutputPath: ${{ inputs.ModulesOutputPath }}
3434
GITHUB_ACTION_INPUT_DocsOutputPath: ${{ inputs.DocsOutputPath }}
35-
run: |
36-
# Build-PSModule
37-
. "$env:GITHUB_ACTION_PATH\scripts\main.ps1" -Verbose
35+
with:
36+
Script: |
37+
# Build-PSModule
38+
. "${{ github.action_path }}\scripts\main.ps1" -Verbose

scripts/helpers/Build-PSModule.ps1

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ function Build-PSModule {
99
Builds a module.
1010
#>
1111
[CmdletBinding()]
12+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
13+
'PSReviewUnusedParameter', '', Scope = 'Function',
14+
Justification = 'LogGroup - Scoping affects the variables line of sight.'
15+
)]
1216
param(
1317
# Name of the module.
1418
[Parameter(Mandatory)]
@@ -27,29 +31,30 @@ function Build-PSModule {
2731
[string] $DocsOutputFolderPath
2832
)
2933

30-
Start-LogGroup "Building module [$ModuleName]"
31-
Write-Verbose "Source path: [$ModuleSourceFolderPath]"
32-
if (-not (Test-Path -Path $ModuleSourceFolderPath)) {
33-
Write-Error "Source folder not found at [$ModuleSourceFolderPath]"
34-
exit 1
35-
}
36-
$moduleSourceFolder = Get-Item -Path $ModuleSourceFolderPath
34+
LogGroup "Building module [$ModuleName]" {
35+
Write-Verbose "Source path: [$ModuleSourceFolderPath]"
36+
if (-not (Test-Path -Path $ModuleSourceFolderPath)) {
37+
Write-Error "Source folder not found at [$ModuleSourceFolderPath]"
38+
exit 1
39+
}
40+
$moduleSourceFolder = Get-Item -Path $ModuleSourceFolderPath
41+
Write-Verbose "Module source folder: [$moduleSourceFolder]"
3742

38-
$moduleOutputFolder = New-Item -Path $ModulesOutputFolderPath -Name $ModuleName -ItemType Directory -Force
39-
Write-Verbose "Module output folder: [$ModulesOutputFolderPath]"
43+
$moduleOutputFolder = New-Item -Path $ModulesOutputFolderPath -Name $ModuleName -ItemType Directory -Force
44+
Write-Verbose "Module output folder: [$moduleOutputFolder]"
4045

41-
$docsOutputFolder = New-Item -Path $DocsOutputFolderPath -Name $ModuleName -ItemType Directory -Force
42-
Write-Verbose "Docs output folder: [$DocsOutputFolderPath]"
43-
Stop-LogGroup
46+
$docsOutputFolder = New-Item -Path $DocsOutputFolderPath -Name $ModuleName -ItemType Directory -Force
47+
Write-Verbose "Docs output folder: [$docsOutputFolder]"
48+
}
4449

4550
Build-PSModuleBase -ModuleName $ModuleName -ModuleSourceFolder $moduleSourceFolder -ModuleOutputFolder $moduleOutputFolder
4651
Build-PSModuleManifest -ModuleName $ModuleName -ModuleOutputFolder $moduleOutputFolder
4752
Build-PSModuleRootModule -ModuleName $ModuleName -ModuleOutputFolder $moduleOutputFolder
4853
Update-PSModuleManifestAliasesToExport -ModuleName $ModuleName -ModuleOutputFolder $moduleOutputFolder
4954
Build-PSModuleDocumentation -ModuleName $ModuleName -DocsOutputFolder $docsOutputFolder
5055

51-
$outputManifestPath = Join-Path -Path $ModuleOutputFolder -ChildPath "$ModuleName.psd1"
52-
Start-LogGroup 'Build manifest file - Final Result'
53-
Show-FileContent -Path $outputManifestPath
54-
Stop-LogGroup
56+
LogGroup 'Build manifest file - Final Result' {
57+
$outputManifestPath = Join-Path -Path $ModuleOutputFolder -ChildPath "$ModuleName.psd1"
58+
Show-FileContent -Path $outputManifestPath
59+
}
5560
}

scripts/helpers/Build/Build-PSModuleBase.ps1

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ function Build-PSModuleBase {
1313
Build-PSModuleBase -SourceFolderPath 'C:\MyModule\src\MyModule' -OutputFolderPath 'C:\MyModule\build\MyModule'
1414
#>
1515
[CmdletBinding()]
16+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
17+
'PSReviewUnusedParameter', '', Scope = 'Function',
18+
Justification = 'LogGroup - Scoping affects the variables line of sight.'
19+
)]
1620
param(
1721
# Name of the module.
1822
[Parameter(Mandatory)]
@@ -27,13 +31,13 @@ function Build-PSModuleBase {
2731
[System.IO.DirectoryInfo] $ModuleOutputFolder
2832
)
2933

30-
Start-LogGroup 'Build base'
31-
Write-Verbose "Copying files from [$ModuleSourceFolder] to [$ModuleOutputFolder]"
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
34-
Stop-LogGroup
34+
LogGroup 'Build base' {
35+
Write-Verbose "Copying files from [$ModuleSourceFolder] to [$ModuleOutputFolder]"
36+
Copy-Item -Path "$ModuleSourceFolder\*" -Destination $ModuleOutputFolder -Recurse -Force -Verbose -Exclude "$ModuleName.psm1"
37+
New-Item -Path $ModuleOutputFolder -Name "$ModuleName.psm1" -ItemType File -Force -Verbose
38+
}
3539

36-
Start-LogGroup 'Build base - Result'
37-
(Get-ChildItem -Path $ModuleOutputFolder -Recurse -Force).FullName | Sort-Object
38-
Stop-LogGroup
40+
LogGroup 'Build base - Result' {
41+
(Get-ChildItem -Path $ModuleOutputFolder -Recurse -Force).FullName | Sort-Object
42+
}
3943
}

scripts/helpers/Build/Build-PSModuleDocumentation.ps1

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ function Build-PSModuleDocumentation {
1313
Build-PSModuleDocumentation -ModuleOutputFolder 'C:\MyModule\src\MyModule' -DocsOutputFolder 'C:\MyModule\build\MyModule'
1414
#>
1515
[CmdletBinding()]
16+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
17+
'PSReviewUnusedParameter', '', Scope = 'Function',
18+
Justification = 'LogGroup - Scoping affects the variables line of sight.'
19+
)]
1620
param(
1721
# Name of the module.
1822
[Parameter(Mandatory)]
@@ -23,42 +27,42 @@ function Build-PSModuleDocumentation {
2327
[System.IO.DirectoryInfo] $DocsOutputFolder
2428
)
2529

26-
Start-LogGroup 'Build docs - Generate markdown help'
27-
$null = New-MarkdownHelp -Module $ModuleName -OutputFolder $DocsOutputFolder -Force -Verbose
28-
Get-ChildItem -Path $DocsOutputFolder -Recurse -Force -Include '*.md' | ForEach-Object {
29-
$content = Get-Content -Path $_.FullName
30-
$fixedOpening = $false
31-
$newContent = @()
32-
foreach ($line in $content) {
33-
if ($line -match '^```$' -and -not $fixedOpening) {
34-
$line = $line -replace '^```$', '```powershell'
35-
$fixedOpening = $true
36-
} elseif ($line -match '^```.+$') {
37-
$fixedOpening = $true
38-
} elseif ($line -match '^```$') {
39-
$fixedOpening = $false
30+
LogGroup 'Build docs - Generate markdown help' {
31+
$null = New-MarkdownHelp -Module $ModuleName -OutputFolder $DocsOutputFolder -Force -Verbose
32+
Get-ChildItem -Path $DocsOutputFolder -Recurse -Force -Include '*.md' | ForEach-Object {
33+
$content = Get-Content -Path $_.FullName
34+
$fixedOpening = $false
35+
$newContent = @()
36+
foreach ($line in $content) {
37+
if ($line -match '^```$' -and -not $fixedOpening) {
38+
$line = $line -replace '^```$', '```powershell'
39+
$fixedOpening = $true
40+
} elseif ($line -match '^```.+$') {
41+
$fixedOpening = $true
42+
} elseif ($line -match '^```$') {
43+
$fixedOpening = $false
44+
}
45+
$newContent += $line
4046
}
41-
$newContent += $line
47+
$newContent | Set-Content -Path $_.FullName
48+
}
49+
Get-ChildItem -Path $DocsOutputFolder -Recurse -Force -Include '*.md' | ForEach-Object {
50+
$content = Get-Content -Path $_.FullName -Raw
51+
$content = $content -replace '\\`', '`'
52+
$content = $content -replace '\\\[', '['
53+
$content = $content -replace '\\\]', ']'
54+
$content = $content -replace '\\\<', '<'
55+
$content = $content -replace '\\\>', '>'
56+
$content = $content -replace '\\\\', '\'
57+
$content | Set-Content -Path $_.FullName
4258
}
43-
$newContent | Set-Content -Path $_.FullName
44-
}
45-
Get-ChildItem -Path $DocsOutputFolder -Recurse -Force -Include '*.md' | ForEach-Object {
46-
$content = Get-Content -Path $_.FullName -Raw
47-
$content = $content -replace '\\`', '`'
48-
$content = $content -replace '\\\[', '['
49-
$content = $content -replace '\\\]', ']'
50-
$content = $content -replace '\\\<', '<'
51-
$content = $content -replace '\\\>', '>'
52-
$content = $content -replace '\\\\', '\'
53-
$content | Set-Content -Path $_.FullName
5459
}
55-
Stop-LogGroup
5660

5761
Get-ChildItem -Path $DocsOutputFolder -Recurse -Force -Include '*.md' | ForEach-Object {
5862
$fileName = $_.Name
5963
$hash = (Get-FileHash -Path $_.FullName -Algorithm SHA256).Hash
60-
Start-LogGroup " - [$fileName] - [$hash]"
61-
Show-FileContent -Path $_
62-
Stop-LogGroup
64+
LogGroup " - [$fileName] - [$hash]" {
65+
Show-FileContent -Path $_
66+
}
6367
}
6468
}

0 commit comments

Comments
 (0)