diff --git a/Mono.TextTemplating.Roslyn/RoslynCodeCompiler.cs b/Mono.TextTemplating.Roslyn/RoslynCodeCompiler.cs index 4ab9a01..13613ab 100644 --- a/Mono.TextTemplating.Roslyn/RoslynCodeCompiler.cs +++ b/Mono.TextTemplating.Roslyn/RoslynCodeCompiler.cs @@ -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 (); @@ -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"); + } + } } } \ No newline at end of file diff --git a/Mono.TextTemplating.Roslyn/TemplatingEngineExtension.cs b/Mono.TextTemplating.Roslyn/TemplatingEngineExtension.cs index 20a6fa3..ba7f312 100644 --- a/Mono.TextTemplating.Roslyn/TemplatingEngineExtension.cs +++ b/Mono.TextTemplating.Roslyn/TemplatingEngineExtension.cs @@ -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) diff --git a/Mono.TextTemplating/Mono.TextTemplating.CodeCompilation/CSharpLangVersionHelper.cs b/Mono.TextTemplating/Mono.TextTemplating.CodeCompilation/CSharpLangVersionHelper.cs index 1e911d2..c1f7365 100644 --- a/Mono.TextTemplating/Mono.TextTemplating.CodeCompilation/CSharpLangVersionHelper.cs +++ b/Mono.TextTemplating/Mono.TextTemplating.CodeCompilation/CSharpLangVersionHelper.cs @@ -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", diff --git a/Mono.TextTemplating/Mono.TextTemplating.CodeCompilation/RuntimeInfo.cs b/Mono.TextTemplating/Mono.TextTemplating.CodeCompilation/RuntimeInfo.cs index 02bd5ab..675a21e 100644 --- a/Mono.TextTemplating/Mono.TextTemplating.CodeCompilation/RuntimeInfo.cs +++ b/Mono.TextTemplating/Mono.TextTemplating.CodeCompilation/RuntimeInfo.cs @@ -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) @@ -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 @@ -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 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)) {