Skip to content

Instantly share code, notes, and snippets.

@garrytrinder
Last active July 9, 2024 14:15
Show Gist options
  • Save garrytrinder/2f86c4a692d1b34077ccc9758d9ffaec to your computer and use it in GitHub Desktop.
Save garrytrinder/2f86c4a692d1b34077ccc9758d9ffaec to your computer and use it in GitHub Desktop.
This script publishes a new release of Dev Proxy to the Windows Package Manager (winget).
<#
.SYNOPSIS
This script publishes a new release of Dev Proxy to the Windows Package Manager (winget).
.DESCRIPTION
This script updates the manifest files for Dev Proxy to create a new release. It copies the old version files to the new version folder, replaces the old version number with the new version number in the files, and updates the installer hash in the Microsoft.DevProxy.installer.yaml file. The script then creates a new branch, commits the changes, and pushes the branch to the remote repository. Finally, the script creates a pull request with the new version information.
.PARAMETER OldVersion
The version number of the current release in semver format.
.PARAMETER NewVersion
The version number of the new release in semver format.
.EXAMPLE
.\Publish-DevProxyToWinget.ps1 -OldVersion 0.19.0 -NewVersion 0.20.0
This example creates a new release of Dev Proxy.
.EXAMPLE
.\Publish-DevProxyToWinget.ps1 -OldVersion 0.19.0-beta.5 -NewVersion 0.20.0-beta.1
This example creates a new beta release of Dev Proxy.
.NOTES
Ensure that the GitHub CLI is installed and is authenticated with GitHub before running this script.
#>
param (
[Parameter(Mandatory=$true, HelpMessage="Enter the old version number in the format Major.Minor.Patch[-beta.X].")]
[ValidatePattern('^(\d+)\.(\d+)\.(\d+)(-beta\.\d+)?$')]
[string]$OldVersion,
[Parameter(Mandatory=$true, HelpMessage="Enter the old version number in the format Major.Minor.Patch[-beta.X].")]
[ValidatePattern('^(\d+)\.(\d+)\.(\d+)(-beta\.\d+)?$')]
[string]$NewVersion
)
# check if the GitHub CLI is installed
try {
# Attempt to get the GitHub CLI command
$ghCommand = Get-Command gh -ErrorAction Stop
} catch {
# If the command is not found, print a message and exit
Write-Host "GitHub CLI ('gh') is not installed. Please install it to continue."
exit
}
$versionForBranchName = $newVersion.replace(".", "-")
git checkout master
git pull --rebase upstream master
git checkout -b microsoft-devproxy-$versionForBranchName
$isBeta = $oldVersion -like "*beta*" || $newVersion -like "*beta*"
$releaseFolder = if ($isBeta -eq $true) { "DevProxy\Beta" } else { "DevProxy" }
$releaseFolder
$oldPath = ".\manifests\m\Microsoft\${releaseFolder}\${oldVersion}\"
$newPath = ".\manifests\m\Microsoft\${releaseFolder}\${newVersion}\"
# Copy the old version file to the new version folder
Copy-Item -Path $oldPath -Destination $newPath -Recurse
# Iterate over all files, replace the old version with the new version
Get-ChildItem -Path $newPath -Recurse | ForEach-Object {
$file = $_
$content = Get-Content -Path $file.FullName
$content -replace $oldVersion, $newVersion | Set-Content -Path $file.FullName
}
# Get the value of the InstallerSha256 property from the Microsoft.DevProxy.installer.yaml file in the old path
# The following YAML represents an example of the file content
# PackageIdentifier: Microsoft.DevProxy
# PackageVersion: 0.19.0
# InstallerType: inno
# Installers:
# - InstallerUrl: https://github.com/microsoft/dev-proxy/releases/download/v0.19.0/dev-proxy-installer-win-x64-v0.19.0.exe
# Architecture: x64
# InstallerSha256: 80973E42EAA05009A83A87D7CB2209996E56A939A1F64D7621E764E9E92E88B6
# ManifestType: installer
# ManifestVersion: 1.6.0
$installerFile = if ($isBeta -eq $true) { "Microsoft.DevProxy.Beta.installer.yaml" } else { "Microsoft.DevProxy.installer.yaml" }
$oldInstallerSha256 = (Get-Content -Path "${oldPath}\${installerFile}" | Select-String -Pattern "InstallerSha256: (.*)").Matches.Groups[1].Value
# Get file hash of the new installer from the release ZIP
$url = "https://github.com/microsoft/dev-proxy/releases/download/v${newVersion}/dev-proxy-installer-win-x64-v${newVersion}.exe"
# Download the file content into memory
$response = Invoke-WebRequest -Uri $url -Method Get -UseBasicParsing
# Create a SHA256 hash algorithm object
$sha256 = [System.Security.Cryptography.SHA256]::Create()
# Compute the hash of the file content
$hashBytes = $sha256.ComputeHash($response.Content)
# Convert the byte array hash to a hexadecimal string
$hashString = -join $hashBytes.ForEach({ $_.ToString("x2") })
# Dispose of the SHA256 object
$sha256.Dispose()
# Replace the old hash with the new hash in the *.installer.yaml file in the new path
(Get-Content -Path "${newPath}\$installerFile") -replace $oldInstallerSha256, $hashString.toUpper() | Set-Content -Path "${newPath}\$installerFile"
git add .
git commit -m "New version: Microsoft.DevProxy version $newVersion"
git push origin
gh pr create --title "New version: Microsoft.DevProxy version $newVersion" --body-file .\.github\PULL_REQUEST_TEMPLATE.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment