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

Support VS Mac 17.x #131

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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: 12 additions & 6 deletions Mono.TextTemplating.Roslyn/RoslynCodeCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,11 @@ CodeCompilerResult CompileFileInternal (
var parseOptions = args?.ParseOptions ?? new CSharpParseOptions();

if (arguments.LangVersion != null) {
if (LanguageVersionFacts.TryParse(arguments.LangVersion, out var langVersion)) {
parseOptions = parseOptions.WithLanguageVersion (langVersion);
} else {
throw new System.Exception($"Unknown value '{arguments.LangVersion}' for langversion");
}
parseOptions = WithLanguageVersion (parseOptions, arguments.LangVersion);
} else {
// need to update this when updating referenced roslyn binaries
CSharpLangVersionHelper.GetBestSupportedLangVersion (runtime, CSharpLangVersion.v9_0);
var langVersion = CSharpLangVersionHelper.GetBestSupportedLangVersion (runtime, CSharpLangVersion.v9_0);
parseOptions = WithLanguageVersion (parseOptions, CSharpLangVersionHelper.ToString (langVersion));
}

var syntaxTrees = new List<SyntaxTree> ();
Expand Down Expand Up @@ -120,5 +117,14 @@ CodeCompilerResult CompileFileInternal (
Errors = errors
};
}

static CSharpParseOptions WithLanguageVersion (CSharpParseOptions parseOptions, string langVersionText)
{
if (LanguageVersionFacts.TryParse (langVersionText, out var langVersion)) {
return parseOptions.WithLanguageVersion (langVersion);
} else {
throw new System.Exception ($"Unknown value '{langVersionText}' for langversion");
}
}
}
}
2 changes: 2 additions & 0 deletions Mono.TextTemplating.Roslyn/TemplatingEngineExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public static class RoslynTemplatingEngineExtensions
public static void UseInProcessCompiler (this TemplatingEngine engine)
{
engine.SetCompilerFunc ((RuntimeInfo r) => new RoslynCodeCompiler (r));

RuntimeInfo.ThrowOnMissingDotNetCoreSdkDirectory = false;
}

public static void UseInProcessCompiler (this TemplateGenerator generator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static bool HasLangVersionArg (string args) =>
&& ProcessArgumentBuilder.TryParse (args, out var parsedArgs)
&& parsedArgs.Any (a => a.IndexOf("langversion") == 1);

static string ToString (CSharpLangVersion v) => v switch {
internal static string ToString (CSharpLangVersion v) => v switch {
CSharpLangVersion.v5_0 => "5",
CSharpLangVersion.v6_0 => "6",
CSharpLangVersion.v7_0 => "7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class RuntimeInfo
public string RefAssembliesDir { get; private set; }
public string RuntimeFacadesDir { get; internal set; }

internal static bool ThrowOnMissingDotNetCoreSdkDirectory { get; set; } = true;

public static RuntimeInfo GetRuntime ()
{
if (Type.GetType ("Mono.Runtime") != null)
Expand Down Expand Up @@ -135,10 +137,11 @@ static RuntimeInfo GetDotNetCoreSdk ()

string MakeCscPath (string d) => Path.Combine (d, "Roslyn", "bincore", "csc.dll");
var sdkDir = FindHighestVersionedDirectory (Path.Combine (dotnetRoot, "sdk"), d => File.Exists (MakeCscPath (d)), out var sdkVersion);
if (sdkDir == null) {
if (sdkDir == null && ThrowOnMissingDotNetCoreSdkDirectory) {
return FromError (RuntimeKind.NetCore, "Could not find csc.dll in any .NET Core SDK");
}
var maxCSharpVersion = CSharpLangVersionHelper.FromNetCoreSdkVersion (sdkVersion);
string cscPath = sdkDir == null ? null : MakeCscPath (sdkDir);
CSharpLangVersion maxCSharpVersion = CSharpLangVersionHelper.FromNetCoreSdkVersion (sdkVersion);

// it's ok if this is null, we may be running on an older SDK that didn't support packs
//in which case we fall back to resolving from the runtime dir
Expand All @@ -148,11 +151,15 @@ static RuntimeInfo GetDotNetCoreSdk ()
out _
);

return new RuntimeInfo (RuntimeKind.NetCore) { RuntimeDir = runtimeDir, RefAssembliesDir = refAssembliesDir, CscPath = MakeCscPath (sdkDir), MaxSupportedLangVersion = maxCSharpVersion, Version = hostVersion };
return new RuntimeInfo (RuntimeKind.NetCore) { RuntimeDir = runtimeDir, RefAssembliesDir = refAssembliesDir, CscPath = cscPath, MaxSupportedLangVersion = maxCSharpVersion, Version = hostVersion };
}

static string FindHighestVersionedDirectory (string parentFolder, Func<string, bool> validate, out SemVersion bestVersion)
{
if (!Directory.Exists (parentFolder)) {
bestVersion = SemVersion.Zero;
return null;
}
string bestMatch = null;
bestVersion = SemVersion.Zero;
foreach (var dir in Directory.EnumerateDirectories (parentFolder)) {
Expand Down