Пример #1
0
 /// <summary>
 /// Return a parsed JObject from a stream.
 /// Courtesy https://stackoverflow.com/questions/8157636/can-json-net-serialize-deserialize-to-from-a-stream/17788118#17788118
 /// </summary>
 private static JObject DeserializeFromStream(Stream stream)
 {
     using (var sr = new StreamReader(stream))
     {
         return(YamlExtensions.Parse(sr).ToJObject());
     }
 }
Пример #2
0
        private static Metadata ReadNetkan()
        {
            if (!Options.File.EndsWith(".netkan"))
            {
                Log.WarnFormat("Input is not a .netkan file");
            }

            return(new Metadata(YamlExtensions.Parse(File.OpenText(Options.File))));
        }
Пример #3
0
        private IEnumerable <SendMessageBatchRequestEntry> Inflate(Message msg)
        {
            log.DebugFormat("Metadata returned: {0}", msg.Body);
            var netkan = new Metadata(YamlExtensions.Parse(msg.Body));

            int releases = 1;
            MessageAttributeValue releasesAttr;

            if (msg.MessageAttributes.TryGetValue("Releases", out releasesAttr))
            {
                releases = int.Parse(releasesAttr.StringValue);
            }

            ModuleVersion         highVer = null;
            MessageAttributeValue highVerAttr;

            if (msg.MessageAttributes.TryGetValue("HighestVersion", out highVerAttr))
            {
                highVer = new ModuleVersion(highVerAttr.StringValue);
            }

            log.InfoFormat("Inflating {0}", netkan.Identifier);
            IEnumerable <Metadata> ckans = null;
            bool   caught        = false;
            string caughtMessage = null;
            var    opts          = new TransformOptions(releases, null, highVer);

            try
            {
                ckans = inflator.Inflate($"{netkan.Identifier}.netkan", netkan, opts);
            }
            catch (Exception e)
            {
                e = e.GetBaseException() ?? e;
                log.InfoFormat("Inflation failed, sending error: {0}", e.Message);
                // If you do this the sensible way, the C# compiler throws:
                // error CS1631: Cannot yield a value in the body of a catch clause
                caught        = true;
                caughtMessage = e.Message;
            }
            if (caught)
            {
                yield return(inflationMessage(null, netkan, opts, false, caughtMessage));
            }
            if (ckans != null)
            {
                foreach (Metadata ckan in ckans)
                {
                    log.InfoFormat("Sending {0}-{1}", ckan.Identifier, ckan.Version);
                    yield return(inflationMessage(ckan, netkan, opts, true));
                }
            }
        }
Пример #4
0
        public void Parse_ValidInput_Works()
        {
            // Arrange
            string input = string.Join("\r\n", new string[]
            {
                "spec_version: v1.4",
                "identifier: Astrogator",
                "$kref: \"#/ckan/github/HebaruSan/Astrogator\"",
                "$vref: \"#/ckan/ksp-avc\"",
                "license: GPL-3.0",
                "tags:",
                "    - plugin",
                "    - information",
                "    - control",
                "resources:",
                "    homepage: https://forum.kerbalspaceprogram.com/index.php?/topic/155998-*",
                "    bugtracker: https://github.com/HebaruSan/Astrogator/issues",
                "    repository: https://github.com/HebaruSan/Astrogator",
                "recommends:",
                "    - name: ModuleManager",
                "    - name: LoadingTipsPlus",
            });

            // Act
            YamlMappingNode yaml = YamlExtensions.Parse(input);

            // Assert
            Assert.AreEqual("v1.4", (string)yaml["spec_version"]);
            Assert.AreEqual("Astrogator", (string)yaml["identifier"]);
            Assert.AreEqual("#/ckan/github/HebaruSan/Astrogator", (string)yaml["$kref"]);
            Assert.AreEqual("#/ckan/ksp-avc", (string)yaml["$vref"]);
            Assert.AreEqual("GPL-3.0", (string)yaml["license"]);

            CollectionAssert.AreEqual(
                new string[] { "plugin", "information", "control" },
                (yaml["tags"] as YamlSequenceNode).Children.Select(yn => (string)yn)
                );
            Assert.AreEqual(
                "https://forum.kerbalspaceprogram.com/index.php?/topic/155998-*",
                (string)yaml["resources"]["homepage"]
                );
            Assert.AreEqual(
                "https://github.com/HebaruSan/Astrogator/issues",
                (string)yaml["resources"]["bugtracker"]
                );
            Assert.AreEqual(
                "https://github.com/HebaruSan/Astrogator",
                (string)yaml["resources"]["repository"]
                );
            Assert.AreEqual("ModuleManager", (string)yaml["recommends"][0]["name"]);
            Assert.AreEqual("LoadingTipsPlus", (string)yaml["recommends"][1]["name"]);
        }
Пример #5
0
        public IEnumerable <Metadata> Transform(Metadata metadata, TransformOptions opts)
        {
            if (metadata.Kref != null && metadata.Kref.Source == KrefSource)
            {
                var json = metadata.Json();

                Log.InfoFormat("Executing MetaNetkan transformation with {0}", metadata.Kref);
                Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

                // Make sure resources exist, save metanetkan
                if (json["resources"] == null)
                {
                    json["resources"] = new JObject();
                }
                var resourcesJson = (JObject)json["resources"];
                resourcesJson.SafeAdd("metanetkan", metadata.Kref.Id);

                var uri            = new Uri(metadata.Kref.Id);
                var targetFileText = _github?.DownloadText(uri)
                                     ?? _http.DownloadText(CKAN.Net.GetRawUri(uri));

                Log.DebugFormat("Target netkan:{0}{1}", Environment.NewLine, targetFileText);

                var targetJson     = YamlExtensions.Parse(targetFileText).ToJObject();
                var targetMetadata = new Metadata(targetJson);

                if (targetMetadata.Kref == null || targetMetadata.Kref.Source != "netkan")
                {
                    json["spec_version"] = ModuleVersion.Max(metadata.SpecVersion, targetMetadata.SpecVersion)
                                           .ToSpecVersionJson();

                    if (targetJson["$kref"] != null)
                    {
                        json["$kref"] = targetJson["$kref"];
                    }
                    else
                    {
                        json.Remove("$kref");
                    }

                    json.SafeMerge("resources", targetJson["resources"]);

                    foreach (var property in targetJson.Properties())
                    {
                        json.SafeAdd(property.Name, property.Value);
                    }

                    Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json);

                    yield return(new Metadata(json));
                }
                else
                {
                    throw new Kraken("The target of a metanetkan may not also be a metanetkan.");
                }
            }
            else
            {
                yield return(metadata);
            }
        }