#! /snap/bin/pwsh function Get-VersionFromBranch { $branch = git rev-parse --abbrev-ref HEAD $versions = $branch.Split('.') $major = "0" $minor = "0" $micro = "0" if ($versions.Count -gt 0) { $major = $versions[0] } if ($versions.Count -gt 1) { $minor = $versions[1] } if ($versions.Count -gt 2) { $micro = $versions[2] } return $major, $minor, $micro } function Get-VersionAsJSON([string]$major, [string]$minor, [string]$micro) { return @{ "Major" = "$major" "Minor" = "$minor" "Micro" = "$micro" } | ConvertTo-Json } function Get-AppsettingsAsJSON() { return Get-Content -Raw -Path "./appsettings.json" | ConvertFrom-Json } function Set-AppsettingsAsJSON($settings) { $settings | ConvertTo-Json | Out-File "./appsettings.json" } function Set-VersionFromBranch([string]$major, [string]$minor, [string]$micro) { $versionJSON = Get-VersionAsJSON $major $minor $micro $appsettings = Get-AppsettingsAsJSON $appsettings.API.ApiVersion.Major = $major $appsettings.API.ApiVersion.Minor = $minor $appsettings.API.ApiVersion.Micro = $micro Set-AppsettingsAsJSON $appsettings } $res = Get-VersionFromBranch Set-VersionFromBranch $res[0] $res[1] $res[2]