Skip to content

Commit

Permalink
Merge pull request #30 from bostrot/feat_update_all_cmd
Browse files Browse the repository at this point in the history
Add feature: update all winget packages
  • Loading branch information
bostrot authored May 30, 2024
2 parents 50fe8ab + 33a2e20 commit 4d04ed3
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Community.PowerToys.Run.Plugin.Winget.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Import Project="..\..\..\..\Version.props" />

<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net8.0-windows8.0</TargetFramework>
<ProjectGuid>{46778CB7-A2FD-4949-A845-B19EF1A6364B}</ProjectGuid>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Community.PowerToys.Run.Plugin.Winget</RootNamespace>
Expand Down
25 changes: 25 additions & 0 deletions Community.PowerToys.Run.Plugin.Winget.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Community.PowerToys.Run.Plugin.Winget", "Community.PowerToys.Run.Plugin.Winget.csproj", "{22FEFF75-335F-4995-8290-1219ED7E09BE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{22FEFF75-335F-4995-8290-1219ED7E09BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{22FEFF75-335F-4995-8290-1219ED7E09BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{22FEFF75-335F-4995-8290-1219ED7E09BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{22FEFF75-335F-4995-8290-1219ED7E09BE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D2F57221-2395-4F5E-8FB5-C0F2084D7E0E}
EndGlobalSection
EndGlobal
59 changes: 46 additions & 13 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public partial class Main : IPlugin, IPluginI18n, IContextMenu, ISettingProvider

public static string PluginID => "46778CB7A2FD4949A845B19EF1A6364B";

private static string waitStr = "--wait";
private static string forceStr = "-force";

private static string upgradeAllText = string.Format(
CultureInfo.CurrentCulture,
Properties.Resources.plugin_winget_upgrade_all_text,
Properties.Resources.plugin_winget_cmd_upgrade_all);

public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
{
new PluginAdditionalOption()
Expand Down Expand Up @@ -133,19 +141,32 @@ public List<Result> Query(Query query)
// empty query
if (string.IsNullOrEmpty(query.Search))
{
string arguments = "winget ";
results.Add(new Result
{
Title = Properties.Resources.plugin_description,
SubTitle = "via winget CLI",
SubTitle = Properties.Resources.plugin_via_winget_cli,
QueryTextDisplay = string.Empty,
IcoPath = _iconPath,
ProgramArguments = Properties.Resources.plugin_winget_start_cmd,
Action = action =>
{
return true;
},
});
results.Add(new Result
{
Title = upgradeAllText,
SubTitle = Properties.Resources.plugin_via_winget_cli,
QueryTextDisplay = string.Empty,
IcoPath = _iconPath,
ProgramArguments = arguments,
ProgramArguments = Properties.Resources.plugin_winget_start_cmd,
Action = action =>
{
Winget(Properties.Resources.plugin_winget_cmd_upgrade_all, asAdmin: true);
return true;
},
});

return results;
}
else
Expand All @@ -166,7 +187,7 @@ public List<Result> Query(Query query)
ProgramArguments = idStr,
Action = action =>
{
Winget($"install {idStr} --wait");
Winget($"install {idStr} {waitStr}");
return true;
},
});
Expand Down Expand Up @@ -198,22 +219,34 @@ public void Init(PluginInitContext context)
LoadInstalledList();
}

public static void Winget(string cmd)
public static void Winget(string cmd, bool asAdmin = false)
{
// Call thread
Thread thread = new Thread(() => WingetCmdThread(cmd));
Thread thread = new Thread(() => WingetCmdThread(cmd, asAdmin));
thread.Start();
}

public static void WingetCmdThread(string cmd)
public static void WingetCmdThread(string cmd, bool asAdmin)
{
Process process = new Process();

process.StartInfo.FileName = "winget";
process.StartInfo.Arguments = cmd;
process.StartInfo.UseShellExecute = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.Start();
if (asAdmin)
{
process.StartInfo.Verb = "runas";
}

try
{
process.Start();
}
catch (Exception)
{
Console.WriteLine("User aborted UAC dialog");
return;
}

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

private static List<ContextMenuResult> GetContextMenu(in Result result, in string assemblyName)
{
if (result?.Title == Properties.Resources.plugin_description)
if (result?.Title == Properties.Resources.plugin_description || result?.Title == upgradeAllText)
{
return new List<ContextMenuResult>(0);
}
Expand All @@ -238,7 +271,7 @@ private static List<ContextMenuResult> GetContextMenu(in Result result, in strin
AcceleratorModifiers = ModifierKeys.Control,
Action = _ =>
{
Winget("install " + idStr + " -i --force --wait");
Winget($"install {idStr} -i {forceStr} {waitStr}");
return true;
},
FontFamily = "Segoe MDL2 Assets",
Expand All @@ -256,7 +289,7 @@ private static List<ContextMenuResult> GetContextMenu(in Result result, in strin
AcceleratorModifiers = ModifierKeys.Control,
Action = _ =>
{
Winget("upgrade " + idStr + " --wait");
Winget($"upgrade {idStr} {waitStr}");
return true;
},
FontFamily = "Segoe MDL2 Assets",
Expand All @@ -270,7 +303,7 @@ private static List<ContextMenuResult> GetContextMenu(in Result result, in strin
AcceleratorModifiers = ModifierKeys.Control,
Action = _ =>
{
Winget("uninstall " + idStr + " --wait");
Winget($"uninstall {idStr} {waitStr}");
return true;
},
FontFamily = "Segoe MDL2 Assets",
Expand Down
36 changes: 36 additions & 0 deletions Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,16 @@
<data name="plugin_search_failed" xml:space="preserve">
<value>Failed to open {0}.</value>
</data>
<data name="plugin_via_winget_cli" xml:space="preserve">
<value>via winget CLI</value>
</data>
<data name="plugin_winget_cmd_upgrade_all" xml:space="preserve">
<value>update --all --source winget</value>
</data>
<data name="plugin_winget_start_cmd" xml:space="preserve">
<value>winget </value>
</data>
<data name="plugin_winget_upgrade_all_text" xml:space="preserve">
<value>Upgrade all winget packages ({0})</value>
</data>
</root>
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ This is a plugin for [PowerToys Run](https://github.com/microsoft/PowerToys/wiki

## Installation

Use the installation script `release.ps1`:

```powershell
.\release.ps1
```

Then restart PowerToys

**Or manually:**

1. Download the latest release of the Winget Plugin from the [releases page](https://github.com/bostrot/PowerToysRunPluginWinget/releases).
2. Extract the zip file's contents to your PowerToys modules directory (usually `%LOCALAPPDATA%\Microsoft\PowerToys\RunPlugins`).
3. Restart PowerToys.
Expand Down
2 changes: 1 addition & 1 deletion installer.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# PowerShell script that downloads latest release from GitHub and installs it to PowerToys Run plugin folder
# Usage: .\installer.ps1

$installLocation = "%LOCALAPPDATA%\PowerToys\RunPlugins\Winget"
$installLocation = "$env:LOCALAPPDATA\Microsoft\PowerToys\PowerToys Run\Plugins\Winget"

# Get latest release from GitHub
$latestRelease = Invoke-RestMethod -Uri "https://api.github.com/repos/bostrot/PowerToysRunPluginWinget/releases/latest"
Expand Down
16 changes: 16 additions & 0 deletions installer_local.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# PowerShell script that downloads latest release from GitHub and installs it to PowerToys Run plugin folder
# Usage: .\installer.ps1


$version = "1.3.0"
$release = "C:\Users\erict\Downloads\winget-powertoys-$version.zip"
$installLocation = "$env:LOCALAPPDATA\PowerToys\RunPlugins\Winget"

# Unzip latest release
Expand-Archive -Path $release -DestinationPath "$installLocation.tmp"

# Move files to plugin folder
Move-Item "$installLocation.tmp\Winget" $installLocation

# Remove temporary folder
Remove-Item "$installLocation.tmp" -Recurse
4 changes: 2 additions & 2 deletions release.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Script for copying the release files and creating the release zip file
# C:\Users\erict\PowerToys-dev\x64\Release\RunPlugins\Winget
$version = "1.2.3"
$version = "1.3.0"
$release = "C:\Users\erict\Downloads\winget-powertoys-$version.zip"
$zip = "C:\Program Files\7-Zip\7z.exe"
$path = "C:\Users\erict\PowerToys-dev\x64\Release\RunPlugins\Winget"
$path = "D:\erict\PowerToys-dev\x64\Release\RunPlugins\Winget"

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

0 comments on commit 4d04ed3

Please sign in to comment.