Add usedTags export

This commit is contained in:
Pieter Vander Vennet 2022-04-14 18:17:45 +02:00
parent 1408916314
commit df63111009
3 changed files with 53 additions and 6 deletions

View file

@ -338,6 +338,12 @@ namespace AspectedRouting.Language
}
/**
* Returns all possible tags which are used in the given expression.
*
* If a tag might match a wildcard, an explicit '*' will be added to the collection.
* This is different from the _other_ possibleTags, which will return an empty set.
*/
public static Dictionary<string, HashSet<string>> PossibleTags(this IEnumerable<IExpression> exprs)
{
var usedTags = new Dictionary<string, HashSet<string>>();
@ -353,14 +359,20 @@ namespace AspectedRouting.Language
{
if (!usedTags.TryGetValue(key, out var collection))
{
// This is the first time we see this collection
collection = new HashSet<string>();
usedTags[key] = collection;
}
foreach (var v in values)
{
collection.Add(v);
}
if (values.Count == 0) {
collection.Add("*");
}
}
}

View file

@ -306,6 +306,9 @@ namespace AspectedRouting
File.WriteAllText($"{outputDir}/ProfileMetadata.json",
Utils.GenerateExplanationJson(profiles.Select(p => p.profile), context)
);
File.WriteAllText($"{outputDir}/UsedTags.json",
Utils.GenerateTagsOverview(profiles.Select(p => p.profile), context)
);
if (!args.Contains("--no-repl"))
Repl(context, profiles

View file

@ -50,6 +50,34 @@ namespace AspectedRouting
return data.SubArray(index, data.Length - index);
}
public static string Quoted(this string s)
{
return "\"" + s + "\"";
}
public static string GenerateTagsOverview(IEnumerable<ProfileMetaData> profiles, Context context)
{
var allExpressions = new List<IExpression>();
foreach (var profile in profiles) {
foreach (var behaviour in profile.Behaviours) {
allExpressions.AddRange(profile.AllExpressions(context));
}
}
var explanations = new List<string>();
foreach (var tag in allExpressions.PossibleTags()) {
var values = new List<string>(tag.Value);
values.Sort();
explanations.Add(tag.Key.Quoted() + ": [" +
string.Join(", ", values.Select(v => v.Quoted()))
+ "]");
}
explanations.Sort();
return "{\n "+ String.Join(",\n ", explanations)+"\n}";
}
/// <summary>
/// Generates a JSON file where all the profiles are listed, together with descriptions and other metadata.
/// Useful for other apps, e.g. the routing api to have
@ -60,6 +88,7 @@ namespace AspectedRouting
/// <returns></returns>
public static string GenerateExplanationJson(IEnumerable<ProfileMetaData> profiles, Context context)
{
var metaItems = new List<string>();
foreach (var profile in profiles) {
@ -71,19 +100,22 @@ namespace AspectedRouting
foreach (var behaviour in profile.Behaviours) {
var behaviourDescription = behaviour.Value["description"].Evaluate(new Context()) as string;
behaviourDescription ??= "";
var keys = string.Join(", ",
profile.AllExpressions(context).PossibleTags().Select(tag => $"\"{tag.Key}\"")
);
var keys = new List<string>();
foreach (var tag in profile.AllExpressions(context).PossibleTags()) {
keys.Add(tag.Key.Quoted());
}
var meta = new Dictionary<string, string> {
{"name", behaviour.Key},
{"type", profileName},
{"author", author},
{"description", behaviourDescription + " (" + profileDescription + ")"}
};
var json = string.Join(",", meta.Select(d =>
$"\"{d.Key}\": \"{d.Value}\""));
metaItems.Add("{" + json + ", \"usedKeys\": [" + keys + "] }\n");
metaItems.Add($"{{{json}, " +
$"\"usedKeys\": [{string.Join(", ",keys)}] }}\n");
}
}