Skip to content

Commit ea082bc

Browse files
Copilotdavidfowl
andcommitted
Simplify CreateTempFile API to take no parameters
Co-authored-by: davidfowl <[email protected]>
1 parent 1f75f9a commit ea082bc

File tree

4 files changed

+17
-63
lines changed

4 files changed

+17
-63
lines changed

IMPLEMENTATION_STATUS.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ This document tracks the implementation of IFileSystemService across the Aspire
55
## Completed Tasks
66

77
### Core API
8-
- ✅ Created `src/Aspire.Hosting/Utils/IFileSystemService.cs` with adjusted API
9-
- Renamed `GetTempFileName``CreateTempFile(string? extension = null)`
10-
- Added overload `CreateTempFile(string prefix, string fileName)`
11-
- Improved XML documentation with clarity about parent directory disposal
8+
- ✅ Created `src/Aspire.Hosting/Utils/IFileSystemService.cs` with simplified API
9+
- Single `CreateTempFile()` method with no parameters for creating temp files
10+
- `CreateTempSubdirectory(string? prefix = null)` for creating temp directories
11+
- Simplified XML documentation
1212
- ✅ Created `src/Aspire.Hosting/Utils/FileSystemService.cs` implementation
1313
- ✅ Updated `src/Aspire.Hosting/IDistributedApplicationBuilder.cs`
1414
- Added FileSystemService property
@@ -125,20 +125,17 @@ var directoryService = builder.ApplicationBuilder.FileSystemService;
125125
### 3. Replace temp file/directory APIs
126126
```csharp
127127
// OLD: Path.GetTempFileName()
128-
// NEW: directoryService.TempDirectory.CreateTempFile("prefix", "filename.ext").Path
128+
// NEW: directoryService.TempDirectory.CreateTempFile().Path
129129
130130
// OLD: Directory.CreateTempSubdirectory("prefix")
131131
// NEW: directoryService.TempDirectory.CreateTempSubdirectory("prefix").Path
132-
133-
// For random temp file with specific extension:
134-
// directoryService.TempDirectory.CreateTempFile(".json").Path
135132
```
136133

137134
## Key Adjustments from Original PR #13244
138135

139-
1.**Renamed API**: `GetTempFileName(extension?)` `CreateTempFile(extension?)`
136+
1.**Simplified API**: `CreateTempFile()` with no parameters - simple temp file creation
140137
2.**Removed Documentation**: Removed references to ASPIRE_TEMP_FOLDER and Aspire:TempDirectory configuration
141-
3.**Improved Docs**: Added clarity about parent directory disposal in TempFile XML docs
138+
3.**Simplified Implementation**: Removed complex overloads and parent directory deletion logic
142139
4.**Line Numbers**: Need to fix line numbers in docs/temp-folder-inventory.md tables
143140
5.**Tests**: Need to create comprehensive FileSystemServiceTests.cs
144141

@@ -156,6 +153,6 @@ Before finalizing, ensure:
156153
## Notes
157154

158155
- The pattern of extracting `.Path` and not disposing is **intentional** - many temp files/dirs persist for app lifetime
159-
- When using `CreateTempFile(prefix, fileName)`, the parent directory is auto-deleted on Dispose
160-
- When using `CreateTempFile(extension)`, only the file is deleted (no parent dir cleanup)
156+
- `CreateTempFile()` creates a simple temp file, caller is responsible for cleanup if needed
157+
- `CreateTempSubdirectory(prefix)` creates a temp directory that can be disposed to clean up
161158
- All APIs are marked `[Experimental("ASPIREFILESYSTEM001")]`

src/Aspire.Hosting/ApplicationModel/AspireStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public string GetFileNameWithContent(string filenameTemplate, Stream contentStre
4949
filenameTemplate = Path.GetFileName(filenameTemplate);
5050

5151
// Create a temporary file to write the content to.
52-
var tempFileName = _directoryService.TempDirectory.CreateTempFile("aspire-store", "content.tmp").Path;
52+
var tempFileName = _directoryService.TempDirectory.CreateTempFile().Path;
5353

5454
// Fast, non-cryptographic hash.
5555
var hash = new XxHash3();

src/Aspire.Hosting/Utils/FileSystemService.cs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,10 @@ public TempDirectory CreateTempSubdirectory(string? prefix = null)
2828
}
2929

