-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathCleanCommand.cs
70 lines (59 loc) · 2.08 KB
/
CleanCommand.cs
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
using System;
using System.CommandLine;
using System.CommandLine.Binding;
using System.CommandLine.Invocation;
using System.IO;
using System.Linq;
namespace GirTool;
public class CleanCommand : Command
{
public CleanCommand() : base(
name: "clean",
description: "Cleans the output directories")
{
var target = new Argument<string>(
name: "target",
description: "Target folder to clean of all generated C# files (*.Generated.cs)"
);
AddArgument(target);
this.SetHandler(context => Execute(
folder: context.ParseResult.GetValueForArgument(target),
invocationContext: context
));
}
private static void Execute(string folder, InvocationContext invocationContext)
{
try
{
if (!VerifyFolderExits(folder, invocationContext))
return;
var deletedFiles = 0;
var searchedFolders = 0;
foreach (var d in Directory.EnumerateDirectories(folder, "*", SearchOption.AllDirectories))
{
foreach (var file in Directory.EnumerateFiles(d).Where(FileIsGenerated))
{
File.Delete(file);
deletedFiles++;
}
searchedFolders++;
}
Log.Information($"Deleted {deletedFiles} files in {searchedFolders} folders");
}
catch (Exception ex)
{
Log.Exception(ex);
Log.Error("An error occurred while cleaning files. Please save a copy of your log output and open an issue at: https://github.com/gircore/gir.core/issues/new");
invocationContext.ExitCode = 1;
}
}
private static bool VerifyFolderExits(string folder, InvocationContext invocationContext)
{
if (Directory.Exists(folder))
return true;
Log.Error($"Folder {folder} does not exist");
invocationContext.ExitCode = 1;
return false;
}
private static bool FileIsGenerated(string file) => file.EndsWith(".Generated.cs");
}