public Metadata Transform(Metadata metadata)
        {
            if (metadata.Download != null)
            {
                var json = metadata.Json();

                var file = _http.DownloadPackage(metadata.Download, metadata.Identifier);

                var internalJson = _moduleService.GetInternalCkan(file);

                if (internalJson != null)
                {
                    Log.InfoFormat("Executing Internal CKAN transformation with {0}", metadata.Kref);
                    Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

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

                    json["spec_version"] = Version.Max(metadata.SpecVersion, new Metadata(internalJson).SpecVersion)
                        .ToSpecVersionJson();

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

                return new Metadata(json);
            }

            return metadata;
        }
Example #2
0
        public Metadata Transform(Metadata metadata)
        {
            var json = metadata.Json();

            JToken forceV;
            if (json.TryGetValue("x_netkan_force_v", out forceV) && (bool)forceV)
            {
                Log.InfoFormat("Executing Forced-V transformation with {0}", metadata.Kref);
                Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

                // Force a 'v' in front of the version string if it's not there
                // already.

                var version = (string)json.GetValue("version");

                if (!version.StartsWith("v"))
                {
                    Log.InfoFormat("Force-adding 'v' to start of {0}", version);
                    version = "v" + version;
                    json["version"] = version;
                }

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

            return new Metadata(json);
        }
        public Metadata Transform(Metadata metadata)
        {
            var json = metadata.Json();

            var versionEditInfo = GetVersionEditInfo(json);
            if (versionEditInfo != null)
            {
                Log.InfoFormat("Executing version edit transformation");
                Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

                var findRegex = new Regex(versionEditInfo.Find);
                if (findRegex.IsMatch(versionEditInfo.Version))
                {
                    json["version"] = new Regex(versionEditInfo.Find)
                        .Replace(versionEditInfo.Version, versionEditInfo.Replace);
                }
                else if(versionEditInfo.Strict)
                {
                    throw new Kraken("Could not match version with find pattern");
                }

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

            return new Metadata(json);
        }
        public Metadata Transform(Metadata metadata)
        {
            if (metadata.Download != null)
            {
                var json = metadata.Json();

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

                var file = _http.DownloadPackage(metadata.Download, metadata.Identifier);

                if (file != null)
                {
                    json["download_size"] = _fileService.GetSizeBytes(file);

                    json["download_hash"] = new JObject();

                    var download_hashJson = (JObject)json["download_hash"];
                    download_hashJson.SafeAdd("sha1", _fileService.GetFileHashSha1(file));
                    download_hashJson.SafeAdd("sha256", _fileService.GetFileHashSha256(file));

                    json["download_content_type"] = _fileService.GetMimetype(file);
                }

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

                return new Metadata(json);
            }

            return metadata;
        }
Example #5
0
        public Metadata Transform(Metadata metadata)
        {
            Log.Debug("Fixing version strings (if required)...");

            var json = metadata.Json();

            JToken epoch;
            if (json.TryGetValue("x_netkan_epoch", out epoch))
            {
                Log.InfoFormat("Executing Epoch transformation with {0}", metadata.Kref);
                Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

                uint epochNumber;
                if (uint.TryParse(epoch.ToString(), out epochNumber))
                {
                    //Implicit if zero. No need to add
                    if (epochNumber != 0)
                        json["version"] = epochNumber + ":" + json["version"];

                    Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json);
                }
                else
                {
                    Log.Error("Invaild epoch: " + epoch);
                    throw new BadMetadataKraken(null, "Invaild epoch: " + epoch + "In " + json["identifier"]);
                }
            }

            return new Metadata(json);
        }
        public Metadata Transform(Metadata metadata)
        {
            if (metadata.Kref != null && metadata.Kref.Source == "jenkins")
            {
                var json = metadata.Json();

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

                var versionBase = (string)json["x_ci_version_base"];
                var resources = (JObject)json["resources"];
                var baseUri = (string)resources["ci"] ?? (string)resources["x_ci"];

                var build = JenkinsAPI.GetLatestBuild(baseUri, versionBase);

                Log.DebugFormat("Found Jenkins Mod: {0} {1}", metadata.Kref.Id, build.version);

                json.SafeAdd("version", build.version.ToString());
                json.SafeAdd("download", Uri.EscapeUriString(build.download.ToString()));

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

                return new Metadata(json);
            }

            return metadata;
        }
Example #7
0
        public void Validate(Metadata metadata)
        {
            var mod = CkanModule.FromJson(metadata.Json().ToString());
            var file = _http.DownloadPackage(metadata.Download, metadata.Identifier);

            // Make sure this would actually generate an install.

            if (!_moduleService.HasInstallableFiles(mod, file))
            {
                throw new Kraken("Module contains no files to install.");
            }
        }
        public Metadata Transform(Metadata metadata)
        {
            var json = metadata.Json();

            JToken optimusPrime;
            if (json.TryGetValue("x_netkan_optimus_prime", out optimusPrime) && (bool)optimusPrime)
            {
                Log.Info("Autobots roll out!");
            }

            return metadata;
        }
        public Metadata Transform(Metadata metadata)
        {
            var json = metadata.Json();

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

            json["x_generated_by"] = "netkan"; // TODO: We should write the specific version here too

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

            return new Metadata(json);
        }
        public Metadata Transform(Metadata metadata)
        {
            var json = metadata.Json();

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

            Strip(json);

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

            return new Metadata(json);
        }
Example #11
0
        public Metadata Transform(Metadata metadata)
        {
            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);

                var uri = new Uri(metadata.Kref.Id);
                var targetFileText = _http.DownloadText(uri);

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

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

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

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

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

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

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

            return metadata;
        }
        public Metadata Transform(Metadata metadata)
        {
            var json = metadata.Json();
            var sortedJson = new JObject();

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

            var sortedPropertyNames = json
                .Properties()
                .Select(i => i.Name)
                .OrderBy(GetPropertySortOrder)
                .ThenBy(i => i);

            foreach (var propertyName in sortedPropertyNames)
            {
                sortedJson[propertyName] = json[propertyName];
            }

            var resources = json["resources"] as JObject;
            if (resources != null)
            {
                var sortedResourcePropertyNames = resources
                    .Properties()
                    .Select(i => i.Name)
                    .OrderBy(GetResourcePropertySortOrder)
                    .ThenBy(i => i);

                var sortedResources = new JObject();

                foreach (var resourceProprtyName in sortedResourcePropertyNames)
                {
                    sortedResources[resourceProprtyName] = resources[resourceProprtyName];
                }

                sortedJson["resources"] = sortedResources;
            }

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

            return new Metadata(sortedJson);
        }
Example #13
0
        public Metadata Transform(Metadata metadata)
        {
            if (metadata.Kref != null && metadata.Kref.Source == "http")
            {
                var json = metadata.Json();

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

                json["download"] = metadata.Kref.Id;

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

                return new Metadata(json);
            }
            else
            {
                return metadata;
            }
        }
Example #14
0
        public Metadata Transform(Metadata metadata)
        {
            if (metadata.Kref != null && metadata.Kref.Source == "github")
            {
                var json = metadata.Json();

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

                var ghRef = new GithubRef(metadata.Kref, _matchPreleases);

                // Find the release on github and download.
                var ghRelease = _api.GetLatestRelease(ghRef);

                if (ghRelease != null)
                {
                    json.SafeAdd("version", ghRelease.Version.ToString());
                    json.SafeAdd("author", ghRelease.Author);
                    json.SafeAdd("download", Uri.EscapeUriString(ghRelease.Download.ToString()));

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

                    var resourcesJson = (JObject)json["resources"];
                    resourcesJson.SafeAdd("repository", GithubPage(ghRef.Repository));

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

                    return new Metadata(json);
                }
                else
                {
                    Log.WarnFormat("No releases found for {0}", ghRef.Repository);
                }
            }

            return metadata;
        }
        public Metadata Transform(Metadata metadata)
        {
            var json = metadata.Json();

            JToken overrideList;
            if (json.TryGetValue("x_netkan_override", out overrideList))
            {
                Log.InfoFormat("Executing KerbalStuff transformation with {0}", metadata.Kref);
                Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

                // There's an override section, process them

                Log.DebugFormat("Override section:{0}{1}", Environment.NewLine, overrideList);

                if (overrideList.Type == JTokenType.Array)
                {
                    // Sweet! We have an override. Let's walk through and see if we can find
                    // an applicable section for us.
                    foreach (var overrideStanza in overrideList)
                    {
                        Log.InfoFormat("Processing override: {0}", overrideStanza);
                        ProcessOverrideStanza((JObject)overrideStanza, json);
                    }

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

                    return new Metadata(json);
                }
                else
                {
                    throw new Kraken(
                        string.Format(
                            "x_netkan_override expects a list of overrides, found: {0}",
                            overrideList));
                }
            }

            return metadata;
        }
Example #16
0
        public Metadata Transform(Metadata metadata)
        {
            if (metadata.Kref != null && metadata.Kref.Source == "http")
            {
                var json = metadata.Json();

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

                if (Uri.IsWellFormedUriString(metadata.Kref.Id, UriKind.Absolute))
                {
                    var resolvedUri = Net.ResolveRedirect(new Uri(metadata.Kref.Id));

                    Log.InfoFormat("URL {0} resolved to {1}", metadata.Kref.Id, resolvedUri);

                    if (resolvedUri != null)
                    {
                        json["download"] = resolvedUri;

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

                        return new Metadata(json);
                    }
                    else
                    {
                        throw new Kraken("Could not resolve HTTP $kref URL, exceeded number of redirects.");
                    }
                }
                else
                {
                    throw new Kraken("Invalid URL in HTTP $kref: " + metadata.Kref.Id);
                }
            }
            else
            {
                return metadata;
            }
        }
        public Metadata Transform(Metadata metadata)
        {
            if (metadata.Download != null)
            {
                var json = metadata.Json();

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

                var file = _http.DownloadPackage(metadata.Download, metadata.Identifier);

                if (file != null)
                {
                    json["download_size"] = _fileService.GetSizeBytes(file);
                }

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

                return new Metadata(json);
            }

            return metadata;
        }
Example #18
0
        public Metadata Transform(Metadata metadata)
        {
            if (metadata.Kref != null && metadata.Kref.Source == "jenkins")
            {
                var json = metadata.Json();

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

                var buildType = "stable";
                var useFilenameVersion = false;
                var assetMatchPattern = Constants.DefaultAssetMatchPattern;

                var jenkinsMetadata = (JObject)json["x_netkan_jenkins"];
                if (jenkinsMetadata != null)
                {
                    var jenkinsBuildMetadata = (string)jenkinsMetadata["build"];

                    if (jenkinsBuildMetadata != null)
                    {
                        buildType = jenkinsBuildMetadata;
                    }

                    var jenkinsUseFilenameVersionMetadata = (bool?)jenkinsMetadata["use_filename_version"];

                    if (jenkinsUseFilenameVersionMetadata != null)
                    {
                        useFilenameVersion = jenkinsUseFilenameVersionMetadata.Value;
                    }

                    var jenkinsAssetMatchMetadata = (string)jenkinsMetadata["asset_match"];

                    if (jenkinsAssetMatchMetadata != null)
                    {
                        assetMatchPattern = new Regex(jenkinsAssetMatchMetadata);
                    }
                }

                Log.InfoFormat("Attempting to retrieve the last {0} build", buildType);

                // Get the job metadata
                var job = JsonConvert.DeserializeObject<JObject>(
                    _http.DownloadText(new Uri(metadata.Kref.Id + "api/json"))
                );

                // Get the build reference metadata
                var buildRef = (JObject)job[BuildTypeToProperty[buildType]];

                // Get the build number and url
                var buildNumber = (int)buildRef["number"];
                var buildUrl = (string)buildRef["url"];

                Log.InfoFormat("The last {0} build is #{1}", buildType, buildNumber);

                // Get the build metadata
                var build = JsonConvert.DeserializeObject<JObject>(
                    _http.DownloadText(new Uri(buildUrl + "api/json"))
                );

                // Get the artifact metadata
                var artifacts = ((JArray)build["artifacts"])
                    .Select(i => (JObject)i)
                    .Where(i => assetMatchPattern.IsMatch((string)i["fileName"]))
                    .ToList();

                switch (artifacts.Count)
                {
                    case 1:
                        var artifact = artifacts.Single();

                        var artifactFileName = artifact["fileName"];
                        var artifactRelativePath = artifact["relativePath"];

                        // I'm not sure if 'relativePath' is the right property to use here
                        var download = Uri.EscapeUriString(buildUrl + "artifact/" + artifactRelativePath);
                        var version = artifactFileName;

                        Log.DebugFormat("Using download URL: {0}", download);
                        json.SafeAdd("download", download);

                        if (useFilenameVersion)
                        {
                            Log.DebugFormat("Using filename as version: {0}", version);
                            json.SafeAdd("version", version);
                        }

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

                        var resourcesJson = (JObject)json["resources"];
                        resourcesJson.SafeAdd("ci", Uri.EscapeUriString(metadata.Kref.Id));

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

                        return new Metadata(json);
                    case 0:
                        throw new Exception("Could not find any matching artifacts");
                    default:
                        throw new Exception("Found too many matching artifacts");
                }
            }

            return metadata;
        }
Example #19
0
        public Metadata Transform(Metadata metadata)
        {
            if (metadata.Kref != null && metadata.Kref.Source == "github")
            {
                var json = metadata.Json();

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

                var useSourceAchive = false;

                var githubMetadata = (JObject)json["x_netkan_github"];
                if (githubMetadata != null)
                {
                    var githubUseSourceArchive = (bool?)githubMetadata["use_source_archive"];

                    if (githubUseSourceArchive != null)
                    {
                        useSourceAchive = githubUseSourceArchive.Value;
                    }
                }

                var ghRef = new GithubRef(metadata.Kref, useSourceAchive, _matchPreleases);

                // Get the GitHub repository
                var ghRepo = _api.GetRepo(ghRef);
                // Get the GitHub release
                var ghRelease = _api.GetLatestRelease(ghRef);

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

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

                if (!string.IsNullOrWhiteSpace(ghRepo.Description))
                    json.SafeAdd("abstract", ghRepo.Description);

                if (!string.IsNullOrWhiteSpace(ghRepo.Homepage))
                    resourcesJson.SafeAdd("homepage", ghRepo.Homepage);

                resourcesJson.SafeAdd("repository", ghRepo.HtmlUrl);

                if (ghRelease != null)
                {
                    json.SafeAdd("version", ghRelease.Version.ToString());
                    json.SafeAdd("author", ghRelease.Author);
                    json.SafeAdd("download", Uri.EscapeUriString(ghRelease.Download.ToString()));

                    if (ghRef.Project.Contains("_"))
                    {
                        json.SafeAdd("name", ghRef.Project.Replace("_", " "));
                    }
                    else if (ghRef.Project.Contains("-"))
                    {
                        json.SafeAdd("name", ghRef.Project.Replace("-", " "));
                    }
                    else if (ghRef.Project.Contains("."))
                    {
                        json.SafeAdd("name", ghRef.Project.Replace(".", " "));
                    }
                    else
                    {
                        var repoName = ghRef.Project;
                        for (var i = 1; i < repoName.Length - 1; ++i)
                        {
                            if (char.IsLower(repoName[i - 1]) && char.IsUpper(repoName[i]) || repoName[i - 1] != ' ' && char.IsUpper(repoName[i]) && char.IsLower(repoName[i + 1]))
                            {
                                repoName = repoName.Insert(i, " ");
                            }
                        }

                        json.SafeAdd("name", repoName);
                    }

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

                    return new Metadata(json);
                }
                else
                {
                    Log.WarnFormat("No releases found for {0}", ghRef.Repository);
                }
            }

            return metadata;
        }
Example #20
0
        public Metadata Transform(Metadata metadata)
        {
            if (metadata.Kref != null && metadata.Kref.Source == "curse")
            {
                var json = metadata.Json();

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

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

                Log.InfoFormat("Found Curse Mod: {0} {1}", curseMod.GetName(), latestVersion.GetFileVersion());

                // 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 Curse: {0}", latestVersion.version);
                    json["ksp_version"] = latestVersion.version.ToString();
                }

                var useDownloadNameVersion = false;
                var useFilenameVersion = false;
                var useCurseIdVersion = false;

                var curseMetadata = (JObject) json["x_netkan_curse"];
                if (curseMetadata != null)
                {
                    var useDownloadNameVersionMetadata = (bool?)curseMetadata["use_download_name_version"];
                    if (useDownloadNameVersionMetadata != null)
                    {
                        useDownloadNameVersion = useDownloadNameVersionMetadata.Value;
                    }

                    var useFilenameVersionMetadata = (bool?) curseMetadata["use_filename_version"];
                    if (useFilenameVersionMetadata != null)
                    {
                        useFilenameVersion = useFilenameVersionMetadata.Value;
                    }

                    var useCurseIdVersionMetadata = (bool?)curseMetadata["use_curse_id_version"];
                    if (useCurseIdVersionMetadata != null)
                    {
                        useCurseIdVersion = useCurseIdVersionMetadata.Value;
                    }

                    if ((useDownloadNameVersion ? 1 : 0) + (useFilenameVersion ? 1 : 0) + (useCurseIdVersion ? 1 : 0) > 1)
                    {
                        throw new Kraken("Conflicting version options set in x_netkan_curse");
                    }
                }

                json.SafeAdd("name", curseMod.GetName());
                //json.SafeAdd("abstract", cMod.short_description);

                if (useDownloadNameVersion)  json.SafeAdd("version", latestVersion.name);
                else if (useFilenameVersion) json.SafeAdd("version", latestVersion.GetFilename());
                else if (useCurseIdVersion)  json.SafeAdd("version", latestVersion.GetCurseIdVersion());
                else                         json.SafeAdd("version", latestVersion.GetFileVersion());

                json.SafeAdd("author", JToken.FromObject(curseMod.authors));
                json.SafeAdd("download", latestVersion.GetDownloadUrl());

                // Curse provides users with the following default selection of licenses. Let's convert them to CKAN
                // compatible license strings if possible.
                //
                // "Academic Free License v3.0"                               - Becomes "AFL-3.0"
                // "Ace3 Style BSD"                                           - Becomes "restricted"
                // "All Rights Reserved"                                      - Becomes "restricted"
                // "Apache License version 2.0"                               - Becomes "Apache-2.0"
                // "Apple Public Source License version 2.0 (APSL)"           - Becomes "APSL-2.0"
                // "BSD License"                                              - Becomes "BSD-3-clause"
                // "Common Development and Distribution License (CDDL) "      - Becomes "CDDL"
                // "GNU Affero General Public License version 3 (AGPLv3)"     - Becomes "AGPL-3.0"
                // "GNU General Public License version 2 (GPLv2)"             - Becomes "GPL-2.0"
                // "GNU General Public License version 3 (GPLv3)"             - Becomes "GPL-3.0"
                // "GNU Lesser General Public License version 2.1 (LGPLv2.1)" - Becomes "LGPL-2.1"
                // "GNU Lesser General Public License version 3 (LGPLv3)"     - Becomes "LGPL-3.0"
                // "ISC License (ISCL)"                                       - Becomes "ISC"
                // "Microsoft Public License (Ms-PL)"                         - Becomes "Ms-PL"
                // "Microsoft Reciprocal License (Ms-RL)"                     - Becomes "Ms-RL"
                // "MIT License"                                              - Becomes "MIT"
                // "Mozilla Public License 1.0 (MPL)"                         - Becomes "MPL-1.0"
                // "Mozilla Public License 1.1 (MPL 1.1)"                     - Becomes "MPL-1.1"
                // "Public Domain"                                            - Becomes "public-domain"
                // "WTFPL"                                                    - Becomes "WTFPL"
                // "zlib/libpng License"                                      - Becomes "Zlib"
                // "Custom License"                                           - Becomes "unknown"

                var curseLicense = curseMod.license.Trim();

                switch (curseLicense)
                {
                    case "Academic Free License v3.0":
                        json.SafeAdd("license", "AFL-3.0");
                        break;
                    case "Ace3 Style BSD":
                        json.SafeAdd("license", "restricted");
                        break;
                    case "All Rights Reserved":
                        json.SafeAdd("license", "restricted");
                        break;
                    case "Apache License version 2.0":
                        json.SafeAdd("license", "Apache-2.0");
                        break;
                    case "Apple Public Source License version 2.0 (APSL)":
                        json.SafeAdd("license", "APSL-2.0");
                        break;
                    case "BSD License":
                        json.SafeAdd("license", "BSD-3-clause");
                        break;
                    case "Common Development and Distribution License (CDDL) ":
                        json.SafeAdd("license", "CDDL");
                        break;
                    case "GNU Affero General Public License version 3 (AGPLv3)":
                        json.SafeAdd("license", "AGPL-3.0");
                        break;
                    case "GNU General Public License version 2 (GPLv2)":
                        json.SafeAdd("license", "GPL-2.0");
                        break;
                    case "GNU General Public License version 3 (GPLv3)":
                        json.SafeAdd("license", "GPL-3.0");
                        break;
                    case "GNU Lesser General Public License version 2.1 (LGPLv2.1)":
                        json.SafeAdd("license", "LGPL-2.1");
                        break;
                    case "GNU Lesser General Public License version 3 (LGPLv3)":
                        json.SafeAdd("license", "LGPL-3.0");
                        break;
                    case "ISC License (ISCL)":
                        json.SafeAdd("license", "ISC");
                        break;
                    case "Microsoft Public License (Ms-PL)":
                        json.SafeAdd("license", "Ms-PL");
                        break;
                    case "Microsoft Reciprocal License (Ms-RL)":
                        json.SafeAdd("license", "Ms-RL");
                        break;
                    case "MIT License":
                        json.SafeAdd("license", "MIT");
                        break;
                    case "Mozilla Public License 1.0 (MPL)":
                        json.SafeAdd("license", "MPL-1.0");
                        break;
                    case "Mozilla Public License 1.1 (MPL 1.1)":
                        json.SafeAdd("license", "MPL-1.1");
                        break;
                    case "Public Domain":
                        json.SafeAdd("license", "public-domain");
                        break;
                    case "WTFPL":
                        json.SafeAdd("license", "WTFPL");
                        break;
                    case "zlib/libpng License":
                        json.SafeAdd("license", "Zlib");
                        break;
                    default:
                        json.SafeAdd("license", "unknown");
                        break;
                }

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

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

                //resourcesJson.SafeAdd("homepage", Normalize(cMod.website));
                //resourcesJson.SafeAdd("repository", Normalize(cMod.source_code));
                resourcesJson.SafeAdd("curse", curseMod.GetProjectUrl());

                if (curseMod.thumbnail != null)
                {
                    resourcesJson.SafeAdd("x_screenshot", Normalize(new Uri(curseMod.thumbnail)));
                }

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

                return new Metadata(json);
            }

            return metadata;
        }
        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;
        }
