Skip to content

Commit 62bdcb1

Browse files
committed
replaced ShowCommand with ReferencesCommand, UsingsCommand and VarsCommand
1 parent 702c90b commit 62bdcb1

8 files changed

Lines changed: 94 additions & 83 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Linq;
2+
using ScriptCs.Contracts;
3+
4+
namespace ScriptCs.ReplCommands
5+
{
6+
public class ReferencesCommand : IReplCommand
7+
{
8+
public string CommandName
9+
{
10+
get { return "refs"; }
11+
}
12+
13+
public object Execute(IScriptExecutor repl, object[] args)
14+
{
15+
if (repl.References != null)
16+
{
17+
return repl.References.Assemblies.Select(x => x.FullName).Union(repl.References.PathReferences);
18+
}
19+
20+
return null;
21+
}
22+
}
23+
}

src/ScriptCs.Core/ReplCommands/ShowCommand.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using ScriptCs.Contracts;
2+
3+
namespace ScriptCs.ReplCommands
4+
{
5+
public class UsingsCommand : IReplCommand
6+
{
7+
public string CommandName
8+
{
9+
get { return "usings"; }
10+
}
11+
12+
public object Execute(IScriptExecutor repl, object[] args)
13+
{
14+
return repl.Namespaces;
15+
}
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using ScriptCs.Contracts;
2+
3+
namespace ScriptCs.ReplCommands
4+
{
5+
public class VarsCommand : IReplCommand
6+
{
7+
public string CommandName
8+
{
9+
get { return "vars"; }
10+
}
11+
12+
public object Execute(IScriptExecutor repl, object[] args)
13+
{
14+
var replEngine = repl.ScriptEngine as IReplEngine;
15+
if (replEngine != null)
16+
{
17+
return replEngine.LocalVariables;
18+
}
19+
20+
return null;
21+
}
22+
}
23+
}

src/ScriptCs.Core/ScriptCs.Core.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@
7676
<Compile Include="Properties\AssemblyInfo.cs" />
7777
<Compile Include="ReferenceLineProcessor.cs" />
7878
<Compile Include="Repl.cs" />
79+
<Compile Include="ReplCommands\ReferencesCommand.cs" />
7980
<Compile Include="ReplCommands\ResetCommand.cs" />
80-
<Compile Include="ReplCommands\ShowCommand.cs" />
81+
<Compile Include="ReplCommands\UsingsCommand.cs" />
82+
<Compile Include="ReplCommands\VarsCommand.cs" />
8183
<Compile Include="ScriptEnvironment.cs" />
8284
<Compile Include="ScriptExecutorExtensions.cs" />
8385
<Compile Include="ScriptHost.cs" />

src/ScriptCs.Engine.Roslyn/RoslynReplEngine.cs

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,42 @@ public class RoslynReplEngine : RoslynScriptEngine, IReplEngine
1313
{
1414
public RoslynReplEngine(IScriptHostFactory scriptHostFactory, ILog logger) : base(scriptHostFactory, logger)
1515
{
16-
LocalVariables = new Collection<string>();
1716
}
1817

19-
public ICollection<string> LocalVariables { get; private set; }
20-
21-
protected override ScriptResult Execute(string code, Session session)
18+
public ICollection<string> LocalVariables
2219
{
23-
Guard.AgainstNullArgument("session", session);
24-
25-
if (string.IsNullOrWhiteSpace(FileName) && !IsCompleteSubmission(code))
26-
{
27-
return ScriptResult.Incomplete;
28-
}
29-
30-
try
20+
get
3121
{
32-
var submission = session.CompileSubmission<object>(code);
22+
var submissionObjectField = Session.GetType().GetField("submissions", BindingFlags.Instance | BindingFlags.NonPublic);
23+
var submissionObjects = (submissionObjectField.GetValue(Session) as object[]).Where(x => x != null);
24+
var variables = new Collection<string>();
3325

34-
try
26+
if (submissionObjects.Any())
3527
{
36-
var result = submission.Execute();
37-
38-
var submissionObjectField = session.GetType().GetField("submissions", BindingFlags.Instance | BindingFlags.NonPublic);
39-
var submissionObject = (submissionObjectField.GetValue(session) as object[]).LastOrDefault(x => x != null);
40-
41-
if (submissionObject != null)
28+
foreach (var submissionObject in submissionObjects)
4229
{
4330
var fields = submissionObject.GetType().GetFields().Where(x => x.Name.ToLowerInvariant() != "<host-object>");
4431
foreach (var field in fields)
4532
{
46-
LocalVariables.Add(string.Format("{0} {1} = {2}", field.FieldType, field.Name, field.GetValue(submissionObject)));
47-
}
33+
variables.Add(string.Format("{0} {1} = {2}", field.FieldType, field.Name, field.GetValue(submissionObject)));
34+
}
4835
}
49-
50-
return new ScriptResult(returnValue: result);
51-
}
52-
catch (Exception ex)
53-
{
54-
return new ScriptResult(executionException: ex);
5536
}
37+
38+
return variables;
5639
}
57-
catch (Exception ex)
40+
}
41+
42+
protected override ScriptResult Execute(string code, Session session)
43+
{
44+
Guard.AgainstNullArgument("session", session);
45+
46+
if (string.IsNullOrWhiteSpace(FileName) && !IsCompleteSubmission(code))
5847
{
59-
return new ScriptResult(compilationException: ex);
48+
return ScriptResult.Incomplete;
6049
}
50+
51+
return base.Execute(code, session);
6152
}
6253
}
6354
}

