|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Management.Automation; |
| 3 | +using System.Management.Automation.Subsystem; |
| 4 | +using System.Management.Automation.Subsystem.Feedback; |
| 5 | +using System.Management.Automation.Subsystem.Prediction; |
| 6 | + |
| 7 | +namespace Microsoft.PowerShell.FeedbackProvider; |
| 8 | + |
| 9 | +public sealed class UnixCommandNotFound : IFeedbackProvider, ICommandPredictor |
| 10 | +{ |
| 11 | + private readonly Guid _guid; |
| 12 | + private List<string>? _candidates; |
| 13 | + |
| 14 | + internal UnixCommandNotFound(string guid) |
| 15 | + { |
| 16 | + _guid = new Guid(guid); |
| 17 | + } |
| 18 | + |
| 19 | + Dictionary<string, string>? ISubsystem.FunctionsToDefine => null; |
| 20 | + |
| 21 | + public Guid Id => _guid; |
| 22 | + |
| 23 | + public string Name => "cmd-not-found"; |
| 24 | + |
| 25 | + public string Description => "The built-in feedback/prediction source for the Unix command utility."; |
| 26 | + |
| 27 | + #region IFeedbackProvider |
| 28 | + |
| 29 | + private static string? GetUtilityPath() |
| 30 | + { |
| 31 | + string cmd_not_found = "/usr/lib/command-not-found"; |
| 32 | + bool exist = IsFileExecutable(cmd_not_found); |
| 33 | + |
| 34 | + if (!exist) |
| 35 | + { |
| 36 | + cmd_not_found = "/usr/share/command-not-found/command-not-found"; |
| 37 | + exist = IsFileExecutable(cmd_not_found); |
| 38 | + } |
| 39 | + |
| 40 | + return exist ? cmd_not_found : null; |
| 41 | + |
| 42 | + static bool IsFileExecutable(string path) |
| 43 | + { |
| 44 | + var file = new FileInfo(path); |
| 45 | + return file.Exists && file.UnixFileMode.HasFlag(UnixFileMode.OtherExecute); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public FeedbackItem? GetFeedback(string commandLine, ErrorRecord lastError, CancellationToken token) |
| 50 | + { |
| 51 | + if (Platform.IsWindows || lastError.FullyQualifiedErrorId != "CommandNotFoundException") |
| 52 | + { |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + var target = (string)lastError.TargetObject; |
| 57 | + if (target is null) |
| 58 | + { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + if (target.EndsWith(".ps1", StringComparison.OrdinalIgnoreCase)) |
| 63 | + { |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + string? cmd_not_found = GetUtilityPath(); |
| 68 | + if (cmd_not_found is not null) |
| 69 | + { |
| 70 | + var startInfo = new ProcessStartInfo(cmd_not_found); |
| 71 | + startInfo.ArgumentList.Add(target); |
| 72 | + startInfo.RedirectStandardError = true; |
| 73 | + startInfo.RedirectStandardOutput = true; |
| 74 | + |
| 75 | + using var process = Process.Start(startInfo); |
| 76 | + if (process is not null) |
| 77 | + { |
| 78 | + string? header = null; |
| 79 | + List<string>? actions = null; |
| 80 | + |
| 81 | + while (true) |
| 82 | + { |
| 83 | + string? line = process.StandardError.ReadLine(); |
| 84 | + if (line is null) |
| 85 | + { |
| 86 | + break; |
| 87 | + } |
| 88 | + |
| 89 | + if (line == string.Empty) |
| 90 | + { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + if (line.StartsWith("sudo ", StringComparison.Ordinal)) |
| 95 | + { |
| 96 | + actions ??= new List<string>(); |
| 97 | + actions.Add(line.TrimEnd()); |
| 98 | + } |
| 99 | + else if (actions is null) |
| 100 | + { |
| 101 | + header = line; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if (actions is not null && header is not null) |
| 106 | + { |
| 107 | + _candidates = actions; |
| 108 | + |
| 109 | + var footer = process.StandardOutput.ReadToEnd().Trim(); |
| 110 | + return string.IsNullOrEmpty(footer) |
| 111 | + ? new FeedbackItem(header, actions) |
| 112 | + : new FeedbackItem(header, actions, footer, FeedbackDisplayLayout.Portrait); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + #endregion |
| 121 | + |
| 122 | + #region ICommandPredictor |
| 123 | + |
| 124 | + public bool CanAcceptFeedback(PredictionClient client, PredictorFeedbackKind feedback) |
| 125 | + { |
| 126 | + return feedback switch |
| 127 | + { |
| 128 | + PredictorFeedbackKind.CommandLineAccepted => true, |
| 129 | + _ => false, |
| 130 | + }; |
| 131 | + } |
| 132 | + |
| 133 | + public SuggestionPackage GetSuggestion(PredictionClient client, PredictionContext context, CancellationToken cancellationToken) |
| 134 | + { |
| 135 | + if (_candidates is not null) |
| 136 | + { |
| 137 | + string input = context.InputAst.Extent.Text; |
| 138 | + List<PredictiveSuggestion>? result = null; |
| 139 | + |
| 140 | + foreach (string c in _candidates) |
| 141 | + { |
| 142 | + if (c.StartsWith(input, StringComparison.OrdinalIgnoreCase)) |
| 143 | + { |
| 144 | + result ??= new List<PredictiveSuggestion>(_candidates.Count); |
| 145 | + result.Add(new PredictiveSuggestion(c)); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if (result is not null) |
| 150 | + { |
| 151 | + return new SuggestionPackage(result); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return default; |
| 156 | + } |
| 157 | + |
| 158 | + public void OnCommandLineAccepted(PredictionClient client, IReadOnlyList<string> history) |
| 159 | + { |
| 160 | + // Reset the candidate state. |
| 161 | + _candidates = null; |
| 162 | + } |
| 163 | + |
| 164 | + public void OnSuggestionDisplayed(PredictionClient client, uint session, int countOrIndex) { } |
| 165 | + |
| 166 | + public void OnSuggestionAccepted(PredictionClient client, uint session, string acceptedSuggestion) { } |
| 167 | + |
| 168 | + public void OnCommandLineExecuted(PredictionClient client, string commandLine, bool success) { } |
| 169 | + |
| 170 | + #endregion; |
| 171 | +} |
| 172 | + |
| 173 | +public class Init : IModuleAssemblyInitializer, IModuleAssemblyCleanup |
| 174 | +{ |
| 175 | + private const string Id = "47013747-CB9D-4EBC-9F02-F32B8AB19D48"; |
| 176 | + |
| 177 | + public void OnImport() |
| 178 | + { |
| 179 | + var feedback = new UnixCommandNotFound(Id); |
| 180 | + SubsystemManager.RegisterSubsystem(SubsystemKind.FeedbackProvider, feedback); |
| 181 | + SubsystemManager.RegisterSubsystem(SubsystemKind.CommandPredictor, feedback); |
| 182 | + } |
| 183 | + |
| 184 | + public void OnRemove(PSModuleInfo psModuleInfo) |
| 185 | + { |
| 186 | + SubsystemManager.UnregisterSubsystem<ICommandPredictor>(new Guid(Id)); |
| 187 | + SubsystemManager.UnregisterSubsystem<IFeedbackProvider>(new Guid(Id)); |
| 188 | + } |
| 189 | +} |
0 commit comments