Program.cs 3.6 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
    {
13
        private enum Mode
J
Jared Parsons 已提交
14
        {
15
            Usage,
J
Jared Parsons 已提交
16 17
            Verify,
            Consumes,
J
Jared Parsons 已提交
18
            Change,
J
Jared Parsons 已提交
19
            Produces,
20 21 22 23 24 25 26 27 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)
        { 
            string sourcesPath;
            Mode mode;
            if (!TryParseCommandLine(args, out mode, out sourcesPath))
            {
                return false;
            }

J
Jared Parsons 已提交
38
            var repoConfig = RepoConfig.ReadFrom(Path.Combine(AppContext.BaseDirectory, "RepoData.json"));
39 40 41 42 43 44
            switch (mode)
            {
                case Mode.Usage:
                    Usage();
                    return true;
                case Mode.Verify:
J
Jared Parsons 已提交
45
                    return VerifyUtil.Go(repoConfig, sourcesPath);
J
Jared Parsons 已提交
46
                case Mode.Consumes:
J
Jared Parsons 已提交
47
                    {
J
Jared Parsons 已提交
48
                        Console.WriteLine(ConsumesUtil.Go(repoConfig, sourcesPath));
J
Jared Parsons 已提交
49
                        return true;
J
Jared Parsons 已提交
50
                    }
J
Jared Parsons 已提交
51 52 53 54
                case Mode.Change:
                    {
                        var repoData = RepoData.Create(repoConfig, sourcesPath);
                        var util = new ChangeUtil(repoData);
J
Jared Parsons 已提交
55
                        util.ChangeAll();
J
Jared Parsons 已提交
56 57
                        return true;
                    }
J
Jared Parsons 已提交
58 59 60 61 62 63
                case Mode.Produces:
                    {
                        var util = new ProduceUtil(repoConfig, sourcesPath);
                        util.Go();
                        return true;
                    }
J
Jared Parsons 已提交
64 65
                default:
                    throw new Exception("Unrecognized mode");
J
Jared Parsons 已提交
66 67 68
            }
        }

69 70 71 72
        private static void Usage()
        {
            var text = @"
  -verify: check the state of the repo
J
Jared Parsons 已提交
73
  -consumes: output the conent consumed by this repo
J
Jared Parsons 已提交
74
  -change: change the dependencies.
75 76 77 78
";
            Console.Write(text);
        }

J
Jared Parsons 已提交
79
        // TODO: don't use dashes here.
80 81 82
        private static bool TryParseCommandLine(string[] args, out Mode mode, out string sourcesPath)
        {
            var allGood = true;
J
Jared Parsons 已提交
83
            var binariesPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(AppContext.BaseDirectory)));
84 85 86 87 88 89 90 91 92 93 94 95 96
            sourcesPath = Path.GetDirectoryName(binariesPath);
            mode = Mode.Usage;

            var index = 0;
            while (index < args.Length)
            {
                var arg = args[index];
                switch (arg.ToLower())
                {
                    case "-verify":
                        mode = Mode.Verify;
                        index++;
                        break;
J
Jared Parsons 已提交
97 98 99 100
                    case "-consumes":
                        mode = Mode.Consumes;
                        index++;
                        break;
J
Jared Parsons 已提交
101 102 103 104
                    case "-change":
                        mode = Mode.Change;
                        index++;
                        break;
J
Jared Parsons 已提交
105 106 107 108
                    case "-produces":
                        mode = Mode.Produces;
                        index++;
                        break;
109 110 111 112 113 114 115 116 117
                    default:
                        Console.Write($"Option {arg} is unrecognized");
                        allGood = false;
                        index++;
                        break;
                }
            }

            return allGood;
J
Jared Parsons 已提交
118 119 120
        }
    }
}