Skip to content

Commit b298dfa

Browse files
authored
perf: optimize for when no work to do
1 parent a4861d6 commit b298dfa

1 file changed

Lines changed: 138 additions & 130 deletions

File tree

check.ps1

Lines changed: 138 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,21 @@ function Invoke-NativeCommand() {
3636
}
3737
}
3838

39-
# Load up code for getting product codes from an MSI file.
40-
$msiTools = Add-Type -PassThru -Name 'MsiTools' -Using 'System.Text' -MemberDefinition $(Get-Content "MsiTools.cs")
41-
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
42-
Install-Module -Name powershell-yaml -AcceptLicense
43-
Import-Module powershell-yaml
44-
Invoke-NativeCommand gh auth setup-git
45-
Invoke-NativeCommand git config --global user.email "kieseljake+rust-winget-bot@live.com"
46-
Invoke-NativeCommand git config --global user.name "Rust-Winget-Bot"
47-
Invoke-NativeCommand gh repo sync "Rust-Winget-Bot/winget-pkgs"
48-
Invoke-NativeCommand gh repo clone "Rust-Winget-Bot/winget-pkgs" "--" --depth 1
49-
Set-Location winget-pkgs
39+
function InitialSetup() {
40+
# Load up code for getting product codes from an MSI file.
41+
$Script:msiTools = Add-Type -PassThru -Name 'MsiTools' -Using 'System.Text' -MemberDefinition $(Get-Content "MsiTools.cs")
42+
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
43+
Install-Module -Name powershell-yaml -AcceptLicense
44+
Import-Module powershell-yaml
45+
Invoke-NativeCommand gh auth setup-git
46+
Invoke-NativeCommand git config --global user.email "kieseljake+rust-winget-bot@live.com"
47+
Invoke-NativeCommand git config --global user.name "Rust-Winget-Bot"
48+
Invoke-NativeCommand gh repo sync "Rust-Winget-Bot/winget-pkgs"
49+
Invoke-NativeCommand gh repo clone "Rust-Winget-Bot/winget-pkgs" "--" --depth 1
50+
Set-Location winget-pkgs
51+
}
52+
53+
5054
$yamlHeaderInstaller = @'
5155
# Auto-generated by Rust-Winget-Bot (https://github.com/Rust-Winget-Bot)
5256
# yaml-language-server: $schema=https://aka.ms/winget-manifest.installer.1.10.0.schema.json
@@ -65,141 +69,145 @@ $yamlHeaderVersion = @'
6569
6670
6771
'@
72+
$initialSetupExecuted = $false
6873
$lastFewVersions = Invoke-NativeCommand git ls-remote --sort=-v:refname --tags https://github.com/rust-lang/rust.git
6974
| Select-String -Pattern "refs/tags/(\d+?\.\d+?\.\d+?$)"
7075
| ForEach-Object { $_.Matches[0].Groups[1].Value }
7176
| Select-Object -First 3;
7277
$myPrs = Invoke-NativeCommand gh pr list --author "Rust-Winget-Bot" --repo "microsoft/winget-pkgs" --state=all
7378
| Foreach-Object {((($_ -split '\t')[2]) -split ':')[1]};
74-
foreach ($toolchain in @("MSVC", "GNU")) {
75-
$toolchainLower = $toolchain.ToLower();
76-
$publishedVersions = Get-ChildItem .\manifests\r\Rustlang\Rust\$toolchain
77-
| Foreach-Object {$_.Name}
78-
| Select-String -Pattern "^\d+?\.\d+?\.\d+$"
79-
| ForEach-Object { $_.Matches[0].Value }
80-
| Select-Object -Last 5
81-
foreach ($version in $lastFewVersions) {
79+
foreach ($version in $lastFewVersions) {
80+
foreach ($toolchain in @("MSVC", "GNU")) {
81+
$toolchainLower = $toolchain.ToLower();
82+
if ($myPrs -and $myPrs.Contains("rust-$version-$toolchainLower")) {
83+
continue;
84+
}
85+
if (-not $initialSetupExecuted) {
86+
InitalSetup
87+
$initialSetupExecuted = $true
88+
}
89+
$publishedVersions = Get-ChildItem .\manifests\r\Rustlang\Rust\$toolchain
90+
| Foreach-Object {$_.Name}
91+
| Select-String -Pattern "^\d+?\.\d+?\.\d+$"
92+
| ForEach-Object { $_.Matches[0].Value }
93+
| Select-Object -Last 5
8294
if ($publishedVersions.Contains($version)) {
8395
continue;
96+
}
97+
Write-Output "Creating branch for $version $toolchain"
98+
Invoke-NativeCommand git checkout master;
99+
Invoke-NativeCommand git checkout -b rust-$version-$toolchainLower;
100+
New-Item "manifests/r/Rustlang/Rust/$toolchain/$version/" -ItemType Directory -ea 0
101+
$yamlPath = "manifests/r/Rustlang/Rust/$toolchain/$version/Rustlang.Rust.$toolchain.installer.yaml";
102+
$yamlObject = [ordered]@{
103+
PackageIdentifier = "Rustlang.Rust.$toolchain";
104+
PackageVersion = $version;
105+
MinimumOSVersion = "10.0.0.0";
106+
InstallerType = "wix";
107+
UpgradeBehavior = "uninstallPrevious";
108+
Installers = @(); # To be filled later
109+
ManifestType = "installer";
110+
ManifestVersion = "1.10.0";
111+
};
112+
if ($toolchain -eq "MSVC") {
113+
$installers = @(
114+
"https://static.rust-lang.org/dist/rust-$version-aarch64-pc-windows-msvc.msi",
115+
"https://static.rust-lang.org/dist/rust-$version-i686-pc-windows-msvc.msi",
116+
"https://static.rust-lang.org/dist/rust-$version-x86_64-pc-windows-msvc.msi"
117+
);
84118
} else {
85-
if ($myPrs -and $myPrs.Contains("rust-$version-$toolchainLower")) {
119+
$installers = @(
120+
"https://static.rust-lang.org/dist/rust-$version-i686-pc-windows-gnu.msi",
121+
"https://static.rust-lang.org/dist/rust-$version-x86_64-pc-windows-gnu.msi"
122+
);
123+
}
124+
foreach ($installer in $installers) {
125+
$path = $installer.Substring($installer.LastIndexOf('/') + 1);
126+
Write-Output "Now downloading $path from $installer"
127+
Invoke-WebRequest -Uri $installer -Outfile $path
128+
if(!$?) {
129+
Write-Output "Failed to download file, skipping"
86130
continue;
87131
}
88-
Write-Output "Creating branch for $version $toolchain"
89-
Invoke-NativeCommand git checkout master;
90-
Invoke-NativeCommand git checkout -b rust-$version-$toolchainLower;
91-
New-Item "manifests/r/Rustlang/Rust/$toolchain/$version/" -ItemType Directory -ea 0
92-
$yamlPath = "manifests/r/Rustlang/Rust/$toolchain/$version/Rustlang.Rust.$toolchain.installer.yaml";
93-
$yamlObject = [ordered]@{
94-
PackageIdentifier = "Rustlang.Rust.$toolchain";
95-
PackageVersion = $version;
96-
MinimumOSVersion = "10.0.0.0";
97-
InstallerType = "wix";
98-
UpgradeBehavior = "uninstallPrevious";
99-
Installers = @(); # To be filled later
100-
ManifestType = "installer";
101-
ManifestVersion = "1.10.0";
102-
};
103-
if ($toolchain -eq "MSVC") {
104-
$installers = @(
105-
"https://static.rust-lang.org/dist/rust-$version-aarch64-pc-windows-msvc.msi",
106-
"https://static.rust-lang.org/dist/rust-$version-i686-pc-windows-msvc.msi",
107-
"https://static.rust-lang.org/dist/rust-$version-x86_64-pc-windows-msvc.msi"
108-
);
109-
} else {
110-
$installers = @(
111-
"https://static.rust-lang.org/dist/rust-$version-i686-pc-windows-gnu.msi",
112-
"https://static.rust-lang.org/dist/rust-$version-x86_64-pc-windows-gnu.msi"
113-
);
132+
$sha256 = (Get-FileHash $path -Algorithm SHA256).Hash;
133+
Remove-Item $path;
134+
Invoke-WebRequest -Uri $installer -Outfile $path
135+
if(!$?) {
136+
Write-Output "Failed to download file, skipping"
137+
continue;
138+
}
139+
$sha256_2 = (Get-FileHash $path -Algorithm SHA256).Hash;
140+
if (-not($sha256 -eq $sha256_2)) {
141+
throw "Sha256 returned two different results, shutting down to lack of confidence in sha value"
114142
}
115-
foreach ($installer in $installers) {
116-
$path = $installer.Substring($installer.LastIndexOf('/') + 1);
117-
Write-Output "Now downloading $path from $installer"
118-
Invoke-WebRequest -Uri $installer -Outfile $path
119-
if(!$?) {
120-
Write-Output "Failed to download file, skipping"
121-
continue;
122-
}
123-
$sha256 = (Get-FileHash $path -Algorithm SHA256).Hash;
124-
Remove-Item $path;
125-
Invoke-WebRequest -Uri $installer -Outfile $path
126-
if(!$?) {
127-
Write-Output "Failed to download file, skipping"
128-
continue;
129-
}
130-
$sha256_2 = (Get-FileHash $path -Algorithm SHA256).Hash;
131-
if (-not($sha256 -eq $sha256_2)) {
132-
throw "Sha256 returned two different results, shutting down to lack of confidence in sha value"
133-
}
134-
$absolutePath = Resolve-Path $path;
135-
$productCode = $msiTools::GetProductCode($absolutePath)
136-
$productName = $msiTools::GetProductName($absolutePath);
137-
Remove-Item $path;
138-
$arch = if ($installer.Contains("i686")) {
139-
"x86"
140-
} elseif ($installer.Contains("x86_64")) {
141-
"x64"
142-
} elseif ($installer.Contains("aarch64")) {
143-
"arm64"
144-
}
145-
146-
$appsAndFeaturesEntry = [ordered]@{
147-
DisplayName = $productName;
148-
ProductCode = $productCode;
149-
};
150-
$installerEntry = [ordered]@{
151-
Architecture = $arch;
152-
InstallerUrl = $installer;
153-
InstallerSha256 = $sha256;
154-
ProductCode = $productCode;
155-
AppsAndFeaturesEntries = @($appsAndFeaturesEntry);
156-
};
157-
$yamlObject.Installers += $installerEntry
143+
$absolutePath = Resolve-Path $path;
144+
$productCode = $Script:msiTools::GetProductCode($absolutePath)
145+
$productName = $Script:msiTools::GetProductName($absolutePath);
146+
Remove-Item $path;
147+
$arch = if ($installer.Contains("i686")) {
148+
"x86"
149+
} elseif ($installer.Contains("x86_64")) {
150+
"x64"
151+
} elseif ($installer.Contains("aarch64")) {
152+
"arm64"
158153
}
159-
$newYamlData = -join($yamlHeaderInstaller, (ConvertTo-YAML $yamlObject));
160-
Set-Content -Path $yamlPath -Value $newYamlData;
161-
$yamlPath = "manifests/r/Rustlang/Rust/$toolchain/$version/Rustlang.Rust.$toolchain.locale.en-US.yaml";
162-
$yamlObject = [ordered]@{
163-
PackageIdentifier = "Rustlang.Rust.$toolchain";
164-
PackageVersion = $version;
165-
PackageLocale = "en-US";
166-
Publisher = "The Rust Project Developers";
167-
PublisherUrl = "https://github.com/rust-lang/rust";
168-
PublisherSupportUrl = "https://github.com/rust-lang/rust/issues";
169-
Author = "The Rust Project Developers";
170-
PackageName = "Rust ($toolchain)";
171-
PackageUrl = "https://www.rust-lang.org/";
172-
License = "Apache 2.0 and MIT";
173-
LicenseUrl = "https://raw.githubusercontent.com/rust-lang/rust/master/COPYRIGHT";
174-
CopyrightUrl = "https://raw.githubusercontent.com/rust-lang/rust/master/COPYRIGHT";
175-
ShortDescription = "this is the rust-lang built with $toolchainLower toolchain";
176-
Moniker = "rust-$toolchainLower";
177-
Tags = @($toolchainLower, "rust", "windows");
178-
ManifestType = "defaultLocale";
179-
ManifestVersion = "1.10.0";
154+
155+
$appsAndFeaturesEntry = [ordered]@{
156+
DisplayName = $productName;
157+
ProductCode = $productCode;
180158
};
181-
$newYamlData = -join($yamlHeaderDefaultLocale, (ConvertTo-YAML $yamlObject));
182-
Set-Content -Path $yamlPath -Value $newYamlData;
183-
$yamlPath = "manifests/r/Rustlang/Rust/$toolchain/$version/Rustlang.Rust.$toolchain.yaml";
184-
$yamlObject = [ordered]@{
185-
PackageIdentifier = "Rustlang.Rust.$toolchain";
186-
PackageVersion = $version;
187-
DefaultLocale = "en-US";
188-
ManifestType = "version";
189-
ManifestVersion = "1.10.0";
159+
$installerEntry = [ordered]@{
160+
Architecture = $arch;
161+
InstallerUrl = $installer;
162+
InstallerSha256 = $sha256;
163+
ProductCode = $productCode;
164+
AppsAndFeaturesEntries = @($appsAndFeaturesEntry);
190165
};
191-
$newYamlData = -join($yamlHeaderVersion, (ConvertTo-YAML $yamlObject));
192-
Set-Content -Path $yamlPath -Value $newYamlData;
193-
Invoke-NativeCommand git add --all .
194-
Invoke-NativeCommand git commit -m"add Rustlang.Rust.$toolchain version $version"
195-
Invoke-NativeCommand git push -u origin rust-$version-$toolchainLower
196-
# Give GitHub a moment to propagate the presence of this new branch
197-
sleep 5
198-
199-
$title = "add Rustlang.Rust.$toolchain version $version";
200-
$body = "This PR is auto-generated. If there's something wrong, please file an issue at https://github.com/Rust-Winget-Bot/my-source-code/issues";
201-
Invoke-NativeCommand gh pr create --title "$title" --body "$body" --repo microsoft/winget-pkgs --base master --head Rust-Winget-Bot:rust-$version-$toolchainLower
166+
$yamlObject.Installers += $installerEntry
202167
}
168+
$newYamlData = -join($yamlHeaderInstaller, (ConvertTo-YAML $yamlObject));
169+
Set-Content -Path $yamlPath -Value $newYamlData;
170+
$yamlPath = "manifests/r/Rustlang/Rust/$toolchain/$version/Rustlang.Rust.$toolchain.locale.en-US.yaml";
171+
$yamlObject = [ordered]@{
172+
PackageIdentifier = "Rustlang.Rust.$toolchain";
173+
PackageVersion = $version;
174+
PackageLocale = "en-US";
175+
Publisher = "The Rust Project Developers";
176+
PublisherUrl = "https://github.com/rust-lang/rust";
177+
PublisherSupportUrl = "https://github.com/rust-lang/rust/issues";
178+
Author = "The Rust Project Developers";
179+
PackageName = "Rust ($toolchain)";
180+
PackageUrl = "https://www.rust-lang.org/";
181+
License = "Apache 2.0 and MIT";
182+
LicenseUrl = "https://raw.githubusercontent.com/rust-lang/rust/master/COPYRIGHT";
183+
CopyrightUrl = "https://raw.githubusercontent.com/rust-lang/rust/master/COPYRIGHT";
184+
ShortDescription = "this is the rust-lang built with $toolchainLower toolchain";
185+
Moniker = "rust-$toolchainLower";
186+
Tags = @($toolchainLower, "rust", "windows");
187+
ManifestType = "defaultLocale";
188+
ManifestVersion = "1.10.0";
189+
};
190+
$newYamlData = -join($yamlHeaderDefaultLocale, (ConvertTo-YAML $yamlObject));
191+
Set-Content -Path $yamlPath -Value $newYamlData;
192+
$yamlPath = "manifests/r/Rustlang/Rust/$toolchain/$version/Rustlang.Rust.$toolchain.yaml";
193+
$yamlObject = [ordered]@{
194+
PackageIdentifier = "Rustlang.Rust.$toolchain";
195+
PackageVersion = $version;
196+
DefaultLocale = "en-US";
197+
ManifestType = "version";
198+
ManifestVersion = "1.10.0";
199+
};
200+
$newYamlData = -join($yamlHeaderVersion, (ConvertTo-YAML $yamlObject));
201+
Set-Content -Path $yamlPath -Value $newYamlData;
202+
Invoke-NativeCommand git add --all .
203+
Invoke-NativeCommand git commit -m"add Rustlang.Rust.$toolchain version $version"
204+
Invoke-NativeCommand git push -u origin rust-$version-$toolchainLower
205+
# Give GitHub a moment to propagate the presence of this new branch
206+
sleep 5
207+
208+
$title = "add Rustlang.Rust.$toolchain version $version";
209+
$body = "This PR is auto-generated. If there's something wrong, please file an issue at https://github.com/Rust-Winget-Bot/my-source-code/issues";
210+
Invoke-NativeCommand gh pr create --title "$title" --body "$body" --repo microsoft/winget-pkgs --base master --head Rust-Winget-Bot:rust-$version-$toolchainLower
203211
}
204212
}
205213
$closedPRs = Invoke-NativeCommand gh pr list --author "Rust-Winget-Bot" --repo "microsoft/winget-pkgs" --state=closed --limit 10

0 commit comments

Comments
 (0)