aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.Protobuf.CLI/Program.cs
blob: 621a6d10ff9325fa64f5c64397744e3b1516c0fe (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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

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

                var options = new Options();

                if (CommandLine.Parser.Default.ParseArguments(args, options))
                {
                    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);

                        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));
            }
            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);
            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;
        }
    }
}