src/ScriptCs.Engine.Roslyn/RoslynScriptEngine.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class RoslynScriptEngine : IScriptEngine
1616
{
1717
protected readonly ScriptEngine ScriptEngine;
1818
private readonly IScriptHostFactory _scriptHostFactory;
19+
protected Session Session;
1920

2021
public const string SessionKey = "Session";
2122
private const string InvalidNamespaceError = "error CS0246";
@@ -63,28 +64,28 @@ public ScriptResult Execute(string code, string[] scriptArgs, AssemblyReferences
6364

6465
var hostType = host.GetType();
6566
ScriptEngine.AddReference(hostType.Assembly);
66-
var session = ScriptEngine.CreateSession(host, hostType);
67+
Session = ScriptEngine.CreateSession(host, hostType);
6768
var allNamespaces = namespaces.Union(scriptPackSession.Namespaces).Distinct();
6869

6970
foreach (var reference in executionReferences.PathReferences)
7071
{
7172
Logger.DebugFormat("Adding reference to {0}", reference);
72-
session.AddReference(reference);
73+
Session.AddReference(reference);
7374
}
7475

7576
foreach (var assembly in executionReferences.Assemblies)
7677
{
7778
Logger.DebugFormat("Adding reference to {0}", assembly.FullName);
78-
session.AddReference(assembly);
79+
Session.AddReference(assembly);
7980
}
8081

8182
foreach (var @namespace in allNamespaces)
8283
{
8384
Logger.DebugFormat("Importing namespace {0}", @namespace);
84-
session.ImportNamespace(@namespace);
85+
Session.ImportNamespace(@namespace);
8586
}
8687

87-
sessionState = new SessionState<Session> { References = executionReferences, Session = session, Namespaces = new HashSet<string>(allNamespaces) };
88+
sessionState = new SessionState<Session> { References = executionReferences, Session = Session, Namespaces = new HashSet<string>(allNamespaces) };
8889
scriptPackSession.State[SessionKey] = sessionState;
8990
}
9091
else

test/ScriptCs.Engine.Roslyn.Tests/RoslynScriptEngineTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ public RoslynTestScriptEngine(IScriptHostFactory scriptHostFactory, ILog logger)
425425
{
426426
}
427427

428-
public Session Session { get; private set; }
428+
public new Session Session { get; private set; }
429429

430430
protected override ScriptResult Execute(string code, Session session)
431431
{

0 commit comments

Comments
 (0)