ps/Modules/Cole.PowerShell.Developer/Public/Update-PowerShellModuleVersion.ps1
2023-05-30 22:51:22 -07:00

55 lines
2.1 KiB
PowerShell

function Update-PowerShellModuleVersion {
<#
.SYNOPSIS
The goal of this function is to prevent needing to remember to update the module version
I can just run this function and magic occurs.
That's the dream anyways
#>
$startingFolder = (Get-Location)
$folder = (Get-Item (Get-Location).Path)
while ($null -eq (Get-ChildItem $folder Modules)) {
$folder = (Get-Item $folder.Parent.FullName)
}
$rootFolderPath = $folder.FullName
Set-Location $rootFolderPath
$filesModified = (git status --short)
$foundModules = @()
foreach($file in $filesModified) {
# get the module name for the affected file
$path = $file.Substring(3)
if ($path -match "Modules/") {
$moduleName = ($path -split '/')[1]
if ($foundModules -notcontains $moduleName) {
$foundModules += $moduleName
}
}
}
foreach($foundModule in $foundModules) {
$moduleFragment = ("Modules/{0}/{0}.psd1" -f $foundModule)
$moduleFullPath = (Join-Path $rootFolderPath $moduleFragment)
$modified = (((git diff --staged $moduleFragment).Where({$_.StartsWith("+ ")}) -match "ModuleVersion").Count -gt 0) -or (((git diff $moduleFragment).Where({$_.StartsWith("+ ")}) -match "ModuleVersion").Count -gt 0)
if (!$modified) {
Write-Host "gonna modify the version number for $moduleFragment"
$lines = Get-Content -Path $moduleFullPath
$newLines = @()
foreach ($line in $lines) {
if ($line -match 'ModuleVersion') {
$lineSplits = $line.Split('=')
$lineLead = $lineSplits[0].TrimEnd()
$version = [System.Version]$lineSplits[1].Trim().Replace("'","").Replace('"','')
$version = New-Object System.Version $version.Major, $version.Minor, ($version.Build + 1)
$line = $lineLead + " = '" + $version + "'"
}
$newLines += $line
}
Set-Content -Path $moduleFullPath -Value $newLines
}
}
Set-Location $startingFolder
}