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+ }
0 commit comments