Skip to content

Commit c8cc08d

Browse files
author
Robin Stolpe
committed
Done some nice fixing, will relase a beta
1 parent 7cbcd0a commit c8cc08d

File tree

9 files changed

+804
-106
lines changed

9 files changed

+804
-106
lines changed

MaintainModule/MaintainModule.psd1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
RootModule = '.\MaintainModule.psm1'
2828

2929
# Version number of this module.
30-
ModuleVersion = '0.0.7'
30+
ModuleVersion = '0.0.8'
3131

3232
# Supported PSEditions
3333
# CompatiblePSEditions = @()
@@ -125,7 +125,7 @@
125125
ReleaseNotes = 'https://github.com/rstolpe/MaintainModule/releases'
126126

127127
# Prerelease string of this module
128-
Prerelease = ''
128+
Prerelease = 'beta'
129129

130130
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
131131
RequireLicenseAcceptance = $false
@@ -138,7 +138,7 @@
138138
} # End of PrivateData hashtable
139139

140140
# HelpInfo URI of this module
141-
HelpInfoURI = 'https://github.com/rstolpe/MaintainModule/blob/main/README.md'
141+
# HelpInfoURI = ''
142142

143143
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
144144
# DefaultCommandPrefix = ''

MaintainModule/MaintainModule.psm1

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<#
2-
Copyright (C) {{year}} Robin Stolpe.
2+
Copyright (C) 2022 Robin Stolpe.
33
<https://stolpe.io>
44
This program is free software: you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -33,6 +33,9 @@ Function Uninstall-RSModule {
3333
Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel"
3434
# This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system.
3535
36+
.RELATED LINKS
37+
https://github.com/rstolpe/MaintainModule/blob/main/README.md
38+
3639
.NOTES
3740
Author: Robin Stolpe
3841
@@ -44,7 +47,7 @@ Function Uninstall-RSModule {
4447

4548
[CmdletBinding()]
4649
Param(
47-
[Parameter(Mandatory = $false, HelpMessage = "Specify modules that you want to uninstall all of the older versions from")]
50+
[Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")]
4851
[string]$Module
4952
)
5053

@@ -122,7 +125,7 @@ Function Update-RSModule {
122125
If you use this switch and the modules that are specified in the Module parameter are not installed on the system they will be installed.
123126
124127
.EXAMPLE
125-
Update-RSModule -Module "PowerCLI, ImportExcel"
128+
Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser
126129
# This will update the modules PowerCLI, ImportExcel for the current user
127130
128131
.EXAMPLE
@@ -137,6 +140,9 @@ Function Update-RSModule {
137140
Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion -ImportModule
138141
# This will update the modules PowerCLI and ImportExcel and delete all of the old versions that are installed of PowerCLI and ImportExcel and then import the modules.
139142
143+
.RELATED LINKS
144+
https://github.com/rstolpe/MaintainModule/blob/main/README.md
145+
140146
.NOTES
141147
Author: Robin Stolpe
142148
@@ -148,16 +154,16 @@ Function Update-RSModule {
148154

149155
[CmdletBinding()]
150156
Param(
151-
[Parameter(Mandatory = $false, HelpMessage = "Specify modules that you want to update, if this is empty all of the modules that are installed on the system will get updated")]
157+
[Parameter(Mandatory = $false, HelpMessage = "Enter module or modules (separated with ,) that you want to update, if you don't enter any all of the modules will be updated")]
152158
[string]$Module,
153-
[ValidateSet("CurrentUser", "AllUsers", $null)]
154-
[Parameter(Mandatory = $true, HelpMessage = "Choose either AllUsers or CurrentUser depending on which layer you want to update/Install/uninstall the module on")]
159+
[ValidateSet("CurrentUser", "AllUsers")]
160+
[Parameter(Mandatory = $true, HelpMessage = "Enter CurrentUser or AllUsers depending on what scope you want to change your modules")]
155161
[string]$Scope = "CurrentUser",
156-
[Parameter(Mandatory = $false, HelpMessage = "Imports all of the modules that are specified in the Module parameter in the end of the script")]
162+
[Parameter(Mandatory = $false, HelpMessage = "Import modules that has been entered in the module parameter at the end of this function")]
157163
[switch]$ImportModule = $false,
158164
[Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")]
159165
[switch]$UninstallOldVersion = $false,
160-
[Parameter(Mandatory = $false, HelpMessage = "When using this switch all modules that are specified in the Module parameter and are not installed will be installed")]
166+
[Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")]
161167
[switch]$InstallMissing = $false
162168
)
163169

@@ -230,33 +236,7 @@ Function Update-RSModule {
230236

231237
# If switch -UninstallOldVersion has been used then the old versions will be uninstalled from the module
232238
if ($UninstallOldVersion -eq $true) {
233-
# Collecting all of the installed versions of the module, this is needed to run again as we might have been installed a new version above.
234-
Write-Verbose "Collecting all installed version of $($m)..."
235-
$GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object PublishedDate -Descending
236-
237-
# If the module has more then one version installed, uninstall all of the old versions.
238-
if ($GetAllInstalledVersions.Count -gt 1) {
239-
$MostRecentVersion = $GetAllInstalledVersions[0].Version
240-
241-
# Start to uninstall all of the older versions
242-
Foreach ($Version in $GetAllInstalledVersions.Version) {
243-
if ($Version -ne $MostRecentVersion) {
244-
try {
245-
Write-Output "Uninstalling previous version $($Version) of module $($m)..."
246-
Uninstall-Module -Name $m -RequiredVersion $Version -Force -ErrorAction SilentlyContinue
247-
Write-Output "Version $($Version) of $($m) are now uninstalled!"
248-
}
249-
catch {
250-
Write-Error "$($PSItem.Exception)"
251-
continue
252-
}
253-
}
254-
}
255-
Write-Output "All older versions of $($m) are now uninstalled, the only installed version of $($m) is $($MostRecentVersion)"
256-
}
257-
else {
258-
Write-Verbose "$($m) don't have any older versions installed then the most current one, no need to uninstall anything."
259-
}
239+
Uninstall-RSModule -Module $m
260240
}
261241
}
262242
else {

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,10 @@ You can uninstall all of the older versions from one or more specific modules.
8888
````
8989
Uninstall-RSModule
9090
````
91-
You can also use the -Scope parameter if you want to change from CurrentUser to AllUsers, for example ```-Scope "AllUser"```
92-
If -Scope parameter are empty it will set it as CurrentUser as default.
9391

9492
### Uninstall all older versions from a specific module
9593
If you want to uninstall all older version of a specific module
9694
````
9795
Uninstall-RSModule -Module "ImportExcel"
9896
````
99-
The parameter Module has support for multiple inputs, separate them with , for example ```-Module "ImportExcel, VMWare.PowerCLI"```
100-
You can also use the -Scope parameter if you want to change from CurrentUser to AllUsers, for example ```-Scope "AllUser"```
101-
If -Scope parameter are empty it will set it as CurrentUser as default.
97+
The parameter Module has support for multiple inputs, separate them with , for example ```-Module "ImportExcel, VMWare.PowerCLI"```

RSPublish.ps1

Lines changed: 96 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,60 @@
11
param (
2-
#[string]$version,
3-
#[string]$preReleaseTag,
4-
[Parameter(Mandatory = $false, HelpMessage = "Use this switch if you want to publish to PSGallery after the module has been prepared")]
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.0.8",
5+
# Fix this
6+
[Parameter(Mandatory = $false, HelpMessage = ".")]
7+
[string]$preRelease,
8+
[Parameter(Mandatory = $false, HelpMessage = "Use this switch to publish this module on PSGallery")]
59
[bool]$Publish = $false,
6-
[Parameter(Mandatory = $false, HelpMessage = "Write your API key for PSGallery")]
10+
# Validate so if $Publish is true this is needed
11+
[Parameter(Mandatory = $false, HelpMessage = "Enter API key for PSGallery")]
712
[string]$apiKey
813
)
914

10-
$Version = "0.0.7"
11-
#$preReleaseTag = "-beta"
12-
$Year = (Get-Date).Year
13-
$ManifestDate = Get-Date -Format "yyyy-MM-dd"
15+
# When module creates this file add what version this file comes from for the EasyModuleBuild
16+
# Check if it's any newer version of this module, if it's any newer alert the user about it.
17+
# Create script to generate GUID and populate it in the manifest when generated, this should only happen in the setup new module script not in this one.
18+
# Create script will also populate the Company, PreReleaseTag, author and webpageURI for the manifest.
19+
20+
# Need to find a way to handle -beta tags, might add a switch for that
21+
$preReleaseTag = "beta"
1422

1523
# Creating ArrayList for use later in the script
1624
[System.Collections.ArrayList]$FunctionPSD = @()
1725

18-
# Name of the module
26+
$Year = (Get-Date).Year
27+
$ManifestDate = Get-Date -Format "yyyy-MM-dd"
28+
29+
# Gets the folder name from the folder where this script are located
1930
$ModuleName = $(Get-Location) -split "/" | Select-Object -last 1
31+
32+
# Paths for different sections inside the module
2033
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
2134
$ModuleFolderPath = "$($scriptPath)/$($ModuleName)"
2235
$srcPath = "$($scriptPath)/src"
2336
$srcPublicFunctionPath = "$($scriptPath)/src/public/function"
2437
$outPSMFile = "$($ModuleFolderPath)/$($ModuleName).psm1"
2538
$outPSDFile = "$($ModuleFolderPath)/$($ModuleName).psd1"
2639
$psdTemplate = "$($srcPath)/$($ModuleName).psd1.source"
27-
$psmLicensPath = "$($srcPath)/FileLicens.ps1.source"
40+
$psmLicensPath = "$($srcPath)/License"
41+
$EMBSourceFiles = "$($env:PSModulePath)/EasyModuleBuild/source_files"
42+
43+
# Needed folders
44+
$srcNeededFolders = @("$($srcPath)/Private", "$($srcPath)/Private/Function", "$($srcPath)/Public", "$($srcPath)/Public/Function")
45+
46+
# Needed files
47+
$moduleNeededFiles = @("$($scriptPath)/.gitignore", "$($scriptPath)/LICENSE", "$($scriptPath)/README.md")
2848

2949
Write-OutPut "`n== Preparing $($ModuleName) for publishing ==`n"
3050
Write-OutPut "Starting to build the module, please wait..."
3151

3252
if (!(Test-Path $ModuleFolderPath)) {
3353
Write-Verbose "Creating folder $($ModuleFolderPath)"
34-
New-Item -Path $ModuleFolderPath -ItemType Directory -Force
54+
[void](New-Item -Path $ModuleFolderPath -ItemType Directory -Force)
55+
56+
# add check to see if the user has this files in there user profile and wrap this to if/else
57+
# if they don't have it copy it from this root module
3558
}
3659
else {
3760
if (Test-Path $outPSMFile) {
@@ -43,10 +66,31 @@ else {
4366
Write-Verbose "Removing file $($outPSDFile)"
4467
Remove-Item -Path $outPSDFile -Force
4568
}
69+
# Check so .gitignore, LICENSE, README.md exists in the module folder, if not se line below what to do.
70+
# check if .gitignore, LICENSE, README.md exists in user settings folder, if not copy the original ones from this modules root folder.
71+
}
72+
73+
if (!(Test-Path $srcPath)) {
74+
Write-Verbose "Creating folder $($srcPath)"
75+
New-Item -Path $srcPath -ItemType Directory -Force
76+
foreach ($f in $srcNeededFolders) {
77+
Write-Verbose "Creating folder $($f)"
78+
[void](New-Item -Path $f -ItemType Directory -Force)
79+
}
80+
# Check if the user has any settingfiles for this module if not, then Copy .psd1.source and FileLicens.ps1.source to this folder from the this modules root folder
81+
}
82+
else {
83+
foreach ($f in $srcNeededFolders) {
84+
if (!(Test-Path $f)) {
85+
Write-Verbose "Creating folder $($f)"
86+
[void](New-Item -Path $f -ItemType Directory -Force)
87+
}
88+
}
89+
# check so all the nessacary files are there, if not check if user has user settingsfiles for this module if he don't have it copy the original files from the root module folder
4690
}
4791

48-
# Adding the text from the filelicens.ps1 to the .psm1 file for licensing of GNU v3
49-
$psmLicens = Get-Content -Path $psmLicensPath -ErrorAction SilentlyContinue
92+
# Adding the text from the gnu3_add_file_licens.source to the to of the .psm1 file for licensing of GNU v3
93+
$psmLicens = Get-Content -Path "$($psmLicensPath)/gnu3_add_file_licens.source" -ErrorAction SilentlyContinue
5094
$psmLicens | Add-Content -Path $outPSMFile
5195

5296
# Collecting all .ps1 files that are located in src/function folders
@@ -70,36 +114,60 @@ foreach ($function in $MigrateFunction) {
70114
[void]($FunctionPSD.Add($function))
71115
}
72116

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-
$FunctionPSD = $FunctionPSD | ForEach-Object {
75-
if ( $FunctionPSD.IndexOf($_) -eq ($FunctionPSD.count - 1) ) {
76-
$_.replace(",", "")
117+
# if $MigrateFunction are not empty remove the last , from the $FunctionPSD ArrayList
118+
if ($null -ne $MigrateFunction) {
119+
# 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
120+
$FunctionPSD = $FunctionPSD | ForEach-Object {
121+
if ( $FunctionPSD.IndexOf($_) -eq ($FunctionPSD.count - 1) ) {
122+
$_.replace(",", "")
123+
}
124+
else { $_ }
77125
}
78-
else { $_ }
79126
}
80127

128+
# Change the placeholder in the $outPSMFile file
129+
Write-Verbose "Getting the content from file $($outPSMFile)"
130+
$PSMfileContent = Get-Content -Path $outPSMFile
131+
Write-Verbose "Replacing the placeholders in the $($outPSMFile) file"
132+
$PSMfileContent = $PSMfileContent -replace '{{year}}', $year
133+
134+
Write-Verbose "Setting the placeholders for $($outPSMFile)"
135+
Set-Content -Path $outPSMFile -Value $PSMfileContent -Force
136+
81137
# Copy the .psd1.source file from the srcPath to the module folder and removing the .source ending
82138
Write-Verbose "Copy the file $($psdTemplate) to $($outPSDFile)"
83139
Copy-Item -Path $psdTemplate -Destination $outPSDFile -Force
84140

85141
# Getting the content from the .psd1 file
86142
Write-Verbose "Getting the content from file $($outPSDFile)"
87-
$fileContent = Get-Content -Path $outPSDFile
143+
$PSDfileContent = Get-Content -Path $outPSDFile
88144

145+
# Can I do a loop here? I just might :) remember to check if the varible is empty or not
89146
# Changing version, preReleaseTag and function in the .psd1 file
90-
$fileContent = $fileContent -replace '{{manifestDate}}', $ManifestDate
91-
$fileContent = $fileContent -replace '{{moduleName}}', $ModuleName
92-
$fileContent = $fileContent -replace '{{year}}', $Year
93-
$fileContent = $fileContent -replace '{{version}}', $version
94-
$fileContent = $fileContent -replace '{{preReleaseTag}}', $preReleaseTag
95-
$fileContent = $fileContent -replace '{{function}}', $FunctionPSD
147+
Write-Verbose "Replacing the placeholders in the $($outPSDFile) file"
148+
$PSDfileContent = $PSDfileContent -replace '{{manifestDate}}', $ManifestDate
149+
$PSDfileContent = $PSDfileContent -replace '{{moduleName}}', $ModuleName
150+
$PSDfileContent = $PSDfileContent -replace '{{year}}', $Year
151+
$PSDfileContent = $PSDfileContent -replace '{{version}}', $version
152+
$PSDfileContent = $PSDfileContent -replace '{{preReleaseTag}}', $preReleaseTag
153+
154+
# If $FunctionPSD are empty, then adding @() instead according to best practices for performance
155+
if ($null -ne $FunctionPSD) {
156+
$PSDfileContent = $PSDfileContent -replace '{{function}}', $FunctionPSD
157+
}
158+
else {
159+
$PSDfileContent = $PSDfileContent -replace '{{function}}', '@()'
160+
}
96161

97-
Write-Verbose "Changing the placeholders in $($outPSDFile)"
98-
Set-Content -Path $outPSDFile -Value $fileContent -Force
162+
Write-Verbose "Setting the placeholders for $($outPSDFile)"
163+
Set-Content -Path $outPSDFile -Value $PSDfileContent -Force
99164

100165
if ($Publish -eq $true) {
166+
# Check so that the module has .ps1 and .psd1 files in the module folder before it trys to publish it.
167+
Write-Verbose "Publishing $($ModuleName) version $($version) to PowerShell Gallery"
101168
Publish-Module -Path $ModuleFolderPath -NuGetApiKey $apiKey -Force
169+
Write-Output "---/// $($ModuleName) version $($Version) has now been built and published to PowerShell Gallery! ///---"
102170
}
103171
else {
104-
Write-Output "---/// $($ModuleName) is now prepared for publishing! ///---"
172+
Write-Output "---/// $($ModuleName) version $($Version) is now prepared for publishing! ///---"
105173
}

0 commit comments

Comments
 (0)