Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow defining script target framework via configuration #1154

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 4 additions & 14 deletions src/OmniSharp.Script/ScriptHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ScriptHelper
private const string ResolverField = "_resolver";
private const string FileReferenceProviderField = "_fileReferenceProvider";

private readonly IConfiguration _configuration;
private readonly ScriptOptions _scriptOptions;

// aligned with CSI.exe
// https://github.com/dotnet/roslyn/blob/version-2.0.0-rc3/src/Interactive/csi/csi.rsp
Expand All @@ -45,9 +45,9 @@ public class ScriptHelper
private readonly Lazy<CSharpCompilationOptions> _compilationOptions;
private readonly MetadataReferenceResolver _resolver = ScriptMetadataResolver.Default;

public ScriptHelper(IConfiguration configuration = null)
public ScriptHelper(ScriptOptions scriptOptions)
{
_configuration = configuration;
_scriptOptions = scriptOptions;
_compilationOptions = new Lazy<CSharpCompilationOptions>(CreateCompilationOptions);
InjectXMLDocumentationProviderIntoRuntimeMetadataReferenceResolver();
}
Expand Down Expand Up @@ -84,17 +84,7 @@ private CSharpCompilationOptions CreateCompilationOptions()

private CachingScriptMetadataResolver CreateMetadataReferenceResolver()
{
bool enableScriptNuGetReferences = false;

if (_configuration != null)
{
if (!bool.TryParse(_configuration["enableScriptNuGetReferences"], out enableScriptNuGetReferences))
{
enableScriptNuGetReferences = false;
}
}

return enableScriptNuGetReferences
return _scriptOptions.IsNugetEnabled()
? new CachingScriptMetadataResolver(new NuGetMetadataReferenceResolver(_resolver))
: new CachingScriptMetadataResolver(_resolver);
}
Expand Down
18 changes: 18 additions & 0 deletions src/OmniSharp.Script/ScriptOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.IO;

namespace OmniSharp.Script
{
public class ScriptOptions
{
public bool EnableScriptNuGetReferences { get; set; }

public string DefaultTargetFramework { get; set; } = "net46";

/// <summary>
/// Nuget for scripts is enabled when <see cref="EnableScriptNuGetReferences"/> is enabled or when <see cref="DefaultTargetFramework"/> is .NET Core
/// </summary>
public bool IsNugetEnabled() =>
EnableScriptNuGetReferences ||
(DefaultTargetFramework != null && DefaultTargetFramework.StartsWith("netcoreapp", System.StringComparison.OrdinalIgnoreCase));
}
}
20 changes: 9 additions & 11 deletions src/OmniSharp.Script/ScriptProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class ScriptProjectSystem : IProjectSystem

private readonly CompilationDependencyResolver _compilationDependencyResolver;

private ScriptOptions _scriptOptions;
private ScriptHelper _scriptHelper;
private bool _enableScriptNuGetReferences;
private CompilationDependency[] _compilationDependencies;

[ImportingConstructor]
Expand Down Expand Up @@ -79,7 +79,9 @@ public ScriptProjectSystem(OmniSharpWorkspace workspace, IOmniSharpEnvironment e

public void Initalize(IConfiguration configuration)
{
_scriptHelper = new ScriptHelper(configuration);
_scriptOptions = new ScriptOptions();
ConfigurationBinder.Bind(configuration, _scriptOptions);
_scriptHelper = new ScriptHelper(_scriptOptions);

_logger.LogInformation($"Detecting CSX files in '{_env.TargetDirectory}'.");

Expand All @@ -101,12 +103,7 @@ public void Initalize(IConfiguration configuration)
inheritedCompileLibraries.AddRange(DependencyContext.Default.CompileLibraries.Where(x =>
x.Name.ToLowerInvariant().StartsWith("system.valuetuple")));

if (!bool.TryParse(configuration["enableScriptNuGetReferences"], out _enableScriptNuGetReferences))
{
_enableScriptNuGetReferences = false;
}

_compilationDependencies = TryGetCompilationDependencies(_enableScriptNuGetReferences);
_compilationDependencies = TryGetCompilationDependencies();

// if we have no compilation dependencies
// we will assume desktop framework
Expand Down Expand Up @@ -171,7 +168,7 @@ private void AddToWorkspace(string csxPath)
var csxFileName = Path.GetFileName(csxPath);
var project = _scriptHelper.CreateProject(csxFileName, _commonReferences, csxPath);

if (_enableScriptNuGetReferences)
if (_scriptOptions.IsNugetEnabled())
{
var scriptMap = _compilationDependencies.ToDictionary(rdt => rdt.Name, rdt => rdt.Scripts);
var options = project.CompilationOptions.WithSourceReferenceResolver(
Expand Down Expand Up @@ -201,11 +198,12 @@ private void RemoveFromWorkspace(string csxPath)
}
}

private CompilationDependency[] TryGetCompilationDependencies(bool enableScriptNuGetReferences)
private CompilationDependency[] TryGetCompilationDependencies()
{
try
{
return _compilationDependencyResolver.GetDependencies(_env.TargetDirectory, enableScriptNuGetReferences).ToArray();
_logger.LogInformation($"Searching for compilation dependencies with the fallback framework of '{_scriptOptions.DefaultTargetFramework}'.");
return _compilationDependencyResolver.GetDependencies(_env.TargetDirectory, _scriptOptions.IsNugetEnabled(), _scriptOptions.DefaultTargetFramework).ToArray();
}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/TestUtility/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static OmniSharpWorkspace CreateCsxWorkspace(TestFile testFile)
public static void AddCsxProjectToWorkspace(OmniSharpWorkspace workspace, TestFile testFile)
{
var references = GetReferences();
var scriptHelper = new ScriptHelper();
var scriptHelper = new ScriptHelper(new ScriptOptions());
var project = scriptHelper.CreateProject(testFile.FileName, references.Union(new[] { MetadataReference.CreateFromFile(typeof(CommandLineScriptGlobals).GetTypeInfo().Assembly.Location) }), testFile.FileName,Enumerable.Empty<string>());
workspace.AddProject(project);

Expand Down