Beispiel #1
0
    protected static PackageMeta?ParsePackageMeta(TomlTable tomlData)
    {
        if (!tomlData.HasKey("package"))
        {
            return(null);
        }

        var packageMeta = tomlData["package"];

        // TODO: Add warnings on missing values
        var result = new PackageMeta()
        {
            Namespace           = TomlUtils.SafegetString(packageMeta, "namespace"),
            Name                = TomlUtils.SafegetString(packageMeta, "name"),
            VersionNumber       = TomlUtils.SafegetString(packageMeta, "versionNumber"),
            Description         = TomlUtils.SafegetString(packageMeta, "description"),
            WebsiteUrl          = TomlUtils.SafegetString(packageMeta, "websiteUrl"),
            ContainsNsfwContent = TomlUtils.SafegetBool(packageMeta, "containsNsfwContent"),
            Dependencies        = new()
        };

        if (packageMeta.HasKey("dependencies"))
        {
            var packageDependencies = packageMeta["dependencies"];
            foreach (var packageName in packageDependencies.Keys)
            {
                // TODO: Validate both are strings if needed?
                result.Dependencies[packageName] = packageDependencies[packageName];
            }
        }

        return(result);
    }
Beispiel #2
0
    public void SafegetBool_WhenValueIsBool_ReturnsValue(string input, bool expected)
    {
        var table = CreateTomlTable(input);

        var actual = TomlUtils.SafegetBool(table, _key);

        Assert.Equal(expected, actual);
    }
Beispiel #3
0
    public void SafegetBool_WhenKeyIsNotFound_ReturnsNull()
    {
        var table = CreateTomlTable("");

        var actual = TomlUtils.SafegetBool(table, _key);

        Assert.Null(actual);
    }
Beispiel #4
0
    public void SafegetString_WhenValueIsNotString_ReturnsNull(string input)
    {
        var table = CreateTomlTable(input);

        var actual = TomlUtils.SafegetString(table, _key);

        Assert.Null(actual);
    }
Beispiel #5
0
    public void SafegetStringArray_WhenValueIsStringArray_ReturnsValue(string input, string[] expected)
    {
        var table = CreateTomlTable(input);

        var actual = TomlUtils.SafegetStringArray(table, _key);

        Assert.Equal(expected, actual);
    }
Beispiel #6
0
    protected static PublishConfig?ParsePublishConfig(TomlTable tomlData)
    {
        if (!tomlData.HasKey("publish"))
        {
            return(null);
        }

        var publishConfig = tomlData["publish"];

        return(new PublishConfig
        {
            Repository = TomlUtils.SafegetString(publishConfig, "repository"),
            Communities = TomlUtils.SafegetStringArray(publishConfig, "communities", Array.Empty <string>()),
            Categories = TomlUtils.SafegetStringArray(publishConfig, "categories", Array.Empty <string>())
        });
    }
Beispiel #7
0
    protected static BuildConfig?ParseBuildConfig(TomlTable tomlData)
    {
        if (!tomlData.HasKey("build"))
        {
            return(null);
        }

        var buildConfig = tomlData["build"];

        var result = new BuildConfig
        {
            IconPath   = TomlUtils.SafegetString(buildConfig, "icon"),
            ReadmePath = TomlUtils.SafegetString(buildConfig, "readme"),
            OutDir     = TomlUtils.SafegetString(buildConfig, "outdir"),
            CopyPaths  = new()
        };

        if (buildConfig.HasKey("copy"))
        {
            var pathSets = buildConfig["copy"];
            foreach (var entry in pathSets)
            {
                if (!(entry is TomlNode))
                {
                    ThunderstoreCLI.Write.Warn($"Unable to properly parse build config: {entry}", "Skipping entry");
                    continue;
                }

                var node = (TomlNode)entry;
                if (!node.HasKey("source") || !node.HasKey("target"))
                {
                    ThunderstoreCLI.Write.Warn(
                        $"Build config instruction is missing parameters: {node}",
                        "Make sure both 'source' and 'target' are defined",
                        "Skipping entry"
                        );
                    continue;
                }

                result.CopyPaths.Add(new CopyPathMap(node["source"], node["target"]));
            }
        }
        return(result);
    }
Beispiel #8
0
    public static void Write(Config config, string path)
    {
        var dependencies = config.PackageMeta.Dependencies ?? new Dictionary <string, string>();
        var copyPaths    = config.BuildConfig.CopyPaths ?? new List <CopyPathMap>();
        var toml         = new TomlTable
        {
            ["config"] =
            {
                ["schemaVersion"] = "0.0.1"
            },

            ["package"] = new TomlTable
            {
                ["namespace"]           = config.PackageMeta.Namespace,
                ["name"]                = config.PackageMeta.Name,
                ["versionNumber"]       = config.PackageMeta.VersionNumber,
                ["description"]         = config.PackageMeta.Description,
                ["websiteUrl"]          = config.PackageMeta.WebsiteUrl,
                ["containsNsfwContent"] = config.PackageMeta.ContainsNsfwContent,
                ["dependencies"]        = TomlUtils.DictToTomlTable(dependencies)
            },

            ["build"] = new TomlTable
            {
                ["icon"]   = config.BuildConfig.IconPath,
                ["readme"] = config.BuildConfig.ReadmePath,
                ["outdir"] = config.BuildConfig.OutDir,
                ["copy"]   = TomlUtils.BuildCopyPathTable(copyPaths)
            },

            ["publish"] = new TomlTable
            {
                ["repository"]  = config.PublishConfig.Repository,
                ["communities"] = TomlUtils.FromArray(config.PublishConfig.Communities ?? new string[0]),
                ["categories"]  = TomlUtils.FromArray(config.PublishConfig.Categories ?? new string[0])
            }
        };

        File.WriteAllText(path, TomlUtils.FormatToml(toml));
    }