Program.cs 7.4 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
        private sealed class ParsedArgs
        {
15
            internal string RepoUtilDataPath { get; set; }
16 17
            internal string SourcesDirectory { get; set; }
            internal string GenerateDirectory { get; set; }
J
Jared Parsons 已提交
18 19 20
            internal string[] RemainingArgs { get; set; }
        }

21
        private delegate ICommand CreateCommand(RepoConfig repoConfig, string sourcesDir, string generateDir);
22 23 24

        internal static int Main(string[] args)
        {
25 26 27 28
            int result = 1;
            try
            {
                if (Run(args))
29
                {
30
                    result = 0;
31
                }
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
            }
            catch (ConflictingPackagesException ex)
            {
                Console.WriteLine(ex.Message);
                foreach (var package in ex.ConflictingPackages)
                {
                    Console.WriteLine(package.PackageName);
                    Console.WriteLine($"\t{package.Conflict.NuGetPackage.Version} - {package.Conflict.FileName}");
                    Console.WriteLine($"\t{package.Original.NuGetPackage.Version} - {package.Original.FileName}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Something unexpected happened.");
                Console.WriteLine(ex.ToString());
            }

            return result;
50 51 52
        }

        private static bool Run(string[] args)
J
Jared Parsons 已提交
53 54
        {
            ParsedArgs parsedArgs;
J
Jared Parsons 已提交
55 56
            CreateCommand func;
            if (!TryParseCommandLine(args, out parsedArgs, out func))
57
            {
58
                Usage();
59 60 61
                return false;
            }

62
            var repoConfig = RepoConfig.ReadFrom(parsedArgs.RepoUtilDataPath);
63
            var command = func(repoConfig, parsedArgs.SourcesDirectory, parsedArgs.GenerateDirectory);
J
Jared Parsons 已提交
64 65 66 67 68 69 70 71 72
            if (command.Run(Console.Out, parsedArgs.RemainingArgs))
            {
                return true;
            }
            else
            {
                Console.WriteLine($"RepoUtil config read from: {parsedArgs.RepoUtilDataPath}");
                return false;
            }
J
Jared Parsons 已提交
73 74
        }

J
Jared Parsons 已提交
75
        private static bool TryParseCommandLine(string[] args, out ParsedArgs parsedArgs, out CreateCommand func)
76
        {
J
Jared Parsons 已提交
77
            func = null;
J
Jared Parsons 已提交
78 79 80
            parsedArgs = new ParsedArgs();

            // Setup the default values
81
            var binariesPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(AppContext.BaseDirectory))));
82 83

            var index = 0;
J
Jared Parsons 已提交
84 85 86 87 88 89 90 91 92 93
            if (!TryParseCommon(args, ref index, parsedArgs))
            {
                return false;
            }

            if (!TryParseCommand(args, ref index, out func))
            {
                return false;
            }

94 95 96
            parsedArgs.SourcesDirectory = parsedArgs.SourcesDirectory ?? GetDirectoryName(AppContext.BaseDirectory, 5);
            parsedArgs.GenerateDirectory = parsedArgs.GenerateDirectory ?? parsedArgs.SourcesDirectory;
            parsedArgs.RepoUtilDataPath = parsedArgs.RepoUtilDataPath ?? Path.Combine(parsedArgs.SourcesDirectory, @"build\config\RepoUtilData.json");
J
Jared Parsons 已提交
97 98 99 100 101 102
            parsedArgs.RemainingArgs = index >= args.Length
                ? Array.Empty<string>()
                : args.Skip(index).ToArray();
            return true;
        }

103 104 105 106 107 108 109 110 111 112
        private static string GetDirectoryName(string path, int depth)
        {
            for (var i = 0; i < depth; i++)
            {
                path = Path.GetDirectoryName(path);
            }

            return path;
        }

J
Jared Parsons 已提交
113 114 115
        private static bool TryParseCommon(string[] args, ref int index, ParsedArgs parsedArgs)
        {
            while (index < args.Length)
116 117
            {
                var arg = args[index];
J
Jared Parsons 已提交
118 119 120 121 122 123
                if (arg[0] != '-')
                {
                    return true;
                }

                index++;
124 125
                switch (arg.ToLower())
                {
126
                    case "-sourcespath":
J
Jared Parsons 已提交
127
                        {
J
Jared Parsons 已提交
128
                            if (index < args.Length)
J
Jared Parsons 已提交
129
                            {
130
                                parsedArgs.SourcesDirectory = args[index];
J
Jared Parsons 已提交
131
                                index++;
J
Jared Parsons 已提交
132 133 134 135
                            }
                            else
                            {
                                Console.WriteLine($"The -sourcesPath switch needs a value");
J
Jared Parsons 已提交
136
                                return false;
J
Jared Parsons 已提交
137 138 139
                            }
                            break;
                        }
140 141 142 143 144 145 146 147 148 149 150 151 152 153
                    case "-generatepath":
                        {
                            if (index < args.Length)
                            {
                                parsedArgs.GenerateDirectory = args[index];
                                index++;
                            }
                            else
                            {
                                Console.WriteLine($"The -generatePath switch needs a value");
                                return false;
                            }
                            break;
                        }
154 155 156 157 158 159 160 161 162 163 164 165 166 167
                    case "-config":
                        {
                            if (index < args.Length)
                            {
                                parsedArgs.RepoUtilDataPath = args[index];
                                index++;
                            }
                            else
                            {
                                Console.WriteLine($"The -config switch needs a value");
                                return false;
                            }
                            break;
                        }
168 169
                    default:
                        Console.Write($"Option {arg} is unrecognized");
J
Jared Parsons 已提交
170
                        return false;
171 172 173
                }
            }

J
Jared Parsons 已提交
174
            return true;
J
Jared Parsons 已提交
175
        }
J
Jared Parsons 已提交
176

J
Jared Parsons 已提交
177
        private static bool TryParseCommand(string[] args, ref int index, out CreateCommand func)
J
Jared Parsons 已提交
178
        {
J
Jared Parsons 已提交
179 180 181 182 183 184 185 186 187 188 189 190
            func = null;

            if (index >= args.Length)
            {
                Console.WriteLine("Need a command to run");
                return false;
            }

            var name = args[index];
            switch (name)
            {
                case "verify":
191
                    func = (c, s, g) => new VerifyCommand(c, s,g );
J
Jared Parsons 已提交
192
                    break;
J
Jared Parsons 已提交
193
                case "view":
194
                    func = (c, s, g) => new ViewCommand(c, s);
J
Jared Parsons 已提交
195
                    break;
J
Jared Parsons 已提交
196
                case "consumes":
197
                    func = (c, s, g) => new ConsumesCommand(RepoData.Create(c, s));
J
Jared Parsons 已提交
198 199
                    break;
                case "change":
200
                    func = (c, s, g) => new ChangeCommand(RepoData.Create(c, s), g);
J
Jared Parsons 已提交
201 202
                    break;
                case "produces":
203
                    func = (c, s, g) => new ProducesCommand(c, s);
J
Jared Parsons 已提交
204 205 206 207 208 209 210 211
                    break;
                default:
                    Console.Write($"Command {name} is not recognized");
                    return false;
            }

            index++;
            return true;
J
Jared Parsons 已提交
212
        }
213 214 215 216 217

        private static void Usage()
        {
            Console.WriteLine("RepoUtil [-sourcesPath <sources path>] [-generatePath <generate path>] [-config <config path>] [verify|view|consumes|change|produces]");
        }
J
Jared Parsons 已提交
218 219
    }
}