ConsumesCommand.cs 3.0 KB
Newer Older
J
Jared Parsons 已提交
1 2 3 4 5 6
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
J
Jared Parsons 已提交
7
using System.IO;
J
Jared Parsons 已提交
8 9 10 11 12 13
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepoUtil
{
J
Jared Parsons 已提交
14
    internal sealed class ConsumesCommand : ICommand
J
Jared Parsons 已提交
15 16 17
    {
        private readonly RepoData _repoData;

J
Jared Parsons 已提交
18
        internal ConsumesCommand(RepoData repoData)
J
Jared Parsons 已提交
19 20 21 22
        {
            _repoData = repoData;
        }

J
Jared Parsons 已提交
23
        public bool Run(TextWriter writer, string[] args)
J
Jared Parsons 已提交
24
        {
J
Jared Parsons 已提交
25 26 27 28
            var obj = GoCore();
            var text = obj.ToString(Formatting.Indented);
            writer.WriteLine(text);
            return true;
J
Jared Parsons 已提交
29 30 31 32 33
        }

        private JObject GoCore()
        {
            var obj = new JObject();
34
            obj.Add(GetNuGetFeeds());
35
            obj.Add(GetFixedPackages());
J
Jared Parsons 已提交
36 37 38 39 40
            obj.Add(GetBuildPackages());
            obj.Add(GetToolsetPackages());
            return obj;
        }

41 42 43 44 45 46 47 48 49 50
        private JProperty GetNuGetFeeds()
        {
            var obj = new JObject();
            foreach (var nugetFeed in _repoData.NuGetFeeds)
            {
                obj.Add(GetProperty(nugetFeed));
            }
            return new JProperty("nugetFeeds", obj);
        }

51
        private JProperty GetFixedPackages()
J
Jared Parsons 已提交
52 53
        {
            var obj = new JObject();
J
Jared Parsons 已提交
54
            foreach (var package in _repoData.FixedPackages.GroupBy(x => x.Name))
J
Jared Parsons 已提交
55
            {
J
Jared Parsons 已提交
56
                obj.Add(GetProperty(package.Key, package.Select(x => x.Version)));
J
Jared Parsons 已提交
57
            }
58
            return new JProperty("fixed", obj);
J
Jared Parsons 已提交
59 60 61 62 63 64 65 66 67 68 69 70
        }

        private JProperty GetBuildPackages()
        {
            return GetFloatingPackages("build", _repoData.FloatingBuildPackages);
        }

        private JProperty GetToolsetPackages()
        {
            return GetFloatingPackages("toolset", _repoData.FloatingToolsetPackages);
        }

71
        private JProperty GetFloatingPackages(string name, IEnumerable<NuGetPackage> packages)
J
Jared Parsons 已提交
72
        {
73
            var obj = new JObject();
J
Jared Parsons 已提交
74 75
            foreach (var package in packages)
            {
76
                obj.Add(GetProperty(package));
J
Jared Parsons 已提交
77 78
            }

79
            return new JProperty(name, obj);
J
Jared Parsons 已提交
80 81
        }

82 83 84 85 86
        private static JProperty GetProperty(NuGetFeed feed)
        {
            return new JProperty(feed.Name, feed.Url);
        }

J
Jared Parsons 已提交
87 88 89 90 91
        private static JProperty GetProperty(NuGetPackage package)
        {
            return new JProperty(package.Name, package.Version);
        }

J
Jared Parsons 已提交
92
        private static JProperty GetProperty(string packageName, IEnumerable<string> versions)
J
Jared Parsons 已提交
93
        {
J
Jared Parsons 已提交
94
            if (versions.Count() == 1)
J
Jared Parsons 已提交
95
            {
J
Jared Parsons 已提交
96
                return GetProperty(new NuGetPackage(packageName, versions.Single()));
J
Jared Parsons 已提交
97 98 99 100 101 102 103 104 105 106 107 108
            }

            var content = JArray.FromObject(versions.ToArray());
            return new JProperty(packageName, content);
        }

        private static string GetKey(NuGetPackage nugetRef)
        {
            return $"{nugetRef.Name}:{nugetRef.Version}";
        }
    }
}