.dev/deploy.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
}

# $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
}

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

function Publish-Local(){
    Write-Output "Publish to local nuget directory"
    Publish-Module -Path "$modulePath" -Repository TestNugetFeed -Verbose -NuGetApiKey "any_random_text"
}
function Publish-PSGallery(){
    Write-Output "Publish to PSGallery"
    $nugetKey = [Environment]::GetEnvironmentVariable("PS_GALLERY_KEY", "User")
    Publish-Module -Path "$modulePath" -Verbose -NuGetApiKey "$nugetKey" -Repository PSGallery
}
function Publish-NugetOrg(){
    Write-Output "Publish to Nuget.org"
    $nugetKey = [Environment]::GetEnvironmentVariable("MY_NUGET_KEY", "User")
    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" 
}

# Vert source paths are set in Vertafore.profile.ps1
function Publish-VSRENuget(){
    Write-Output "Publish to Vertafore SRE at $vnuget_sre"
    $nugetKey = [Environment]::GetEnvironmentVariable("VNUGET_TOKEN", "User")
    nuget push $packagePath -Source $vnuget_sre -Verbosity detailed -ApiKey $nugetKey 
}
function Publish-MyVert(){
    Write-Output "Publish to MY Vertafore SRE"
    $nugetKey = [Environment]::GetEnvironmentVariable("VNUGET_TOKEN", "User")
    nuget push $packagePath -Source $my_nuget -Verbosity detailed -ApiKey $nugetKey 
}

sync-git
$version = Update-RqtVersion
# $version = $newVersion
Write-Output "Version = $version"
New-NugetPackage
$packagePath = "$modulePath/nupkg/RallyQuickToolsCli.$version.nupkg"

# Publish-Local
Publish-PSGallery
# Publish-NugetOrg
# Publish-VSRENuget
# Publish-MyVert

# Stop-Transcript