Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/ScriptCs.Hosting/ModuleLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ namespace ScriptCs.Hosting
{
public class ModuleLoader : IModuleLoader
{
internal static readonly Dictionary<string, string> DefaultCSharpModules = new Dictionary<string, string>
{
{"roslyn", "ScriptCs.Engine.Roslyn.dll"},
{"mono", "ScriptCs.Engine.Mono.dll"}
};

private readonly IAssemblyResolver _resolver;
private readonly ILog _logger;
private readonly Action<Assembly, AggregateCatalog> _addToCatalog;
Expand Down Expand Up @@ -58,11 +64,36 @@ public ModuleLoader(IAssemblyResolver resolver, ILog logger, Action<Assembly, Ag
_assemblyUtility = assemblyUtility;
}

public void Load(IModuleConfiguration config, string[] modulePackagesPaths, string hostBin, string extension, params string[] moduleNames)
public void Load(IModuleConfiguration config, string[] modulePackagesPaths, string hostBin, string extension,
params string[] moduleNames)
{
if (modulePackagesPaths == null) return;

_logger.Debug("Loading modules from: " + string.Join(", ", modulePackagesPaths.Select(i => i)));
if (moduleNames.Length == 1 && DefaultCSharpModules.ContainsKey(moduleNames[0])) // only CSharp module needed
{
_logger.Debug("Only CSharp module is needed - will skip module lookup");
var csharpModuleAssembly = DefaultCSharpModules[moduleNames[0]];
var module = _assemblyUtility.LoadFile(Path.Combine(hostBin, csharpModuleAssembly));

if (module != null)
{
var moduleType = module.GetExportedTypes().FirstOrDefault(f => typeof (IModule).IsAssignableFrom(f));

if (moduleType != null)
{
var moduleInstance = Activator.CreateInstance(moduleType) as IModule;

if (moduleInstance != null)
{
_logger.Debug(String.Format("Initializing module: {0}", module.GetType().FullName));
moduleInstance.Initialize(config);
return;
}
}
}
}

_logger.Debug("Loading modules from: " + String.Join(", ", modulePackagesPaths.Select(i => i)));
var paths = new List<string>();

AddPaths(modulePackagesPaths, hostBin, paths);
Expand Down Expand Up @@ -91,7 +122,7 @@ private void InitializeModules(

foreach (var module in modules)
{
_logger.Debug(string.Format("Initializing module: {0}", module.GetType().FullName));
_logger.Debug(String.Format("Initializing module: {0}", module.GetType().FullName));
module.Initialize(config);
}

Expand Down
3 changes: 3 additions & 0 deletions src/ScriptCs.Hosting/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: AssemblyTitle("ScriptCs.Hosting")]
[assembly: AssemblyDescription("ScriptCs.Hosting provides common services necessary for hosting scriptcs in your application.")]

[assembly: InternalsVisibleTo("ScriptCs.Hosting.Tests")]
10 changes: 6 additions & 4 deletions src/ScriptCs.Hosting/ScriptServicesBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Common.Logging;
using ScriptCs.Contracts;
Expand Down Expand Up @@ -71,16 +72,17 @@ public ScriptServices Build()
public IScriptServicesBuilder LoadModules(string extension, params string[] moduleNames)
{
var engineModule = _typeResolver.ResolveType("Mono.Runtime") != null || moduleNames.Contains("mono")
? "mono"
? "mono"
: "roslyn";

moduleNames = moduleNames.Union(new[] { engineModule }).ToArray();

moduleNames = moduleNames.Union(new[] {engineModule}).ToArray();

var config = new ModuleConfiguration(_cache, _scriptName, _repl, _logLevel, _debug, Overrides);
var loader = InitializationServices.GetModuleLoader();

var fs = InitializationServices.GetFileSystem();

var folders = new[] { fs.GlobalFolder };
var folders = new[] {fs.GlobalFolder};
loader.Load(config, folders, InitializationServices.GetFileSystem().HostBin, extension, moduleNames);
return this;
}
Expand Down
33 changes: 33 additions & 0 deletions test/ScriptCs.Hosting.Tests/ModuleLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using ScriptCs.Contracts;
using Should;
using Xunit;
using LogLevel = ScriptCs.Contracts.LogLevel;

namespace ScriptCs.Hosting.Tests
{
Expand Down Expand Up @@ -110,6 +111,30 @@ public void ShouldNotInitializeModulesThatAreNotSetToAutoload()
_mockModule1.Verify(m => m.Initialize(It.IsAny<IModuleConfiguration>()), Times.Never());
}

[Fact]
public void ShouldLoadEngineAssemblyByHandIfItsTheOnlyModule()
{
var path = Path.Combine("c:\\foo", ModuleLoader.DefaultCSharpModules["roslyn"]);
_mockAssemblyUtility.Setup(x => x.LoadFile(path));
var loader = new ModuleLoader(_mockAssemblyResolver.Object, _mockLogger.Object, (a, c) => { }, _getModules, _mockFileSystem.Object, _mockAssemblyUtility.Object);
loader.Load(null, new string[0], "c:\\foo", null, "roslyn");

_mockAssemblyUtility.Verify(x => x.LoadFile(path), Times.Once());
}

[Fact]
public void ShouldLoadEngineModuleFromFile()
{
_mockAssemblyUtility.Setup(x => x.LoadFile(It.IsAny<string>())).Returns(typeof (DummyModule).Assembly);
var loader = new ModuleLoader(_mockAssemblyResolver.Object, _mockLogger.Object, (a, c) => { }, _getModules, _mockFileSystem.Object, _mockAssemblyUtility.Object);

var config = new ModuleConfiguration(true, string.Empty, false, LogLevel.Debug, true,
new Dictionary<Type, object> {{typeof (string), "not loaded"}});
loader.Load(config, new string[0], "c:\\foo", null, "roslyn");

config.Overrides[typeof(string)].ShouldEqual("module loaded");
}

public class ModuleMetadata : IModuleMetadata
{
public string Name { get; set; }
Expand All @@ -118,6 +143,14 @@ public class ModuleMetadata : IModuleMetadata

public bool Autoload { get; set; }
}

public class DummyModule : IModule
{
public void Initialize(IModuleConfiguration config)
{
config.Overrides[typeof (string)] = "module loaded";
}
}
}
}
}