|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.IO; |
| 9 | +using System.Linq; |
| 10 | +using System.Xml.Linq; |
| 11 | +using Microsoft.Build.Framework; |
| 12 | +using Microsoft.Build.Utilities; |
| 13 | + |
| 14 | +namespace Microsoft.DotNet.Build.Tasks |
| 15 | +{ |
| 16 | + /* |
| 17 | + * This task parses the versions in a .NET SDK |
| 18 | + */ |
| 19 | + public class ParseDotNetVersions : Task |
| 20 | + { |
| 21 | + [Required] |
| 22 | + public string SdkRootDirectory { get; set; } |
| 23 | + |
| 24 | + [Output] |
| 25 | + public string SdkVersion { get; set; } |
| 26 | + [Output] |
| 27 | + public string AspNetCoreVersion { get; set; } |
| 28 | + [Output] |
| 29 | + public string RuntimeVersion { get; set; } |
| 30 | + |
| 31 | + public override bool Execute() |
| 32 | + { |
| 33 | + |
| 34 | + var pathToDotNet = Path.Join(SdkRootDirectory, "dotnet"); |
| 35 | + SdkVersion = ExecuteDotNetCommand(SdkRootDirectory, |
| 36 | + pathToDotNet, |
| 37 | + new List<string> { "--list-sdks" }) |
| 38 | + .First() |
| 39 | + .Split(" ") |
| 40 | + .First(); |
| 41 | + AspNetCoreVersion = ExecuteDotNetCommand(SdkRootDirectory, |
| 42 | + pathToDotNet, |
| 43 | + new List<string> { "--list-runtimes" }) |
| 44 | + .Where(line => line.Contains("Microsoft.AspNetCore.App")) |
| 45 | + .First() |
| 46 | + .Split(" ") |
| 47 | + .Skip(1) |
| 48 | + .First(); |
| 49 | + RuntimeVersion = ExecuteDotNetCommand(SdkRootDirectory, |
| 50 | + pathToDotNet, |
| 51 | + new List<string> { "--list-runtimes"}) |
| 52 | + .Where(line => line.Contains("Microsoft.NETCore.App")) |
| 53 | + .First() |
| 54 | + .Split(" ") |
| 55 | + .Skip(1) |
| 56 | + .First(); |
| 57 | + |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Executes a dotnet command and returns the result. |
| 63 | + /// </summary> |
| 64 | + /// <param name="workingDirectory">The working directory for the dotnet command.</param> |
| 65 | + /// <param name="command">The complete path to the dotnet command to execute.</param> |
| 66 | + /// <param name="argumentList">The arguments to the dotnet command to execute.</param> |
| 67 | + /// <returns>An array of the output lines of the dotnet command.</returns> |
| 68 | + private string[] ExecuteDotNetCommand(string workingDirectory, string command, List<string> argumentList) |
| 69 | + { |
| 70 | + string[] returnData; |
| 71 | + Process _process = new Process(); |
| 72 | + _process.StartInfo.FileName = command; |
| 73 | + foreach (string argument in argumentList) |
| 74 | + { |
| 75 | + _process.StartInfo.ArgumentList.Add(argument); |
| 76 | + } |
| 77 | + _process.StartInfo.WorkingDirectory = workingDirectory; |
| 78 | + _process.StartInfo.RedirectStandardOutput = true; |
| 79 | + _process.StartInfo.UseShellExecute = false; |
| 80 | + _process.Start(); |
| 81 | + returnData = _process.StandardOutput.ReadToEnd().Split('\n'); |
| 82 | + _process.WaitForExit(); |
| 83 | + return returnData; |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | +} |
0 commit comments