Tools/Update-Manifest.ps1

[CmdletBinding()]
param(
    [Parameter(Mandatory = $false)]
    [string]$ProjectModuleRoot,
    [Parameter(Mandatory = $false)]
    [string]$ProjectName,
    [Parameter(Mandatory = $false)]
    [string]$GitHubUserName = 'rez23',
    [Parameter(Mandatory = $false)]
    [switch]$WithCommit,
    [Parameter(Mandatory = $false)]
    [string]$ModuleVersion
)

function Get-VersionFromGit {
    $gitDescribe = git describe --tags --abbrev=0 2>$null
    if ($LASTEXITCODE -eq 0) {
        $gitDescribe -replace "v", ""
    }
    else {
        throw "Git command failed. Using default version '1.0.0'"
    }
}

if (-not $ProjectModuleRoot) {
    $ProjectModuleRoot = (Get-Item "$PSScriptRoot").Parent.FullName
}
else {
    $ProjectModuleRoot = (Get-Item $ProjectModuleRoot).FullName
}

if (-not (Test-Path $ProjectModuleRoot)) {
    throw "Project module root path '$ProjectModuleRoot' does not exist."
}


if (-not $ProjectName) {
    $ProjectName = (Get-Item $ProjectModuleRoot).BaseName
}

if (-not (Test-Path "$ProjectModuleRoot\$ProjectName.psd1")) {
    throw "Manifest file '$ProjectModuleRoot\$ProjectName.psd1' does not exist."
}

$ChangeLog = @"
# Release $(Get-VersionFromGit) Notes
 
$(@(& "$PSScriptRoot\Generate-Changelog.ps1" -Nolinks) -join "`n")
"@


# Test if repo exists on GitHub
try {
    Invoke-WebRequest `
        -Uri "https://github.com/$GitHubUserName/$ProjectName" `
        -Method Head `
        -ErrorAction Stop | Out-Null
}
catch {
    throw "Repository not found: https://github.com/$GitHubUserName/$ProjectName"
}


# Public functions
$FunctionsToExport = Get-ChildItem "$ProjectModuleRoot\Public\*.ps1" | ForEach-Object { $_.BaseName }
$GitRelease = Get-VersionFromGit
$IsPrerelease = $GitRelease -match '-rc|-beta|-alpha'
$GitModuleVersion = $GitRelease -replace '-rc|-beta|-alpha|-test', ''
$ModuleVersion = if ($ModuleVersion) { $ModuleVersion } else { $GitModuleVersion }
$ActualModuleVersion = Import-PowerShellDataFile -Path $ProjectModuleRoot\$ProjectName.psd1 | Select-Object -ExpandProperty ModuleVersion

if ($ModuleVersion -eq $ActualModuleVersion) {
    Write-Warning "Module version is already set to $ModuleVersion."
}

if ($GitModuleVersion -ne $ModuleVersion) {
    Write-Warning "Git tag version '$GitModuleVersion' does not match the specified module version '$ModuleVersion'."
}

Write-Host "Updating module manifest for $ProjectName.psd1 with version $ModuleVersion and changelog:" -ForegroundColor Blue
Update-ModuleManifest `
    -Path "$ProjectModuleRoot\$ProjectName.psd1" `
    -FunctionsToExport @($FunctionsToExport) `
    -AliasesToExport ((Get-Content "$ProjectModuleRoot\$ProjectName.psm1" | Select-String -Pattern 'Set-Alias' | ForEach-Object { "$(($_ -split "\s")[2])" })) `
    -ModuleVersion $ModuleVersion `
    -LicenseUri "https://raw.githubusercontent.com/$GitHubUserName/$ProjectName/refs/heads/main/LICENSE" `
    -ProjectUri "https://raw.githubusercontent.com/$GitHubUserName/$ProjectName/refs/heads/main/README.md" `
    -Copyright '© 2026 Spartaco Amadei. All rights reserved.' `
    -Description 'An alias manager for Hyper-V credentials and some additional features.' `
    -Author 'Spartaco Amadei' `
    -RootModule "$ProjectName.psm1" `
    #-ReleaseNotes $ChangeLog


if ($WithCommit) {
    # update the release to new manifest
    git add "$ProjectModuleRoot\$ProjectName.psd1"
    if ($LASTEXITCODE -ne 0) {
        throw "Git add command failed. Ensure you have git installed and the repository is initialized."
    }

    git commit -S -m "refactor: update to version v$ModuleVersion"
    
    if ($LASTEXITCODE -ne 0) {
        throw "Git commit command failed. Ensure you have git installed and the repository is initialized."
    }

    git tag -f "$ModuleVersion"
    if ($LASTEXITCODE -ne 0) {
        throw "Git tag command failed. Ensure you have git installed and the repository is initialized."
    }
}