-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.cs
More file actions
145 lines (125 loc) · 4.22 KB
/
Copy pathFileSystem.cs
File metadata and controls
145 lines (125 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Documents;
namespace FirstCell
{
public interface IFileManager
{
Task LoadAsync(string path);
Task SaveAsync();
Task CreateAsync();
void Add(File file);
}
public static class FileAccessLock
{
private static readonly Dictionary<string, SemaphoreSlim> _locks = new();
public static SemaphoreSlim GetLock(string path)
{
lock (_locks)
{
if (!_locks.ContainsKey(path))
_locks[path] = new SemaphoreSlim(1, 1);
return _locks[path];
}
}
}
public abstract class File
{
public string Name { get; set; }
public string Path { get; set; }
public string Extension { get; set; }
public TabItem? CodeBlock { get; set; } // Connected to UI
protected File(string path)
{
Path = path;
Name = System.IO.Path.GetFileName(path);
Extension = System.IO.Path.GetExtension(path);
}
public virtual async Task LoadAsync()
{
if (!System.IO.File.Exists(Path)) return;
string content = await System.IO.File.ReadAllTextAsync(Path);
// Set RichTextBox content later via orchestrator/UI
}
public virtual async Task SaveAsync()
{
//var textBox = CodeBlock?.Content as RichTextBox;
//if (textBox != null)
//{
// var range = new TextRange(textBox.Document.ContentStart, textBox.Document.ContentEnd);
// await System.IO.File.WriteAllTextAsync(Path, range.Text);
//}
var fileLock = FileAccessLock.GetLock(Path);
await fileLock.WaitAsync();
try
{
var textBox = CodeBlock?.Content as RichTextBox;
if (textBox != null)
{
var range = new TextRange(textBox.Document.ContentStart, textBox.Document.ContentEnd);
await System.IO.File.WriteAllTextAsync(Path, range.Text);
}
}
finally
{
fileLock.Release();
}
}
public virtual Task CreateAsync()
{
System.IO.File.WriteAllText(Path, "");
return Task.CompletedTask;
}
public virtual void Add() { } //no need on base
}
public class Project : IFileManager
{
public string Name { get; set; }
public string Path { get; set; }
public bool IsLoaded { get; private set; }
public List<File> Files { get; private set; } = new();
public async Task LoadAsync(string path)
{
this.Path = path;
this.Name = System.IO.Path.GetFileName(path);
IsLoaded = true;
// Optional: Load existing files from folder
foreach (var file in Directory.GetFiles(path))
{
string ext = System.IO.Path.GetExtension(file);
File? loaded = ext switch
{
".html" => new HTMLFile(file),
".css" => new CSSFile(file),
".js" => new JSFile(file),
_ => null
};
if (loaded != null)
{
await loaded.LoadAsync();
Files.Add(loaded);
}
}
}
public Task SaveAsync() => Task.WhenAll(Files.Select(f => f.SaveAsync()));
public Task CreateAsync() => Task.CompletedTask;
public void Add(File file) => Files.Add(file);
}
public class HTMLFile : File
{
public HTMLFile(string path) : base(path) { }
}
public class CSSFile : File
{
public CSSFile(string path) : base(path) { }
}
public class JSFile : File
{
public JSFile(string path) : base(path) { }
}
}