Skip to content

Commit

Permalink
Merge pull request #104 from NoxOrg/feature/static-items-in-solution-…
Browse files Browse the repository at this point in the history
…yanl

- added feature to populate yaml from defaults when prompt is not inc…
  • Loading branch information
jan-schutte committed Sep 28, 2023
2 parents 0011607 + a406b07 commit 30c0d57
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public interface ICliConfiguration
string? CommandAlias { get; set; }
string? Description { get; set; }
List<string[]>? Examples { get; set; }

}
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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<bool>(newKey).ToYesNo();
var response = AnsiConsole.Prompt(
new TextPrompt<char>(prompt)
.DefaultValueStyle(Style.Parse("mediumpurple3_1"))
.ChoicesStyle(Style.Parse("mediumpurple3_1"))
.PromptStyle(Style.Parse("seagreen1"))
.DefaultValue('y')
.DefaultValue(defaultValue)
.AddChoice('y')
.AddChoice('n')
) == 'y';
Expand Down Expand Up @@ -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]}");
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Nox.Cli.Plugin.Console;

public static class DefaultValueExtensions
{
public static char ToYesNo(this bool value)
{
return value ? 'y' : 'n';
}
}
4 changes: 2 additions & 2 deletions src/Nox.Cli/Extensions/ConfiguratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Nox.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 30c0d57

Please sign in to comment.