Skip to content

Commit 24bfa61

Browse files
author
Robin Stolpe
authored
Merge pull request #6 from rstolpe/Dev
Dev
2 parents 3445f1c + c897e6e commit 24bfa61

6 files changed

Lines changed: 664 additions & 88 deletions

File tree

MaintainModule/MaintainModule.psd1

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<#
1+
<#
22
Copyright (C) 2022 Robin Stolpe
33
<https://stolpe.io>
44
This program is free software: you can redistribute it and/or modify
@@ -27,7 +27,7 @@
2727
RootModule = '.\MaintainModule.psm1'
2828

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

3232
# Supported PSEditions
3333
# CompatiblePSEditions = @()
@@ -84,7 +84,7 @@
8484
# NestedModules = @()
8585

8686
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
87-
FunctionsToExport = @("Update-RSModule", "Uninstall-RSModule")
87+
FunctionsToExport = "Uninstall-RSModule", "Update-RSModule"
8888

8989
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
9090
CmdletsToExport = @()
@@ -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
@@ -142,5 +142,4 @@
142142

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

MaintainModule/MaintainModule.psm1

Lines changed: 96 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<#
1+
Function Uninstall-RSModule {
2+
<#
23
Copyright (C) 2022 Robin Stolpe
34
<https://stolpe.io>
45
This program is free software: you can redistribute it and/or modify
@@ -11,9 +12,102 @@
1112
GNU General Public License for more details.
1213
You should have received a copy of the GNU General Public License
1314
along with this program. If not, see <https://www.gnu.org/licenses/>.
14-
#>
15+
#>
16+
#
17+
<#
18+
.SYNOPSIS
19+
Uninstall older versions of your modules in a easy way.
20+
21+
.DESCRIPTION
22+
This script let users uninstall older versions of the modules that are installed on the system.
23+
24+
.PARAMETER Module
25+
Specify modules that you want to uninstall older versions from, if this is left empty all of the older versions of the systems modules will be uninstalled
26+
27+
.EXAMPLE
28+
Uninstall-RSModule -Module "VMWare.PowerCLI"
29+
# This will uninstall all older versions of the module VMWare.PowerCLI system.
30+
31+
.EXAMPLE
32+
Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel"
33+
# This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system.
34+
35+
.NOTES
36+
Author: Robin Stolpe
37+
38+
Website: https://stolpe.io
39+
GitHub: https://github.com/rstolpe
40+
Twitter: https://twitter.com/rstolpes
41+
PSGallery: https://www.powershellgallery.com/profiles/rstolpe
42+
#>
43+
44+
[CmdletBinding()]
45+
Param(
46+
[Parameter(Mandatory = $false, HelpMessage = "Specify modules that you want to uninstall all of the older versions from")]
47+
[string]$Module
48+
)
49+
50+
Write-Output "`n=== Starting to uninstall older versions of modules ===`n"
51+
Write-Output "Please wait, this can take some time..."
1552

53+
# Collect all installed modules from the system
54+
Write-Verbose "Caching all installed modules from the system..."
55+
$InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name
56+
57+
# If Module parameter is empty populate it with all modules that are installed on the system
58+
if ([string]::IsNullOrEmpty($Module)) {
59+
Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..."
60+
$Module = $InstalledModules.Name
61+
}
62+
else {
63+
Write-Verbose "User has added modules to the Module parameter, splitting them"
64+
$Module = $Module.Split(",").Trim()
65+
}
66+
67+
foreach ($m in $Module.Split()) {
68+
Write-Verbose "Collecting all installed version of the module $($m)"
69+
$GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object PublishedDate -Descending
70+
71+
# If the module has more then one version loop trough the versions and only keep the most current one
72+
if ($GetAllInstalledVersions.Count -gt 1) {
73+
$MostRecentVersion = $GetAllInstalledVersions[0].Version
74+
Foreach ($Version in $GetAllInstalledVersions.Version) {
75+
if ($Version -ne $MostRecentVersion) {
76+
try {
77+
Write-Output "Uninstalling previous version $($Version) of module $($m)..."
78+
Uninstall-Module -Name $m -RequiredVersion $Version -Force -ErrorAction SilentlyContinue
79+
Write-Output "Version $($Version) of $($m) are now uninstalled!"
80+
}
81+
catch {
82+
Write-Error "$($PSItem.Exception)"
83+
continue
84+
}
85+
}
86+
}
87+
Write-Output "All older versions of $($m) are now uninstalled, the only installed version of $($m) is $($MostRecentVersion)"
88+
}
89+
else {
90+
Write-Verbose "$($m) don't have any older versions installed then the most current one, no need to uninstall anything."
91+
}
92+
}
93+
Write-Output "`n---/// Script Finished! ///---"
94+
}
1695
Function Update-RSModule {
96+
<#
97+
Copyright (C) 2022 Robin Stolpe
98+
<https://stolpe.io>
99+
This program is free software: you can redistribute it and/or modify
100+
it under the terms of the GNU General Public License as published by
101+
the Free Software Foundation, either version 3 of the License, or
102+
(at your option) any later version.
103+
This program is distributed in the hope that it will be useful,
104+
but WITHOUT ANY WARRANTY; without even the implied warranty of
105+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
106+
GNU General Public License for more details.
107+
You should have received a copy of the GNU General Public License
108+
along with this program. If not, see <https://www.gnu.org/licenses/>.
109+
#>
110+
#
17111
<#
18112
.SYNOPSIS
19113
This module let you maintain your installed modules in a easy way.
@@ -229,83 +323,3 @@ Function Update-RSModule {
229323
}
230324
Write-Output "`n---/// Script Finished! ///---"
231325
}
232-
233-
Function Uninstall-RSModule {
234-
<#
235-
.SYNOPSIS
236-
Uninstall older versions of your modules in a easy way.
237-
238-
.DESCRIPTION
239-
This script let users uninstall older versions of the modules that are installed on the system.
240-
241-
.PARAMETER Module
242-
Specify modules that you want to uninstall older versions from, if this is left empty all of the older versions of the systems modules will be uninstalled
243-
244-
.EXAMPLE
245-
Uninstall-RSModule -Module "VMWare.PowerCLI"
246-
# This will uninstall all older versions of the module VMWare.PowerCLI system.
247-
248-
.EXAMPLE
249-
Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel"
250-
# This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system.
251-
252-
.NOTES
253-
Author: Robin Stolpe
254-
255-
Website: https://stolpe.io
256-
GitHub: https://github.com/rstolpe
257-
Twitter: https://twitter.com/rstolpes
258-
PSGallery: https://www.powershellgallery.com/profiles/rstolpe
259-
#>
260-
261-
[CmdletBinding()]
262-
Param(
263-
[Parameter(Mandatory = $false, HelpMessage = "Specify modules that you want to uninstall all of the older versions from")]
264-
[string]$Module
265-
)
266-
267-
Write-Output "`n=== Starting to uninstall older versions of modules ===`n"
268-
Write-Output "Please wait, this can take some time..."
269-
270-
# Collect all installed modules from the system
271-
Write-Verbose "Caching all installed modules from the system..."
272-
$InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name
273-
274-
# If Module parameter is empty populate it with all modules that are installed on the system
275-
if ([string]::IsNullOrEmpty($Module)) {
276-
Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..."
277-
$Module = $InstalledModules.Name
278-
}
279-
else {
280-
Write-Verbose "User has added modules to the Module parameter, splitting them"
281-
$Module = $Module.Split(",").Trim()
282-
}
283-
284-
foreach ($m in $Module.Split()) {
285-
Write-Verbose "Collecting all installed version of the module $($m)"
286-
$GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object PublishedDate -Descending
287-
288-
# If the module has more then one version loop trough the versions and only keep the most current one
289-
if ($GetAllInstalledVersions.Count -gt 1) {
290-
$MostRecentVersion = $GetAllInstalledVersions[0].Version
291-
Foreach ($Version in $GetAllInstalledVersions.Version) {
292-
if ($Version -ne $MostRecentVersion) {
293-
try {
294-
Write-Output "Uninstalling previous version $($Version) of module $($m)..."
295-
Uninstall-Module -Name $m -RequiredVersion $Version -Force -ErrorAction SilentlyContinue
296-
Write-Output "Version $($Version) of $($m) are now uninstalled!"
297-
}
298-
catch {
299-
Write-Error "$($PSItem.Exception)"
300-
continue
301-
}
302-
}
303-
}
304-
Write-Output "All older versions of $($m) are now uninstalled, the only installed version of $($m) is $($MostRecentVersion)"
305-
}
306-
else {
307-
Write-Verbose "$($m) don't have any older versions installed then the most current one, no need to uninstall anything."
308-
}
309-
}
310-
Write-Output "`n---/// Script Finished! ///---"
311-
}

