aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.Protobuf.CLI/Program.cs
blob: a1ae39c310059970f906665cf3e20787367dc5e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Protobuf.Compilers;

namespace Tango.Protobuf.CLI
{
    class Program
    {
        static int Main(string[] args)
        {
            try
            {
                Console.Title = "Tango Protobuf Compiler";

#if DEBUG

                if (args.Length == 0)
                {
                    args = new string[]
                    {
                        "-i " + Path.GetFullPath("..\\..\\..\\..\\PMR\\Messages"),
                        "-o " + Path.GetFullPath("..\\..\\..\\Tango.PMR"),
                        "-l CSharp",
                    };
                }

#endif

                if (args.Length == 0)
                {
                    return ExitHelp();
                }

                var options = new Options();

                if (CommandLine.Parser.Default.ParseArguments(args, options))
                {
                    options.SourceFolder = options.SourceFolder.Trim();
                    options.OutputFolder = options.OutputFolder.Trim();
                    options.Language = options.Language.Trim();

                    if (!Directory.Exists(options.SourceFolder))
                    {
                        return ExitError("Could not locate source folder \"" + Path.GetFullPath(options.SourceFolder) + "\"");
                    }
                    if (!Directory.Exists(options.OutputFolder))
                    {
                        return ExitError("Could not locate output folder " + Path.GetFullPath(options.OutputFolder));
                    }

                    CompilerLanguage language;
                    if (!Enum.TryParse<CompilerLanguage>(options.Language, out language))
                    {
                        return ExitError("Invalid language: " + options.Language);
                    }

                    try
                    {
                        var compiler = CompilerFactory.CreateCompiler(language);

                        if (options.Min && compiler is CCompiler)
                        {
                            (compiler as CCompiler).Minimize = true;
                        }


                        compiler.CompilationProgress += (x, e) =>
                        {
                            try
                            {
                                Console.SetCursorPosition(0, 0);
                                Console.Write(new string(' ', Console.WindowWidth));
                                Console.SetCursorPosition(0, 0);
                                Console.Write("Compiling " + Path.GetFileName(e.File) + "...");
                                Console.SetCursorPosition(Console.WindowWidth - 7, 0);
                                Console.Write("(% " + Math.Round((((double)e.Current / (double)e.Total) * 100d), 0) + ")");
                            }
                            catch { }
                        };

                        var result = compiler.CompileFolder(Path.GetFullPath(options.SourceFolder), options.Includes != null ? options.Includes.Split(',') : null);
                        result.Save(Path.GetFullPath(options.OutputFolder));
                    }
                    catch (Exception ex)
                    {
                        return ExitError(ex.ToString());
                    }

                    return ExitSuccess("Protobuf folder compiled to " + Path.GetFullPath(options.OutputFolder));
                }
                else
                {
                    throw new ArgumentException("Error parsing input arguments.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return -1;
            }
        }

        private static int ExitSuccess(String text)
        {
            Console.WriteLine(text);
            return 0;
        }

        private static int ExitError(String error)
        {
            Console.WriteLine(error);

            if (Debugger.IsAttached)
            {
                Console.ReadLine();
            }

            return -1;
        }

        private static int ExitHelp()
        {
            Console.WriteLine(
@"
    Example: proto-tc -i <source folder> -o <output folder> -l <language>

    Available -l arguments:
    
        CSharp
        Java
        JavaNano
        CPP
        C
        EmbeddedC
        JS
        Python
        PHP
        Ruby
");

            return -1;
        }
    }
}