diff --git a/src/Nox.Cli.Abstractions/Configuration/ICliConfiguration.cs b/src/Nox.Cli.Abstractions/Configuration/ICliConfiguration.cs index 62995403..0ae58207 100755 --- a/src/Nox.Cli.Abstractions/Configuration/ICliConfiguration.cs +++ b/src/Nox.Cli.Abstractions/Configuration/ICliConfiguration.cs @@ -7,4 +7,5 @@ public interface ICliConfiguration string? CommandAlias { get; set; } string? Description { get; set; } List? Examples { get; set; } + } \ No newline at end of file diff --git a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Console/ConsolePromptSchema_v1.cs b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Console/ConsolePromptSchema_v1.cs index 07f79d2e..544d482e 100755 --- a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Console/ConsolePromptSchema_v1.cs +++ b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Console/ConsolePromptSchema_v1.cs @@ -84,6 +84,8 @@ public NoxActionMetaData Discover() }; } + private Regex _defaultArrayRegex = new(@"\[(.*?)\]\.(.*)", RegexOptions.Compiled | RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1)); + private string? _schemaUrl = null!; private string? _schema = null!; @@ -307,13 +309,20 @@ private async Task ProcessSchema(JsonSchema.JsonSchema schema, string rootKey = if (!string.IsNullOrWhiteSpace(key) && _includedPrompts != null && !_includedPrompts.Any(f => newKey.StartsWith(f, StringComparison.OrdinalIgnoreCase))) { - if (_defaults != null && _defaults.Any(d => key.Equals(d.Key, StringComparison.OrdinalIgnoreCase))) + if (_defaults != null) { - _yaml.AppendLine($"{key}: {_defaults[key]}"); - _responses[newKey] = _defaults[newKey]; + //The exact key exists in defaults + if (_defaults.Any(d => newKey.Equals(d.Key, StringComparison.OrdinalIgnoreCase))) + { + _yaml.AppendLine($"{yamlSpacing}{yamlSpacingPostfix}{key}: {_defaults[newKey]}"); + _responses[newKey] = _defaults[newKey]; + } else if (_defaults.Any(d => d.Key.StartsWith(newKey, StringComparison.CurrentCultureIgnoreCase))) + { + //The key is not in included prompts, but there are defaults for the sub-keys + ProcessDefaults(newKey, yamlSpacing, yamlSpacingPostfix); + } + return; } - - return; } if (!string.IsNullOrWhiteSpace(key) && _excludedPrompts != null && _excludedPrompts.Any(f => newKey.StartsWith(f, StringComparison.OrdinalIgnoreCase))) @@ -438,12 +447,13 @@ private async Task ProcessSchema(JsonSchema.JsonSchema schema, string rootKey = private void PromptBoolean(string prompt, string rootKey, string key, string yamlPrefix) { var newKey = $"{rootKey}.{key}".TrimStart('.'); + var defaultValue = GetDefault(newKey).ToYesNo(); var response = AnsiConsole.Prompt( new TextPrompt(prompt) .DefaultValueStyle(Style.Parse("mediumpurple3_1")) .ChoicesStyle(Style.Parse("mediumpurple3_1")) .PromptStyle(Style.Parse("seagreen1")) - .DefaultValue('y') + .DefaultValue(defaultValue) .AddChoice('y') .AddChoice('n') ) == 'y'; @@ -549,5 +559,35 @@ private void AppendKey(string yamlSpacing, string key) return default; } + + private void ProcessDefaults(string key, string yamlSpacing, string yamlSpacingPostfix) + { + _yaml.AppendLine($"{yamlSpacing}{key}:"); + var arrayIndex = -1; + foreach (var defaultItem in _defaults!.Where(d => d.Key.StartsWith(key, StringComparison.CurrentCultureIgnoreCase))) + { + //check if this item is an array + var itemKey = defaultItem.Key; + var defaultSpacing = new string(' ', itemKey.Count(d => d == '.') * 2); + var match = _defaultArrayRegex.Match(itemKey); + if (match.Success) //array + { + if (int.TryParse(match.Groups[1].ToString(), out var itemIndex)) + { + var defaultPrefix = " "; + if (itemIndex != arrayIndex) + { + defaultPrefix = "- "; + arrayIndex = itemIndex; + } + _yaml.AppendLine($"{defaultSpacing}{defaultPrefix}{match.Groups[2]}: {defaultItem.Value}"); + } + } + else + { + _yaml.AppendLine($"{yamlSpacing}{yamlSpacingPostfix}{key}: {_defaults![key]}"); + } + } + } } diff --git a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Console/DefaultValueExtensions.cs b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Console/DefaultValueExtensions.cs new file mode 100644 index 00000000..737da990 --- /dev/null +++ b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Console/DefaultValueExtensions.cs @@ -0,0 +1,9 @@ +namespace Nox.Cli.Plugin.Console; + +public static class DefaultValueExtensions +{ + public static char ToYesNo(this bool value) + { + return value ? 'y' : 'n'; + } +} \ No newline at end of file diff --git a/src/Nox.Cli/Extensions/ConfiguratorExtensions.cs b/src/Nox.Cli/Extensions/ConfiguratorExtensions.cs index d4d15848..10249ce9 100755 --- a/src/Nox.Cli/Extensions/ConfiguratorExtensions.cs +++ b/src/Nox.Cli/Extensions/ConfiguratorExtensions.cs @@ -73,9 +73,9 @@ public static IConfigurator AddNoxCommands(this IConfigurator cliConfig, IServic { cliConfig.AddBranch(branch.Key, b => { - if (branchDescriptions.ContainsKey(branch.Key)) + if (branchDescriptions.TryGetValue(branch.Key, out var description)) { - b.SetDescription(branchDescriptions[branch.Key]); + b.SetDescription(description); } foreach(var workflow in branch) diff --git a/src/Nox.Cli/Properties/launchSettings.json b/src/Nox.Cli/Properties/launchSettings.json index e980dcd2..fd26193d 100755 --- a/src/Nox.Cli/Properties/launchSettings.json +++ b/src/Nox.Cli/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "Nox.Cli": { "commandName": "Project", - "commandLineArgs": "sync entity-store", + "commandLineArgs": "init solution", "workingDirectory": "/home/jan/demo/NoxCliDemoProject", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development"