Publish.ps1

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#param (
2+
# [string] $version,
3+
# [string] $preReleaseTag,
4+
# [string] $apiKey
5+
#)
6+
7+
$Version = "0.0.7"
8+
$preReleaseTag = "-beta"
9+
#$apiKey = "test"
10+
11+
# Creating ArrayList for use later in the script
12+
[System.Collections.ArrayList]$FunctionPSD = @()
13+
14+
# Name of the module
15+
$ModuleName = $(Get-Location) -split "/" | select -last 1
16+
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
17+
$ModuleFolderPath = "$($scriptPath)/$($ModuleName)"
18+
$srcPath = "$($scriptPath)/src"
19+
$srcFunctionPath = "$($scriptPath)/src/Function"
20+
$outPSMFile = "$($ModuleFolderPath)/$($ModuleName).psm1"
21+
$outPSDFile = "$($ModuleFolderPath)/$($ModuleName).psd1"
22+
$psdTemplate = "$($srcPath)/$($ModuleName).psd1.source"
23+
24+
Write-OutPut "== Preparing $($ModuleName) for publishing ==`n"
25+
Write-OutPut "Starting to build the module, please wait..."
26+
27+
if (!(Test-Path $ModuleFolderPath)) {
28+
Write-Verbose "Creating folder $($ModuleFolderPath)"
29+
New-Item -Path $ModuleFolderPath -ItemType Directory -Force
30+
}
31+
else {
32+
if (Test-Path $outPSMFile) {
33+
Write-Verbose "Removing file $($outPSMFile)"
34+
Remove-Item -Path $outPSMFile -Force
35+
}
36+
37+
if (Test-Path $outPSDFile) {
38+
Write-Verbose "Removing file $($outPSDFile)"
39+
Remove-Item -Path $outPSDFile -Force
40+
}
41+
}
42+
43+
# Collecting all .ps1 files that are located in src/function folders
44+
Write-Verbose "Collecting all .ps1 files from $($srcFunctionPath)"
45+
$MigrateFunction = @( Get-ChildItem -Path $srcFunctionPath/*.ps1 -ErrorAction SilentlyContinue -Recurse )
46+
47+
# Looping trough the .ps1 files and migrating them to one singel .psm1 file and saving it in the module folder
48+
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"
49+
foreach ($function in $MigrateFunction) {
50+
# Migrates all of the .ps1 files that are located in src/Function in to one .psm1 file saved in the module folder
51+
$Results = [System.Management.Automation.Language.Parser]::ParseFile($function, [ref]$null, [ref]$null)
52+
$Functions = $Results.EndBlock.Extent.Text
53+
$Functions | Add-Content -Path $outPSMFile
54+
55+
# Converting the function name to fit the .psd1 file for exporting
56+
$function = $function.Name -replace ".ps1"
57+
$function = """$($function)"","
58+
$function.trim()
59+
60+
# Collect the name of all .ps1 files so it can be added as functions in the psd1 file.
61+
$FunctionPSD.Add($function)
62+
}
63+
64+
# 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
65+
$FunctionPSD = $FunctionPSD | ForEach-Object {
66+
if ( $FunctionPSD.IndexOf($_) -eq ($FunctionPSD.count - 1) ) {
67+
$_.replace(",", "")
68+
}
69+
else { $_ }
70+
}
71+
72+
# Copy the .psd1.source file from the srcPath to the module folder and removing the .source ending
73+
Write-Verbose "Copy the file $($psdTemplate) to $($outPSDFile)"
74+
Copy-Item -Path $psdTemplate -Destination $outPSDFile -Force
75+
76+
# Getting the content from the .psd1 file
77+
Write-Verbose "Getting the content from file $($outPSDFile)"
78+
$fileContent = Get-Content -Path $outPSDFile
79+
80+
# Changing version, preReleaseTag and function in the .psd1 file
81+
$fileContent = $fileContent -replace '{{version}}', $version
82+
$fileContent = $fileContent -replace '{{preReleaseTag}}', $preReleaseTag
83+
$fileContent = $fileContent -replace '{{function}}', $FunctionPSD
84+
85+
Write-Verbose "Changing the placeholders in $($outPSDFile)"
86+
Set-Content -Path $outPSDFile -Value $fileContent -Force
87+
88+
Write-Output "---/// $($ModuleName) is now prepared for publishing! ///---"
89+
90+
#Publish-Module `
91+
# -Path $scriptPath\$ModuleName `
92+
# -NuGetApiKey $apiKey `
93+
# -Verbose -Force

0 commit comments

Comments
 (0)