Esempio n. 1
0
        public static PackageMetadata Parse(string text)
        {
            var json = JsonObject.Parse(text);

            var metadata = new PackageMetadata {
                Name = json["name"]
            };

            if (json.ContainsKey("version"))
            {
                metadata.Version = SemanticVersion.Parse(json["version"]);
            }

            if (json.ContainsKey("main"))
            {
                metadata.Main = json["main"];
            }

            if (json.ContainsKey("repository"))
            {
                var repository = json["repository"];

                if (repository is JsonObject)
                {
                    /*
                    { 
                      "type" : "git",
                      "url" : "https://github.com/npm/npm.git"
                    }
                    */

                    metadata.Repository = repository.As<PackageRepository>();
                }
                else
                {
                    metadata.Repository = PackageRepository.Parse(repository);
                }
            }

            if (json.ContainsKey("dependencies"))
            {
                foreach (var pair in (JsonObject)json["dependencies"])
                {
                    var dep = new PackageDependency(pair.Key, pair.Value);

                    metadata.Dependencies.Add(dep);
                }
            }
   
            if (json.ContainsKey("files"))
            {
                metadata.Files = json["files"].ToArrayOf<string>();
            }

            if (json.ContainsKey("author"))
            {
                metadata.Author = json["author"].As<PackageContributor>();
            }

            if (json.ContainsKey("contributors"))
            {
                metadata.Contributors = json["contributors"].ToArrayOf<PackageContributor>();
            }

            return metadata;
        }
Esempio n. 2
0
        public static PackageMetadata Parse(string text)
        {
            var json = JsonObject.Parse(text);

            var metadata = new PackageMetadata {
                Name = json["name"]
            };

            if (json.TryGetValue("version", out var versionNode))
            {
                metadata.Version = SemanticVersion.Parse(versionNode);
            }

            if (json.TryGetValue("main", out var mainNode))
            {
                metadata.Main = mainNode;
            }

            if (json.TryGetValue("repository", out var repositoryNode))
            {
                if (repositoryNode is JsonObject)
                {
                    /*
                     * {
                     * "type" : "git",
                     * "url" : "https://github.com/npm/npm.git"
                     * }
                     */

                    metadata.Repository = repositoryNode.As <PackageRepository>();
                }
                else
                {
                    metadata.Repository = PackageRepository.Parse(repositoryNode);
                }
            }

            if (json.TryGetValue("dependencies", out var dependenciesNode) &&
                dependenciesNode is JsonObject dependenciesObject)
            {
                var deps = new PackageDependency[dependenciesObject.Values.Count];

                var i = 0;

                foreach (var pair in dependenciesObject)
                {
                    deps[i] = new PackageDependency(pair.Key, pair.Value);

                    i++;
                }

                metadata.Dependencies = deps;
            }

            if (json.TryGetValue("files", out var filesNode))
            {
                metadata.Files = filesNode.ToArrayOf <string>();
            }

            if (json.TryGetValue("author", out var authorNode))
            {
                // Your Name <*****@*****.**>

                if (authorNode is JsonString)
                {
                    metadata.Author = new PackageContributor {
                        Text = authorNode
                    };
                }
                else
                {
                    metadata.Author = authorNode.As <PackageContributor>();
                }
            }

            if (json.TryGetValue("contributors", out var contributorsNode))
            {
                metadata.Contributors = contributorsNode.ToArrayOf <PackageContributor>();
            }

            return(metadata);
        }