Skip to content

Commit f29b449

Browse files
author
Robin Stolpe
committed
Test
1 parent e785015 commit f29b449

9 files changed

Lines changed: 189 additions & 6 deletions

.src/Public/Function/Uninstall-RSModule.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
else {
5151
Write-Verbose "User has added modules to the Module parameter, splitting them"
5252
$OldModule = $Module.Split(",").Trim()
53+
$Module = [System.Collections.Generic.List[string]]::new()
5354

54-
[System.Collections.ArrayList]$Module = @()
5555
Write-Verbose "Looking so the modules exists in the system..."
5656
foreach ($m in $OldModule) {
5757
if ($m -in $InstalledModules.name) {

.src/Public/Function/Update-RSModule.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@
8787
else {
8888
Write-Verbose "User has added modules to the Module parameter, splitting them"
8989
$OldModule = $Module.Split(",").Trim()
90+
$Module = [System.Collections.Generic.List[string]]::new()
9091

91-
[System.Collections.ArrayList]$Module = @()
9292
if ($InstallMissing -eq $false) {
9393
Write-Verbose "Looking so the modules exists in the system..."
9494
foreach ($m in $OldModule) {

MaintainModule/MaintainModule.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#
1919
# Generated by: Robin Stolpe
2020
#
21-
# Generated on: 2022-12-03
21+
# Generated on: 2022-12-10
2222
#
2323

2424
@{
@@ -27,7 +27,7 @@
2727
RootModule = '.\MaintainModule.psm1'
2828

2929
# Version number of this module.
30-
ModuleVersion = '0.0.9'
30+
ModuleVersion = '0.1.0'
3131

3232
# Supported PSEditions
3333
# CompatiblePSEditions = @()

MaintainModule/MaintainModule.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ Function Uninstall-RSModule {
6666
else {
6767
Write-Verbose "User has added modules to the Module parameter, splitting them"
6868
$OldModule = $Module.Split(",").Trim()
69+
$Module = [System.Collections.Generic.List[string]]::new()
6970

70-
[System.Collections.ArrayList]$Module = @()
7171
Write-Verbose "Looking so the modules exists in the system..."
7272
foreach ($m in $OldModule) {
7373
if ($m -in $InstalledModules.name) {
@@ -197,8 +197,8 @@ Function Update-RSModule {
197197
else {
198198
Write-Verbose "User has added modules to the Module parameter, splitting them"
199199
$OldModule = $Module.Split(",").Trim()
200+
$Module = [System.Collections.Generic.List[string]]::new()
200201

201-
[System.Collections.ArrayList]$Module = @()
202202
if ($InstallMissing -eq $false) {
203203
Write-Verbose "Looking so the modules exists in the system..."
204204
foreach ($m in $OldModule) {

RSModuleBuilder.ps1

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
param (
2+
# Set this to true before releasing the module
3+
[Parameter(Mandatory = $false, HelpMessage = "Enter the version number of this release")]
4+
[string]$Version = "0.1.0",
5+
# Fix this
6+
[Parameter(Mandatory = $false, HelpMessage = ".")]
7+
[string]$preRelease = "Alpha",
8+
[Parameter(Mandatory = $false, HelpMessage = "Use this switch to publish this module on PSGallery")]
9+
[bool]$Publish = $false,
10+
# Validate so if $Publish is true this is needed
11+
[Parameter(Mandatory = $false, HelpMessage = "Enter API key for PSGallery")]
12+
[string]$apiKey
13+
)
14+
15+
#Requires -Modules PSScriptAnalyzer
16+
Import-Module -Name EasyModuleBuild -Force
17+
18+
# Creating ArrayList for use later in the script
19+
$FunctionPSD = [System.Collections.Generic.List[string]]::new()
20+
21+
$Year = (Get-Date).Year
22+
$TodaysDate = Get-Date -Format "yyyy-MM-dd"
23+
$ModuleName = $(Get-Location) -split "/" | Select-Object -last 1
24+
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
25+
$HelpPath = Join-Path -Path $scriptPath -ChildPath "help"
26+
$ModuleFolderPath = Join-Path -Path $scriptPath -ChildPath $ModuleName
27+
$srcPath = Join-Path -Path $scriptPath -ChildPath ".src"
28+
$srcPublicFunctionPath = Join-Path -Path $srcPath -ChildPath "public/function"
29+
$srcPrivateFunctionPath = Join-Path -Path $srcPath -ChildPath "private/function"
30+
$outPSMFile = Join-Path -Path $ModuleFolderPath -ChildPath "$($ModuleName).psm1"
31+
$outPSDFile = Join-Path -Path $ModuleFolderPath -ChildPath "$($ModuleName).psd1"
32+
$psdTemplate = Join-Path -Path $srcPath -ChildPath "$($ModuleName).psd1.source"
33+
$psmLicensPath = Join-Path -Path $srcPath -ChildPath "License"
34+
$TestPath = Join-Path -Path $scriptPath -ChildPath "test"
35+
36+
Write-OutPut "`n== Building module $($ModuleName) ==`n"
37+
Write-OutPut "Starting to build the module, please wait..."
38+
39+
# Check so all the needed folders exists, if they don't they will get created.
40+
Checkpoint-RSFolderFile -ModulePath $scriptPath -ModuleName $ModuleName -New $false
41+
42+
# Deleting existing files that will get replaced by this script
43+
Remove-RSContent -ModuleName $ModuleName -ScriptPath $scriptPath -ExistingModule
44+
45+
# Adding the text from the gnu3_add_file_licens.source to the to of the .psm1 file for licensing of GNU v3
46+
# Let user choose between GNU 3 or MIT
47+
$psmLicens = Get-Content -Path "$($psmLicensPath)/gnu3_add_file_licens.source" -ErrorAction SilentlyContinue
48+
$psmLicens | Add-Content -Path $outPSMFile
49+
50+
# Collecting all .ps1 files that are located in .src private/function and public/function folders
51+
Write-Verbose "Collecting all .ps1 files from $($srcPublicFunctionPath) and $($srcPrivateFunctionPath)"
52+
$MigrateFunction = @( $(Get-ChildItem -Path $srcPublicFunctionPath/*.ps1 | Select-Object FullName, Name -ErrorAction SilentlyContinue), $(Get-ChildItem -Path $srcPrivateFunctionPath/*.ps1 | Select-Object FullName, Name -ErrorAction SilentlyContinue) )
53+
54+
# Looping trough the .ps1 files and migrating them to one singel .psm1 file and saving it in the module folder
55+
Write-Verbose "Start to migrate all functions in to the .psm1 file and collecting the function names to add in the FunctionToExport in the .psd1 file"
56+
foreach ($function in $MigrateFunction.FullName) {
57+
# Migrates all of the .ps1 files that are located in src/Function in to one .psm1 file saved in the module folder
58+
$Results = [System.Management.Automation.Language.Parser]::ParseFile($function, [ref]$null, [ref]$null)
59+
$Functions = $Results.EndBlock.Extent.Text
60+
$Functions | Add-Content -Path $outPSMFile
61+
62+
# Converting the function name to fit the .psd1 file for exporting
63+
$function = $function -split "/" -replace ".ps1" | Select-Object -Last 1
64+
$function = """$($function)"","
65+
[void]($function.trim())
66+
67+
# Collect the name of all .ps1 files so it can be added as functions in the psd1 file.
68+
[void]($FunctionPSD.Add($function))
69+
}
70+
71+
# if $MigrateFunction are not empty remove the last , from the $FunctionPSD ArrayList
72+
if ($null -ne $MigrateFunction) {
73+
# I know that I need to fix this one, but it's the best I can think of for now to remove the last , in the ArrayList
74+
# Bug! If the module only contain one function the , after the name are not removed, need to remove that
75+
$FunctionPSD = $FunctionPSD | ForEach-Object {
76+
if ( $FunctionPSD.IndexOf($_) -eq ($FunctionPSD.count - 1) ) {
77+
$_.replace(",", "")
78+
}
79+
else {
80+
$_
81+
}
82+
}
83+
}
84+
85+
# Change the placeholder in the $outPSMFile file
86+
Write-Verbose "Getting the content from file $($outPSMFile)"
87+
$PSMfileContent = Get-Content -Path $outPSMFile
88+
89+
Write-Verbose "Replacing the placeholders in the $($outPSMFile) file"
90+
$PSMfileContent = $PSMfileContent -replace '{{year}}', $year
91+
92+
Write-Verbose "Setting the placeholders for $($outPSMFile)"
93+
Set-Content -Path $outPSMFile -Value $PSMfileContent -Encoding utf8BOM -Force
94+
95+
# Copy the .psd1.source file from the srcPath to the module folder and removing the .source ending
96+
Write-Verbose "Copy the file $($psdTemplate) to $($outPSDFile)"
97+
Copy-Item -Path $psdTemplate -Destination $outPSDFile -Force
98+
99+
# Getting the content from the .psd1 file
100+
Write-Verbose "Getting the content from file $($outPSDFile)"
101+
$PSDfileContent = Get-Content -Path $outPSDFile
102+
103+
# Can I do a loop here? I just might :) remember to check if the varible is empty or not
104+
# Changing version, preReleaseTag and function in the .psd1 file
105+
Write-Verbose "Replacing the placeholders in the $($outPSDFile) file"
106+
$PSDfileContent = $PSDfileContent -replace '{{manifestDate}}', $TodaysDate
107+
$PSDfileContent = $PSDfileContent -replace '{{moduleName}}', $ModuleName
108+
$PSDfileContent = $PSDfileContent -replace '{{year}}', $Year
109+
$PSDfileContent = $PSDfileContent -replace '{{version}}', $version
110+
$PSDfileContent = $PSDfileContent -replace '{{preReleaseTag}}', $preReleaseTag
111+
112+
# If $FunctionPSD are empty, then adding @() instead according to best practices for performance
113+
if ($null -ne $FunctionPSD) {
114+
$PSDfileContent = $PSDfileContent -replace '{{function}}', $FunctionPSD
115+
}
116+
else {
117+
$PSDfileContent = $PSDfileContent -replace '{{function}}', '@()'
118+
}
119+
120+
Write-Verbose "Setting the placeholders for $($outPSDFile)"
121+
Set-Content -Path $outPSDFile -Value $PSDfileContent -Encoding utf8BOM -Force
122+
123+
Write-Output "Running PSScriptAnalyzer on $($MigrateFunction.name)..."
124+
$ResultPS1 = foreach ($ps1 in $MigrateFunction.FullName) {
125+
$ps1Name = $ps1 -split "/" -replace ".ps1" | Select-Object -Last 1
126+
Write-Verbose "Running PSScriptAnalyzer on $($ps1Name).ps1..."
127+
$PSAnalyzerPS1 = Invoke-ScriptAnalyzer -Path $ps1 -ReportSummary
128+
if ($null -ne $PSAnalyzerPS1) {
129+
$PSAnalyzerPS1 | select-object * | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md")
130+
}
131+
else {
132+
Write-Output "0 rule violations found." | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md")
133+
}
134+
$PSAnalyzerPS1
135+
}
136+
137+
Write-Output "Running PSScriptAnalyzer on $($outPSDFile) and $($outPSMFile)..."
138+
$CheckPSA = @($outPSDFile, $outPSMFile)
139+
$ResultPSDPSM = foreach ($file in $CheckPSA) {
140+
$psdPSMName = $file -split "/" | Select-Object -Last 1
141+
Write-Verbose "Running PSScriptAnalyzer on $($psdPSMName)..."
142+
$PSAnalyzer = Invoke-ScriptAnalyzer -Path $file -ReportSummary
143+
if ($null -ne $PSAnalyzer) {
144+
$PSAnalyzer | select-object * | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($psdPSMName)_$($TodaysDate).md")
145+
}
146+
else {
147+
Write-Output "0 rule violations found." | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($psdPSMName)_$($TodaysDate).md")
148+
}
149+
$PSAnalyzer
150+
}
151+
152+
# Import the module and save the Get-Help files to the $HelpPath for the module, files get saved in .md format
153+
Write-Verbose "Importing $($ModuleName) to the session..."
154+
Import-Module -Name $($ModuleFolderPath) -MinimumVersion $Version -Force
155+
156+
Write-Verbose "Writing $($ModuleName) functions to help files in $($HelpPath)..."
157+
$mCommands = Get-Command -Module $ModuleName
158+
foreach ($m in $mCommands) {
159+
if ($null -ne $m) {
160+
Write-Verbose "Creating help file of function $($m.Name)..."
161+
Get-Help -name $m.Name -Full | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $HelpPath -ChildPath "$($m.Name).md")
162+
}
163+
}
164+
165+
Write-Output "`n== Summery of PSScriptAnalyzer =="
166+
$ResultPS1
167+
$ResultPSDPSM
168+
169+
if ($ResultPS1.Severity -contains "Warning" -or $ResultPSM.Severity -contains "Warning") {
170+
Write-Error "PSAnalyzer severity did contain Warning, please fix this and run the RSModuleBuilder again. You can se the results from PSScriptAnalyzer below."
171+
Break
172+
}
173+
174+
# Add so it check if it has any other flags in the analyzer then just inform about it.
175+
176+
if ($Publish -eq $true) {
177+
Write-Verbose "Publishing $($ModuleName) version $($version) to PowerShell Gallery"
178+
Publish-Module -Path $ModuleFolderPath -NuGetApiKey $apiKey -Force
179+
Write-Output "---/// $($ModuleName) version $($Version) has now been built and published to PowerShell Gallery! ///---"
180+
}
181+
else {
182+
Write-Output "---/// $($ModuleName) version $($Version) is now prepared for publishing! ///---"
183+
}

test/PSScriptAnalyzer_MaintainModule.psd1_2022-12-03.md renamed to test/PSScriptAnalyzer_MaintainModule.psd1_2022-12-10.md

File renamed without changes.

test/PSScriptAnalyzer_MaintainModule.psm1_2022-12-03.md renamed to test/PSScriptAnalyzer_MaintainModule.psm1_2022-12-10.md

File renamed without changes.

test/PSScriptAnalyzer_Uninstall-RSModule_2022-12-03.md renamed to test/PSScriptAnalyzer_Uninstall-RSModule_2022-12-10.md

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)