Skip to content

Commit aa01533

Browse files
committed
Merge remote-tracking branch 'filipw/master'
2 parents 84d45ce + 664f320 commit aa01533

File tree

17 files changed

+152
-48
lines changed

17 files changed

+152
-48
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,22 @@ This now gives us a chance to attach the debugger before stepping into the scrip
378378

379379
Once that is done we should see out breakpoint being hit.
380380

381+
## Configuration(Debug/Release)
382+
383+
By default, scripts will be compiled using the `debug` configuration. This is to ensure that we can debug a script in VS Code as well as attaching a debugger for long running scripts.
384+
385+
There are however situations where we might need to execute a script that is compiled with the `release` configuration. For instance, running benchmarks using [BenchmarkDotNet](http://benchmarkdotnet.org/) is not possible unless the script is compiled with the `release` configuration.
386+
387+
We can specify this when executing the script.
388+
389+
```shell
390+
dotnet script -c release foo.csx
391+
```
392+
393+
394+
395+
396+
381397
## Team
382398

383399
* [Bernhard Richter](https://github.com/seesharper) ([@bernhardrichter](https://twitter.com/bernhardrichter))

build/install-dotnet-script.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
2+
13
$scriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
24
$client = New-Object "System.Net.WebClient"
35
$url = "https://github.com/filipw/dotnet-script/releases/download/0.18.0/dotnet-script.0.18.0.zip"
46
$file = "$scriptRoot/dotnet-script.zip"
57
$client.DownloadFile($url,$file)
6-
Expand-Archive $file -DestinationPath $scriptRoot -Force
7-
8+
Expand-Archive $file -DestinationPath $scriptRoot -Force

install/install.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
2+
13
# Create a temporary folder to download to.
24
$tempFolder = Join-Path $env:TEMP "dotnet-script"
35
New-Item $tempFolder -ItemType Directory -Force
@@ -9,7 +11,7 @@ Select-Object tag_name
911
$tag_name = $latestRelease.tag_name
1012

1113
# Download the zip
12-
Write-Host "Downloading latest verson ($tag_name)"
14+
Write-Host "Downloading latest version ($tag_name)"
1315
$client = New-Object "System.Net.WebClient"
1416
$url = "https://github.com/filipw/dotnet-script/releases/download/$tag_name/dotnet-script.$tag_name.zip"
1517
$zipFile = Join-Path $tempFolder "dotnet-script.zip"
@@ -28,3 +30,5 @@ $paths += Join-Path $installationFolder "dotnet-script"
2830
$path = $paths -join ";"
2931

3032
[System.Environment]::SetEnvironmentVariable("path", $path, [System.EnvironmentVariableTarget]::User)
33+
34+
Write-Host "Successfully installed version ($tag_name)"

src/Dotnet.Script.Core/Interactive/InteractiveRunner.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public InteractiveRunner(ScriptCompiler scriptCompiler, ScriptLogger logger, Scr
3838
_globals = new InteractiveScriptGlobals(Console.Out, CSharpObjectFormatter.Instance);
3939
}
4040

41-
public virtual async Task RunLoop(bool debugMode)
41+
public virtual async Task RunLoop()
4242
{
43-
while (true && !_shouldExit)
43+
while (!_shouldExit)
4444
{
4545
Console.Out.Write("> ");
4646
var input = ReadInput();
@@ -51,24 +51,24 @@ public virtual async Task RunLoop(bool debugMode)
5151
continue;
5252
}
5353

54-
await Execute(input, debugMode);
54+
await Execute(input);
5555
}
5656
}
5757

58-
public virtual async Task RunLoopWithSeed(bool debugMode, ScriptContext scriptContext)
58+
public virtual async Task RunLoopWithSeed(ScriptContext scriptContext)
5959
{
6060
await HandleScriptErrors(async () => await RunFirstScript(scriptContext));
61-
await RunLoop(debugMode);
61+
await RunLoop();
6262
}
6363

64-
protected virtual async Task Execute(string input, bool debugMode)
64+
protected virtual async Task Execute(string input)
6565
{
6666
await HandleScriptErrors(async () =>
6767
{
6868
if (_scriptState == null)
6969
{
7070
var sourceText = SourceText.From(input);
71-
var context = new ScriptContext(sourceText, CurrentDirectory, Enumerable.Empty<string>(), debugMode: debugMode, scriptMode: ScriptMode.REPL);
71+
var context = new ScriptContext(sourceText, CurrentDirectory, Enumerable.Empty<string>(),scriptMode: ScriptMode.REPL);
7272
await RunFirstScript(context);
7373
}
7474
else

src/Dotnet.Script.Core/ScriptCompiler.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public virtual ScriptOptions CreateScriptOptions(ScriptContext context, IList<Ru
7979
.WithSourceResolver(new NuGetSourceReferenceResolver(new SourceFileResolver(ImmutableArray<string>.Empty, context.WorkingDirectory),scriptMap))
8080
.WithMetadataResolver(new NuGetMetadataReferenceResolver(ScriptMetadataResolver.Default.WithBaseDirectory(context.WorkingDirectory)))
8181
.WithEmitDebugInformation(true)
82-
.WithFileEncoding(context.Code.Encoding);
82+
.WithFileEncoding(context.Code.Encoding);
83+
8384
if (!string.IsNullOrWhiteSpace(context.FilePath))
8485
{
8586
opts = opts.WithFilePath(context.FilePath);
@@ -129,7 +130,7 @@ public virtual ScriptCompilationContext<TReturn> CreateCompilationContext<TRetur
129130
{
130131
Logger.Verbose($"Adding reference to an inherited dependency => {inheritedAssemblyName.FullName}");
131132
var assembly = Assembly.Load(inheritedAssemblyName);
132-
opts = opts.AddReferences(assembly);
133+
opts = opts.AddReferences(assembly);
133134
}
134135
}
135136

@@ -152,6 +153,17 @@ public virtual ScriptCompilationContext<TReturn> CreateCompilationContext<TRetur
152153

153154
var loader = new InteractiveAssemblyLoader();
154155
var script = CSharpScript.Create<TReturn>(code, opts, typeof(THost), loader);
156+
157+
if (context.OptimizationLevel == OptimizationLevel.Release)
158+
{
159+
Logger.Verbose("Configuration/Optimization mode: Release");
160+
SetReleaseOptimizationLevel(script.GetCompilation());
161+
}
162+
else
163+
{
164+
Logger.Verbose("Configuration/Optimization mode: Debug");
165+
}
166+
155167
var orderedDiagnostics = script.GetDiagnostics(SuppressedDiagnosticIds);
156168

157169
if (orderedDiagnostics.Any(d => d.Severity == DiagnosticSeverity.Error))
@@ -163,6 +175,14 @@ public virtual ScriptCompilationContext<TReturn> CreateCompilationContext<TRetur
163175
return new ScriptCompilationContext<TReturn>(script, context.Code, loader, opts);
164176
}
165177

178+
private static void SetReleaseOptimizationLevel(Compilation compilation)
179+
{
180+
var compilationOptionsField = typeof(CSharpCompilation).GetTypeInfo().GetDeclaredField("_options");
181+
var compilationOptions = (CSharpCompilationOptions)compilationOptionsField.GetValue(compilation);
182+
compilationOptions = compilationOptions.WithOptimizationLevel(OptimizationLevel.Release);
183+
compilationOptionsField.SetValue(compilation, compilationOptions);
184+
}
185+
166186
private Assembly MapUnresolvedAssemblyToRuntimeLibrary(IDictionary<string, RuntimeAssembly> dependencyMap, ResolveEventArgs args)
167187
{
168188
var assemblyName = new AssemblyName(args.Name);

src/Dotnet.Script.Core/ScriptContext.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
using System.Collections.ObjectModel;
33
using System.Linq;
44
using Dotnet.Script.DependencyModel.Context;
5+
using Microsoft.CodeAnalysis;
56
using Microsoft.CodeAnalysis.Text;
67

78
namespace Dotnet.Script.Core
89
{
910
public class ScriptContext
1011
{
11-
public ScriptContext(SourceText code, string workingDirectory, IEnumerable<string> args, string filePath = null, bool debugMode = false, ScriptMode scriptMode = ScriptMode.Script)
12+
public ScriptContext(SourceText code, string workingDirectory, IEnumerable<string> args, string filePath = null, OptimizationLevel optimizationLevel = OptimizationLevel.Debug, ScriptMode scriptMode = ScriptMode.Script)
1213
{
1314
Code = code;
1415
WorkingDirectory = workingDirectory;
1516
Args = new ReadOnlyCollection<string>(args.ToArray());
1617
FilePath = filePath;
17-
DebugMode = debugMode;
18+
OptimizationLevel = optimizationLevel;
1819
ScriptMode = filePath != null ? ScriptMode.Script : scriptMode;
1920
}
2021

@@ -26,7 +27,7 @@ public ScriptContext(SourceText code, string workingDirectory, IEnumerable<strin
2627

2728
public string FilePath { get; }
2829

29-
public bool DebugMode { get; }
30+
public OptimizationLevel OptimizationLevel { get; }
3031

3132
public ScriptMode ScriptMode { get; }
3233
}

src/Dotnet.Script.DependencyModel/Context/DotnetRestorer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public DotnetRestorer(CommandRunner commandRunner, LogFactory logFactory)
1818

1919
public void Restore(string pathToProjectFile)
2020
{
21-
_logger.Debug($"Restoring {pathToProjectFile} using the dotnet cli.");
2221
var runtimeIdentifier = RuntimeHelper.GetRuntimeIdentifier();
22+
_logger.Debug($"Restoring {pathToProjectFile} using the dotnet cli. RuntimeIdentifier : {runtimeIdentifier}");
2323
var exitcode = _commandRunner.Execute("dotnet", $"restore \"{pathToProjectFile}\" -r {runtimeIdentifier}");
2424
if (exitcode != 0)
2525
{

src/Dotnet.Script.DependencyModel/Dotnet.Script.DependencyModel.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<RepositoryUrl>https://github.com/filipw/dotnet-script.git</RepositoryUrl>
1212
<RepositoryType>git</RepositoryType>
1313
<PackageTags>script;csx;csharp;roslyn;omnisharp</PackageTags>
14-
<Version>0.4.0</Version>
14+
<Version>0.5.0</Version>
1515
</PropertyGroup>
1616

1717
<ItemGroup>

src/Dotnet.Script.DependencyModel/Process/CommandRunner.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Diagnostics;
1+
using System.Collections.Generic;
2+
using System.Diagnostics;
23
using Dotnet.Script.DependencyModel.Logging;
34

45
namespace Dotnet.Script.DependencyModel.Process
@@ -27,11 +28,20 @@ private static ProcessStartInfo CreateProcessStartInfo(string commandPath, strin
2728
CreateNoWindow = true,
2829
Arguments = arguments ?? "",
2930
RedirectStandardOutput = true,
30-
RedirectStandardError = true,
31+
RedirectStandardError = true,
3132
UseShellExecute = false
3233
};
34+
RemoveMsBuildEnvironmentVariables(startInformation.Environment);
3335
return startInformation;
3436
}
37+
private static void RemoveMsBuildEnvironmentVariables(IDictionary<string, string> environment)
38+
{
39+
// Remove various MSBuild environment variables set by OmniSharp to ensure that
40+
// the .NET CLI is not launched with the wrong values.
41+
environment.Remove("MSBUILD_EXE_PATH");
42+
environment.Remove("MSBuildExtensionsPath");
43+
}
44+
3545

3646
private static void RunAndWait(System.Diagnostics.Process process)
3747
{

src/Dotnet.Script.Tests/CompilationDependencyResolverTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Dotnet.Script.Tests
66
{
7+
[Collection("IntegrationTests")]
78
public class CompilationDependencyResolverTests
89
{
910
private readonly ITestOutputHelper _testOutputHelper;

0 commit comments

Comments
 (0)