From 9690d71ea6609d023de1e6ddca94b3745c083064 Mon Sep 17 00:00:00 2001 From: Yuri Krivosheyev Date: Thu, 16 Apr 2020 11:10:19 +0300 Subject: [PATCH] Fixes issue #376 by introducing the IgnoreAssembly method which ignores all assemblies from a set of publishers defined by a RegEx. Also a possibility of deploying assemblies from only a specified publisher is added, the 'solution prefix' command line argument serves as publisher prefix source. --- spkl/SparkleXrm.Tasks/PluginRegistraton.cs | 8 +++---- spkl/SparkleXrm.Tasks/Reflection.cs | 23 +++++++++++++++++++ .../Tasks/DeployPluginsTask.cs | 4 +++- .../Tasks/DeployWorkflowActivitiesTask.cs | 4 +++- spkl/spkl/Program.cs | 10 ++++++-- 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/spkl/SparkleXrm.Tasks/PluginRegistraton.cs b/spkl/SparkleXrm.Tasks/PluginRegistraton.cs index 8359c500..640aae8c 100644 --- a/spkl/SparkleXrm.Tasks/PluginRegistraton.cs +++ b/spkl/SparkleXrm.Tasks/PluginRegistraton.cs @@ -30,10 +30,10 @@ public PluginRegistraton(IOrganizationService service, OrganizationServiceContex /// public string SolutionUniqueName { get; set; } - public void RegisterWorkflowActivities(string path) + public void RegisterWorkflowActivities(string path, string publisherPrefix = "") { var assemblyFilePath = new FileInfo(path); - if (Reflection.IgnoredAssemblies.Contains(assemblyFilePath.Name)) + if (Reflection.IgnoreAssembly(assemblyFilePath, publisherPrefix)) return; // Load each assembly Assembly assembly = Reflection.LoadAssembly(assemblyFilePath.FullName); @@ -148,11 +148,11 @@ private void AddStepToSolution(string solutionName, SdkMessageProcessingStep sdk _service.Execute(addToSolution); } - public void RegisterPlugin(string file, bool excludePluginSteps = false) + public void RegisterPlugin(string file, bool excludePluginSteps = false, string publisherPrefix = "") { var assemblyFilePath = new FileInfo(file); - if (assemblyFilePath.Name.StartsWith("System.") || Reflection.IgnoredAssemblies.Contains(assemblyFilePath.Name)) + if (Reflection.IgnoreAssembly(assemblyFilePath, publisherPrefix)) return; // Load each assembly diff --git a/spkl/SparkleXrm.Tasks/Reflection.cs b/spkl/SparkleXrm.Tasks/Reflection.cs index c2d25204..8d02e238 100644 --- a/spkl/SparkleXrm.Tasks/Reflection.cs +++ b/spkl/SparkleXrm.Tasks/Reflection.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Reflection; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace SparkleXrm.Tasks @@ -28,6 +29,8 @@ public class Reflection "Microsoft.Rest.ClientRuntime.dll" }; + private static Regex _ignoredPublisherRegEx = new Regex("^system|microsoft|newtonsoft|sparklexrm.*/.dll"); + public static Assembly LoadAssembly(string path) { Assembly assembly = null; @@ -43,6 +46,26 @@ public static Assembly LoadAssembly(string path) return assembly; } + public static bool IgnoreAssembly(FileInfo assemblyFilePath, string publisherPrefix = "", bool onlyPublisherAssemblies = false) + { + var assemblyName = assemblyFilePath.Name.ToLower(); + + // if publisher prefix is specified we consider assemblies starting with this prefix OK to deploy + if (!string.IsNullOrEmpty(publisherPrefix)) + { + if (assemblyName.StartsWith(publisherPrefix.ToLower())) + { + return false; + } + else if (onlyPublisherAssemblies) + { + return true; // ignore other assemblies if the corresponding flag is raised + } + } + + return _ignoredPublisherRegEx.IsMatch(assemblyName); + } + public static IEnumerable GetTypesImplementingInterface(Assembly assembly, Type interfaceName) { var types = assembly.ExportedTypes.Where(p => p.GetInterfaces().FirstOrDefault(a => a.Name == interfaceName.Name) != null); diff --git a/spkl/SparkleXrm.Tasks/Tasks/DeployPluginsTask.cs b/spkl/SparkleXrm.Tasks/Tasks/DeployPluginsTask.cs index 77c96dcd..b6676a2b 100644 --- a/spkl/SparkleXrm.Tasks/Tasks/DeployPluginsTask.cs +++ b/spkl/SparkleXrm.Tasks/Tasks/DeployPluginsTask.cs @@ -16,6 +16,8 @@ public class DeployPluginsTask : BaseTask { public bool ExcludePluginSteps { get; set; } + public string PublisherPrefix { get; set; } + public DeployPluginsTask(IOrganizationService service, ITrace trace) : base(service, trace) { @@ -57,7 +59,7 @@ private void DeployPlugins(OrganizationServiceContext ctx, ConfigFile config) { try { - pluginRegistration.RegisterPlugin(assemblyFilePath, ExcludePluginSteps); + pluginRegistration.RegisterPlugin(assemblyFilePath, ExcludePluginSteps, PublisherPrefix); } catch (ReflectionTypeLoadException ex) diff --git a/spkl/SparkleXrm.Tasks/Tasks/DeployWorkflowActivitiesTask.cs b/spkl/SparkleXrm.Tasks/Tasks/DeployWorkflowActivitiesTask.cs index 51019cdf..ec1af5b8 100644 --- a/spkl/SparkleXrm.Tasks/Tasks/DeployWorkflowActivitiesTask.cs +++ b/spkl/SparkleXrm.Tasks/Tasks/DeployWorkflowActivitiesTask.cs @@ -13,6 +13,8 @@ namespace SparkleXrm.Tasks { public class DeployWorkflowActivitiesTask : BaseTask { + public string PublisherPrefix { get; set; } + public DeployWorkflowActivitiesTask(IOrganizationService service, ITrace trace) : base(service, trace) { } @@ -49,7 +51,7 @@ private void DeployWorkflowActivities(OrganizationServiceContext ctx, ConfigFile { try { - pluginRegistration.RegisterWorkflowActivities(assemblyFilePath); + pluginRegistration.RegisterWorkflowActivities(assemblyFilePath, PublisherPrefix); } catch (ReflectionTypeLoadException ex) { diff --git a/spkl/spkl/Program.cs b/spkl/spkl/Program.cs index f314b441..65e23afd 100644 --- a/spkl/spkl/Program.cs +++ b/spkl/spkl/Program.cs @@ -325,12 +325,18 @@ private static void RunTask(CommandLineArgs arguments, IOrganizationService serv case "plugins": trace.WriteLine("Deploying Plugins"); task = new DeployPluginsTask(service, trace) - { ExcludePluginSteps = arguments.ExcludePluginSteps }; + { + ExcludePluginSteps = arguments.ExcludePluginSteps, + PublisherPrefix = arguments.Prefix + }; break; case "workflow": trace.WriteLine("Deploying Custom Workflow Activities"); - task = new DeployWorkflowActivitiesTask(service, trace); + task = new DeployWorkflowActivitiesTask(service, trace) + { + PublisherPrefix = arguments.Prefix + }; break; case "webresources":