public IEnumerable <Metadata> Transform(Metadata metadata)
        {
            if (metadata.Kref != null && metadata.Kref.Source == "spacedock")
            {
                var json = metadata.Json();

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

                // Look up our mod on SD by its Id.
                var sdMod    = _api.GetMod(Convert.ToInt32(metadata.Kref.Id));
                var versions = sdMod.All();
                if (_releases.HasValue)
                {
                    versions = versions.Take(_releases.Value);
                }
                if (versions.Any())
                {
                    foreach (SDVersion vers in versions)
                    {
                        yield return(TransformOne(metadata, metadata.Json(), sdMod, vers));
                    }
                }
                else
                {
                    Log.WarnFormat("No releases found for {0}", sdMod.ToString());
                    yield return(metadata);
                }
            }
            else
            {
                yield return(metadata);
            }
        }
Exemple #2
0
        public Metadata Transform(Metadata metadata)
        {
            if (metadata.Kref != null && metadata.Kref.Source == "spacedock")
            {
                var json = metadata.Json();

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

                // Look up our mod on SD by its Id.
                var sdMod         = _api.GetMod(Convert.ToInt32(metadata.Kref.Id));
                var latestVersion = sdMod.Latest();

                Log.InfoFormat("Found SpaceDock Mod: {0} {1}", sdMod.name, latestVersion.friendly_version);

                // Only pre-fill version info if there's none already. GH #199
                if (json["ksp_version_min"] == null && json["ksp_version_max"] == null && json["ksp_version"] == null)
                {
                    Log.DebugFormat("Writing ksp_version from SpaceDock: {0}", latestVersion.KSP_version);
                    json["ksp_version"] = latestVersion.KSP_version.ToString();
                }

                json.SafeAdd("name", sdMod.name);
                json.SafeAdd("abstract", sdMod.short_description);
                json.SafeAdd("version", latestVersion.friendly_version.ToString());
                json.SafeAdd("download", latestVersion.download_path.OriginalString);

                var authors = GetAuthors(sdMod);

                if (authors.Count == 1)
                {
                    json.SafeAdd("author", sdMod.author);
                }
                else if (authors.Count > 1)
                {
                    json.SafeAdd("author", new JArray(authors));
                }

                // SD provides users with the following default selection of licenses. Let's convert them to CKAN
                // compatible license strings if possible.
                //
                // "MIT" - OK
                // "BSD" - Specific version is indeterminate
                // "GPLv2" - Becomes "GPL-2.0"
                // "GPLv3" - Becomes "GPL-3.0"
                // "LGPL" - Specific version is indeterminate

                var sdLicense = sdMod.license.Trim();

                switch (sdLicense)
                {
                case "GPLv2":
                    json.SafeAdd("license", "GPL-2.0");
                    break;

                case "GPLv3":
                    json.SafeAdd("license", "GPL-3.0");
                    break;

                default:
                    json.SafeAdd("license", sdLicense);
                    break;
                }

                // Make sure resources exist.
                if (json["resources"] == null)
                {
                    json["resources"] = new JObject();
                }

                var resourcesJson = (JObject)json["resources"];

                resourcesJson.SafeAdd("homepage", Normalize(sdMod.website));
                resourcesJson.SafeAdd("repository", Normalize(sdMod.source_code));
                resourcesJson.SafeAdd("spacedock", sdMod.GetPageUrl().OriginalString);

                if (sdMod.background != null)
                {
                    resourcesJson.SafeAdd("x_screenshot", Normalize(sdMod.background));
                }

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

                return(new Metadata(json));
            }

            return(metadata);
        }