3030
/// <inheritdoc/>
31-
public TempFile CreateTempFile(string? extension = null)
31+
public TempFile CreateTempFile()
3232
{
3333
var tempFile = Path.GetTempFileName();
34-
if (extension is not null)
35-
{
36-
var newPath = Path.ChangeExtension(tempFile, extension);
37-
File.Move(tempFile, newPath);
38-
return new DefaultTempFile(newPath, deleteParentDirectory: false);
39-
}
40-
return new DefaultTempFile(tempFile, deleteParentDirectory: false);
41-
}
42-
43-
/// <inheritdoc/>
44-
public TempFile CreateTempFile(string prefix, string fileName)
45-
{
46-
var tempDir = Directory.CreateTempSubdirectory(prefix).FullName;
47-
var filePath = Path.Combine(tempDir, fileName);
48-
File.Create(filePath).Dispose();
49-
return new DefaultTempFile(filePath, deleteParentDirectory: true);
34+
return new DefaultTempFile(tempFile);
5035
}
5136
}
5237

@@ -86,12 +71,10 @@ public override void Dispose()
8671
private sealed class DefaultTempFile : TempFile
8772
{
8873
private readonly string _path;
89-
private readonly bool _deleteParentDirectory;
9074

91-
public DefaultTempFile(string path, bool deleteParentDirectory)
75+
public DefaultTempFile(string path)
9276
{
9377
_path = path;
94-
_deleteParentDirectory = deleteParentDirectory;
9578
}
9679

9780
public override string Path => _path;
@@ -104,15 +87,6 @@ public override void Dispose()
10487
{
10588
File.Delete(_path);
10689
}
107-
108-
if (_deleteParentDirectory)
109-
{
110-
var parentDir = System.IO.Path.GetDirectoryName(_path);
111-
if (parentDir is not null && Directory.Exists(parentDir))
112-
{
113-
Directory.Delete(parentDir, recursive: true);
114-
}
115-
}
11690
}
11791
catch
11892
{

src/Aspire.Hosting/Utils/IFileSystemService.cs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,17 @@ public interface ITempFileSystemService
5252
/// <summary>
5353
/// Creates a new temporary file and returns it.
5454
/// </summary>
55-
/// <param name="extension">Optional file extension including the dot (e.g., ".txt", ".json"). If null, uses the default .tmp extension.</param>
5655
/// <returns>A <see cref="TempFile"/> representing the created temporary file. Dispose to delete.</returns>
5756
/// <remarks>
5857
/// <para>
59-
/// This method creates a new temporary file using <see cref="Path.GetTempFileName"/>
60-
/// and optionally renames it with the specified extension. Dispose the returned object to delete the file.
58+
/// This method creates a new temporary file using <see cref="Path.GetTempFileName"/>.
59+
/// Dispose the returned object to delete the file.
6160
/// </para>
6261
/// <para>
6362
/// Use this instead of calling <see cref="Path.GetTempFileName"/> directly.
6463
/// </para>
6564
/// </remarks>
66-
TempFile CreateTempFile(string? extension = null);
67-
68-
/// <summary>
69-
/// Creates a new temporary file with the specified name in a temporary directory.
70-
/// </summary>
71-
/// <param name="prefix">Prefix for the temporary directory name.</param>
72-
/// <param name="fileName">The name for the temporary file (e.g., "config.json", "script.php").</param>
73-
/// <returns>A <see cref="TempFile"/> representing the created temporary file. Dispose to delete the file and its parent directory.</returns>
74-
/// <remarks>
75-
/// <para>
76-
/// This method creates a temporary subdirectory with the given prefix and places a file with the specified name inside it.
77-
/// This is useful when the filename matters (e.g., for scripts that check their own filename).
78-
/// Dispose the returned object to delete the file and its parent directory.
79-
/// </para>
80-
/// </remarks>
81-
TempFile CreateTempFile(string prefix, string fileName);
65+
TempFile CreateTempFile();
8266
}
8367

8468
/// <summary>
@@ -110,8 +94,7 @@ public abstract class TempFile : IDisposable
11094
public abstract string Path { get; }
11195

11296
/// <summary>
113-
/// Deletes the temporary file. When created with a specific file name in a temporary directory,
114-
/// also deletes the parent directory.
97+
/// Deletes the temporary file.
11598
/// </summary>
11699
public abstract void Dispose();
117100
}

0 commit comments

Comments
 (0)