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

[Housekeeping] Add MSBuild Task to Verify .NET MAUI Workload Installed on Local Machine #2266

Draft
wants to merge 6 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
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<NetVersion>net8.0</NetVersion>
<MauiPackageVersion>8.0.71</MauiPackageVersion>
<_RequiredMauiWorkloadVersion>8.0.402.1</_RequiredMauiWorkloadVersion>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
6 changes: 6 additions & 0 deletions samples/CommunityToolkit.Maui.Sample.sln
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Benchmarks", "Benchmarks",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommunityToolkit.Maui.Analyzers.Benchmarks.csproj", "..\src\CommunityToolkit.Maui.Analyzers.Benchmarks\CommunityToolkit.Maui.Analyzers.Benchmarks.csproj", "{B80F59B7-276C-4A55-A8DD-54587C8BC3D2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Maui.BuildTasks", "..\src\CommunityToolkit.Maui.BuildTasks\CommunityToolkit.Maui.BuildTasks.csproj", "{9F3FA434-F075-46E9-852F-098214966A57}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -131,6 +133,10 @@ Global
{B80F59B7-276C-4A55-A8DD-54587C8BC3D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B80F59B7-276C-4A55-A8DD-54587C8BC3D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B80F59B7-276C-4A55-A8DD-54587C8BC3D2}.Release|Any CPU.Build.0 = Release|Any CPU
{9F3FA434-F075-46E9-852F-098214966A57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F3FA434-F075-46E9-852F-098214966A57}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F3FA434-F075-46E9-852F-098214966A57}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F3FA434-F075-46E9-852F-098214966A57}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetVersion)</TargetFramework>
Copy link
Member

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 use netstandard for MsBuild tasks, because the project may run on IDEs and/or tools that doesn't support net8. For example, Visual Studio runs on top of .net framework, and using net 8 may cause errors

</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)}");
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)}");
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
Expand Up @@ -44,6 +44,12 @@
<Configurations>Debug;Release</Configurations>
</PropertyGroup>

<UsingTask TaskName="VerifyMauiWorkloadVersionTask" AssemblyFile="..\CommunityToolkit.Maui.BuildTasks\bin\$(Configuration)\$(NetVersion)\CommunityToolkit.Maui.BuildTasks.dll" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this will not work the way you expect...
If I understood correctly you want to run the MSBuild task when the users build the project. The way it's coded here, this will run during the build of Core project and not on the MauiApp project.

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 BuildTasks folder and pack from there


<Target Name="VerifyMauiWorkloadVersionTask" BeforeTargets="Build">
<VerifyMauiWorkloadVersionTask MinimumRequiredMauiVersion="$(_RequiredMauiWorkloadVersion)" />
</Target>

<ItemGroup>
<None Include="..\..\build\nuget.png" PackagePath="icon.png" Pack="true" />
<None Include="ReadMe.txt" pack="true" PackagePath="." />
Expand Down
6 changes: 6 additions & 0 deletions src/CommunityToolkit.Maui.sln
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Maui.Camer
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Maui.Camera", "CommunityToolkit.Maui.Camera\CommunityToolkit.Maui.Camera.csproj", "{9F4B8437-B200-4F96-B71C-4F4B4A517694}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Maui.BuildTasks", "CommunityToolkit.Maui.BuildTasks\CommunityToolkit.Maui.BuildTasks.csproj", "{55170BFE-DEDB-43D1-8351-302E3CC68BA2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -111,6 +113,10 @@ Global
{9F4B8437-B200-4F96-B71C-4F4B4A517694}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F4B8437-B200-4F96-B71C-4F4B4A517694}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F4B8437-B200-4F96-B71C-4F4B4A517694}.Release|Any CPU.Build.0 = Release|Any CPU
{55170BFE-DEDB-43D1-8351-302E3CC68BA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{55170BFE-DEDB-43D1-8351-302E3CC68BA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55170BFE-DEDB-43D1-8351-302E3CC68BA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55170BFE-DEDB-43D1-8351-302E3CC68BA2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading