Skip to content

Commit

Permalink
Add support for community-specific categories
Browse files Browse the repository at this point in the history
  • Loading branch information
Windows10CE committed Apr 26, 2023
1 parent 863912d commit 9de949b
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ThunderstoreCLI/Commands/BuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public static string SerializeManifest(Config config)

public static List<string> ValidateConfig(Config config, bool throwIfErrors = true)
{
var v = new Validator("build");
var v = new CommandValidator("build");
v.AddIfEmpty(config.PackageConfig.Namespace, "Package Namespace");
v.AddIfEmpty(config.PackageConfig.Name, "Package Name");
v.AddIfNotSemver(config.PackageConfig.VersionNumber, "Package VersionNumber");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using ThunderstoreCLI.Utils;

namespace ThunderstoreCLI.Configuration;
namespace ThunderstoreCLI.Commands;

/// <summary>Helper for validating command-specific configurations</summary>
public class Validator
public class CommandValidator
{
private List<string> _errors;
private string _name;

public Validator(string commandName, List<string>? errors = null)
public CommandValidator(string commandName, List<string>? errors = null)
{
_name = commandName;
_errors = errors ?? new List<string>();
Expand Down
2 changes: 1 addition & 1 deletion ThunderstoreCLI/Commands/InitCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static string BuildReadme(Config config)

private static void ValidateConfig(Config config)
{
var v = new Validator("init");
var v = new CommandValidator("init");
v.AddIfEmpty(config.PackageConfig.Namespace, "Package Namespace");
v.AddIfEmpty(config.PackageConfig.Name, "Package Name");
v.AddIfNotSemver(config.PackageConfig.VersionNumber, "Package VersionNumber");
Expand Down
2 changes: 1 addition & 1 deletion ThunderstoreCLI/Commands/PublishCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private static async Task<CompletedUpload.CompletedPartData> UploadChunk(UploadI
private static void ValidateConfig(Config config, bool justReturnErrors = false)
{
var buildConfigErrors = BuildCommand.ValidateConfig(config, false);
var v = new Validator("publish", buildConfigErrors);
var v = new CommandValidator("publish", buildConfigErrors);
v.AddIfEmpty(config.AuthConfig.AuthToken, "Auth AuthToken");
v.ThrowIfErrors();
}
Expand Down
9 changes: 6 additions & 3 deletions ThunderstoreCLI/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static Config FromCLI(IConfigProvider cliConfig)
providers.Add(new EnvironmentConfig());
if (cliConfig is CLIConfig)
providers.Add(new ProjectFileConfig());
providers.Add(new BaseConfig());
providers.Add(new DefaultConfig());
return Parse(providers.ToArray());
}

Expand Down Expand Up @@ -102,7 +102,10 @@ public PackageUploadMetadata GetUploadMetadata(string fileUuid)
return new PackageUploadMetadata()
{
AuthorName = PackageConfig.Namespace,
Categories = PublishConfig.Categories,
Categories = PublishConfig.Categories!.GetOrDefault("") ?? Array.Empty<string>(),
CommunityCategories = PublishConfig.Categories!
.Where(kvp => kvp.Key != "")
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
Communities = PublishConfig.Communities,
HasNsfwContent = PackageConfig.ContainsNsfwContent ?? false,
UploadUUID = fileUuid
Expand Down Expand Up @@ -212,7 +215,7 @@ public class PublishConfig
{
public string? File { get; set; }
public string[]? Communities { get; set; }
public string[]? Categories { get; set; }
public Dictionary<string, string[]>? Categories { get; set; }
}

public class AuthConfig
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace ThunderstoreCLI.Configuration;

class BaseConfig : EmptyConfig
class DefaultConfig : EmptyConfig
{
public override GeneralConfig? GetGeneralConfig()
{
Expand Down
2 changes: 1 addition & 1 deletion ThunderstoreCLI/Configuration/ProjectFileConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override void Parse(Config currentConfig)
{
return new PublishConfig()
{
Categories = Project.Publish?.Categories,
Categories = Project.Publish?.Categories.Categories,
Communities = Project.Publish?.Communities
};
}
Expand Down
2 changes: 2 additions & 0 deletions ThunderstoreCLI/Models/PublishModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class PackageUploadMetadata : BaseJson<PackageUploadMetadata>

[JsonProperty("communities")] public string[]? Communities { get; set; }

[JsonProperty("community_categories")] public Dictionary<string, string[]>? CommunityCategories { get; set; }

[JsonProperty("has_nsfw_content")] public bool HasNsfwContent { get; set; }

[JsonProperty("upload_uuid")] public string? UploadUUID { get; set; }
Expand Down
34 changes: 31 additions & 3 deletions ThunderstoreCLI/Models/ThunderstoreProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ namespace ThunderstoreCLI.Models;
[TomlDoNotInlineObject]
public class ThunderstoreProject : BaseToml<ThunderstoreProject>
{
public struct CategoryDictionary
{
public Dictionary<string, string[]> Categories;
}

static ThunderstoreProject()
{
TomletMain.RegisterMapper(
dict => TomletMain.ValueFrom(dict.Categories),
toml => toml switch
{
TomlArray arr => new CategoryDictionary
{
Categories = new Dictionary<string, string[]>
{
{ "", arr.ArrayValues.Select(v => v.StringValue).ToArray() }
}
},
TomlTable table => new CategoryDictionary { Categories = TomletMain.To<Dictionary<string, string[]>>(table) },
_ => throw new NotSupportedException()
});
}

[TomlDoNotInlineObject]
public class ConfigData
{
Expand Down Expand Up @@ -75,10 +98,15 @@ public class PublishData
{
"riskofrain2"
};

[TomlProperty("categories")]
public string[] Categories { get; set; } =
[TomlDoNotInlineObject]
public CategoryDictionary Categories { get; set; } = new()
{
"items", "skills"
Categories = new Dictionary<string, string[]>
{
{ "riskofrain2", new[] { "items", "skills" } }
}
};
}
[TomlProperty("publish")]
Expand Down Expand Up @@ -112,7 +140,7 @@ public ThunderstoreProject(Config config)
};
Publish = new PublishData()
{
Categories = config.PublishConfig.Categories!,
Categories = new CategoryDictionary { Categories = config.PublishConfig.Categories! },
Communities = config.PublishConfig.Communities!,
Repository = config.GeneralConfig.Repository
};
Expand Down
9 changes: 9 additions & 0 deletions ThunderstoreCLI/Utils/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ThunderstoreCLI.Utils;

public static class DictionaryExtensions
{
public static TValue? GetOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key) where TKey : notnull
{
return dict.TryGetValue(key, out var value) ? value : default;
}
}

0 comments on commit 9de949b

Please sign in to comment.