-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreCommitHook.cs
52 lines (43 loc) · 1.66 KB
/
PreCommitHook.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
namespace GitHooksCsharp.Hooks.PreCommit;
/// <summary>
/// Represents the pre-commit hook.
/// </summary>
public static class PreCommitHook
{
/// <summary>
/// Tasks to execute before committing.
/// </summary>
public static List<BaseHookTask> Tasks { get; } = [];
/// <summary>
/// Projects paths to format, clean, build and test.
/// </summary>
public static List<string> ProjectPaths { get; } = [];
/// <summary>
/// Test projects paths to test.
/// </summary>
public static List<string> TestProjectPaths { get; } = [];
/// <summary>
/// Executes the pre-commit hook tasks.
/// Formats, cleans, builds, and tests the project.
/// </summary>
public static void Execute()
{
// Synchronize configuration settings
Configuration.Synchronize();
// Initialize the hook logger for the pre-commit hook
HookLogger hookLogger = new(HookTypes.PreCommit);
hookLogger.StartHook();
// Add tasks to the task list
Tasks.Add(new FormatCodeTask(hookLogger, ProjectPaths.ToArray()));
Tasks.Add(new CleanTask(hookLogger, ProjectPaths.ToArray()));
Tasks.Add(new BuildCodeTask(hookLogger, ProjectPaths.ToArray()));
Tasks.Add(new TestCodeTask(hookLogger, TestProjectPaths.ToArray()));
// Execute all tasks
Tasks.ForEach(t => t.Execute());
// Determine if all tasks were successful
bool hookPassed = Tasks.TrueForAll(t => t.Success);
// Finish the hook and exit with the appropriate status code
hookLogger.FinishHook(hookPassed);
Environment.Exit(hookPassed ? 0 : 1);
}
}