-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppSettings.cs
More file actions
150 lines (128 loc) · 4.34 KB
/
AppSettings.cs
File metadata and controls
150 lines (128 loc) · 4.34 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
146
147
148
149
150
using System.Text.Json;
namespace DevRunner;
public class AppSettings
{
public List<Profile> Profiles { get; set; } = new List<Profile>();
public string CurrentProfileName { get; set; } = "Default";
// Legacy properties for backward compatibility
public string FrontEndDirectory { get; set; } = "";
public string FrontEndRunCommand { get; set; } = "npm start";
public string FrontEndBuildCommand { get; set; } = "npm run build";
public string BackEndDirectory { get; set; } = "";
public string BackEndRunCommand { get; set; } = "dotnet run";
public string BackEndBuildCommand { get; set; } = "dotnet build";
private static string SettingsPath => Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"DevRunner",
"settings.json"
);
public static AppSettings Load()
{
try
{
if (File.Exists(SettingsPath))
{
var json = File.ReadAllText(SettingsPath);
var settings = JsonSerializer.Deserialize<AppSettings>(json) ?? new AppSettings();
// Migrate legacy settings to profiles if needed
if (settings.Profiles.Count == 0)
{
settings.MigrateLegacySettings();
}
return settings;
}
}
catch
{
// If loading fails, return default settings
}
var newSettings = new AppSettings();
newSettings.CreateDefaultProfile();
return newSettings;
}
private void MigrateLegacySettings()
{
var defaultProfile = new Profile
{
Name = "Default",
Terminals = new List<TerminalConfig>()
};
// Only add terminals if they have configurations
if (!string.IsNullOrEmpty(FrontEndDirectory) || !string.IsNullOrEmpty(FrontEndRunCommand))
{
defaultProfile.Terminals.Add(new TerminalConfig
{
Title = "Front End",
Directory = FrontEndDirectory,
RunCommand = FrontEndRunCommand,
BuildCommand = FrontEndBuildCommand
});
}
if (!string.IsNullOrEmpty(BackEndDirectory) || !string.IsNullOrEmpty(BackEndRunCommand))
{
defaultProfile.Terminals.Add(new TerminalConfig
{
Title = "Back End",
Directory = BackEndDirectory,
RunCommand = BackEndRunCommand,
BuildCommand = BackEndBuildCommand
});
}
// If no terminals were configured, create default structure
if (defaultProfile.Terminals.Count == 0)
{
CreateDefaultTerminals(defaultProfile);
}
Profiles.Add(defaultProfile);
CurrentProfileName = "Default";
}
private void CreateDefaultProfile()
{
var defaultProfile = new Profile
{
Name = "Default",
Terminals = new List<TerminalConfig>()
};
CreateDefaultTerminals(defaultProfile);
Profiles.Add(defaultProfile);
CurrentProfileName = "Default";
}
private void CreateDefaultTerminals(Profile profile)
{
profile.Terminals.Add(new TerminalConfig
{
Title = "Front End",
Directory = "",
RunCommand = "npm start",
BuildCommand = "npm run build"
});
profile.Terminals.Add(new TerminalConfig
{
Title = "Back End",
Directory = "",
RunCommand = "dotnet run",
BuildCommand = "dotnet build"
});
}
public Profile? GetCurrentProfile()
{
return Profiles.FirstOrDefault(p => p.Name == CurrentProfileName);
}
public void Save()
{
try
{
var directory = Path.GetDirectoryName(SettingsPath);
if (directory != null && !Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var json = JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(SettingsPath, json);
}
catch
{
// Silently fail if we can't save settings
}
}
}