build/build.ps1

# Start-Transcript -Path ".dev/Transcript.log" -Verbose
$moduleFolderPath = (Get-Item $PSScriptRoot).Parent.FullName
Write-Output "PSScriptRoot: $moduleFolderPath"
$modulePath = $moduleFolderPath

function sync-git{
    Write-Output "Syncing with git repo"
    Push-Location $moduleFolderPath
    git add .
    git commit -m 'updates'
    git push
    git push upstream
    Pop-Location
}
sync-git

# $newVersion = ""
Write-Output "Updating version information in psd1 file"
function Update-RqtVersion{
    $manifestPath = "$modulePath/RallyQuickToolsCli.psd1"
    $content = Get-Content $manifestPath -Raw
    $version = (
        $content `
        | Select-String "(?<=ModuleVersion = ').*(?=')"
    ).Matches.Value.Split(".")
    $newVersion = $version
    $newVersion[2] = [int]$version[2] + 1
    $newVersion = $newVersion -join "."
    $newContent = $content `
        -Replace "(?<=ModuleVersion = ').*(?=')", $newVersion
    Set-Content -Path $manifestPath -Value $newContent

    # UPDATE Package.nuspec version number
    $nuspec = [Xml](Get-Content -Raw "$moduleFolderPath/RallyQuickToolsCli.nuspec")
    $nuspec.package.metadata.version = "$newVersion"
    $nuspec.Save("$moduleFolderPath/RallyQuickToolsCli.nuspec")
    return $newVersion
}
$version = Update-RqtVersion
# $version = $newVersion
Write-Output "Version = $version"


function New-NugetPackage{
    Write-Output "Creating Nuget Package in $moduleFolderPath/nupkg/"
    Push-Location $moduleFolderPath
    nuget pack -OutputDirectory nupkg/ #-ConfigFile nuget.config
    Pop-Location
}
New-NugetPackage

# Write-Output "Publish to local nuget directory"
# Publish-Module -Path "$modulePath" -Repository TestNugetFeed -Verbose -NuGetApiKey "any_random_text"

Write-Output "Publish to PSGallery"
$nugetKey = [Environment]::GetEnvironmentVariable("PS_GALLERY_KEY", "User")
Publish-Module -Path "$modulePath" -Verbose -NuGetApiKey "$nugetKey" -Repository PSGallery

# Write-Output "Publish to Nuget.org"
# $nugetKey = [Environment]::GetEnvironmentVariable("MY_NUGET_KEY", "User")
# $packagePath = "$modulePath/nupkg/RallyQuickToolsCli.$version.nupkg"
# Write-Output "Package Path: $packagePath"
# Write-Output "Package exists: $(Test-Path $packagePath)"
# nuget push $packagePath -Source "https://api.nuget.org/v3/index.json" -Verbosity detailed -ApiKey "$nugetKey"

# Write-Output "Publish to Vertafore SRE"
# $nugetKey = Get-Secret -Name vnuget_token -Vault "Vertafore" -AsPlainText
# Publish-Module -Path "$modulePath" -Verbose -NuGetApiKey "$nugetKey" -Repository "$vnuget_sre_name"

# [Register-PSRepository (PowerShellGet) - PowerShell](https://docs.microsoft.com/en-us/powershell/module/powershellget/register-psrepository?view=powershell-7.2)

# Get-PSRepository
# Update-Module RallyQuickToolsCli
# Stop-Transcript