Example #22
0
        public Metadata Transform(Metadata metadata)
        {
            if (metadata.Vref != null && metadata.Vref.Source == "ksp-avc")
            {
                var json = metadata.Json();

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

                var noVersion = metadata.Version == null;

                if (noVersion)
                {
                    json["version"] = "0"; // TODO: DBB: Dummy version necessary to the next statement doesn't throw
                }

                var mod = CkanModule.FromJson(json.ToString());

                if (noVersion)
                {
                    json.Remove("version");
                }

                var file = _http.DownloadPackage(metadata.Download, metadata.Identifier);
                var avc = _moduleService.GetInternalAvc(mod, file, metadata.Vref.Id);

                if (avc != null)
                {
                    Log.Info("Found internal AVC version file");

                    // The CKAN spec states that only a KSP version can be supplied, *or* a min/max can
                    // be provided. Since min/max are more descriptive, we check and use them first.
                    if (avc.ksp_version_min != null || avc.ksp_version_max != null)
                    {
                        json.Remove("ksp_version");

                        if (avc.ksp_version_min != null)
                            json["ksp_version_min"] = avc.ksp_version_min.ToString();

                        if (avc.ksp_version_max != null)
                            json["ksp_version_max"] = avc.ksp_version_max.ToString();
                    }
                    else if (avc.ksp_version != null)
                    {
                        json["ksp_version"] = avc.ksp_version.ToString();
                    }

                    if (avc.version != null)
                    {
                        json["version"] = avc.version.ToString();
                    }

                    // It's cool if we don't have version info at all, it's optional in the AVC spec.

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

                return new Metadata(json);
            }
            else
            {
                return metadata;
            }
        }
Example #23
0
 public void Validate(Metadata metadata)
 {
     CkanModule.FromJson(metadata.Json().ToString());
 }
Example #24
0
        public Metadata Transform(Metadata metadata)
        {
            if (metadata.Vref != null && metadata.Vref.Source == "ksp-avc")
            {
                var json = metadata.Json();

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

                var noVersion = metadata.Version == null;

                if (noVersion)
                {
                    json["version"] = "0"; // TODO: DBB: Dummy version necessary to the next statement doesn't throw
                }
                
                var mod = CkanModule.FromJson(json.ToString());

                if (noVersion)
                {
                    json.Remove("version");
                }

                var file = _http.DownloadPackage(metadata.Download, metadata.Identifier);
                var avc = _moduleService.GetInternalAvc(mod, file, metadata.Vref.Id);

                if (avc != null)
                {
                    Log.Info("Found internal AVC version file");

                    // Get the minimum and maximum KSP versions that already exist in the metadata.
                    // Use specific KSP version if min/max don't exist.
                    var existingKspMinStr = (string)json["ksp_version_min"] ?? (string)json["ksp_version"];
                    var existingKspMaxStr = (string)json["ksp_version_max"] ?? (string)json["ksp_version"];

                    var existingKspMin = existingKspMinStr == null ? null : new KSPVersion(existingKspMinStr);
                    var existingKspMax = existingKspMaxStr == null ? null : new KSPVersion(existingKspMaxStr);
                    
                    // Get the minimum and maximum KSP versions that are in the AVC file.
                    // Use specific KSP version if min/max don't exist.
                    var avcKspMin = avc.ksp_version_min ?? avc.ksp_version;
                    var avcKspMax = avc.ksp_version_max ?? avc.ksp_version;

                    // Now calculate the minimum and maximum KSP versions between both the existing metadata and the
                    // AVC file.
                    var kspMins = new List<KSPVersion>();
                    var kspMaxes = new List<KSPVersion>();

                    if (existingKspMin != null)
                        kspMins.Add(existingKspMin);

                    if (avcKspMin != null)
                        kspMins.Add(avcKspMin);

                    if (existingKspMax != null)
                        kspMaxes.Add(existingKspMax);

                    if (avcKspMax != null)
                        kspMaxes.Add(avcKspMax);

                    var kspMin = kspMins.Any() ? kspMins.Min() : null;
                    var kspMax = kspMaxes.Any() ? kspMaxes.Max() : null;

                    if (kspMin != null || kspMax != null)
                    {
                        // If we have either a minimum or maximum KSP version, remove all existing KSP version
                        // information from the metadata.
                        json.Remove("ksp_version");
                        json.Remove("ksp_version_min");
                        json.Remove("ksp_version_max");

                        if (kspMin != null && kspMax != null)
                        {
                            // If we have both a minimum and maximum KSP version...
                            if (kspMin.CompareTo(kspMax) == 0)
                            {
                                // ...and they are equal, then just set ksp_version
                                json["ksp_version"] = kspMin.ToString();
                            }
                            else
                            {
                                // ...otherwise set both ksp_version_min and ksp_version_max
                                json["ksp_version_min"] = kspMin.ToString();
                                json["ksp_version_max"] = kspMax.ToString();
                            }
                        }
                        else
                        {
                            // If we have only one or the other then set which ever is applicable

                            if (kspMin != null)
                                json["ksp_version_min"] = kspMin.ToString();

                            if (kspMax != null)
                                json["ksp_version_max"] = kspMax.ToString();
                        }
                    }

                    if (avc.version != null)
                    {
                        // In practice, the version specified in .version files tends to be unreliable, with authors
                        // forgetting to update it when new versions are released. Therefore if we have a version
                        // specified from another source such as KerbalStuff or a GitHub tag, don't overwrite it.
                        json.SafeAdd("version", avc.version.ToString());
                    }

                    // It's cool if we don't have version info at all, it's optional in the AVC spec.

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

                return new Metadata(json);
            }
            else
            {
                return metadata;
            }
        }
Example #25
0
        private static void WriteCkan(Metadata metadata)
        {
            var versionFilename = metadata.Version.ToString().Replace(':', '-');
            var finalPath = Path.Combine(
                Options.OutputDir,
                string.Format("{0}-{1}.ckan", metadata.Identifier, versionFilename)
            );

            var sb = new StringBuilder();
            var sw = new StringWriter(sb);

            using (var writer = new JsonTextWriter(sw))
            {
                writer.Formatting = Formatting.Indented;
                writer.Indentation = 4;
                writer.IndentChar = ' ';

                var serializer = new JsonSerializer();
                serializer.Serialize(writer, metadata.Json());
            }

            File.WriteAllText(finalPath, sw + Environment.NewLine);

            Log.InfoFormat("Transformation written to {0}", finalPath);
        }
Example #26
0
        public Metadata Transform(Metadata metadata)
        {
            if (metadata.Vref != null && metadata.Vref.Source == "ksp-avc")
            {
                var json = metadata.Json();

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

                var noVersion = metadata.Version == null;

                if (noVersion)
                {
                    json["version"] = "0"; // TODO: DBB: Dummy version necessary to the next statement doesn't throw
                }

                var mod = CkanModule.FromJson(json.ToString());

                if (noVersion)
                {
                    json.Remove("version");
                }

                var file = _http.DownloadPackage(metadata.Download, metadata.Identifier);
                var avc = _moduleService.GetInternalAvc(mod, file, metadata.Vref.Id);

                if (avc != null)
                {
                    Log.Info("Found internal AVC version file");

                    if (Uri.IsWellFormedUriString(avc.Url, UriKind.Absolute))
                    {
                        Log.InfoFormat("Found remote AVC version file at {0}", avc.Url);

                        try
                        {
                            var remoteJson = Net.DownloadText(avc.Url);
                            var remoteAvc = JsonConvert.DeserializeObject<AvcVersion>(remoteJson);

                            if (avc.version.CompareTo(remoteAvc.version) == 0)
                            {
                                // Local AVC and Remote AVC describe the same version, prefer
                                Log.Info("Remote AVC version file describes same version as local AVC version file, using it preferrentially.");
                                avc = remoteAvc;
                            }
                        }
                        catch (Exception e)
                        {
                            Log.InfoFormat("An error occured fetching the remote AVC version file, ignoring: {0}", e.Message);
                            Log.Debug(e);
                        }
                    }

                    // Get the minimum and maximum KSP versions that already exist in the metadata.
                    // Use specific KSP version if min/max don't exist.
                    var existingKspMinStr = (string)json["ksp_version_min"] ?? (string)json["ksp_version"];
                    var existingKspMaxStr = (string)json["ksp_version_max"] ?? (string)json["ksp_version"];

                    var existingKspMin = existingKspMinStr == null ? null : KspVersion.Parse(existingKspMinStr);
                    var existingKspMax = existingKspMaxStr == null ? null : KspVersion.Parse(existingKspMaxStr);

                    // Get the minimum and maximum KSP versions that are in the AVC file.
                    // Use specific KSP version if min/max don't exist.
                    var avcKspMin = avc.ksp_version_min ?? avc.ksp_version;
                    var avcKspMax = avc.ksp_version_max ?? avc.ksp_version;

                    // Now calculate the minimum and maximum KSP versions between both the existing metadata and the
                    // AVC file.
                    var kspMins = new List<KspVersion>();
                    var kspMaxes = new List<KspVersion>();

                    if (existingKspMin != null)
                        kspMins.Add(existingKspMin);

                    if (avcKspMin != null)
                        kspMins.Add(avcKspMin);

                    if (existingKspMax != null)
                        kspMaxes.Add(existingKspMax);

                    if (avcKspMax != null)
                        kspMaxes.Add(avcKspMax);

                    var kspMin = kspMins.Any() ? kspMins.Min() : null;
                    var kspMax = kspMaxes.Any() ? kspMaxes.Max() : null;

                    if (kspMin != null || kspMax != null)
                    {
                        // If we have either a minimum or maximum KSP version, remove all existing KSP version
                        // information from the metadata.
                        json.Remove("ksp_version");
                        json.Remove("ksp_version_min");
                        json.Remove("ksp_version_max");

                        if (kspMin != null && kspMax != null)
                        {
                            // If we have both a minimum and maximum KSP version...
                            if (kspMin.CompareTo(kspMax) == 0)
                            {
                                // ...and they are equal, then just set ksp_version
                                json["ksp_version"] = kspMin.ToString();
                            }
                            else
                            {
                                // ...otherwise set both ksp_version_min and ksp_version_max
                                json["ksp_version_min"] = kspMin.ToString();
                                json["ksp_version_max"] = kspMax.ToString();
                            }
                        }
                        else
                        {
                            // If we have only one or the other then set which ever is applicable

                            if (kspMin != null)
                                json["ksp_version_min"] = kspMin.ToString();

                            if (kspMax != null)
                                json["ksp_version_max"] = kspMax.ToString();
                        }
                    }

                    if (avc.version != null)
                    {
                        // In practice, the version specified in .version files tends to be unreliable, with authors
                        // forgetting to update it when new versions are released. Therefore if we have a version
                        // specified from another source such as SpaceDock, curse or a GitHub tag, don't overwrite it.
                        json.SafeAdd("version", avc.version.ToString());
                    }

                    // It's cool if we don't have version info at all, it's optional in the AVC spec.

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

                return new Metadata(json);
            }
            else
            {
                return metadata;
            }
        }