Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
222 lines (197 loc) · 7.69 KB
/
Copy pathProgram.cs
File metadata and controls
222 lines (197 loc) · 7.69 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using DotfilesManager.Core;
using DotfilesManager.Operations;
using TermFlow.Components.FullScreen;
using TermFlow.Components.InLine;
using TermFlow.Core;
namespace DotfilesManager;
internal static class Program
{
public static async Task Main(string[] args)
{
Engine.Setup();
await Env.LoadOrInit();
// Watcher para recargar DotfilesDir si cambia config.json
using var watcher = new FileSystemWatcher(
Path.GetDirectoryName(Env.ConfigFile),
Path.GetFileName(Env.ConfigFile))
{
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.CreationTime,
EnableRaisingEvents = true
};
watcher.Changed += (_, _) =>
{
Thread.Sleep(50); // Pequeña pausa para que termine de escribirse
Env.ReloadConfig();
TextViewer.Info($"DotfilesDir actualizado a: {Env.DotfilesDir}");
};
if (args.Length == 0)
{
Env.CliMode = 0;
await RunInteractive();
}
else
{
Env.CliMode = 1;
RunCli(ArgParser.Parse(args));
}
}
private static async Task RunInteractive()
{
while (true)
{
ThemeColors.Primary = AnsiColor.Green + AnsiColor.Bold;
ThemeColors.Selector = ThemeColors.Primary;
string[] options = ["Symlinks", "Ejecutar script", "Perfiles"];
int choice = await Menu.SelectOneAsync($"{ThemeColors.Primary}Dotfiles Manager{ThemeColors.Reset}", options);
switch (choice)
{
case -1: TextViewer.WritePlain(""); return;
case 0:
ThemeColors.Primary = AnsiColor.Red + AnsiColor.Bold;
ThemeColors.Selector = ThemeColors.Primary;
await SymlinksMenu();
break;
case 1:
ThemeColors.Primary = AnsiColor.Yellow + AnsiColor.Bold;
ThemeColors.Selector = ThemeColors.Primary;
await ExecuteOp.Run();
break;
case 2:
ThemeColors.Primary = AnsiColor.Magenta + AnsiColor.Bold;
ThemeColors.Selector = ThemeColors.Primary;
await ProfilesMenu();
break;
}
}
}
private static void RunCli(CliCommand cmd)
{
switch (cmd.Type)
{
case CommandType.Error:
Environment.ExitCode = 1;
break;
case CommandType.Help:
ArgParser.ShowHelp(cmd.Action);
break;
case CommandType.Apply:
if (cmd.SystemPaths.Length > 0)
ApplyOp.ApplySystem(cmd.SystemPaths);
else if (cmd.Packages.Length > 0)
ApplyOp.ApplyHome(cmd.Packages);
break;
case CommandType.Add:
if (cmd.AddToSystem && cmd.SystemPaths.Length > 0)
AddOp.AddToSystem(cmd.SystemPaths[0]);
else if (cmd.AddHomePath is not null && cmd.AddHomePackage is not null)
AddOp.AddToHome(cmd.AddHomePath, cmd.AddHomePackage);
break;
case CommandType.Backup:
if (cmd.BackupTarget == BackupTarget.Packages)
{
if (cmd.BackupAll)
Backup.BackupAllRepoPackages(Env.NewBackupDir + "_manualPackagesAllAction");
else
Backup.BackupRepoPackages(cmd.Packages, Env.NewBackupDir + "_manualPackagesAction");
}
else if (cmd.BackupTarget == BackupTarget.System)
{
if (cmd.BackupAll)
Backup.BackupAllRepoSystem(Env.NewBackupDir + "_manualSystemAllAction");
else
Backup.BackupRepoSystemPaths(cmd.SystemPaths, Env.NewBackupDir + "_manualSystemAction");
}
break;
case CommandType.Delete:
if (cmd.DeleteSystem && cmd.SystemPaths.Length > 0)
DeleteOp.DeleteSystem(cmd.SystemPaths, cmd.DeleteAction);
else if (cmd.Packages.Length > 0)
DeleteOp.DeleteHome(cmd.Packages[0], cmd.DeleteAction);
break;
case CommandType.Status:
StatusOp.Check();
break;
case CommandType.Script:
ExecuteOp.RunScript(cmd.ScriptName, cmd.ScriptArgs);
break;
case CommandType.Profile:
switch (cmd.ProfileAction)
{
case ProfileAction.Create:
CreateProfileOp.Create(cmd.Profile, cmd.Packages, cmd.Dotfiles);
break;
case ProfileAction.EditName:
EditProfileOp.EditName(cmd.Profile, cmd.NewName);
break;
case ProfileAction.EditPackages:
EditProfileOp.EditPackages(cmd.Profile, cmd.Packages);
break;
case ProfileAction.EditDotfiles:
EditProfileOp.EditDotfiles(cmd.Profile, cmd.Dotfiles);
break;
case ProfileAction.Apply:
ApplyProfileOp.ApplyProfile(cmd.Profile, startStep: cmd.StartStep);
break;
case ProfileAction.Export:
ExportProfileOp.Export(cmd.Profile);
break;
}
break;
case CommandType.SetDir:
Env.SetDotfilesDir(cmd.DotfilesDir);
break;
}
}
// ── Menú de Symlinks ──────────────────────────────────────────────────
private static async Task SymlinksMenu()
{
string[] options =
[
"Aplicar / instalar dotfiles",
"Ver estado de symlinks",
"Borrar symlinks",
"Agregar archivo al repo",
];
bool running = true;
while (running)
{
int choice = await Menu.SelectOneAsync($"{ThemeColors.Primary}Symlinks Manager{ThemeColors.Reset}", options);
switch (choice)
{
case -1: running = false; break;
case 0: await ApplyOp.Run(); break;
case 1: StatusOp.Run(); break;
case 2: await DeleteOp.Run(); break;
case 3: await AddOp.Run(); break;
}
}
}
// ── Menú de Perfiles ──────────────────────────────────────────────────
private static async Task ProfilesMenu()
{
string[] options =
[
"Crear perfil",
"Editar perfil",
"Aplicar perfil",
"Exportar perfil",
];
bool running = true;
while (running)
{
int choice = await Menu.SelectOneAsync($"{ThemeColors.Primary}Perfiles{ThemeColors.Reset}", options);
switch (choice)
{
case -1: running = false; break;
case 0: await CreateProfileOp.Run(); break;
case 1: await EditProfileOp.Run(); break;
case 2: await ApplyProfileOp.Run(); break;
case 3: await ExportProfileOp.Run(); break;
}
}
}
}