Skip to content

Commit 06b4293

Browse files
committed
Support release mode for eval
1 parent e95f172 commit 06b4293

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

src/Dotnet.Script.Tests/ScriptExecutionTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ public void ShouldEvaluateCode()
175175
Assert.Contains("12345", result.output);
176176
}
177177

178+
[Fact]
179+
public void ShouldEvaluateCodeInReleaseMode()
180+
{
181+
var code = File.ReadAllText(Path.Combine("..", "..", "..", "TestFixtures", "Configuration", "Configuration.csx"));
182+
var result = ExecuteCodeInReleaseMode(code);
183+
Assert.Contains("false", result.output, StringComparison.OrdinalIgnoreCase);
184+
}
185+
178186
[Fact]
179187
public void ShouldSupportInlineNugetReferencesinEvaluatedCode()
180188
{
@@ -266,6 +274,12 @@ private static (string output, int exitCode) ExecuteCode(string code)
266274
return result;
267275
}
268276

277+
private static (string output, int exitCode) ExecuteCodeInReleaseMode(string code)
278+
{
279+
var result = ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments($"-c", new[] {"release", "eval", $"\"{code}\"" }));
280+
return result;
281+
}
282+
269283
/// <summary>
270284
/// Use this method if you need to debug
271285
/// </summary>
@@ -280,6 +294,21 @@ private static int ExecuteInProcess(string fixture, params string[] arguments)
280294
return Program.Main(allArguments.ToArray());
281295
}
282296

297+
/// <summary>
298+
/// Use this method if you need to debug
299+
/// </summary>
300+
private static int ExecuteCodeProcess(string code, params string[] arguments)
301+
{
302+
var allArguments = new List<string>();
303+
if (arguments != null)
304+
{
305+
allArguments.AddRange(arguments);
306+
}
307+
allArguments.Add("eval");
308+
allArguments.Add(code);
309+
return Program.Main(allArguments.ToArray());
310+
}
311+
283312
private static string[] GetDotnetScriptArguments(string fixture, params string[] arguments)
284313
{
285314
string configuration;

src/Dotnet.Script/Program.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,13 @@ private static int Wain(string[] args)
8080
{
8181
int exitCode = 0;
8282
if (!string.IsNullOrWhiteSpace(code.Value))
83-
{
84-
exitCode = await RunCode(code.Value, debugMode.HasValue(), app.RemainingArguments.Concat(argsAfterDoubleHypen), cwd.Value());
83+
{
84+
var optimizationLevel = OptimizationLevel.Debug;
85+
if (configuration.HasValue() && configuration.Value().ToLower() == "release")
86+
{
87+
optimizationLevel = OptimizationLevel.Release;
88+
}
89+
exitCode = await RunCode(code.Value, debugMode.HasValue(), optimizationLevel, app.RemainingArguments.Concat(argsAfterDoubleHypen), cwd.Value());
8590
}
8691
return exitCode;
8792
});
@@ -145,8 +150,8 @@ private static async Task<int> RunScript(string file, bool debugMode, Optimizati
145150
if (IsHttpUri(file))
146151
{
147152
var downloader = new ScriptDownloader();
148-
var scriptFile = await downloader.Download(file);
149-
var exitCode = await RunScript(scriptFile, debugMode, optimizationLevel, args, interactive);
153+
var scriptFile = await downloader.Download(file);
154+
var exitCode = await RunScript(scriptFile, debugMode, optimizationLevel, args, interactive);
150155
File.Delete(scriptFile);
151156
return exitCode;
152157
}
@@ -186,10 +191,10 @@ private static async Task RunInteractive(bool debugMode)
186191
await runner.RunLoop();
187192
}
188193

189-
private static Task<int> RunCode(string code, bool debugMode, IEnumerable<string> args, string currentWorkingDirectory)
194+
private static Task<int> RunCode(string code, bool debugMode, OptimizationLevel optimizationLevel, IEnumerable<string> args, string currentWorkingDirectory)
190195
{
191196
var sourceText = SourceText.From(code);
192-
var context = new ScriptContext(sourceText, currentWorkingDirectory ?? Directory.GetCurrentDirectory(), args, null, scriptMode: ScriptMode.Eval);
197+
var context = new ScriptContext(sourceText, currentWorkingDirectory ?? Directory.GetCurrentDirectory(), args, null,optimizationLevel, ScriptMode.Eval);
193198
return Run(debugMode, context);
194199
}
195200

0 commit comments

Comments
 (0)