From 00305129c85db50f45250a6e04b17bae6bc5d729 Mon Sep 17 00:00:00 2001 From: Adam Ralph Date: Thu, 5 Feb 2015 12:05:54 +0100 Subject: [PATCH] various refactorings: internalised/privatised implementation details expanded parameter types made fields readonly converted simple delegates to expressions converted conditionals to LINQ calls converted for..each list add to AddRange() removed redundant code whitespace fixes added R# settings --- ScriptCs.sln.DotSettings | 2 + .../IAppDomainAssemblyResolver.cs | 1 - .../AppDomainAssemblyResolver.cs | 2 +- src/ScriptCs.Core/AssemblyResolver.cs | 6 +-- src/ScriptCs.Hosting/ModuleLoader.cs | 39 +++++++++---------- .../AssemblyResolverTests.cs | 2 +- 6 files changed, 25 insertions(+), 27 deletions(-) create mode 100644 ScriptCs.sln.DotSettings diff --git a/ScriptCs.sln.DotSettings b/ScriptCs.sln.DotSettings new file mode 100644 index 00000000..993cd6d0 --- /dev/null +++ b/ScriptCs.sln.DotSettings @@ -0,0 +1,2 @@ + + <Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /> \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IAppDomainAssemblyResolver.cs b/src/ScriptCs.Contracts/IAppDomainAssemblyResolver.cs index 5954da70..ff805473 100644 --- a/src/ScriptCs.Contracts/IAppDomainAssemblyResolver.cs +++ b/src/ScriptCs.Contracts/IAppDomainAssemblyResolver.cs @@ -4,7 +4,6 @@ namespace ScriptCs.Contracts { public interface IAppDomainAssemblyResolver { - void AddAssemblyPaths(IEnumerable assemblyPaths); void Initialize(); } } \ No newline at end of file diff --git a/src/ScriptCs.Core/AppDomainAssemblyResolver.cs b/src/ScriptCs.Core/AppDomainAssemblyResolver.cs index b5c48247..82580d23 100644 --- a/src/ScriptCs.Core/AppDomainAssemblyResolver.cs +++ b/src/ScriptCs.Core/AppDomainAssemblyResolver.cs @@ -13,7 +13,7 @@ public class AppDomainAssemblyResolver : IAppDomainAssemblyResolver private readonly IFileSystem _fileSystem; private readonly IAssemblyResolver _resolver; private readonly IAssemblyUtility _assemblyUtility; - private IDictionary _assemblyInfoMap; + private readonly IDictionary _assemblyInfoMap; public AppDomainAssemblyResolver( ILog logger, diff --git a/src/ScriptCs.Core/AssemblyResolver.cs b/src/ScriptCs.Core/AssemblyResolver.cs index ec7bea57..a129deac 100644 --- a/src/ScriptCs.Core/AssemblyResolver.cs +++ b/src/ScriptCs.Core/AssemblyResolver.cs @@ -2,9 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reflection.Emit; using Common.Logging; - using ScriptCs.Contracts; namespace ScriptCs @@ -12,7 +10,7 @@ namespace ScriptCs public class AssemblyResolver : IAssemblyResolver { private readonly Dictionary> _assemblyPathCache = new Dictionary>(); - + private readonly IFileSystem _fileSystem; private readonly IPackageAssemblyResolver _packageAssemblyResolver; @@ -62,7 +60,7 @@ public IEnumerable GetAssemblyPaths(string path, bool binariesOnly = fal return assemblies; } - public IEnumerable GetBinAssemblyPaths(string path) + private IEnumerable GetBinAssemblyPaths(string path) { var binFolder = Path.Combine(path, _fileSystem.BinFolder); if (!_fileSystem.DirectoryExists(binFolder)) diff --git a/src/ScriptCs.Hosting/ModuleLoader.cs b/src/ScriptCs.Hosting/ModuleLoader.cs index efe98773..c6ff0c1e 100644 --- a/src/ScriptCs.Hosting/ModuleLoader.cs +++ b/src/ScriptCs.Hosting/ModuleLoader.cs @@ -5,8 +5,6 @@ using System.IO; using System.Linq; using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using Common.Logging; using ScriptCs.Contracts; @@ -23,8 +21,8 @@ public class ModuleLoader : IModuleLoader [ImportingConstructor] public ModuleLoader(IAssemblyResolver resolver, ILog logger, IFileSystem fileSystem, IAssemblyUtility assemblyUtility) : - this(resolver, logger, null, null, fileSystem, assemblyUtility ) - { + this(resolver, logger, null, null, fileSystem, assemblyUtility) + { } public ModuleLoader(IAssemblyResolver resolver, ILog logger, Action addToCatalog, Func>> getLazyModules, IFileSystem fileSystem, IAssemblyUtility assemblyUtility) @@ -41,7 +39,7 @@ public ModuleLoader(IAssemblyResolver resolver, ILog logger, Action - { - return container.GetExports(); - }; + getLazyModules = container => container.GetExports(); } + _getLazyModules = getLazyModules; _fileSystem = fileSystem; _assemblyUtility = assemblyUtility; @@ -78,13 +74,17 @@ public void Load(IModuleConfiguration config, string[] modulePackagesPaths, stri InitializeModules(config, extension, moduleNames, lazyModules); } - private void InitializeModules(IModuleConfiguration config, string extension, string[] moduleNames, + private void InitializeModules( + IModuleConfiguration config, + string extension, + IEnumerable moduleNames, IEnumerable> lazyModules) { var modules = lazyModules .Where(m => moduleNames.Contains(m.Metadata.Name) || (extension != null && m.Metadata.Extensions != null && - (m.Metadata.Extensions.Split(',').Contains(extension))) || m.Metadata.Autoload == true) + (m.Metadata.Extensions.Split(',').Contains(extension))) || + m.Metadata.Autoload) .Select(m => m.Value); _logger.Debug("Initializing modules"); @@ -98,7 +98,7 @@ private void InitializeModules(IModuleConfiguration config, string extension, st _logger.Debug("Modules initialized"); } - private AggregateCatalog CreateAggregateCatalog(List paths) + private AggregateCatalog CreateAggregateCatalog(IEnumerable paths) { var catalog = new AggregateCatalog(); foreach (var path in paths) @@ -127,27 +127,24 @@ private AggregateCatalog CreateAggregateCatalog(List paths) return catalog; } - private void AddPaths(string[] modulePackagesPaths, string hostBin, List paths) + private void AddPaths(IEnumerable modulePackagesPaths, string hostBin, List paths) { - foreach (var modulePackagesPath in modulePackagesPaths) + foreach (var modulePaths in modulePackagesPaths + .Select(modulePackagesPath => _resolver.GetAssemblyPaths(modulePackagesPath, true))) { - var modulePaths = _resolver.GetAssemblyPaths(modulePackagesPath, true); paths.AddRange(modulePaths); } if (hostBin != null) { var assemblyPaths = _fileSystem.EnumerateBinaries(hostBin, SearchOption.TopDirectoryOnly); - foreach (var path in assemblyPaths) - { - paths.Add(path); - } + paths.AddRange(assemblyPaths); } } private IEnumerable> GetLazyModules(CompositionContainer container) { - IEnumerable> lazyModules = null; + IEnumerable> lazyModules; try { @@ -166,8 +163,10 @@ private IEnumerable> GetLazyModules(CompositionCo { _logger.DebugFormat("Module Loader exception: {0}", exception.Message); } + lazyModules = Enumerable.Empty>(); } + return lazyModules; } } diff --git a/test/ScriptCs.Core.Tests/AssemblyResolverTests.cs b/test/ScriptCs.Core.Tests/AssemblyResolverTests.cs index 777dfcb5..088af58f 100644 --- a/test/ScriptCs.Core.Tests/AssemblyResolverTests.cs +++ b/test/ScriptCs.Core.Tests/AssemblyResolverTests.cs @@ -110,7 +110,7 @@ public void ShouldOnlyReturnBinariesWhenFlagIsSet( AssemblyResolver resolver) { const string WorkingDirectory = @"C:\"; - + var binFolder = Path.Combine(WorkingDirectory, "bin"); assemblyUtilityMock.Setup(a => a.IsManagedAssembly(It.IsAny())).Returns(true);