Last active
December 9, 2022 15:30
-
-
Save dimonovdd/a9f3bc18c363e993875b7bdd547f0023 to your computer and use it in GitHub Desktop.
Try detect msbuild for cake macOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class TestClass | |
{ | |
static string GetMsBuildPath(int versionMajor, string pathContains) | |
{ | |
var defaultDirectories = new[] | |
{ | |
Path.Combine(System.Environment.GetEnvironmentVariable("HOME"), "Library/Caches/VisualStudio"), | |
"/Library/Frameworks/Mono.framework/Versions", | |
"/usr/local/share/dotnet/sdk", | |
}; | |
var files = defaultDirectories | |
.OrderByDescending(a => a) | |
.Where(a => a.ToLowerInvariant().Contains(pathContains.ToLowerInvariant())) | |
.Select(FindMsbuild) | |
.SelectMany(a => a) | |
.Distinct() | |
.Select(a => | |
{ | |
var info = FileVersionInfo.GetVersionInfo(a); | |
return (a, info.GetVersion()); | |
}) | |
.Where(a => a.Item2 != null) | |
.OrderByDescending(a => a.Item2) | |
.ToArray(); | |
var (path, _) = files.FirstOrDefault(a => a.Item2!.Major == versionMajor); | |
if (string.IsNullOrWhiteSpace(path)) | |
return files.FirstOrDefault().Item1; | |
return path; | |
} | |
static IEnumerable<string> FindMsbuild(string directory) | |
{ | |
var dllFiles = Directory.GetFiles(directory, "*.dll", SearchOption.AllDirectories); | |
return dllFiles.Where(a => Path.GetFileName(a) == "MSBuild.dll"); | |
} | |
public static Version GetVersion(this FileVersionInfo info) | |
{ | |
if (info == null || !info.FileVersion.TryParseVersion(out var version)) | |
return null; | |
return version; | |
} | |
public static bool TryParseVersion(this string input, out Version result) | |
=> Version.TryParse(VersionFormat(input), out result); | |
static string VersionFormat(this string version) | |
{ | |
if(string.IsNullOrWhiteSpace(version)) | |
return string.Empty; | |
version = version.Trim(' ').Trim('.'); | |
if (!version.Contains('.')) | |
version += ".0"; | |
return version; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment