Skip to content

Commit 4d04ed3

Browse files
authored
Merge pull request #30 from bostrot/feat_update_all_cmd
Add feature: update all winget packages
2 parents 50fe8ab + 33a2e20 commit 4d04ed3

9 files changed

+149
-17
lines changed

Community.PowerToys.Run.Plugin.Winget.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Import Project="..\..\..\..\Version.props" />
33

44
<PropertyGroup>
5-
<TargetFramework>net8.0-windows</TargetFramework>
5+
<TargetFramework>net8.0-windows8.0</TargetFramework>
66
<ProjectGuid>{46778CB7-A2FD-4949-A845-B19EF1A6364B}</ProjectGuid>
77
<AppDesignerFolder>Properties</AppDesignerFolder>
88
<RootNamespace>Community.PowerToys.Run.Plugin.Winget</RootNamespace>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.002.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Community.PowerToys.Run.Plugin.Winget", "Community.PowerToys.Run.Plugin.Winget.csproj", "{22FEFF75-335F-4995-8290-1219ED7E09BE}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{22FEFF75-335F-4995-8290-1219ED7E09BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{22FEFF75-335F-4995-8290-1219ED7E09BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{22FEFF75-335F-4995-8290-1219ED7E09BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{22FEFF75-335F-4995-8290-1219ED7E09BE}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {D2F57221-2395-4F5E-8FB5-C0F2084D7E0E}
24+
EndGlobalSection
25+
EndGlobal

Main.cs

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public partial class Main : IPlugin, IPluginI18n, IContextMenu, ISettingProvider
4646

4747
public static string PluginID => "46778CB7A2FD4949A845B19EF1A6364B";
4848

49+
private static string waitStr = "--wait";
50+
private static string forceStr = "-force";
51+
52+
private static string upgradeAllText = string.Format(
53+
CultureInfo.CurrentCulture,
54+
Properties.Resources.plugin_winget_upgrade_all_text,
55+
Properties.Resources.plugin_winget_cmd_upgrade_all);
56+
4957
public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
5058
{
5159
new PluginAdditionalOption()
@@ -133,19 +141,32 @@ public List<Result> Query(Query query)
133141
// empty query
134142
if (string.IsNullOrEmpty(query.Search))
135143
{
136-
string arguments = "winget ";
137144
results.Add(new Result
138145
{
139146
Title = Properties.Resources.plugin_description,
140-
SubTitle = "via winget CLI",
147+
SubTitle = Properties.Resources.plugin_via_winget_cli,
148+
QueryTextDisplay = string.Empty,
149+
IcoPath = _iconPath,
150+
ProgramArguments = Properties.Resources.plugin_winget_start_cmd,
151+
Action = action =>
152+
{
153+
return true;
154+
},
155+
});
156+
results.Add(new Result
157+
{
158+
Title = upgradeAllText,
159+
SubTitle = Properties.Resources.plugin_via_winget_cli,
141160
QueryTextDisplay = string.Empty,
142161
IcoPath = _iconPath,
143-
ProgramArguments = arguments,
162+
ProgramArguments = Properties.Resources.plugin_winget_start_cmd,
144163
Action = action =>
145164
{
165+
Winget(Properties.Resources.plugin_winget_cmd_upgrade_all, asAdmin: true);
146166
return true;
147167
},
148168
});
169+
149170
return results;
150171
}
151172
else
@@ -166,7 +187,7 @@ public List<Result> Query(Query query)
166187
ProgramArguments = idStr,
167188
Action = action =>
168189
{
169-
Winget($"install {idStr} --wait");
190+
Winget($"install {idStr} {waitStr}");
170191
return true;
171192
},
172193
});
@@ -198,22 +219,34 @@ public void Init(PluginInitContext context)
198219
LoadInstalledList();
199220
}
200221

201-
public static void Winget(string cmd)
222+
public static void Winget(string cmd, bool asAdmin = false)
202223
{
203-
// Call thread
204-
Thread thread = new Thread(() => WingetCmdThread(cmd));
224+
Thread thread = new Thread(() => WingetCmdThread(cmd, asAdmin));
205225
thread.Start();
206226
}
207227

208-
public static void WingetCmdThread(string cmd)
228+
public static void WingetCmdThread(string cmd, bool asAdmin)
209229
{
210230
Process process = new Process();
211231

212232
process.StartInfo.FileName = "winget";
213233
process.StartInfo.Arguments = cmd;
214234
process.StartInfo.UseShellExecute = true;
215235
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
216-
process.Start();
236+
if (asAdmin)
237+
{
238+
process.StartInfo.Verb = "runas";
239+
}
240+
241+
try
242+
{
243+
process.Start();
244+
}
245+
catch (Exception)
246+
{
247+
Console.WriteLine("User aborted UAC dialog");
248+
return;
249+
}
217250

218251
// Wait for process to exit
219252
process.WaitForExit();
@@ -222,7 +255,7 @@ public static void WingetCmdThread(string cmd)
222255

223256
private static List<ContextMenuResult> GetContextMenu(in Result result, in string assemblyName)
224257
{
225-
if (result?.Title == Properties.Resources.plugin_description)
258+
if (result?.Title == Properties.Resources.plugin_description || result?.Title == upgradeAllText)
226259
{
227260
return new List<ContextMenuResult>(0);
228261
}
@@ -238,7 +271,7 @@ private static List<ContextMenuResult> GetContextMenu(in Result result, in strin
238271
AcceleratorModifiers = ModifierKeys.Control,
239272
Action = _ =>
240273
{
241-
Winget("install " + idStr + " -i --force --wait");
274+
Winget($"install {idStr} -i {forceStr} {waitStr}");
242275
return true;
243276
},
244277
FontFamily = "Segoe MDL2 Assets",
@@ -256,7 +289,7 @@ private static List<ContextMenuResult> GetContextMenu(in Result result, in strin
256289
AcceleratorModifiers = ModifierKeys.Control,
257290
Action = _ =>
258291
{
259-
Winget("upgrade " + idStr + " --wait");
292+
Winget($"upgrade {idStr} {waitStr}");
260293
return true;
261294
},
262295
FontFamily = "Segoe MDL2 Assets",
@@ -270,7 +303,7 @@ private static List<ContextMenuResult> GetContextMenu(in Result result, in strin
270303
AcceleratorModifiers = ModifierKeys.Control,
271304
Action = _ =>
272305
{
273-
Winget("uninstall " + idStr + " --wait");
306+
Winget($"uninstall {idStr} {waitStr}");
274307
return true;
275308
},
276309
FontFamily = "Segoe MDL2 Assets",

Properties/Resources.Designer.cs

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Properties/Resources.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,16 @@
145145
<data name="plugin_search_failed" xml:space="preserve">
146146
<value>Failed to open {0}.</value>
147147
</data>
148+
<data name="plugin_via_winget_cli" xml:space="preserve">
149+
<value>via winget CLI</value>
150+
</data>
151+
<data name="plugin_winget_cmd_upgrade_all" xml:space="preserve">
152+
<value>update --all --source winget</value>
153+
</data>
154+
<data name="plugin_winget_start_cmd" xml:space="preserve">
155+
<value>winget </value>
156+
</data>
157+
<data name="plugin_winget_upgrade_all_text" xml:space="preserve">
158+
<value>Upgrade all winget packages ({0})</value>
159+
</data>
148160
</root>

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ This is a plugin for [PowerToys Run](https://github.com/microsoft/PowerToys/wiki
1212

1313
## Installation
1414

15+
Use the installation script `release.ps1`:
16+
17+
```powershell
18+
.\release.ps1
19+
```
20+
21+
Then restart PowerToys
22+
23+
**Or manually:**
24+
1525
1. Download the latest release of the Winget Plugin from the [releases page](https://github.com/bostrot/PowerToysRunPluginWinget/releases).
1626
2. Extract the zip file's contents to your PowerToys modules directory (usually `%LOCALAPPDATA%\Microsoft\PowerToys\RunPlugins`).
1727
3. Restart PowerToys.

installer.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# PowerShell script that downloads latest release from GitHub and installs it to PowerToys Run plugin folder
22
# Usage: .\installer.ps1
33

4-
$installLocation = "%LOCALAPPDATA%\PowerToys\RunPlugins\Winget"
4+
$installLocation = "$env:LOCALAPPDATA\Microsoft\PowerToys\PowerToys Run\Plugins\Winget"
55

66
# Get latest release from GitHub
77
$latestRelease = Invoke-RestMethod -Uri "https://api.github.com/repos/bostrot/PowerToysRunPluginWinget/releases/latest"

installer_local.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# PowerShell script that downloads latest release from GitHub and installs it to PowerToys Run plugin folder
2+
# Usage: .\installer.ps1
3+
4+
5+
$version = "1.3.0"
6+
$release = "C:\Users\erict\Downloads\winget-powertoys-$version.zip"
7+
$installLocation = "$env:LOCALAPPDATA\PowerToys\RunPlugins\Winget"
8+
9+
# Unzip latest release
10+
Expand-Archive -Path $release -DestinationPath "$installLocation.tmp"
11+
12+
# Move files to plugin folder
13+
Move-Item "$installLocation.tmp\Winget" $installLocation
14+
15+
# Remove temporary folder
16+
Remove-Item "$installLocation.tmp" -Recurse

release.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Script for copying the release files and creating the release zip file
22
# C:\Users\erict\PowerToys-dev\x64\Release\RunPlugins\Winget
3-
$version = "1.2.3"
3+
$version = "1.3.0"
44
$release = "C:\Users\erict\Downloads\winget-powertoys-$version.zip"
55
$zip = "C:\Program Files\7-Zip\7z.exe"
6-
$path = "C:\Users\erict\PowerToys-dev\x64\Release\RunPlugins\Winget"
6+
$path = "D:\erict\PowerToys-dev\x64\Release\RunPlugins\Winget"
77

88
# pack the files from path and excluding
99
&$zip a -aoa -bb0 -bso0 -xr!PowerToys* -xr!Backup* -xr!Ijwhost* -tzip $release $path

0 commit comments

Comments
 (0)