Skip to content

Commit 3ca601f

Browse files
committed
Refactor file handling to use IFileSystemService for temporary file management in Maui environment annotations and MySql builder extensions
1 parent 62749e8 commit 3ca601f

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

src/Aspire.Hosting.Maui/Utilities/MauiAndroidEnvironmentAnnotation.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
#pragma warning disable ASPIREFILESYSTEM001 // Type is for evaluation purposes only
5+
46
using Aspire.Hosting.ApplicationModel;
57
using Aspire.Hosting.Eventing;
68
using Aspire.Hosting.Lifecycle;
9+
using Microsoft.Extensions.DependencyInjection;
710
using Microsoft.Extensions.Logging;
811

912
namespace Aspire.Hosting.Maui.Utilities;
@@ -38,7 +41,8 @@ internal sealed class MauiAndroidEnvironmentProcessedAnnotation : IResourceAnnot
3841
internal sealed class MauiAndroidEnvironmentSubscriber(
3942
DistributedApplicationExecutionContext executionContext,
4043
ResourceLoggerService loggerService,
41-
ResourceNotificationService notificationService) : IDistributedApplicationEventingSubscriber
44+
ResourceNotificationService notificationService,
45+
IFileSystemService fileSystemService) : IDistributedApplicationEventingSubscriber
4246
{
4347
public Task SubscribeAsync(IDistributedApplicationEventing eventing, DistributedApplicationExecutionContext execContext, CancellationToken cancellationToken)
4448
{
@@ -83,6 +87,7 @@ private async Task OnBeforeResourceStartedAsync(BeforeResourceStartedEvent @even
8387
if (generatedFilePath is null)
8488
{
8589
generatedFilePath = await MauiEnvironmentHelper.CreateAndroidEnvironmentTargetsFileAsync(
90+
fileSystemService,
8691
resource,
8792
executionContext,
8893
logger,

src/Aspire.Hosting.Maui/Utilities/MauiEnvironmentHelper.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
#pragma warning disable ASPIREFILESYSTEM001 // Type is for evaluation purposes only
5+
46
using System.Globalization;
57
using System.Text;
68
using System.Xml.Linq;
@@ -22,12 +24,14 @@ internal static class MauiEnvironmentHelper
2224
/// <summary>
2325
/// Creates an MSBuild targets file for Android that sets environment variables.
2426
/// </summary>
27+
/// <param name="fileSystemService">The file system service for managing temp files.</param>
2528
/// <param name="resource">The resource to collect environment variables from.</param>
2629
/// <param name="executionContext">The execution context.</param>
2730
/// <param name="logger">Logger for diagnostic output.</param>
2831
/// <param name="cancellationToken">Cancellation token.</param>
2932
/// <returns>The path to the generated targets file, or null if no environment variables are present.</returns>
3033
public static async Task<string?> CreateAndroidEnvironmentTargetsFileAsync(
34+
IFileSystemService fileSystemService,
3135
IResource resource,
3236
DistributedApplicationExecutionContext executionContext,
3337
ILogger logger,
@@ -68,8 +72,7 @@ await resource.ProcessEnvironmentVariableValuesAsync(
6872
}
6973

7074
// Create a temporary targets file
71-
var tempDirectory = Path.Combine(Path.GetTempPath(), "aspire", "maui", "android-env");
72-
Directory.CreateDirectory(tempDirectory);
75+
var tempDirectory = fileSystemService.TempDirectory.CreateTempSubdirectory("aspire-maui-android-env").Path;
7376

7477
// Prune old targets files
7578
PruneOldTargets(tempDirectory, logger);
@@ -215,12 +218,14 @@ private static string EncodeSemicolons(string value, out bool wasEncoded)
215218
/// <summary>
216219
/// Creates an MSBuild targets file for iOS that sets environment variables.
217220
/// </summary>
221+
/// <param name="fileSystemService">The file system service for managing temp files.</param>
218222
/// <param name="resource">The resource to collect environment variables from.</param>
219223
/// <param name="executionContext">The execution context.</param>
220224
/// <param name="logger">Logger for diagnostic output.</param>
221225
/// <param name="cancellationToken">Cancellation token.</param>
222226
/// <returns>The path to the generated targets file, or null if no environment variables are present.</returns>
223227
public static async Task<string?> CreateiOSEnvironmentTargetsFileAsync(
228+
IFileSystemService fileSystemService,
224229
IResource resource,
225230
DistributedApplicationExecutionContext executionContext,
226231
ILogger logger,
@@ -251,8 +256,7 @@ await resource.ProcessEnvironmentVariableValuesAsync(
251256
}
252257

253258
// Create a temporary targets file
254-
var tempDirectory = Path.Combine(Path.GetTempPath(), "aspire", "maui", "mlaunch-env");
255-
Directory.CreateDirectory(tempDirectory);
259+
var tempDirectory = fileSystemService.TempDirectory.CreateTempSubdirectory("aspire-maui-mlaunch-env").Path;
256260

257261
// Prune old targets files
258262
PruneOldTargetsiOS(tempDirectory, logger);

src/Aspire.Hosting.Maui/Utilities/MauiiOSEnvironmentAnnotation.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
#pragma warning disable ASPIREFILESYSTEM001 // Type is for evaluation purposes only
5+
46
using Aspire.Hosting.ApplicationModel;
57
using Aspire.Hosting.Eventing;
68
using Aspire.Hosting.Lifecycle;
9+
using Microsoft.Extensions.DependencyInjection;
710
using Microsoft.Extensions.Logging;
811

912
namespace Aspire.Hosting.Maui.Utilities;
@@ -38,7 +41,8 @@ internal sealed class MauiiOSEnvironmentProcessedAnnotation : IResourceAnnotatio
3841
internal sealed class MauiiOSEnvironmentSubscriber(
3942
DistributedApplicationExecutionContext executionContext,
4043
ResourceLoggerService loggerService,
41-
ResourceNotificationService notificationService) : IDistributedApplicationEventingSubscriber
44+
ResourceNotificationService notificationService,
45+
IFileSystemService fileSystemService) : IDistributedApplicationEventingSubscriber
4246
{
4347
public Task SubscribeAsync(IDistributedApplicationEventing eventing, DistributedApplicationExecutionContext execContext, CancellationToken cancellationToken)
4448
{
@@ -83,6 +87,7 @@ private async Task OnBeforeResourceStartedAsync(BeforeResourceStartedEvent @even
8387
if (generatedFilePath is null)
8488
{
8589
generatedFilePath = await MauiEnvironmentHelper.CreateiOSEnvironmentTargetsFileAsync(
90+
fileSystemService,
8691
resource,
8792
executionContext,
8893
logger,

src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
#pragma warning disable ASPIREFILESYSTEM001 // Type is for evaluation purposes only
5+
46
using Aspire.Hosting;
57
using Aspire.Hosting.ApplicationModel;
68
using Aspire.Hosting.MySql;
@@ -260,7 +262,8 @@ public static IResourceBuilder<T> WithPhpMyAdmin<T>(this IResourceBuilder<T> bui
260262
}
261263
else
262264
{
263-
var tempConfigFile = await WritePhpMyAdminConfiguration(mySqlInstances, ct).ConfigureAwait(false);
265+
var fileSystemService = e.Services.GetRequiredService<IFileSystemService>();
266+
var tempConfigFile = await WritePhpMyAdminConfiguration(fileSystemService, mySqlInstances, ct).ConfigureAwait(false);
264267

265268
try
266269
{
@@ -374,10 +377,10 @@ public static IResourceBuilder<MySqlServerResource> WithInitFiles(this IResource
374377
return builder.WithContainerFiles(initPath, importFullPath);
375378
}
376379

377-
private static async Task<string> WritePhpMyAdminConfiguration(IEnumerable<MySqlServerResource> mySqlInstances, CancellationToken cancellationToken)
380+
private static async Task<string> WritePhpMyAdminConfiguration(IFileSystemService fileSystemService, IEnumerable<MySqlServerResource> mySqlInstances, CancellationToken cancellationToken)
378381
{
379382
// This temporary file is not used by the container, it will be copied and then deleted
380-
var filePath = Path.GetTempFileName();
383+
var filePath = fileSystemService.TempDirectory.CreateTempFile().Path;
381384

382385
using var writer = new StreamWriter(filePath);
383386

0 commit comments

Comments
 (0)