-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Implement initialization command #31
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| using AL2DBML.CLI.Models; | ||
| using AL2DBML.CLI.Services; | ||
| using Spectre.Console; | ||
| using Spectre.Console.Cli; | ||
|
|
||
| namespace AL2DBML.CLI.Commands; | ||
|
|
||
| public class InitSettings : CommandSettings | ||
| { | ||
| } | ||
|
|
||
| public class InitCommand : AsyncCommand<InitSettings> | ||
| { | ||
| private const string HookStartMarker = "# [al2dbml-start]"; | ||
| private const string HookEndMarker = "# [al2dbml-end]"; | ||
| private const string HookCommand = "al2dbml generate"; | ||
| private const string GitignoreEntry = ".al2dbml/config.local.json"; | ||
|
|
||
| private readonly IConfigService _configService; | ||
|
|
||
| public InitCommand(IConfigService configService) | ||
| { | ||
| _configService = configService; | ||
| } | ||
|
|
||
| protected override Task<int> ExecuteAsync(CommandContext context, InitSettings settings, CancellationToken cancellationToken) | ||
| { | ||
| var existingShared = _configService.LoadSharedConfig(); | ||
| var existingLocal = _configService.LoadLocalConfig(); | ||
|
|
||
| if (_configService.ConfigExists()) | ||
| AnsiConsole.MarkupLine("[yellow]Existing config found — pre-filling with current values.[/]"); | ||
|
|
||
| var inputPath = AnsiConsole.Prompt( | ||
| new TextPrompt<string>("Input path (you can point to a code-workspace file):") | ||
| .DefaultValue(existingLocal?.Input.Path ?? ".")); | ||
|
|
||
| var outputPath = AnsiConsole.Prompt( | ||
| new TextPrompt<string>("Output path:") | ||
| .DefaultValue(existingShared?.Output.Path ?? "./docs/")); | ||
|
|
||
| var outputName = AnsiConsole.Prompt( | ||
| new TextPrompt<string>("Output file name (without extension):") | ||
| .DefaultValue(existingShared?.Output.Name ?? "schema")); | ||
|
|
||
| var hookExists = File.Exists(".git/hooks/pre-commit") && | ||
| File.ReadAllText(".git/hooks/pre-commit").Contains(HookStartMarker, StringComparison.Ordinal); | ||
| var createHook = AnsiConsole.Confirm("Create a pre-commit hook?", hookExists); | ||
|
|
||
| _configService.SaveSharedConfig(new SharedConfig | ||
| { | ||
| Output = new OutputConfig { Path = outputPath, Name = outputName } | ||
| }); | ||
| _configService.SaveLocalConfig(new LocalConfig | ||
| { | ||
| Input = new InputConfig { Path = inputPath } | ||
| }); | ||
|
|
||
| EnsureGitignoreEntry(GitignoreEntry); | ||
|
|
||
| if (createHook) | ||
| WritePreCommitHook(); | ||
|
|
||
| AnsiConsole.MarkupLine("[green]Done:[/] AL2DBML initialized."); | ||
| return Task.FromResult(0); | ||
| } | ||
|
|
||
| private static void EnsureGitignoreEntry(string entry) | ||
| { | ||
| const string gitignorePath = ".gitignore"; | ||
| var lines = File.Exists(gitignorePath) | ||
| ? File.ReadAllLines(gitignorePath).ToList() | ||
| : []; | ||
|
|
||
| if (lines.Contains(entry)) | ||
| return; | ||
|
|
||
| lines.Add(entry); | ||
| File.WriteAllLines(gitignorePath, lines); | ||
| } | ||
|
|
||
| private static void WritePreCommitHook() | ||
| { | ||
| const string hookPath = ".git/hooks/pre-commit"; | ||
|
|
||
| if (!Directory.Exists(".git/hooks")) | ||
| { | ||
| AnsiConsole.MarkupLine("[yellow]Warning:[/] .git/hooks directory not found — skipping pre-commit hook creation."); | ||
| return; | ||
| } | ||
| var hookSection = $"{HookStartMarker}\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{HookEndMarker}"; | ||
|
|
||
| string content; | ||
| if (File.Exists(hookPath)) | ||
| { | ||
| content = File.ReadAllText(hookPath); | ||
| var startIdx = content.IndexOf(HookStartMarker, StringComparison.Ordinal); | ||
| var endIdx = content.IndexOf(HookEndMarker, StringComparison.Ordinal); | ||
|
|
||
| if (startIdx >= 0 && endIdx >= 0) | ||
| content = content[..startIdx] + hookSection + content[(endIdx + HookEndMarker.Length)..]; | ||
| else | ||
| 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); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace AL2DBML.CLI.Models; | ||
|
|
||
| public class LocalConfig | ||
| { | ||
| public InputConfig Input { get; set; } = new(); | ||
| } | ||
|
|
||
| public class InputConfig | ||
| { | ||
| public string Path { get; set; } = "."; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| namespace AL2DBML.CLI.Models; | ||
|
|
||
| public class SharedConfig | ||
| { | ||
| public OutputConfig Output { get; set; } = new(); | ||
| } | ||
|
|
||
| public class OutputConfig | ||
| { | ||
| public string Path { get; set; } = "./docs/"; | ||
| public string Name { get; set; } = "schema"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,9 @@ | |
| services | ||
| .AddAL2Dbml() | ||
| .AddScoped<GenerateCommand>() | ||
| .AddScoped<IParsingTracker, ParsingTracker>(); | ||
| .AddScoped<InitCommand>() | ||
| .AddScoped<IParsingTracker, ParsingTracker>() | ||
| .AddScoped<IConfigService, ConfigService>(); | ||
|
|
||
| var registrar = new TypeRegistrar(services); | ||
|
|
||
|
|
@@ -20,6 +22,7 @@ | |
| app.Configure(config => | ||
| { | ||
| config.AddCommand<GenerateCommand>("generate"); | ||
| config.AddCommand<InitCommand>("init"); | ||
| }); | ||
|
Comment on lines
22
to
26
|
||
|
|
||
| return await app.RunAsync(args); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| using System.Text.Json; | ||
| using AL2DBML.CLI.Models; | ||
|
|
||
| namespace AL2DBML.CLI.Services; | ||
|
|
||
| public class ConfigService : IConfigService | ||
| { | ||
| private const string ConfigDir = ".al2dbml"; | ||
| private const string SharedConfigFile = "config.json"; | ||
| private const string LocalConfigFile = "config.local.json"; | ||
|
|
||
| private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true }; | ||
|
|
||
| public bool ConfigExists() => Directory.Exists(ConfigDir); | ||
|
|
||
| public SharedConfig? LoadSharedConfig() => Load<SharedConfig>(Path.Combine(ConfigDir, SharedConfigFile)); | ||
|
|
||
| public LocalConfig? LoadLocalConfig() => Load<LocalConfig>(Path.Combine(ConfigDir, LocalConfigFile)); | ||
|
|
||
| private static T? Load<T>(string path) | ||
| { | ||
| if (!File.Exists(path)) return default; | ||
| try | ||
| { | ||
| return JsonSerializer.Deserialize<T>(File.ReadAllText(path), JsonOptions); | ||
| } | ||
| catch (Exception e) when (e is IOException or JsonException) | ||
| { | ||
| return default; | ||
| } | ||
| } | ||
|
|
||
| public void SaveSharedConfig(SharedConfig config) | ||
| { | ||
| Directory.CreateDirectory(ConfigDir); | ||
| File.WriteAllText(Path.Combine(ConfigDir, SharedConfigFile), JsonSerializer.Serialize(config, JsonOptions)); | ||
| } | ||
|
|
||
| public void SaveLocalConfig(LocalConfig config) | ||
| { | ||
| Directory.CreateDirectory(ConfigDir); | ||
| File.WriteAllText(Path.Combine(ConfigDir, LocalConfigFile), JsonSerializer.Serialize(config, JsonOptions)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| using AL2DBML.CLI.Models; | ||
|
|
||
| namespace AL2DBML.CLI.Services; | ||
|
|
||
| public interface IConfigService | ||
| { | ||
| bool ConfigExists(); | ||
| SharedConfig? LoadSharedConfig(); | ||
| LocalConfig? LoadLocalConfig(); | ||
| void SaveSharedConfig(SharedConfig config); | ||
| void SaveLocalConfig(LocalConfig config); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
WritePreCommitHook()writes to.git/hooks/pre-commitbut doesn’t verify that the repository is a git repo or that.git/hooksexists. Runninginitoutside a git checkout will throwDirectoryNotFoundException. Consider detecting.git/hooks(or.git) first and either skipping hook creation with a warning or creating the directory structure where appropriate.