Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Change: Create ScriptCs host based on concrete type rather than IScriptHost interface #508

Merged
merged 5 commits into from

3 participants

@filipw
Owner

This is a follow up to #506.

1) we now create the host based on the host type not IScriptHost interface (all type members are accessible from script)
2) IScriptHost is now only a marker interface. Implementing custom host can mean I don't have to use ScriptArgs or Require
For example

public class MyScriptHostFactory : IScriptHostFactory
{
    public IScriptHost CreateScriptHost(IScriptPackManager scriptPackManager, string[] scriptArgs)
    {
        return new MyHost();
    }
}

public class MyHost: IScriptHost
{
    public void SayHi()
    {
        Console.WriteLine("Hi");
    }
}

However I can choose to support them ,and add my own host methods (by inheriting from ScriptHost):

public class MyScriptHostFactory : IScriptHostFactory
{
    public IScriptHost CreateScriptHost(IScriptPackManager scriptPackManager, string[] scriptArgs)
    {
        return new MyHost(scriptPackManager, scriptArgs);
    }
}

public class MyHost: ScriptHost
{
    public MyHost(IScriptPackManager scriptPackManager, string[] scriptArgs) : base(scriptPackManager, scriptArgs)
    {
    }

    public void SayHi()
    {
        Console.WriteLine("Hi");
    }
}

3) IScriptHostFactory and IScriptHost have been moved to Contracts. This is finally possible as we don't need an explicit reference to contracts in the Roslyn Engine (since the concrete host type, is not there). This makes it much easier to create custom hosts too. Also Added IScriptHostFactory to builder/overrides

4) Changed some tests since Castle Proxy mocks cannot expose assembly property of their type (dynamic) so we need to test against concrete types

@adamralph

public field?

@jrusbatch jrusbatch merged commit 351e708 into from
@glennblock glennblock referenced this pull request in Code52/pretzel
Open

Plugins/scripts #217

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
6 src/ScriptCs.Contracts/IScriptHost.cs
@@ -0,0 +1,6 @@
+namespace ScriptCs.Contracts
+{
+ public interface IScriptHost
+ {
+ }
+}
View
4 src/ScriptCs.Core/IScriptHostFactory.cs → src/ScriptCs.Contracts/IScriptHostFactory.cs
@@ -1,6 +1,4 @@
-using ScriptCs.Contracts;
-
-namespace ScriptCs
+namespace ScriptCs.Contracts
{
public interface IScriptHostFactory
{
View
2  src/ScriptCs.Contracts/IServiceOverrides.cs
@@ -31,5 +31,7 @@ public interface IServiceOverrides<out TConfig> : IServiceOverrides where TConfi
TConfig LineProcessor<T>() where T : ILineProcessor;
TConfig Console<T>() where T : IConsole;
+
+ TConfig ScriptHostFactory<T>() where T : IScriptHostFactory;
}
}
View
2  src/ScriptCs.Contracts/ScriptCs.Contracts.csproj
@@ -45,6 +45,8 @@
<Compile Include="IPackageReference.cs" />
<Compile Include="IScriptEngine.cs" />
<Compile Include="IScriptExecutor.cs" />
+ <Compile Include="IScriptHost.cs" />
+ <Compile Include="IScriptHostFactory.cs" />
<Compile Include="IScriptPack.cs" />
<Compile Include="IScriptPackContext.cs" />
<Compile Include="IScriptPackManager.cs" />
View
11 src/ScriptCs.Core/IScriptHost.cs
@@ -1,11 +0,0 @@
-using ScriptCs.Contracts;
-
-namespace ScriptCs
-{
- public interface IScriptHost
- {
- string[] ScriptArgs { get; }
-
- T Require<T>() where T : IScriptPackContext;
- }
-}
View
2  src/ScriptCs.Core/ScriptCs.Core.csproj
@@ -41,8 +41,6 @@
<Compile Include="FileSystem.cs" />
<Compile Include="Guard.cs" />
<Compile Include="ILoggerConfigurator.cs" />
- <Compile Include="IScriptHost.cs" />
- <Compile Include="IScriptHostFactory.cs" />
<Compile Include="LoadLineProcessor.cs" />
<Compile Include="PackageAssemblyResolver.cs" />
<Compile Include="Constants.cs" />
View
5 src/ScriptCs.Engine.Roslyn/RoslynScriptEngine.cs
@@ -49,7 +49,10 @@ public ScriptResult Execute(string code, string[] scriptArgs, IEnumerable<string
{
var host = _scriptHostFactory.CreateScriptHost(new ScriptPackManager(scriptPackSession.Contexts), scriptArgs);
Logger.Debug("Creating session");
- var session = ScriptEngine.CreateSession(host);
+
+ var hostType = host.GetType();
+ ScriptEngine.AddReference(hostType.Assembly);
+ var session = ScriptEngine.CreateSession(host, hostType);
foreach (var reference in distinctReferences)
{
View
1  src/ScriptCs.Engine.Roslyn/RoslynScriptInMemoryEngine.cs
@@ -1,6 +1,7 @@
using System;
using System.Reflection;
using Common.Logging;
+using ScriptCs.Contracts;
namespace ScriptCs.Engine.Roslyn
{
View
8 src/ScriptCs.Hosting/ServiceOverrides.cs
@@ -10,7 +10,7 @@ public abstract class ServiceOverrides<TConfig> : IServiceOverrides<TConfig>
{
protected readonly IList<Type> LineProcessors = new List<Type>();
- protected readonly IDictionary<Type, object> Overrides = new Dictionary<Type, object>();
+ public readonly IDictionary<Type, object> Overrides = new Dictionary<Type, object>();
private readonly TConfig _this;
@@ -19,6 +19,12 @@ protected ServiceOverrides()
_this = this as TConfig;
}
+ public TConfig ScriptHostFactory<T>() where T : IScriptHostFactory
+ {
+ Overrides[typeof (IScriptHostFactory)] = typeof (T);
+ return _this;
+ }
+
protected ServiceOverrides(IDictionary<Type, object> overrides)
{
Overrides = overrides;
View
12 test/ScriptCs.Engine.Roslyn.Tests/RoslynScriptInMemoryEngineTests.cs
@@ -2,7 +2,8 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
-
+using Common.Logging;
+using Moq;
using Ploeh.AutoFixture.Xunit;
using Roslyn.Compilers;
using ScriptCs.Contracts;
@@ -20,9 +21,9 @@ public class RoslynScriptInMemoryEngineTests
public class TheExecuteMethod
{
[Theory, ScriptCsAutoData]
- public void ShouldExposeExceptionThrownByScriptWhenErrorOccurs(
- [NoAutoProperties] RoslynScriptInMemoryEngine scriptEngine)
+ public void ShouldExposeExceptionThrownByScriptWhenErrorOccurs()
{
+ var scriptEngine = new RoslynScriptInMemoryEngine(new ScriptHostFactory(), new Mock<ILog>().Object);
// Arrange
var lines = new List<string>
{
@@ -44,9 +45,10 @@ public class TheExecuteMethod
}
[Theory, ScriptCsAutoData]
- public void ShouldExposeExceptionThrownByCompilation(
- [NoAutoProperties] RoslynScriptInMemoryEngine scriptEngine)
+ public void ShouldExposeExceptionThrownByCompilation()
{
+ var scriptEngine = new RoslynScriptInMemoryEngine(new ScriptHostFactory(), new Mock<ILog>().Object);
+
// Arrange
var lines = new List<string>
{
Something went wrong with that request. Please try again.