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

namespace RepoUtil
{
    internal static class Program
    {
J
Jared Parsons 已提交
13 14 15 16 17 18 19 20
        private sealed class ParsedArgs
        {
            internal Mode Mode { get; set; } = Mode.Usage;
            internal string RepoDataPath { get; set; }
            internal string SourcesPath { get; set; }
            internal string[] RemainingArgs { get; set; }
        }

21
        private enum Mode
J
Jared Parsons 已提交
22
        {
23
            Usage,
J
Jared Parsons 已提交
24 25
            Verify,
            Consumes,
J
Jared Parsons 已提交
26
            Change,
J
Jared Parsons 已提交
27
            Produces,
28 29 30 31 32 33 34 35 36 37
        }

        internal static readonly string[] ProjectJsonFileRelativeNames = Array.Empty<string>();

        internal static int Main(string[] args)
        {
            return Run(args) ? 0 : 1;
        }

        private static bool Run(string[] args)
J
Jared Parsons 已提交
38 39 40
        {
            ParsedArgs parsedArgs;
            if (!TryParseCommandLine(args, out parsedArgs))
41 42 43 44
            {
                return false;
            }

J
Jared Parsons 已提交
45 46
            var repoConfig = RepoConfig.ReadFrom(parsedArgs.RepoDataPath);
            switch (parsedArgs.Mode)
47 48 49 50 51
            {
                case Mode.Usage:
                    Usage();
                    return true;
                case Mode.Verify:
J
Jared Parsons 已提交
52
                    return VerifyUtil.Go(repoConfig, parsedArgs.SourcesPath);
J
Jared Parsons 已提交
53
                case Mode.Consumes:
J
Jared Parsons 已提交
54
                    {
J
Jared Parsons 已提交
55
                        Console.WriteLine(ConsumesUtil.Go(repoConfig, parsedArgs.SourcesPath));
J
Jared Parsons 已提交
56
                        return true;
J
Jared Parsons 已提交
57
                    }
J
Jared Parsons 已提交
58 59
                case Mode.Change:
                    {
J
Jared Parsons 已提交
60
                        var repoData = RepoData.Create(repoConfig, parsedArgs.SourcesPath);
J
Jared Parsons 已提交
61
                        var util = new ChangeUtil(repoData);
J
Jared Parsons 已提交
62
                        return util.Run(Console.Out, parsedArgs.RemainingArgs);
J
Jared Parsons 已提交
63
                    }
J
Jared Parsons 已提交
64 65
                case Mode.Produces:
                    {
J
Jared Parsons 已提交
66
                        var util = new ProduceUtil(repoConfig, parsedArgs.SourcesPath);
J
Jared Parsons 已提交
67 68 69
                        util.Go();
                        return true;
                    }
J
Jared Parsons 已提交
70 71
                default:
                    throw new Exception("Unrecognized mode");
J
Jared Parsons 已提交
72 73 74
            }
        }

J
Jared Parsons 已提交
75
        // TODO: don't use dashes here.
J
Jared Parsons 已提交
76
        private static bool TryParseCommandLine(string[] args, out ParsedArgs parsedArgs)
77
        {
J
Jared Parsons 已提交
78 79 80
            parsedArgs = new ParsedArgs();

            // Setup the default values
J
Jared Parsons 已提交
81
            var binariesPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(AppContext.BaseDirectory)));
J
Jared Parsons 已提交
82 83 84
            parsedArgs.SourcesPath = Path.GetDirectoryName(binariesPath);
            parsedArgs.Mode = Mode.Usage;
            parsedArgs.RepoDataPath = Path.Combine(AppContext.BaseDirectory, "RepoData.json");
85

J
Jared Parsons 已提交
86 87
            var allGood = true;
            var done = false;
88
            var index = 0;
J
Jared Parsons 已提交
89
            while (index < args.Length && !done)
90 91 92 93
            {
                var arg = args[index];
                switch (arg.ToLower())
                {
J
Jared Parsons 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
                    case "-sourcesPath":
                        {
                            if (index + 1 < args.Length)
                            {
                                parsedArgs.SourcesPath = args[index + 1];
                                index += 2;
                            }
                            else
                            {
                                Console.WriteLine($"The -sourcesPath switch needs a value");
                                index++;
                                allGood = false;
                            }
                            break;
                        }
                    case "verify":
                        parsedArgs.Mode = Mode.Verify;
111
                        index++;
J
Jared Parsons 已提交
112
                        done = true;
113
                        break;
J
Jared Parsons 已提交
114 115
                    case "consumes":
                        parsedArgs.Mode = Mode.Consumes;
J
Jared Parsons 已提交
116
                        index++;
J
Jared Parsons 已提交
117
                        done = true;
J
Jared Parsons 已提交
118
                        break;
J
Jared Parsons 已提交
119 120
                    case "change":
                        parsedArgs.Mode = Mode.Change;
J
Jared Parsons 已提交
121
                        index++;
J
Jared Parsons 已提交
122
                        done = true;
J
Jared Parsons 已提交
123
                        break;
J
Jared Parsons 已提交
124 125
                    case "produces":
                        parsedArgs.Mode = Mode.Produces;
J
Jared Parsons 已提交
126
                        index++;
J
Jared Parsons 已提交
127
                        done = true;
J
Jared Parsons 已提交
128
                        break;
129 130 131 132 133 134 135 136
                    default:
                        Console.Write($"Option {arg} is unrecognized");
                        allGood = false;
                        index++;
                        break;
                }
            }

J
Jared Parsons 已提交
137 138 139 140
            parsedArgs.RemainingArgs = index >= args.Length
                ? Array.Empty<string>()
                : args.Skip(index).ToArray();

141
            return allGood;
J
Jared Parsons 已提交
142
        }
J
Jared Parsons 已提交
143 144 145 146 147 148 149 150 151 152 153

        private static void Usage()
        {
            var text = @"
  verify: check the state of the repo
  consumes: output the conent consumed by this repo
  produces: output the content produced by this repo
  change: change the dependencies.
";
            Console.Write(text);
        }
J
Jared Parsons 已提交
154 155
    }
}