aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Tango.NET/Tango.Protobuf/ProtoCompiler.cs
blob: eedebf63b1b7b343b236d83ba64455d09e33ca94 (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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;

namespace Tango.Protobuf
{
    /// <summary>
    /// Represents a protobuf compiler base class.
    /// </summary>
    /// <seealso cref="Tango.Protobuf.IProtoCompiler" />
    public abstract class ProtoCompiler : IProtoCompiler
    {
        private const String COMPILERS_FOLDER_NAME = "ProtoCompilers"; //Compilers folder name.
        protected String _compilersPath; //Compilers folder path.

        /// <summary>
        /// Gets the compiler language.
        /// </summary>
        public abstract ProtoLanguage Language { get; }

        /// <summary>
        /// Gets the proto imports folders.
        /// </summary>
        public List<String> ImportsFolders { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ProtoCompiler"/> class.
        /// </summary>
        public ProtoCompiler()
        {
            _compilersPath = Path.Combine(AssemblyHelper.GetCurrentAssemblyFolder(), COMPILERS_FOLDER_NAME);
        }

        /// <summary>
        /// Compiles the specified .proto message file and saves the result at the specified output folder.
        /// </summary>
        /// <param name="inputFile">.proto file to compile</param>
        /// <returns>
        /// A list of compiled results.
        /// </returns>
        public virtual IEnumerable<ProtoResult> Compile(string inputFile)
        {
            String tmpPath = PathHelper.GetTempFolderPath();

            String importsString = String.Empty;

            foreach (var path in ImportsFolders)
            {
                importsString = "--proto_path \"" + path + "\" ";
            }

            Process p = new Process();
            p.StartInfo.FileName = Path.Combine(_compilersPath, GetProtoCompilerName());
            p.StartInfo.Arguments = String.Format(
                "{0} {1}=\"{2}\" \"{3}\"",
                importsString,
                GetProtoArguments(),
                tmpPath,
                inputFile);

            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            p.Start();
            p.WaitForExit(5000);

            List<ProtoResult> results = new List<ProtoResult>();

            foreach (var file in Directory.GetFiles(tmpPath))
            {
                ProtoResult result = new ProtoResult(Path.GetFileName(file), File.ReadAllText(file), Language);
            }

            PathHelper.TryDeleteFolder(tmpPath);

            return results;
        }

        /// <summary>
        /// Compiles the specified .proto message file asynchronously and saves the result at the specified output folder.
        /// </summary>
        /// <param name="inputFile">.proto file to compile</param>
        /// <returns>
        /// A list of compiled results.
        /// </returns>
        public async Task<IEnumerable<ProtoResult>> CompileAsync(string inputFile)
        {
            return await new Task<IEnumerable<ProtoResult>>(() => { return Compile(inputFile); });
        }

        /// <summary>
        /// Gets the protobuf compiler CLI arguments (without input/output files!).
        /// </summary>
        /// <returns></returns>
        protected abstract String GetProtoArguments();

        /// <summary>
        /// Gets the protobuf compiler CLI file name (override when using a compiler other than the default 'protoc.exe').
        /// </summary>
        /// <remarks>
        /// The compiler program must be located in the compilers folder.
        /// </remarks>
        /// <returns></returns>
        protected virtual String GetProtoCompilerName()
        {
            return "protoc.exe";
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            //TODO: Dispose...
        }
    }
}