aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.Protobuf.CLI/Program.cs
blob: 123de0996e121c23a259522946bccf8a166b7268 (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
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)
        {
            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);
                    var result = compiler.CompileFolder(Path.GetFullPath(options.SourceFolder));
                    result.Save(Path.GetFullPath(options.OutputFolder));
                }
                catch (Exception ex)
                {
                    return ExitError(ex.Message);
                }
            }

            return ExitSuccess("Protobuf folder compiled to " + Path.GetFullPath(options.OutputFolder));
        }

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