|  | 
|  | 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 | + | 
|  | 4 | +using Aspire.Hosting.ApplicationModel; | 
|  | 5 | +using Aspire.Hosting.Lifecycle; | 
|  | 6 | +using Aspire.Hosting.Maui.Annotations; | 
|  | 7 | +using Aspire.Hosting.Maui.Lifecycle; | 
|  | 8 | +using Aspire.Hosting.Maui.Utilities; | 
|  | 9 | +using Aspire.Hosting.Utils; | 
|  | 10 | + | 
|  | 11 | +namespace Aspire.Hosting.Maui; | 
|  | 12 | + | 
|  | 13 | +/// <summary> | 
|  | 14 | +/// Helper methods for adding platform-specific MAUI device resources. | 
|  | 15 | +/// </summary> | 
|  | 16 | +internal static class MauiPlatformHelper | 
|  | 17 | +{ | 
|  | 18 | +    /// <summary> | 
|  | 19 | +    /// Gets the absolute project path and working directory from a MAUI project resource. | 
|  | 20 | +    /// </summary> | 
|  | 21 | +    /// <param name="builder">The MAUI project resource builder.</param> | 
|  | 22 | +    /// <returns>A tuple containing the absolute project path and working directory.</returns> | 
|  | 23 | +    internal static (string ProjectPath, string WorkingDirectory) GetProjectPaths(IResourceBuilder<MauiProjectResource> builder) | 
|  | 24 | +    { | 
|  | 25 | +        var projectPath = builder.Resource.ProjectPath; | 
|  | 26 | +        if (!Path.IsPathRooted(projectPath)) | 
|  | 27 | +        { | 
|  | 28 | +            projectPath = PathNormalizer.NormalizePathForCurrentPlatform( | 
|  | 29 | +                Path.Combine(builder.ApplicationBuilder.AppHostDirectory, projectPath)); | 
|  | 30 | +        } | 
|  | 31 | + | 
|  | 32 | +        var workingDirectory = Path.GetDirectoryName(projectPath) | 
|  | 33 | +            ?? throw new InvalidOperationException($"Unable to determine directory from project path: {projectPath}"); | 
|  | 34 | + | 
|  | 35 | +        return (projectPath, workingDirectory); | 
|  | 36 | +    } | 
|  | 37 | + | 
|  | 38 | +    /// <summary> | 
|  | 39 | +    /// Configures a platform resource with common settings and TFM validation. | 
|  | 40 | +    /// </summary> | 
|  | 41 | +    /// <typeparam name="T">The type of platform resource.</typeparam> | 
|  | 42 | +    /// <param name="resourceBuilder">The resource builder.</param> | 
|  | 43 | +    /// <param name="projectPath">The absolute path to the project file.</param> | 
|  | 44 | +    /// <param name="platformName">The platform name (e.g., "windows", "maccatalyst").</param> | 
|  | 45 | +    /// <param name="platformDisplayName">The display name for the platform (e.g., "Windows", "Mac Catalyst").</param> | 
|  | 46 | +    /// <param name="tfmExample">Example TFM for error messages (e.g., "net10.0-windows10.0.19041.0").</param> | 
|  | 47 | +    /// <param name="isSupported">Function to check if the platform is supported on the current host.</param> | 
|  | 48 | +    /// <param name="iconName">The icon name for the resource.</param> | 
|  | 49 | +    /// <param name="additionalArgs">Optional additional command-line arguments to pass to dotnet run.</param> | 
|  | 50 | +    internal static void ConfigurePlatformResource<T>( | 
|  | 51 | +        IResourceBuilder<T> resourceBuilder, | 
|  | 52 | +        string projectPath, | 
|  | 53 | +        string platformName, | 
|  | 54 | +        string platformDisplayName, | 
|  | 55 | +        string tfmExample, | 
|  | 56 | +        Func<bool> isSupported, | 
|  | 57 | +        string iconName = "Desktop", | 
|  | 58 | +        params string[] additionalArgs) where T : ProjectResource | 
|  | 59 | +    { | 
|  | 60 | +        // Check if the project has the platform TFM and get the actual TFM value | 
|  | 61 | +        var platformTfm = ProjectFileReader.GetPlatformTargetFramework(projectPath, platformName); | 
|  | 62 | + | 
|  | 63 | +        // Set the command line arguments with the detected TFM if available | 
|  | 64 | +        resourceBuilder.WithArgs(context => | 
|  | 65 | +        { | 
|  | 66 | +            context.Args.Add("run"); | 
|  | 67 | +            if (!string.IsNullOrEmpty(platformTfm)) | 
|  | 68 | +            { | 
|  | 69 | +                context.Args.Add("-f"); | 
|  | 70 | +                context.Args.Add(platformTfm); | 
|  | 71 | +            } | 
|  | 72 | +            // Add any additional platform-specific arguments | 
|  | 73 | +            foreach (var arg in additionalArgs) | 
|  | 74 | +            { | 
|  | 75 | +                context.Args.Add(arg); | 
|  | 76 | +            } | 
|  | 77 | +        }); | 
|  | 78 | + | 
|  | 79 | +        resourceBuilder | 
|  | 80 | +            .WithOtlpExporter() | 
|  | 81 | +            .WithIconName(iconName) | 
|  | 82 | +            .WithExplicitStart(); | 
|  | 83 | + | 
|  | 84 | +        // Validate the platform TFM when the resource is about to start | 
|  | 85 | +        resourceBuilder.OnBeforeResourceStarted((resource, eventing, ct) => | 
|  | 86 | +        { | 
|  | 87 | +            // If we couldn't detect the TFM earlier, fail the resource start | 
|  | 88 | +            if (string.IsNullOrEmpty(platformTfm)) | 
|  | 89 | +            { | 
|  | 90 | +                throw new DistributedApplicationException( | 
|  | 91 | +                    $"Unable to detect {platformDisplayName} target framework in project '{projectPath}'. " + | 
|  | 92 | +                    $"Ensure the project file contains a TargetFramework or TargetFrameworks element with a {platformDisplayName} target framework (e.g., {tfmExample}) " + | 
|  | 93 | +                    $"or remove the Add{platformDisplayName.Replace(" ", "")}Device() call from your AppHost."); | 
|  | 94 | +            } | 
|  | 95 | + | 
|  | 96 | +            return Task.CompletedTask; | 
|  | 97 | +        }); | 
|  | 98 | + | 
|  | 99 | +        // Check if platform is supported on the current host | 
|  | 100 | +        if (!isSupported()) | 
|  | 101 | +        { | 
|  | 102 | +            var reason = $"{platformDisplayName} platform not available on this host"; | 
|  | 103 | + | 
|  | 104 | +            // Mark as unsupported | 
|  | 105 | +            resourceBuilder.WithAnnotation(new UnsupportedPlatformAnnotation(reason), ResourceAnnotationMutationBehavior.Append); | 
|  | 106 | + | 
|  | 107 | +            // Add an event subscriber to set the "Unsupported" state after orchestrator initialization | 
|  | 108 | +            var appBuilder = resourceBuilder.ApplicationBuilder; | 
|  | 109 | +            appBuilder.Services.TryAddEventingSubscriber<UnsupportedPlatformEventSubscriber>(); | 
|  | 110 | +        } | 
|  | 111 | +    } | 
|  | 112 | +} | 
0 commit comments