Introduce Command Not Found module (#26319)

* Introduce Command Not Found module

* rewrite module to depend on WinGet PowerShell module

* address Dongbo's feedback

* try and implement settings UI

* fix SUI build; try and store PowerShell object

* add and use object pool

* apply Dongbo's feedback

* add warm up; implement IPooledObjectPolicy

* Add module interface

* WIP trying to import module from settings

* Add EnableModule.ps1

* spellcheck

* spellcheck again

* Installer. Add DisableModule.ps1

* Fix styling

* Give the user some output from installing

* Prettify the Settings controls

* Add button to check PowerShell 7's version

* Fix Settings Assets paths

* Fix PowerShell 7 output

* Make module enable and disable scripts give better information

* Fix spellcheck

* Fix image files and placeholders

* Don't remove CmdNotFound on upgrade and don't fail on uninstall of
CmdNotFound

* Consistent install module scripts location on debug and installed

* installer: Avoid messageboxes and hide powershell on uninstalling CmdNotFound

* Fix psd1 file resolution when installed

* Fix spellcheck

* Add telemetry events

* Fix gpo files

* If GPO is set, enable/disable module on PT start depending on gpo value

* Cleanup module interface

* Cleanup settings code

* If GPO is set, disable Settings page logic

* Adding icons

* Update settings UI and strings

* Add telemetry for suggestions and feedbacks

* Fix sln file

* Fix build

* minor fixes

* Updating icon

* Remove global.json

* Remove unused PowerShell dependency

* Don't use preview version of Automation and fix NOTICE

* Fix signing

* Fix NOTICE.md

* Fix version checking for getfilesiginforedist.dll

* Fix spellchecker

* Fix README.md

* Fix false positives section in expect.txt

* Add logs to module interface

---------

Co-authored-by: Stefan Markovic <stefan@janeasystems.com>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
Co-authored-by: Niels Laute <niels.laute@live.nl>
This commit is contained in:
Carlos Zamora
2024-01-03 07:43:42 -08:00
committed by GitHub
parent 3b90f73bd0
commit 46f5316858
65 changed files with 1633 additions and 63 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@@ -0,0 +1,35 @@
$profileContent = Get-Content $PROFILE
$newContent = ""
$linesToDeleteFound = $False
$atLeastOneInstanceFound = $False
$profileContent | ForEach-Object {
if ($_.Contains("34de4b3d-13a8-4540-b76d-b9e8d3851756") -and !$linesToDeleteFound)
{
$linesToDeleteFound = $True
$atLeastOneInstanceFound = $True
return
}
if ($_.Contains("34de4b3d-13a8-4540-b76d-b9e8d3851756") -and $linesToDeleteFound)
{
$linesToDeleteFound = $False
return
}
if($linesToDeleteFound)
{
return
}
$newContent += $_ + "`r`n"
}
if($atLeastOneInstanceFound)
{
Set-Content -Path $PROFILE -Value $newContent
Write-Host "Removed the Command Not Found reference from the profile file."
} else {
Write-Host "No instance of Command Not Found was found in the profile file."
}

View File

@@ -0,0 +1,38 @@
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$scriptPath
)
Write-Host "Enabling experimental feature: PSFeedbackProvider"
Enable-ExperimentalFeature PSFeedbackProvider
Write-Host "Enabling experimental feature: PSCommandNotFoundSuggestion"
Enable-ExperimentalFeature PSCommandNotFoundSuggestion
if (Get-Module -ListAvailable -Name Microsoft.WinGet.Client) {
Write-Host "WinGet Client module detected"
}
else {
Write-Host "WinGet module was not found. Installation instructions can be found on https://www.powershellgallery.com/packages/Microsoft.WinGet.Client `r`n"
}
if (!(Test-Path $PROFILE))
{
Write-Host "Profile file $PROFILE not found".
New-Item -Path $PROFILE -ItemType File
Write-Host "Created profile file $PROFILE".
}
$profileContent = Get-Content -Path $PROFILE -Raw
if ((-not [string]::IsNullOrEmpty($profileContent)) -and ($profileContent.Contains("34de4b3d-13a8-4540-b76d-b9e8d3851756")))
{
Write-Host "Module is already registered in the profile file."
}
else
{
Add-Content -Path $PROFILE -Value "`r`n#34de4b3d-13a8-4540-b76d-b9e8d3851756 PowerToys CommandNotFound module"
Add-Content -Path $PROFILE -Value "`r`nImport-Module `"$scriptPath\WinGetCommandNotFound.psd1`""
Add-Content -Path $PROFILE -Value "#34de4b3d-13a8-4540-b76d-b9e8d3851756"
Write-Host "Module was successfully registered in the profile file."
}

View File

@@ -44,6 +44,7 @@ namespace Microsoft.PowerToys.Settings.UI.Helpers
case ModuleType.AlwaysOnTop: return generalSettingsConfig.Enabled.AlwaysOnTop;
case ModuleType.Awake: return generalSettingsConfig.Enabled.Awake;
case ModuleType.ColorPicker: return generalSettingsConfig.Enabled.ColorPicker;
case ModuleType.CmdNotFound: return generalSettingsConfig.Enabled.CmdNotFound;
case ModuleType.CropAndLock: return generalSettingsConfig.Enabled.CropAndLock;
case ModuleType.EnvironmentVariables: return generalSettingsConfig.Enabled.EnvironmentVariables;
case ModuleType.FancyZones: return generalSettingsConfig.Enabled.FancyZones;
@@ -76,6 +77,7 @@ namespace Microsoft.PowerToys.Settings.UI.Helpers
case ModuleType.AlwaysOnTop: generalSettingsConfig.Enabled.AlwaysOnTop = isEnabled; break;
case ModuleType.Awake: generalSettingsConfig.Enabled.Awake = isEnabled; break;
case ModuleType.ColorPicker: generalSettingsConfig.Enabled.ColorPicker = isEnabled; break;
case ModuleType.CmdNotFound: generalSettingsConfig.Enabled.CmdNotFound = isEnabled; break;
case ModuleType.CropAndLock: generalSettingsConfig.Enabled.CropAndLock = isEnabled; break;
case ModuleType.EnvironmentVariables: generalSettingsConfig.Enabled.EnvironmentVariables = isEnabled; break;
case ModuleType.FancyZones: generalSettingsConfig.Enabled.FancyZones = isEnabled; break;
@@ -107,6 +109,7 @@ namespace Microsoft.PowerToys.Settings.UI.Helpers
case ModuleType.AlwaysOnTop: return GPOWrapper.GetConfiguredAlwaysOnTopEnabledValue();
case ModuleType.Awake: return GPOWrapper.GetConfiguredAwakeEnabledValue();
case ModuleType.ColorPicker: return GPOWrapper.GetConfiguredColorPickerEnabledValue();
case ModuleType.CmdNotFound: return GPOWrapper.GetConfiguredCmdNotFoundEnabledValue();
case ModuleType.CropAndLock: return GPOWrapper.GetConfiguredCropAndLockEnabledValue();
case ModuleType.EnvironmentVariables: return GPOWrapper.GetConfiguredEnvironmentVariablesEnabledValue();
case ModuleType.FancyZones: return GPOWrapper.GetConfiguredFancyZonesEnabledValue();
@@ -139,6 +142,7 @@ namespace Microsoft.PowerToys.Settings.UI.Helpers
ModuleType.AlwaysOnTop => Color.FromArgb(255, 74, 196, 242), // #4ac4f2
ModuleType.Awake => Color.FromArgb(255, 40, 177, 233), // #28b1e9
ModuleType.ColorPicker => Color.FromArgb(255, 7, 129, 211), // #0781d3
ModuleType.CmdNotFound => Color.FromArgb(255, 31, 164, 227), // #1fa4e3
ModuleType.CropAndLock => Color.FromArgb(255, 32, 166, 228), // #20a6e4
ModuleType.EnvironmentVariables => Color.FromArgb(255, 16, 132, 208), // #1084d0
ModuleType.FancyZones => Color.FromArgb(255, 65, 209, 247), // #41d1f7
@@ -171,6 +175,7 @@ namespace Microsoft.PowerToys.Settings.UI.Helpers
ModuleType.AlwaysOnTop => typeof(AlwaysOnTopPage),
ModuleType.Awake => typeof(AwakePage),
ModuleType.ColorPicker => typeof(ColorPickerPage),
ModuleType.CmdNotFound => typeof(CmdNotFoundPage),
ModuleType.CropAndLock => typeof(CropAndLockPage),
ModuleType.EnvironmentVariables => typeof(EnvironmentVariablesPage),
ModuleType.FancyZones => typeof(FancyZonesPage),

View File

@@ -9,6 +9,7 @@ namespace Microsoft.PowerToys.Settings.UI.OOBE.Enums
Overview = 0,
AlwaysOnTop,
Awake,
CmdNotFound,
ColorPicker,
CropAndLock,
EnvironmentVariables,

View File

@@ -53,7 +53,7 @@
<WarningsNotAsErrors>CA1720</WarningsNotAsErrors>
<Optimize>true</Optimize>
</PropertyGroup>
<!-- See https://learn.microsoft.com/windows/apps/develop/platform/csharp-winrt/net-projection-from-cppwinrt-component for more info -->
<PropertyGroup>
<CsWinRTIncludes>PowerToys.GPOWrapper</CsWinRTIncludes>
@@ -61,10 +61,10 @@
<ErrorOnDuplicatePublishOutputFiles>false</ErrorOnDuplicatePublishOutputFiles>
</PropertyGroup>
<!-- Needed for CommunityToolkit.Labs.WinUI.SettingsControls. -->
<PropertyGroup>
<RestoreAdditionalProjectSources>https://pkgs.dev.azure.com/dotnet/CommunityToolkit/_packaging/CommunityToolkit-Labs/nuget/v3/index.json</RestoreAdditionalProjectSources>
</PropertyGroup>
<!-- Needed for CommunityToolkit.Labs.WinUI.SettingsControls. -->
<PropertyGroup>
<RestoreAdditionalProjectSources>https://pkgs.dev.azure.com/dotnet/CommunityToolkit/_packaging/CommunityToolkit-Labs/nuget/v3/index.json</RestoreAdditionalProjectSources>
</PropertyGroup>
<ItemGroup>
<Content Include="Assets\Settings\SplashScreen.scale-200.png" />
@@ -96,11 +96,11 @@
<!-- Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
Tools extension to be activated for this project even if the Windows App SDK Nuget
package has not yet been restored -->
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnablePreviewMsixTooling)'=='true'">
<ProjectCapability Include="Msix" />
</ItemGroup>
<ItemGroup>
<!-- HACK: Common.UI is referenced, even if it is not used, to force dll versions to be the same as in other projects that use it. It's still unclear why this is the case, but this is need for flattening the install directory. -->
<ProjectReference Include="..\..\common\Common.UI\Common.UI.csproj" />
@@ -111,7 +111,7 @@
<ProjectReference Include="..\..\common\ManagedTelemetry\Telemetry\ManagedTelemetry.csproj" />
<ProjectReference Include="..\Settings.UI.Library\Settings.UI.Library.csproj" />
</ItemGroup>
<PropertyGroup>
<!-- TODO: fix issues and reenable -->
<!-- These are caused by streamjsonrpc dependency on Microsoft.VisualStudio.Threading.Analyzers -->
@@ -119,10 +119,19 @@
<NoWarn>VSTHRD002;VSTHRD110;VSTHRD100;VSTHRD200;VSTHRD101</NoWarn>
</PropertyGroup>
<ItemGroup>
<None Update="Assets\Settings\icon.ico">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update="Assets\Settings\icon.ico">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update="Assets\Settings\Scripts\EnableModule.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Assets\Settings\Scripts\DisableModule.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -395,6 +395,7 @@ namespace Microsoft.PowerToys.Settings.UI
case "Overview": return typeof(GeneralPage);
case "AlwaysOnTop": return typeof(AlwaysOnTopPage);
case "Awake": return typeof(AwakePage);
case "CmdNotFound": return typeof(CmdNotFoundPage);
case "ColorPicker": return typeof(ColorPickerPage);
case "FancyZones": return typeof(FancyZonesPage);
case "FileLocksmith": return typeof(FileLocksmithPage);

View File

@@ -0,0 +1,32 @@
<Page
x:Class="Microsoft.PowerToys.Settings.UI.OOBE.Views.OobeCmdNotFound"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Microsoft.PowerToys.Settings.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Microsoft.PowerToys.Settings.UI.OOBE.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkitcontrols="using:CommunityToolkit.WinUI.UI.Controls"
mc:Ignorable="d">
<controls:OOBEPageControl x:Uid="Oobe_CmdNotFound" HeroImage="ms-appx:///Assets/Settings/Modules/OOBE/CmdNotFound.png">
<controls:OOBEPageControl.PageContent>
<StackPanel Orientation="Vertical">
<TextBlock x:Uid="Oobe_HowToUse" Style="{ThemeResource OobeSubtitleStyle}" />
<controls:ShortcutWithTextLabelControl x:Name="HotkeyControl" x:Uid="Oobe_CmdNotFound_HowToUse" />
<StackPanel
Margin="0,24,0,0"
Orientation="Horizontal"
Spacing="12">
<Button x:Uid="OOBE_Settings" Click="SettingsLaunchButton_Click" />
<HyperlinkButton NavigateUri="https://aka.ms/PowerToysOverview_CmdNotFound" Style="{StaticResource TextButtonStyle}">
<TextBlock x:Uid="LearnMore_CmdNotFound" TextWrapping="Wrap" />
</HyperlinkButton>
</StackPanel>
</StackPanel>
</controls:OOBEPageControl.PageContent>
</controls:OOBEPageControl>
</Page>

View File

@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.OOBE.Enums;
using Microsoft.PowerToys.Settings.UI.OOBE.ViewModel;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;
namespace Microsoft.PowerToys.Settings.UI.OOBE.Views
{
public sealed partial class OobeCmdNotFound : Page
{
public OobePowerToysModule ViewModel { get; set; }
public OobeCmdNotFound()
{
this.InitializeComponent();
ViewModel = new OobePowerToysModule(OobeShellPage.OobeShellHandler.Modules[(int)PowerToysModules.CmdNotFound]);
DataContext = ViewModel;
}
private void SettingsLaunchButton_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
if (OobeShellPage.OpenMainWindowCallback != null)
{
OobeShellPage.OpenMainWindowCallback(typeof(CmdNotFoundPage));
}
ViewModel.LogOpeningSettingsEvent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ViewModel.LogOpeningModuleEvent();
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
ViewModel.LogClosingModuleEvent();
}
}
}

View File

@@ -69,6 +69,10 @@
x:Uid="Shell_Awake"
Icon="{ui:BitmapIcon Source=/Assets/Settings/FluentIcons/FluentIconsAwake.png}"
Tag="Awake" />
<NavigationViewItem
x:Uid="Shell_CmdNotFound"
Icon="{ui:BitmapIcon Source=/Assets/Settings/FluentIcons/FluentIconsCmdNotFound.png}"
Tag="CmdNotFound" />
<NavigationViewItem
x:Uid="Shell_ColorPicker"
Icon="{ui:BitmapIcon Source=/Assets/Settings/FluentIcons/FluentIconsColorPicker.png}"

View File

@@ -79,6 +79,11 @@ namespace Microsoft.PowerToys.Settings.UI.OOBE.Views
ModuleName = "Awake",
IsNew = false,
});
Modules.Insert((int)PowerToysModules.CmdNotFound, new OobePowerToysModule()
{
ModuleName = "CmdNotFound",
IsNew = true,
});
Modules.Insert((int)PowerToysModules.ColorPicker, new OobePowerToysModule()
{
ModuleName = "ColorPicker",
@@ -250,6 +255,7 @@ namespace Microsoft.PowerToys.Settings.UI.OOBE.Views
case "WhatsNew": NavigationFrame.Navigate(typeof(OobeWhatsNew)); break;
case "AlwaysOnTop": NavigationFrame.Navigate(typeof(OobeAlwaysOnTop)); break;
case "Awake": NavigationFrame.Navigate(typeof(OobeAwake)); break;
case "CmdNotFound": NavigationFrame.Navigate(typeof(OobeCmdNotFound)); break;
case "ColorPicker": NavigationFrame.Navigate(typeof(OobeColorPicker)); break;
case "CropAndLock": NavigationFrame.Navigate(typeof(OobeCropAndLock)); break;
case "EnvironmentVariables": NavigationFrame.Navigate(typeof(OobeEnvironmentVariables)); break;

View File

@@ -0,0 +1,60 @@
<Page
x:Class="Microsoft.PowerToys.Settings.UI.Views.CmdNotFoundPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:CommunityToolkit.WinUI.Controls"
xmlns:custom="using:Microsoft.PowerToys.Settings.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="using:CommunityToolkit.WinUI"
AutomationProperties.LandmarkType="Main"
mc:Ignorable="d">
<custom:SettingsPageControl x:Uid="CmdNotFound" ModuleImageSource="ms-appx:///Assets/Settings/Modules/CmdNotFound.png">
<custom:SettingsPageControl.ModuleContent>
<StackPanel ChildrenTransitions="{StaticResource SettingsCardsAnimations}" Orientation="Vertical">
<InfoBar
x:Uid="GPO_IsSettingForced"
IsClosable="False"
IsOpen="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabledGpoConfigured}"
IsTabStop="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabledGpoConfigured}"
Severity="Informational" />
<controls:SettingsCard
x:Uid="CmdNotFound_Enable"
HeaderIcon="{ui:BitmapIcon Source=/Assets/Settings/FluentIcons/FluentIconsCmdNotFound.png}"
IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabledGpoConfigured, Converter={StaticResource BoolNegationConverter}}">
<StackPanel Orientation="Horizontal" Spacing="8">
<Button
x:Uid="CmdNotFound_InstallButton"
Command="{x:Bind ViewModel.InstallModuleEventHandler}"
Style="{StaticResource AccentButtonStyle}" />
<HyperlinkButton x:Uid="CmdNotFound_UninstallButton" Command="{x:Bind ViewModel.UninstallModuleEventHandler}" />
</StackPanel>
<controls:SettingsCard.Description>
<StackPanel Orientation="Vertical">
<TextBlock x:Uid="CmdNotFound_Enable_DescriptionText" />
<HyperlinkButton x:Uid="CmdNotFound_CheckCompatibility" Command="{x:Bind ViewModel.CheckPowershellVersionEventHandler}" />
</StackPanel>
</controls:SettingsCard.Description>
</controls:SettingsCard>
<TextBlock
x:Uid="CmdNotFound_ModuleInstallationLogs"
Margin="0,12,0,4"
Style="{ThemeResource BodyStrongTextBlockStyle}" />
<TextBox
Height="300"
FontFamily="Consolas"
IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabledGpoConfigured, Converter={StaticResource BoolNegationConverter}}"
IsReadOnly="True"
Text="{x:Bind Mode=OneWay, Path=ViewModel.CommandOutputLog}"
TextWrapping="Wrap" />
</StackPanel>
</custom:SettingsPageControl.ModuleContent>
<custom:SettingsPageControl.PrimaryLinks>
<custom:PageLink x:Uid="LearnMore_CmdNotFound" Link="https://aka.ms/PowerToysOverview_CmdNotFound" />
</custom:SettingsPageControl.PrimaryLinks>
</custom:SettingsPageControl>
</Page>

View File

@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.PowerToys.Settings.UI.ViewModels;
using Microsoft.UI.Xaml.Controls;
namespace Microsoft.PowerToys.Settings.UI.Views
{
public sealed partial class CmdNotFoundPage : Page
{
private CmdNotFoundViewModel ViewModel { get; set; }
public CmdNotFoundPage()
{
ViewModel = new CmdNotFoundViewModel();
DataContext = ViewModel;
InitializeComponent();
}
}
}

View File

@@ -115,6 +115,11 @@
helpers:NavHelper.NavigateTo="views:AwakePage"
Icon="{ui:BitmapIcon Source=/Assets/Settings/FluentIcons/FluentIconsAwake.png}" />
<NavigationViewItem
x:Uid="Shell_CmdNotFound"
helpers:NavHelper.NavigateTo="views:CmdNotFoundPage"
Icon="{ui:BitmapIcon Source=/Assets/Settings/FluentIcons/FluentIconsCmdNotFound.png}" />
<NavigationViewItem
x:Uid="Shell_ColorPicker"
helpers:NavHelper.NavigateTo="views:ColorPickerPage"

View File

@@ -2358,6 +2358,10 @@ From there, simply click on one of the supported files in the File Explorer and
<value>Learn more about Awake</value>
<comment>Awake is a product name, do not loc</comment>
</data>
<data name="LearnMore_CmdNotFound.Text" xml:space="preserve">
<value>Learn more about Command Not Found</value>
<comment> Command Not Found is the name of the module. </comment>
</data>
<data name="SecondaryLink_Awake.Text" xml:space="preserve">
<value>Den Delimarsky's work on creating Awake</value>
<comment>Awake is a product name, do not loc</comment>
@@ -3655,6 +3659,51 @@ Activate by holding the key for the character you want to add an accent to, then
<data name="Oobe_PastePlain_HowToUse.Text" xml:space="preserve">
<value>to paste your clipboard data as plain text. Note: this will replace the formatted text in your clipboard with the plain text.</value>
</data>
<data name="Shell_CmdNotFound.Content" xml:space="preserve">
<value>Command Not Found</value>
<comment>Product name: Navigation view item name for Command Not Found</comment>
</data>
<data name="CmdNotFound.ModuleDescription" xml:space="preserve">
<value>A PowerShell module that detects an error thrown by a command and suggests a relevant WinGet package to install, if available.</value>
<comment>"Command Not Found" is a product name</comment>
</data>
<data name="CmdNotFound.ModuleTitle" xml:space="preserve">
<value>Command Not Found</value>
<comment>"Command Not Found" is a product name</comment>
</data>
<data name="CmdNotFound_Enable.Header" xml:space="preserve">
<value>Command Not Found</value>
<comment>"Command Not Found" is a product name</comment>
</data>
<data name="CmdNotFound_Enable_DescriptionText.Text" xml:space="preserve">
<value>Add this module to the PowerShell 7 profile script so that it is enabled with every new session</value>
</data>
<data name="CmdNotFound_ModuleInstallationLogs.Text" xml:space="preserve">
<value>Installation logs</value>
</data>
<data name="CmdNotFound_CheckPowerShellVersionButtonControl.Description" xml:space="preserve">
<value>PowerShell 7 is required to use this module</value>
</data>
<data name="CmdNotFound_CheckCompatibility.Content" xml:space="preserve">
<value>Check if your PowerShell configuration is compatible</value>
</data>
<data name="CmdNotFound_InstallButton.Content" xml:space="preserve">
<value>Install</value>
</data>
<data name="CmdNotFound_UninstallButton.Content" xml:space="preserve">
<value>Uninstall</value>
</data>
<data name="Oobe_CmdNotFound.Description" xml:space="preserve">
<value>Command Not Found detects an error thrown by a command in PowerShell and suggests a relevant WinGet package to install, if available.</value>
<comment>"Command Not Found" is a product name</comment>
</data>
<data name="Oobe_CmdNotFound.Title" xml:space="preserve">
<value>Command Not Found</value>
<comment>"Command Not Found" is a product name</comment>
</data>
<data name="Oobe_CmdNotFound_HowToUse.Text" xml:space="preserve">
<value>If a command returns an error, the module will suggest a WinGet package that may provide the relevant executable.</value>
</data>
<data name="AllAppsTxt.Text" xml:space="preserve">
<value>All apps</value>
</data>

View File

@@ -0,0 +1,123 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using global::PowerToys.GPOWrapper;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Telemetry.Events;
using Microsoft.PowerToys.Settings.UI.Library.ViewModels.Commands;
using Microsoft.PowerToys.Telemetry;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class CmdNotFoundViewModel : Observable
{
public ButtonClickCommand CheckPowershellVersionEventHandler => new ButtonClickCommand(CheckPowershellVersion);
public ButtonClickCommand InstallModuleEventHandler => new ButtonClickCommand(InstallModule);
public ButtonClickCommand UninstallModuleEventHandler => new ButtonClickCommand(UninstallModule);
private GpoRuleConfigured _enabledGpoRuleConfiguration;
private bool _enabledStateIsGPOConfigured;
public static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().Location;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
public CmdNotFoundViewModel()
{
InitializeEnabledValue();
}
private void InitializeEnabledValue()
{
_enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredCmdNotFoundEnabledValue();
if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
{
// Get the enabled state from GPO.
_enabledStateIsGPOConfigured = true;
}
}
private string _commandOutputLog;
public string CommandOutputLog
{
get => _commandOutputLog;
set
{
if (_commandOutputLog != value)
{
_commandOutputLog = value;
OnPropertyChanged(nameof(CommandOutputLog));
}
}
}
public bool IsEnabledGpoConfigured
{
get => _enabledStateIsGPOConfigured;
}
public void RunPowerShellScript(string powershellArguments)
{
string outputLog = string.Empty;
try
{
var startInfo = new ProcessStartInfo()
{
FileName = "pwsh.exe",
Arguments = powershellArguments,
UseShellExecute = false,
RedirectStandardOutput = true,
};
startInfo.EnvironmentVariables["NO_COLOR"] = "1";
var process = Process.Start(startInfo);
while (!process.StandardOutput.EndOfStream)
{
outputLog += process.StandardOutput.ReadLine() + "\r\n"; // Weirdly, PowerShell 7 won't give us new lines.
}
}
catch (Exception ex)
{
outputLog = ex.ToString();
}
CommandOutputLog = outputLog;
}
public void CheckPowershellVersion()
{
var arguments = $"-NoProfile -NonInteractive -Command $PSVersionTable";
RunPowerShellScript(arguments);
}
public void InstallModule()
{
var ps1File = AssemblyDirectory + "\\Assets\\Settings\\Scripts\\EnableModule.ps1";
var arguments = $"-NoProfile -ExecutionPolicy Unrestricted -File \"{ps1File}\" -scriptPath \"{AssemblyDirectory}\\..\"";
RunPowerShellScript(arguments);
PowerToysTelemetry.Log.WriteEvent(new CmdNotFoundInstallEvent());
}
public void UninstallModule()
{
var ps1File = AssemblyDirectory + "\\Assets\\Settings\\Scripts\\DisableModule.ps1";
var arguments = $"-NoProfile -ExecutionPolicy Unrestricted -File \"{ps1File}\"";
RunPowerShellScript(arguments);
PowerToysTelemetry.Log.WriteEvent(new CmdNotFoundUninstallEvent());
}
}
}