-
Notifications
You must be signed in to change notification settings - Fork 409
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
[Housekeeping] Add MSBuild Task to Verify .NET MAUI Workload Installed on Local Machine #2266
base: main
Are you sure you want to change the base?
Changes from 5 commits
bc0be29
e1d8477
9bcbc6f
68528f2
4b303c9
0ad8124
77ea6a9
936b52e
08de92a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>$(NetVersion)</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.11.4" PrivateAssets="all"/> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System.Diagnostics; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.Build.Framework; | ||
|
||
namespace CommunityToolkit.Maui.BuildTasks; | ||
|
||
public partial class VerifyMauiWorkloadVersionTask : Microsoft.Build.Utilities.Task | ||
{ | ||
static readonly ProcessStartInfo processStartInfo = new() | ||
{ | ||
FileName = "dotnet", | ||
Arguments = "workload list", | ||
UseShellExecute = false, | ||
RedirectStandardOutput = true, | ||
CreateNoWindow = true, | ||
WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory | ||
}; | ||
|
||
[Required] | ||
public string MinimumRequiredMauiVersion { get; set; } = string.Empty; | ||
|
||
public override bool Execute() | ||
{ | ||
try | ||
{ | ||
var dotnetOutput = GetDotnetWorkloadOutput(); | ||
var installedMauiWorkloadVersion = ExtractMauiVersion(dotnetOutput); | ||
|
||
if (string.IsNullOrEmpty(installedMauiWorkloadVersion)) | ||
{ | ||
Log.LogError($"No MAUI workload installed/n{GenerateTroubleshootingText(MinimumRequiredMauiVersion)}"); | ||
brminnick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return false; | ||
} | ||
|
||
var currentVersion = Version.Parse(installedMauiWorkloadVersion); | ||
var minimumRequiredVersion = Version.Parse(MinimumRequiredMauiVersion); | ||
|
||
if (currentVersion < minimumRequiredVersion) | ||
{ | ||
Log.LogError($"The installed MAUI workload version, {installedMauiWorkloadVersion}, does not meet the minimum version required by the .NET MAUI Community Toolkit: {MinimumRequiredMauiVersion}./n{GenerateTroubleshootingText(MinimumRequiredMauiVersion)}"); | ||
brminnick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return false; | ||
} | ||
|
||
Log.LogMessage(MessageImportance.Normal, $"MAUI workload version {installedMauiWorkloadVersion} satisfies the required version {MinimumRequiredMauiVersion}."); | ||
|
||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.LogErrorFromException(ex); | ||
return false; | ||
} | ||
} | ||
|
||
static string GetDotnetWorkloadOutput() | ||
{ | ||
using var process = new Process(); | ||
process.StartInfo = processStartInfo; | ||
|
||
process.Start(); | ||
var output = process.StandardOutput.ReadToEnd(); | ||
process.WaitForExit(); | ||
|
||
return output; | ||
} | ||
|
||
static string? ExtractMauiVersion(string dotnetOutput) | ||
{ | ||
var match = VerifyWorkloadVersion().Match(dotnetOutput); | ||
|
||
return match.Success ? match.Groups[0].Value : null; | ||
} | ||
|
||
static string GenerateTroubleshootingText(in string minimumMauiVersion) => | ||
$""" | ||
Follow these steps to install the required .NET MAUI workload: | ||
1. Using your web browser, download the latest stable release of the .NET SDK and install it on your machine: https://dotnet.microsoft.com/download/dotnet | ||
2. After installing the latest version of .NET, using your console, use the command `dotnet workload install maui --version {minimumMauiVersion}` to update to the latest stable version of .NET MAUI \n | ||
"""; | ||
|
||
[GeneratedRegex(@"(?<=Workload version: )\d+\.\d+\.\d+\.\d+")] | ||
private static partial Regex VerifyWorkloadVersion(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,12 @@ | |
<Configurations>Debug;Release</Configurations> | ||
</PropertyGroup> | ||
|
||
<UsingTask TaskName="VerifyMauiWorkloadVersionTask" AssemblyFile="..\CommunityToolkit.Maui.BuildTasks\bin\$(Configuration)\$(NetVersion)\CommunityToolkit.Maui.BuildTasks.dll" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this will not work the way you expect... If you want it to run on the way that I mentioned, in the app project, this should be moved to a target file inside the |
||
|
||
<Target Name="VerifyMauiWorkloadVersionTask" BeforeTargets="Build"> | ||
<VerifyMauiWorkloadVersionTask MinimumRequiredMauiVersion="$(RequiredMauiWorkloadVersion)" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The property defined above is named
brminnick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</Target> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\build\nuget.png" PackagePath="icon.png" Pack="true" /> | ||
<None Include="ReadMe.txt" pack="true" PackagePath="." /> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if you want to use the
net8
here... It's recommended to usenetstandard
for MsBuild tasks, because the project may run on IDEs and/or tools that doesn't supportnet8
. For example, Visual Studio runs on top of .net framework, and usingnet 8
may cause errors