From 7c32f0ebeba2519481307c2034dcce636bfc1ab3 Mon Sep 17 00:00:00 2001 From: AdmiringWorm Date: Mon, 15 Jan 2024 17:05:20 +0100 Subject: [PATCH] (#3381) Add new rule command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds a new command called ´rule` with an alias of `rules` to be able to view any Validation Rules that have been implemented by Chocolatey CLI, or the details of a specific view. This allows a user to see what is implemented when they are packaging their Chocolatey CLI package. This is intended to be used in the future to enhance other functionality. --- src/chocolatey/chocolatey.csproj | 1085 +++++++++-------- .../commands/ChocolateyRuleCommand.cs | 150 +++ 2 files changed, 693 insertions(+), 542 deletions(-) create mode 100644 src/chocolatey/infrastructure.app/commands/ChocolateyRuleCommand.cs diff --git a/src/chocolatey/chocolatey.csproj b/src/chocolatey/chocolatey.csproj index a945314f6d..011a5563b1 100644 --- a/src/chocolatey/chocolatey.csproj +++ b/src/chocolatey/chocolatey.csproj @@ -1,543 +1,544 @@ - - - - - Debug - AnyCPU - 8.0.30703 - 2.0 - {5563DC61-35FD-4FAB-B331-9AE1FDB23F80} - Library - Properties - chocolatey - chocolatey - v4.8 - 7.3 - 512 - ..\ - - - $(NoWarn);CS1591 - - - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - bin\Debug\chocolatey.xml - false - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - true - true - bin\Release\chocolatey.xml - false - - - ..\..\docs\logo\chocolatey.ico - - - true - bin\NoResources\ - TRACE;NoResources - true - bin\Release\chocolatey.xml - true - pdbonly - AnyCPU - prompt - MinimumRecommendedRules.ruleset - false - - - true - bin\ReleaseOfficial\ - TRACE;FORCE_CHOCOLATEY_OFFICIAL_KEY - true - bin\Release\chocolatey.xml - true - pdbonly - AnyCPU - prompt - MinimumRecommendedRules.ruleset - false - - - true - bin\ReleaseOfficialNo7zip\ - TRACE;FORCE_CHOCOLATEY_OFFICIAL_KEY - true - bin\Release\chocolatey.xml - true - pdbonly - AnyCPU - prompt - MinimumRecommendedRules.ruleset - - - true - bin\ReleaseOfficialNo7zip\ - TRACE;FORCE_CHOCOLATEY_OFFICIAL_KEY - true - bin\Release\chocolatey.xml - true - pdbonly - AnyCPU - prompt - MinimumRecommendedRules.ruleset - - - - False - ..\packages\AlphaFS.2.1.3\lib\net40\AlphaFS.dll - - - ..\packages\Chocolatey.NuGet.Commands.3.4.2\lib\net472\Chocolatey.NuGet.Commands.dll - - - ..\packages\Chocolatey.NuGet.Common.3.4.2\lib\net472\Chocolatey.NuGet.Common.dll - - - ..\packages\Chocolatey.NuGet.Configuration.3.4.2\lib\net472\Chocolatey.NuGet.Configuration.dll - - - ..\packages\Chocolatey.NuGet.Credentials.3.4.2\lib\net472\Chocolatey.NuGet.Credentials.dll - - - ..\packages\Chocolatey.NuGet.DependencyResolver.Core.3.4.2\lib\net472\Chocolatey.NuGet.DependencyResolver.Core.dll - - - ..\packages\Chocolatey.NuGet.Frameworks.3.4.2\lib\net472\Chocolatey.NuGet.Frameworks.dll - - - ..\packages\Chocolatey.NuGet.LibraryModel.3.4.2\lib\net472\Chocolatey.NuGet.LibraryModel.dll - - - ..\packages\Chocolatey.NuGet.PackageManagement.3.4.2\lib\net472\Chocolatey.NuGet.PackageManagement.dll - - - ..\packages\Chocolatey.NuGet.Packaging.3.4.2\lib\net472\Chocolatey.NuGet.Packaging.dll - - - ..\packages\Chocolatey.NuGet.ProjectModel.3.4.2\lib\net472\Chocolatey.NuGet.ProjectModel.dll - - - ..\packages\Chocolatey.NuGet.Protocol.3.4.2\lib\net472\Chocolatey.NuGet.Protocol.dll - - - ..\packages\Chocolatey.NuGet.Resolver.3.4.2\lib\net472\Chocolatey.NuGet.Resolver.dll - - - ..\packages\Chocolatey.NuGet.Versioning.3.4.2\lib\net472\Chocolatey.NuGet.Versioning.dll - - - ..\packages\log4net.2.0.12\lib\net45\log4net.dll - - - ..\packages\Microsoft.Bcl.HashCode.1.1.1\lib\net461\Microsoft.Bcl.HashCode.dll - - - - ..\packages\Microsoft.Web.Xdt.3.1.0\lib\net40\Microsoft.Web.XmlTransform.dll - - - ..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll - - - ..\..\lib\Rhino.Licensing.1.4.1\lib\net40\Rhino.Licensing.dll - - - ..\packages\SimpleInjector.2.8.3\lib\net45\SimpleInjector.dll - - - - - - - - - - - - False - ..\..\lib\PowerShell\System.Management.Automation.dll - - - - - ..\packages\System.Reactive.5.0.0\lib\net472\System.Reactive.dll - - - ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll - - - - - ..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll - - - - - - - - - - - - - Properties\SolutionVersion.cs - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Code - - - - - - - - - - - - - - - - - - - - - - - - - Designer - - - - - Designer - - - Designer - - - - - Properties\chocolatey.ico - - - - - - - {AF584111-FE32-448D-A1D0-63217AF8B43C} - chocolatey.resources - - - - - - - - - - - - - - - - - - - - - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - - - + + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {5563DC61-35FD-4FAB-B331-9AE1FDB23F80} + Library + Properties + chocolatey + chocolatey + v4.8 + 7.3 + 512 + ..\ + + + $(NoWarn);CS1591 + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + bin\Debug\chocolatey.xml + false + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + true + true + bin\Release\chocolatey.xml + false + + + ..\..\docs\logo\chocolatey.ico + + + true + bin\NoResources\ + TRACE;NoResources + true + bin\Release\chocolatey.xml + true + pdbonly + AnyCPU + prompt + MinimumRecommendedRules.ruleset + false + + + true + bin\ReleaseOfficial\ + TRACE;FORCE_CHOCOLATEY_OFFICIAL_KEY + true + bin\Release\chocolatey.xml + true + pdbonly + AnyCPU + prompt + MinimumRecommendedRules.ruleset + false + + + true + bin\ReleaseOfficialNo7zip\ + TRACE;FORCE_CHOCOLATEY_OFFICIAL_KEY + true + bin\Release\chocolatey.xml + true + pdbonly + AnyCPU + prompt + MinimumRecommendedRules.ruleset + + + true + bin\ReleaseOfficialNo7zip\ + TRACE;FORCE_CHOCOLATEY_OFFICIAL_KEY + true + bin\Release\chocolatey.xml + true + pdbonly + AnyCPU + prompt + MinimumRecommendedRules.ruleset + + + + False + ..\packages\AlphaFS.2.1.3\lib\net40\AlphaFS.dll + + + ..\packages\Chocolatey.NuGet.Commands.3.4.2\lib\net472\Chocolatey.NuGet.Commands.dll + + + ..\packages\Chocolatey.NuGet.Common.3.4.2\lib\net472\Chocolatey.NuGet.Common.dll + + + ..\packages\Chocolatey.NuGet.Configuration.3.4.2\lib\net472\Chocolatey.NuGet.Configuration.dll + + + ..\packages\Chocolatey.NuGet.Credentials.3.4.2\lib\net472\Chocolatey.NuGet.Credentials.dll + + + ..\packages\Chocolatey.NuGet.DependencyResolver.Core.3.4.2\lib\net472\Chocolatey.NuGet.DependencyResolver.Core.dll + + + ..\packages\Chocolatey.NuGet.Frameworks.3.4.2\lib\net472\Chocolatey.NuGet.Frameworks.dll + + + ..\packages\Chocolatey.NuGet.LibraryModel.3.4.2\lib\net472\Chocolatey.NuGet.LibraryModel.dll + + + ..\packages\Chocolatey.NuGet.PackageManagement.3.4.2\lib\net472\Chocolatey.NuGet.PackageManagement.dll + + + ..\packages\Chocolatey.NuGet.Packaging.3.4.2\lib\net472\Chocolatey.NuGet.Packaging.dll + + + ..\packages\Chocolatey.NuGet.ProjectModel.3.4.2\lib\net472\Chocolatey.NuGet.ProjectModel.dll + + + ..\packages\Chocolatey.NuGet.Protocol.3.4.2\lib\net472\Chocolatey.NuGet.Protocol.dll + + + ..\packages\Chocolatey.NuGet.Resolver.3.4.2\lib\net472\Chocolatey.NuGet.Resolver.dll + + + ..\packages\Chocolatey.NuGet.Versioning.3.4.2\lib\net472\Chocolatey.NuGet.Versioning.dll + + + ..\packages\log4net.2.0.12\lib\net45\log4net.dll + + + ..\packages\Microsoft.Bcl.HashCode.1.1.1\lib\net461\Microsoft.Bcl.HashCode.dll + + + + ..\packages\Microsoft.Web.Xdt.3.1.0\lib\net40\Microsoft.Web.XmlTransform.dll + + + ..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll + + + ..\..\lib\Rhino.Licensing.1.4.1\lib\net40\Rhino.Licensing.dll + + + ..\packages\SimpleInjector.2.8.3\lib\net45\SimpleInjector.dll + + + + + + + + + + + + False + ..\..\lib\PowerShell\System.Management.Automation.dll + + + + + ..\packages\System.Reactive.5.0.0\lib\net472\System.Reactive.dll + + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll + + + + + ..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll + + + + + + + + + + + + + Properties\SolutionVersion.cs + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Code + + + + + + + + + + + + + + + + + + + + + + + + + Designer + + + + + Designer + + + Designer + + + + + Properties\chocolatey.ico + + + + + + + {AF584111-FE32-448D-A1D0-63217AF8B43C} + chocolatey.resources + + + + + + + + + + + + + + + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyRuleCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyRuleCommand.cs new file mode 100644 index 0000000000..aa8d132353 --- /dev/null +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyRuleCommand.cs @@ -0,0 +1,150 @@ +namespace chocolatey.infrastructure.app.commands +{ + using System; + using System.Collections.Generic; + using System.Linq; + using chocolatey.infrastructure.app.attributes; + using chocolatey.infrastructure.app.configuration; + using chocolatey.infrastructure.commandline; + using chocolatey.infrastructure.commands; + using chocolatey.infrastructure.logging; + using chocolatey.infrastructure.rules; + using chocolatey.infrastructure.services; + + [CommandFor("rule", "view or list implemented package rules.", Version = "2.3.0")] + [CommandFor("rules", "view or list implemented package rules.", Version = "2.3.0")] + public class ChocolateyRuleCommand : ChocolateyCommandBase, ICommand + { + private readonly IRuleService _ruleService; + + public ChocolateyRuleCommand(IRuleService ruleService) + { + _ruleService = ruleService ?? throw new ArgumentNullException(nameof(ruleService)); + } + + public void ConfigureArgumentParser(OptionSet optionSet, ChocolateyConfiguration configuration) + { + optionSet + .Add("id=", + "The identifier of the rule to show more details about.", + option => configuration.Input = option); + } + + public void ParseAdditionalArguments(IList unparsedArguments, ChocolateyConfiguration configuration) + { + } + + public void Validate(ChocolateyConfiguration configuration) + { + // Nothing to validate + } + + public void DryRun(ChocolateyConfiguration configuration) + { + Run(configuration); + } + + public void Run(ChocolateyConfiguration config) + { + var implementedRules = _ruleService.GetAllAvailableRules() + .OrderBy(r => r.Id); + + if (!string.IsNullOrEmpty(config.Input)) + { + var foundRule = implementedRules.FirstOrDefault(i => i.Id.IsEqualTo(config.Input)); + + // Since the return value is a structure, it will never be null. + // As such we check that the identifier is not empty. + if (string.IsNullOrEmpty(foundRule.Id)) + { + throw new ApplicationException("No rule with the identifier {0} could be found.".FormatWith(config.Input)); + } + + this.Log().Info(@"ID: {0} | Severity: {1} +Summary: {2} +Help URL: {3}", + foundRule.Id, + foundRule.Severity, + foundRule.Summary, + foundRule.HelpUrl); + + return; + } + + if (config.RegularOutput) + { + this.Log().Info(ChocolateyLoggers.Important, "Implemented Package Rules"); + } + + OutputRules("Error/Required", config, implementedRules.Where(r => r.Severity == RuleType.Error).ToList()); + OutputRules("Warning/Guideline", config, implementedRules.Where(r => r.Severity == RuleType.Warning).ToList()); + OutputRules("Information/Suggestion", config, implementedRules.Where(r => r.Severity == RuleType.Information).ToList()); + OutputRules("Notes", config, implementedRules.Where(r => r.Severity == RuleType.Note).ToList()); + OutputRules("Disabled", config, implementedRules.Where(r => r.Severity == RuleType.None).ToList()); + } + + protected virtual void OutputRules(string type, ChocolateyConfiguration config, IReadOnlyList rules) + { + if (config.RegularOutput) + { + this.Log().Info(""); + this.Log().Info(ChocolateyLoggers.Important, type + " Rules"); + this.Log().Info(""); + + if (rules.Count == 0) + { + this.Log().Info("No implemented " + type + " rules available."); + + return; + } + } + + foreach (var rule in rules) + { + if (config.RegularOutput) + { + this.Log().Info("{0}: {1}", rule.Id, rule.Summary); + } + else + { + this.Log().Info("{0}|{1}|{2}|{3}", rule.Severity, rule.Id, rule.Summary, rule.HelpUrl); + } + } + } + + public bool MayRequireAdminAccess() + { + return false; + } + +#pragma warning disable IDE1006 + [Obsolete("This overload is deprecated and will be removed in v3.")] + public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyConfiguration configuration) + => ConfigureArgumentParser(optionSet, configuration); + + [Obsolete("This overload is deprecated and will be removed in v3.")] + public virtual void handle_additional_argument_parsing(IList unparsedArguments, ChocolateyConfiguration configuration) + => ParseAdditionalArguments(unparsedArguments, configuration); + + [Obsolete("This overload is deprecated and will be removed in v3.")] + public virtual void handle_validation(ChocolateyConfiguration configuration) + => Validate(configuration); + + [Obsolete("This overload is deprecated and will be removed in v3.")] + public virtual void help_message(ChocolateyConfiguration configuration) + => HelpMessage(configuration); + + [Obsolete("This overload is deprecated and will be removed in v3.")] + public virtual void noop(ChocolateyConfiguration configuration) + => DryRun(configuration); + + [Obsolete("This overload is deprecated and will be removed in v3.")] + public virtual void run(ChocolateyConfiguration configuration) + => Run(configuration); + + [Obsolete("This overload is deprecated and will be removed in v3.")] + public virtual bool may_require_admin_access() + => MayRequireAdminAccess(); +#pragma warning restore IDE1006 + } +}