mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 11:17:53 +01:00
FxCopAnalyzer fixes for Wox.Core (#7209)
* FxCop Fixes- added InvarientCulture info, suppressed warnings for general exception types * Used IsNullOrEmpty to test for empty strings (CA1820: Test for empty strings using string length) * Check if arguments are null and throw ArgumentNullException * Removed unused function argument from PluginsLoader.cs * Addressing comments- Using attributes to suppress errors for general exception types * Addressing comments- Using attributes to suppress errors * Addressed comments and changed InvariantCulture to CurrentCulture/Ordinal where appropriate * Addressing comments - catching and logging specific exception in FontHelper.cs
This commit is contained in:
@@ -32,6 +32,7 @@ namespace Wox.Core.Plugin
|
||||
return PluginMetadatas;
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "All exception information is being logged")]
|
||||
private static void ParsePluginConfigs(IEnumerable<string> directories)
|
||||
{
|
||||
// todo use linq when diable plugin is implemented since parallel.foreach + list is not thread saft
|
||||
@@ -59,6 +60,7 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "All exception information is being logged")]
|
||||
private static PluginMetadata GetPluginMetadata(string pluginDirectory)
|
||||
{
|
||||
string configPath = Path.Combine(pluginDirectory, PluginConfigName);
|
||||
|
||||
@@ -41,15 +41,16 @@ namespace Wox.Core.Plugin
|
||||
|
||||
string pluginFolderPath = Infrastructure.Constant.PluginsDirectory;
|
||||
|
||||
// Using Ordinal since this is part of a path
|
||||
string newPluginName = plugin.Name
|
||||
.Replace("/", "_")
|
||||
.Replace("\\", "_")
|
||||
.Replace(":", "_")
|
||||
.Replace("<", "_")
|
||||
.Replace(">", "_")
|
||||
.Replace("?", "_")
|
||||
.Replace("*", "_")
|
||||
.Replace("|", "_")
|
||||
.Replace("/", "_", StringComparison.Ordinal)
|
||||
.Replace("\\", "_", StringComparison.Ordinal)
|
||||
.Replace(":", "_", StringComparison.Ordinal)
|
||||
.Replace("<", "_", StringComparison.Ordinal)
|
||||
.Replace(">", "_", StringComparison.Ordinal)
|
||||
.Replace("?", "_", StringComparison.Ordinal)
|
||||
.Replace("*", "_", StringComparison.Ordinal)
|
||||
.Replace("|", "_", StringComparison.Ordinal)
|
||||
+ "-" + Guid.NewGuid();
|
||||
string newPluginPath = Path.Combine(pluginFolderPath, newPluginName);
|
||||
string content = $"Do you want to install following plugin?{Environment.NewLine}{Environment.NewLine}" +
|
||||
@@ -157,12 +158,13 @@ namespace Wox.Core.Plugin
|
||||
/// <param name="overWrite">overwrite</param>
|
||||
private static void UnZip(string zippedFile, string strDirectory, bool overWrite)
|
||||
{
|
||||
if (strDirectory == string.Empty)
|
||||
if (string.IsNullOrEmpty(strDirectory))
|
||||
{
|
||||
strDirectory = Directory.GetCurrentDirectory();
|
||||
}
|
||||
|
||||
if (!strDirectory.EndsWith("\\"))
|
||||
// Using Ordinal since this is a path
|
||||
if (!strDirectory.EndsWith("\\", StringComparison.Ordinal))
|
||||
{
|
||||
strDirectory += "\\";
|
||||
}
|
||||
@@ -177,7 +179,7 @@ namespace Wox.Core.Plugin
|
||||
string pathToZip = string.Empty;
|
||||
pathToZip = theEntry.Name;
|
||||
|
||||
if (pathToZip != string.Empty)
|
||||
if (!string.IsNullOrEmpty(pathToZip))
|
||||
{
|
||||
directoryName = Path.GetDirectoryName(pathToZip) + "\\";
|
||||
}
|
||||
@@ -186,7 +188,7 @@ namespace Wox.Core.Plugin
|
||||
|
||||
Directory.CreateDirectory(strDirectory + directoryName);
|
||||
|
||||
if (fileName != string.Empty)
|
||||
if (!string.IsNullOrEmpty(fileName))
|
||||
{
|
||||
if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
|
||||
{
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@@ -79,17 +80,18 @@ namespace Wox.Core.Plugin
|
||||
public static void LoadPlugins(PluginSettings settings)
|
||||
{
|
||||
_metadatas = PluginConfig.Parse(Directories);
|
||||
Settings = settings;
|
||||
Settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
||||
Settings.UpdatePluginSettings(_metadatas);
|
||||
AllPlugins = PluginsLoader.Plugins(_metadatas, Settings);
|
||||
AllPlugins = PluginsLoader.Plugins(_metadatas);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Call initialize for all plugins
|
||||
/// </summary>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "All exception information is being logged")]
|
||||
public static void InitializePlugins(IPublicAPI api)
|
||||
{
|
||||
API = api;
|
||||
API = api ?? throw new ArgumentNullException(nameof(api));
|
||||
var failedPlugins = new ConcurrentQueue<PluginPair>();
|
||||
Parallel.ForEach(AllPlugins, pair =>
|
||||
{
|
||||
@@ -142,6 +144,11 @@ namespace Wox.Core.Plugin
|
||||
|
||||
public static List<PluginPair> ValidPluginsForQuery(Query query)
|
||||
{
|
||||
if (query == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(query));
|
||||
}
|
||||
|
||||
if (NonGlobalPlugins.ContainsKey(query.ActionKeyword))
|
||||
{
|
||||
var plugin = NonGlobalPlugins[query.ActionKeyword];
|
||||
@@ -153,8 +160,14 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "All exception information is being logged")]
|
||||
public static List<Result> QueryForPlugin(PluginPair pair, Query query, bool delayedExecution = false)
|
||||
{
|
||||
if (pair == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(pair));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
List<Result> results = null;
|
||||
@@ -201,7 +214,8 @@ namespace Wox.Core.Plugin
|
||||
|
||||
if (!string.IsNullOrEmpty(query.ActionKeyword))
|
||||
{
|
||||
result.QueryTextDisplay = string.Format("{0} {1}", query.ActionKeyword, result.QueryTextDisplay);
|
||||
// Using CurrentCulture since this is user facing
|
||||
result.QueryTextDisplay = string.Format(CultureInfo.CurrentCulture, "{0} {1}", query.ActionKeyword, result.QueryTextDisplay);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,6 +224,16 @@ namespace Wox.Core.Plugin
|
||||
|
||||
public static void UpdatePluginMetadata(List<Result> results, PluginMetadata metadata, Query query)
|
||||
{
|
||||
if (results == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(results));
|
||||
}
|
||||
|
||||
if (metadata == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(metadata));
|
||||
}
|
||||
|
||||
foreach (var r in results)
|
||||
{
|
||||
r.PluginDirectory = metadata.PluginDirectory;
|
||||
@@ -239,6 +263,7 @@ namespace Wox.Core.Plugin
|
||||
return AllPlugins.Where(p => p.Plugin is T);
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "All exception information is being logged")]
|
||||
public static List<ContextMenuResult> GetContextMenusForPlugin(Result result)
|
||||
{
|
||||
var pluginPair = _contextMenuPlugins.FirstOrDefault(o => o.Metadata.ID == result.PluginID);
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Wox.Core.Plugin
|
||||
{
|
||||
public const string PATH = "PATH";
|
||||
|
||||
public static List<PluginPair> Plugins(List<PluginMetadata> metadatas, PluginSettings settings)
|
||||
public static List<PluginPair> Plugins(List<PluginMetadata> metadatas)
|
||||
{
|
||||
var csharpPlugins = CSharpPlugins(metadatas).ToList();
|
||||
var executablePlugins = ExecutablePlugins(metadatas);
|
||||
@@ -25,10 +25,11 @@ namespace Wox.Core.Plugin
|
||||
return plugins;
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "All exception information is being logged")]
|
||||
public static IEnumerable<PluginPair> CSharpPlugins(List<PluginMetadata> source)
|
||||
{
|
||||
var plugins = new List<PluginPair>();
|
||||
var metadatas = source.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp);
|
||||
var metadatas = source.Where(o => o.Language.ToUpperInvariant() == AllowedLanguage.CSharp);
|
||||
|
||||
foreach (var metadata in metadatas)
|
||||
{
|
||||
@@ -89,7 +90,7 @@ namespace Wox.Core.Plugin
|
||||
|
||||
public static IEnumerable<PluginPair> ExecutablePlugins(IEnumerable<PluginMetadata> source)
|
||||
{
|
||||
var metadatas = source.Where(o => o.Language.ToUpper() == AllowedLanguage.Executable);
|
||||
var metadatas = source.Where(o => o.Language.ToUpperInvariant() == AllowedLanguage.Executable);
|
||||
|
||||
var plugins = metadatas.Select(metadata => new PluginPair
|
||||
{
|
||||
|
||||
@@ -13,6 +13,16 @@ namespace Wox.Core.Plugin
|
||||
{
|
||||
public static Dictionary<PluginPair, Query> Build(ref string text, Dictionary<string, PluginPair> nonGlobalPlugins)
|
||||
{
|
||||
if (text == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(text));
|
||||
}
|
||||
|
||||
if (nonGlobalPlugins == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(nonGlobalPlugins));
|
||||
}
|
||||
|
||||
// replace multiple white spaces with one white space
|
||||
var terms = text.Split(new[] { Query.TermSeparator }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (terms.Length == 0)
|
||||
@@ -32,7 +42,8 @@ namespace Wox.Core.Plugin
|
||||
|
||||
foreach (string pluginActionKeyword in nonGlobalPlugins.Keys)
|
||||
{
|
||||
if (possibleActionKeyword.StartsWith(pluginActionKeyword))
|
||||
// Using Ordinal since this is used internally
|
||||
if (possibleActionKeyword.StartsWith(pluginActionKeyword, StringComparison.Ordinal))
|
||||
{
|
||||
if (nonGlobalPlugins.TryGetValue(pluginActionKeyword, out var pluginPair) && !pluginPair.Metadata.Disabled)
|
||||
{
|
||||
|
||||
@@ -3,9 +3,12 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using Wox.Infrastructure.Logger;
|
||||
|
||||
namespace Wox.Core.Resource
|
||||
{
|
||||
@@ -24,8 +27,9 @@ namespace Wox.Core.Resource
|
||||
{
|
||||
return (FontWeight)_fontWeightConverter.ConvertFromInvariantString(value);
|
||||
}
|
||||
catch
|
||||
catch (NotSupportedException e)
|
||||
{
|
||||
Log.Exception($"Can't convert {value} to FontWeight", e, MethodBase.GetCurrentMethod().DeclaringType);
|
||||
return FontWeights.Normal;
|
||||
}
|
||||
}
|
||||
@@ -43,8 +47,9 @@ namespace Wox.Core.Resource
|
||||
{
|
||||
return (FontStyle)_fontStyleConverter.ConvertFromInvariantString(value);
|
||||
}
|
||||
catch
|
||||
catch (NotSupportedException e)
|
||||
{
|
||||
Log.Exception($"Can't convert {value} to FontStyle", e, MethodBase.GetCurrentMethod().DeclaringType);
|
||||
return FontStyles.Normal;
|
||||
}
|
||||
}
|
||||
@@ -62,14 +67,20 @@ namespace Wox.Core.Resource
|
||||
{
|
||||
return (FontStretch)_fontStretchConverter.ConvertFromInvariantString(value);
|
||||
}
|
||||
catch
|
||||
catch (NotSupportedException e)
|
||||
{
|
||||
Log.Exception($"Can't convert {value} to FontStretch", e, MethodBase.GetCurrentMethod().DeclaringType);
|
||||
return FontStretches.Normal;
|
||||
}
|
||||
}
|
||||
|
||||
public static FamilyTypeface ChooseRegularFamilyTypeface(this FontFamily family)
|
||||
{
|
||||
if (family == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(family));
|
||||
}
|
||||
|
||||
return family.FamilyTypefaces.OrderBy(o =>
|
||||
{
|
||||
return (Math.Abs(o.Stretch.ToOpenTypeStretch() - FontStretches.Normal.ToOpenTypeStretch()) * 100) +
|
||||
@@ -80,6 +91,11 @@ namespace Wox.Core.Resource
|
||||
|
||||
public static FamilyTypeface ConvertFromInvariantStringsOrNormal(this FontFamily family, string style, string weight, string stretch)
|
||||
{
|
||||
if (family == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(family));
|
||||
}
|
||||
|
||||
var styleObj = GetFontStyleFromInvariantStringOrNormal(style);
|
||||
var weightObj = GetFontWeightFromInvariantStringOrNormal(weight);
|
||||
var stretchObj = GetFontStretchFromInvariantStringOrNormal(stretch);
|
||||
|
||||
Reference in New Issue
Block a user