Last active
December 10, 2024 11:59
-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
This script publishes a new release of Dev Proxy to the Windows Package Manager (winget). | |
.DESCRIPTION | |
This script creates a new release folder and manifest files for the new version. 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 NewVersion | |
The version number of the new release in semver format. | |
.EXAMPLE | |
& '<path-to-script>/Publish-DevProxyToWinget.ps1' -NewVersion 0.23.0 | |
This example creates a new release of Dev Proxy. | |
.EXAMPLE | |
& '<path-to-script>/Publish-DevProxyToWinget.ps1' -NewVersion 0.20.0-beta.1 | |
This example creates a new beta release of Dev Proxy. | |
.NOTES | |
Ensure that the script is executed from the root of the winget-pkgs repository. | |
Ensure that the GitHub CLI is installed and is authenticated with GitHub before running this script. | |
#> | |
param ( | |
[Parameter(Mandatory=$true, HelpMessage="Enter the new version number in the format Major.Minor.Patch[-beta.X].")] | |
[ValidatePattern('^(\d+)\.(\d+)\.(\d+)(-beta\.\d+)?$')] | |
[string]$NewVersion | |
) | |
# Constants | |
$GITHUB_RELEASE_URL = "https://github.com/microsoft/dev-proxy/releases/download/v" | |
$MANIFEST_SCHEMA_URL = "https://aka.ms/winget-manifest" | |
# Function to check if GitHub CLI is installed | |
function Check-GitHubCLI { | |
try { | |
Get-Command gh -ErrorAction Stop | |
} catch { | |
Write-Host "GitHub CLI ('gh') is not installed. Please install it to continue." | |
exit | |
} | |
} | |
# Function to get the installer hash | |
function Get-InstallerHash { | |
param ( | |
[string]$version | |
) | |
$url = "${GITHUB_RELEASE_URL}${version}/dev-proxy-installer-win-x64-v${version}.exe" | |
$response = Invoke-WebRequest -Uri $url -Method Get -UseBasicParsing | |
$sha256 = [System.Security.Cryptography.SHA256]::Create() | |
$hashBytes = $sha256.ComputeHash($response.Content) | |
$hashString = -join $hashBytes.ForEach({ $_.ToString("x2") }) | |
$sha256.Dispose() | |
return $hashString | |
} | |
# Function to create manifest content | |
function Create-ManifestContent { | |
param ( | |
[string]$packageIdentifier, | |
[string]$newVersion, | |
[string]$packageName | |
) | |
return @" | |
# yaml-language-server: `$schema=${MANIFEST_SCHEMA_URL}.installer.1.6.0.schema.json | |
PackageIdentifier: $packageIdentifier | |
PackageVersion: $newVersion | |
InstallerType: inno | |
Installers: | |
- InstallerUrl: ${GITHUB_RELEASE_URL}$newVersion/dev-proxy-installer-win-x64-v$newVersion.exe | |
Architecture: x64 | |
InstallerSha256: $(Get-InstallerHash -version $newVersion) | |
ManifestType: installer | |
ManifestVersion: 1.6.0 | |
"@, @" | |
# yaml-language-server: `$schema=${MANIFEST_SCHEMA_URL}.defaultLocale.1.6.0.schema.json | |
PackageIdentifier: $packageIdentifier | |
PackageVersion: $newVersion | |
PackageLocale: en-US | |
Publisher: Microsoft | |
PackageName: $packageName | |
License: MIT License | |
ShortDescription: $packageName Installer | |
ManifestType: defaultLocale | |
ManifestVersion: 1.6.0 | |
"@, @" | |
# yaml-language-server: `$schema=${MANIFEST_SCHEMA_URL}.version.1.6.0.schema.json | |
PackageIdentifier: $packageIdentifier | |
PackageVersion: $newVersion | |
DefaultLocale: en-US | |
ManifestType: version | |
ManifestVersion: 1.6.0 | |
"@ | |
} | |
# Function to create a new branch | |
function Create-NewBranch { | |
param ( | |
[string]$branchName | |
) | |
git checkout master | |
git pull --rebase upstream master | |
git checkout -b $branchName | |
} | |
# Function to create a new version folder | |
function Create-NewVersionFolder { | |
param ( | |
[string]$path | |
) | |
if (-Not (Test-Path -Path $path)) { | |
New-Item -ItemType Directory -Path $path -Force | |
} | |
} | |
# Function to write manifest files | |
function Write-ManifestFiles { | |
param ( | |
[string]$path, | |
[string]$packageIdentifier, | |
[string]$installerContent, | |
[string]$localeContent, | |
[string]$versionContent | |
) | |
$installerContent | Set-Content -Path "${path}\$packageIdentifier.installer.yaml" | |
$localeContent | Set-Content -Path "${path}\$packageIdentifier.locale.en-US.yaml" | |
$versionContent | Set-Content -Path "${path}\$packageIdentifier.yaml" | |
} | |
# Function to commit and push changes | |
function Commit-And-Push { | |
param ( | |
[string]$commitMessage | |
) | |
git add . | |
git commit -m $commitMessage | |
git push origin | |
} | |
# Function to create a pull request | |
function Create-PullRequest { | |
param ( | |
[string]$title, | |
[string]$bodyFile | |
) | |
gh pr create --title $title --body-file $bodyFile | |
} | |
Check-GitHubCLI | |
# Create a new branch for the new version | |
$branchName = "microsoft-devproxy-" + $NewVersion.replace(".", "-") | |
Create-NewBranch -branchName $branchName | |
# Determine the release folder, package identifier, and package name based on the version | |
$isBeta = $NewVersion -like "*beta*" | |
$releaseFolder = if ($isBeta) { "DevProxy\Beta" } else { "DevProxy" } | |
$packageIdentifier = if ($isBeta) { "Microsoft.DevProxy.Beta" } else { "Microsoft.DevProxy" } | |
$packageName = if ($isBeta) { "Dev Proxy Beta" } else { "Dev Proxy" } | |
# Create the new version folder if it doesn't exist | |
$newPath = ".\manifests\m\Microsoft\${releaseFolder}\${NewVersion}\" | |
Create-NewVersionFolder -path $newPath | |
# Create manifest content | |
$installerManifestContent, $installerLocaleContent, $versionManifestContent = Create-ManifestContent ` | |
-packageIdentifier $packageIdentifier ` | |
-newVersion $NewVersion ` | |
-packageName $packageName | |
# Write the content to the new version files | |
Write-ManifestFiles ` | |
-path $newPath ` | |
-packageIdentifier $packageIdentifier ` | |
-installerContent $installerManifestContent ` | |
-localeContent $installerLocaleContent ` | |
-versionContent $versionManifestContent | |
# Add and commit the new files | |
Commit-And-Push -commitMessage "New version: Microsoft.DevProxy version $NewVersion" | |
# Create a pull request using the pull request template | |
Create-PullRequest ` | |
-title "New version: Microsoft.DevProxy version $NewVersion" ` | |
-bodyFile "./.github/PULL_REQUEST_TEMPLATE.md" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment