This repository has been archived by the owner on Jun 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ProtobufGeneratorBase.cs
88 lines (74 loc) · 3.08 KB
/
ProtobufGeneratorBase.cs
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
/***************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
This code is licensed under the Visual Studio SDK license terms.
THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
***************************************************************************/
using System;
using System.Runtime.InteropServices;
using System.CodeDom.Compiler;
using System.CodeDom;
using System.IO;
using System.Text;
using Microsoft.Win32;
using Microsoft.VisualStudio.Shell;
using VSLangProj80;
namespace Knacka.Se.ProtobufGenerator
{
public abstract class ProtobufGeneratorBase : BaseCodeGeneratorWithSite
{
protected const string TempNetSdkProjectGuid = "{9A19103F-16F7-4668-BE54-9A1E7A4F7556}";
protected static string _protocPath;
protected static string _grpcPath;
protected abstract bool GenerateGRPC { get; }
/// <summary>
/// Function that builds the contents of the generated file based on the contents of the input file
/// </summary>
/// <param name="inputFileContent">Content of the input file</param>
/// <returns>Generated file as a byte array</returns>
protected override byte[] GenerateCode(string inputFileContent)
{
var vsItem = this.GetVSProjectItem();
var name = vsItem?.ProjectItem?.Document?.Name;
var path = vsItem?.ProjectItem?.Document?.Path;
if (_protocPath == null)
{
IHaveProtoc protocFinder = new HaveProtoc(path, GenerateGRPC);
if (protocFinder.HaveFoundProtoc)
{
_protocPath = protocFinder.ProtocPath;
_grpcPath = protocFinder.GrpcPath;
}
}
if (string.IsNullOrEmpty(_protocPath))
{
this.GeneratorError(4, "Protoc.exe not found. Please read the documentation for ProtobufGenerator", 1, 1);
return null;
}
ICanGenerateFromProto generator = new GenerateFromProto(_protocPath, _grpcPath);
try
{
var inputFile = Path.GetFileName(InputFilePath)
?? (Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".proto");
if (this.CodeGeneratorProgress != null)
{
this.CodeGeneratorProgress.Progress(50, 100);
}
var res = generator.GenerateCsharpFromProto(Path.Combine(path, name));
if (this.CodeGeneratorProgress != null)
{
this.CodeGeneratorProgress.Progress(100, 100);
}
return res;
}
catch (Exception e)
{
this.GeneratorError(4, e.ToString(), 1, 1);
//Returning null signifies that generation has failed
return null;
}
}
}
}