-
Notifications
You must be signed in to change notification settings - Fork 0
37 feat add remove hook command #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
190ae75
94da759
09faa4a
dcbe505
ab10ba3
cc3ee4b
a85eac4
a03738c
7b4483c
24c2de1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using AL2DBML.CLI.Services; | ||
| using Spectre.Console; | ||
| using Spectre.Console.Cli; | ||
|
|
||
| namespace AL2DBML.CLI.Commands; | ||
|
|
||
| public class RemoveHookSettings : CommandSettings | ||
| { | ||
| } | ||
|
|
||
| public class RemoveHookCommand : Command<RemoveHookSettings> | ||
| { | ||
| protected override int Execute(CommandContext context, RemoveHookSettings settings, CancellationToken cancellationToken) | ||
| { | ||
| var removed = HookService.Remove(); | ||
| if (!removed) | ||
| AnsiConsole.MarkupLine("[yellow]Warning:[/] No AL2DBML section found in pre-commit hook."); | ||
| else | ||
| AnsiConsole.MarkupLine("[green]Done:[/] AL2DBML section removed from pre-commit hook."); | ||
|
|
||
| return 0; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| namespace AL2DBML.CLI.Constants; | ||
|
|
||
| internal static class HookMarkers | ||
| { | ||
| public const string Start = "# [al2dbml-start]"; | ||
| public const string End = "# [al2dbml-end]"; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| using AL2DBML.CLI.Constants; | ||
|
|
||
| namespace AL2DBML.CLI.Services; | ||
|
|
||
| internal static class HookService | ||
| { | ||
| private const string HookPath = ".git/hooks/pre-commit"; | ||
| private const string HookCommand = "al2dbml generate"; | ||
|
|
||
| public static void Write() | ||
| { | ||
| if (!Directory.Exists(".git/hooks")) | ||
| return; | ||
|
|
||
| var hookSection = $"{HookMarkers.Start}\nif command -v al2dbml > /dev/null 2>&1; then\n {HookCommand} || printf \"\\033[33mWarning: al2dbml generate failed, skipping DBML update.\\033[0m\\n\"\nelse\n printf \"\\033[33mWarning: al2dbml not found, skipping DBML update.\\033[0m\\n\"\nfi\n{HookMarkers.End}"; | ||
|
|
||
| string content; | ||
| if (File.Exists(HookPath)) | ||
| { | ||
| content = File.ReadAllText(HookPath); | ||
| var startIdx = content.IndexOf(HookMarkers.Start, StringComparison.Ordinal); | ||
| var endIdx = content.IndexOf(HookMarkers.End, StringComparison.Ordinal); | ||
|
|
||
| if (startIdx >= 0 && endIdx > startIdx) | ||
| content = content[..startIdx] + hookSection + content[(endIdx + HookMarkers.End.Length)..]; | ||
| else | ||
|
Comment on lines
+20
to
+26
|
||
| content = content.TrimEnd() + $"\n\n{hookSection}\n"; | ||
| } | ||
| else | ||
| { | ||
| content = $"#!/bin/sh\n\n{hookSection}\n"; | ||
| } | ||
|
|
||
| File.WriteAllText(HookPath, content); | ||
|
|
||
| if (!OperatingSystem.IsWindows()) | ||
| File.SetUnixFileMode(HookPath, | ||
| UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute | | ||
| UnixFileMode.GroupRead | UnixFileMode.GroupExecute | | ||
| UnixFileMode.OtherRead | UnixFileMode.OtherExecute); | ||
| } | ||
|
|
||
| public static bool Remove() | ||
| { | ||
| if (!File.Exists(HookPath)) return false; | ||
|
|
||
| var content = File.ReadAllText(HookPath); | ||
| var startIdx = content.IndexOf(HookMarkers.Start, StringComparison.Ordinal); | ||
| var endIdx = content.IndexOf(HookMarkers.End, StringComparison.Ordinal); | ||
| if (startIdx < 0 || endIdx <= startIdx) return false; | ||
|
|
||
| var before = content[..startIdx].TrimEnd(); | ||
| var after = content[(endIdx + HookMarkers.End.Length)..].TrimStart('\r', '\n'); | ||
|
Comment on lines
+47
to
+53
|
||
| var newContent = before.Length > 0 && after.Length > 0 | ||
| ? before + "\n\n" + after | ||
| : (before + after).Trim(); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(newContent.Replace("#!/bin/sh", ""))) | ||
| File.Delete(HookPath); | ||
| else | ||
| File.WriteAllText(HookPath, newContent + "\n"); | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The prompt asks "Create a pre-commit hook?", but when the user answers "No" and an AL2DBML hook already exists, the code removes the existing hook section. That’s a surprising/implicit behavior change (previously "No" would typically mean "leave as-is"). Consider either (a) changing the prompt text to reflect removal, or (b) adding a separate confirmation for removal / making "No" a no-op.