Skip to content

Commit 5df5a16

Browse files
Tom BrewerTom Brewer
authored andcommitted
Open folder basic support
really slow right now
1 parent 94549ff commit 5df5a16

File tree

3 files changed

+66
-29
lines changed

3 files changed

+66
-29
lines changed

Apollo.Components/Editor/ApolloCodeEditor.razor

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,7 @@
445445
activeFile.Content = currentContent;
446446
}
447447

448-
Console.AddTrace($"Sending solution with files:");
449-
foreach (var item in solution.Items)
450-
{
451-
Console.AddTrace($"{item.Path}: {item.Content}");
452-
}
448+
Console.AddTrace($"Sending solution with files: {string.Join(", ", solution.Items.Select(i => i.Path))}");
453449

454450
var diagnostics = await CodeAnalysisState.GetDiagnosticsAsync(
455451
solution,

Apollo.Components/Editor/FileMenu.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<MudMenuItem Icon="@ApolloIcons.OpenFile" OnClick="@(async () => await Bus.PublishAsync(new PromptOpenFile()))">
1717
File
1818
</MudMenuItem>
19-
<MudMenuItem Icon="@ApolloIcons.OpenFolder" OnClick="@(async () => await Bus.PublishAsync(new PromptOpenFolder()))" Disabled="true">
19+
<MudMenuItem Icon="@ApolloIcons.OpenFolder" OnClick="@(async () => await Bus.PublishAsync(new PromptOpenFolder()))">
2020
Folder
2121
</MudMenuItem>
2222
<MudMenuItem Icon="@ApolloIcons.GitHub" OnClick="@(async () => await Bus.PublishAsync(new PromptOpenGitHubRepo()))" Disabled="true">

Apollo.Components/Solutions/Consumers/FolderOpener.cs

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,84 @@ public FolderOpener(IFileSystemAccessService fileSystem, SolutionsState state, I
2323

2424
public async Task Consume(PromptOpenFolder message)
2525
{
26-
FileSystemFileHandle[]? fileHandles = null;
26+
FileSystemDirectoryHandle? directoryHandle = null;
2727
try
2828
{
29-
OpenFilePickerOptionsStartInWellKnownDirectory options = new()
29+
DirectoryPickerOptionsStartInWellKnownDirectory options = new()
3030
{
31-
Multiple = true,
32-
StartIn = WellKnownDirectory.Downloads
31+
StartIn = WellKnownDirectory.Documents
3332
};
34-
fileHandles = await _fileSystem.ShowOpenFilePickerAsync(options);
33+
directoryHandle = await _fileSystem.ShowDirectoryPickerAsync(options);
3534
}
36-
catch (JSException ex)
35+
catch (JSException)
3736
{
3837
_snackbar.AddApolloNotification("File system api not supported by current browser", Severity.Error);
38+
return;
3939
}
40-
finally
40+
41+
if (directoryHandle is null)
4142
{
42-
if (fileHandles != null && fileHandles?.Length > 0)
43-
{
43+
return;
44+
}
4445

45-
var solution = new SolutionModel();
46-
foreach (var fh in fileHandles)
47-
{
48-
if (fh is not null)
49-
{
50-
var file = await fh.GetFileAsync();
51-
var text = await file.TextAsync();
52-
var name = await file.GetNameAsync();
46+
try
47+
{
48+
var solutionName = await directoryHandle.GetNameAsync();
49+
var solution = new SolutionModel(solutionName);
5350

54-
if (string.IsNullOrWhiteSpace(solution.Name))
55-
{
56-
solution.Name = name;
57-
}
51+
await ProcessDirectoryAsync(directoryHandle, solution, string.Empty);
5852

59-
solution.AddFile(name, text);
60-
}
53+
if (solution.Files.Count > 0)
54+
{
55+
await _state.LoadSolutionAsync(solution);
56+
}
57+
else
58+
{
59+
_snackbar.AddApolloNotification("No files found in the selected folder", Severity.Warning);
60+
}
61+
}
62+
catch (Exception ex)
63+
{
64+
_snackbar.AddApolloNotification($"Error reading folder: {ex.Message}", Severity.Error);
65+
}
66+
}
67+
68+
private async Task ProcessDirectoryAsync(
69+
FileSystemDirectoryHandle directoryHandle,
70+
SolutionModel solution,
71+
string relativePath)
72+
{
73+
var handles = await directoryHandle.ValuesAsync();
74+
foreach (var entry in handles)
75+
{
76+
if (entry is FileSystemFileHandle fileHandle)
77+
{
78+
try
79+
{
80+
var file = await fileHandle.GetFileAsync();
81+
var fileName = await fileHandle.GetNameAsync();
82+
var text = await file.TextAsync();
83+
84+
var folderPath = string.IsNullOrEmpty(relativePath)
85+
? solution.Name
86+
: $"{solution.Name}/{relativePath}";
87+
88+
solution.AddFile(fileName, folderPath, text);
89+
}
90+
catch
91+
{
92+
// Skip files that can't be read as text
6193
}
6294
}
95+
else if (entry is FileSystemDirectoryHandle subDirectoryHandle)
96+
{
97+
var subDirectoryName = await subDirectoryHandle.GetNameAsync();
98+
var subDirectoryRelativePath = string.IsNullOrEmpty(relativePath)
99+
? subDirectoryName
100+
: $"{relativePath}/{subDirectoryName}";
101+
102+
await ProcessDirectoryAsync(subDirectoryHandle, solution, subDirectoryRelativePath);
103+
}
63104
}
64105
}
65106
}

0 commit comments

Comments
 (0)