forked from Azure/autorest.java
/
Program.cs
130 lines (116 loc) · 6.13 KB
/
Program.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoRest.Core;
using AutoRest.Core.Extensibility;
using AutoRest.Core.Model;
using AutoRest.Core.Parsing;
using AutoRest.Core.Utilities;
using Microsoft.Perks.JsonRPC;
using IAnyPlugin = AutoRest.Core.Extensibility.IPlugin<AutoRest.Core.Extensibility.IGeneratorSettings, AutoRest.Core.IModelSerializer<AutoRest.Core.Model.CodeModel>, AutoRest.Core.ITransformer<AutoRest.Core.Model.CodeModel>, AutoRest.Core.CodeGenerator, AutoRest.Core.CodeNamer, AutoRest.Core.Model.CodeModel>;
namespace AutoRest.Java
{
public static class ExtensionsLoader
{
public static IAnyPlugin GetPlugin(bool azure, bool fluent)
{
if (fluent) return new AutoRest.Java.Azure.PluginJvaf();
if (azure) return new AutoRest.Java.Azure.PluginJva();
return new AutoRest.Java.PluginJv();
}
}
public class Program : NewPlugin
{
public static int Main(string[] args)
{
if(args != null && args.Length > 0 && args[0] == "--server")
{
var connection = new Connection(Console.OpenStandardOutput(), Console.OpenStandardInput());
connection.Dispatch<IEnumerable<string>>("GetPluginNames", async () => new []{ "java" });
connection.Dispatch<string, string, bool>("Process", (plugin, sessionId) => new Program(connection, plugin, sessionId).Process());
connection.DispatchNotification("Shutdown", connection.Stop);
// wait for something to do.
connection.GetAwaiter().GetResult();
Console.Error.WriteLine("Shutting Down");
return 0;
}
Console.WriteLine("This is not an entry point.");
Console.WriteLine("Please invoke this extension through AutoRest.");
return 1;
}
public Program(Connection connection, string plugin, string sessionId) : base(connection, plugin, sessionId) { }
private T GetXmsCodeGenSetting<T>(CodeModel codeModel, string name)
{
try
{
return (T)Convert.ChangeType(
codeModel.CodeGenExtensions[name],
typeof(T).GenericTypeArguments.Length == 0 ? typeof(T) : typeof(T).GenericTypeArguments[0] // un-nullable
);
}
catch
{
return default(T);
}
}
protected override async Task<bool> ProcessInternal()
{
var files = await ListInputs();
if (files.Length != 1)
{
throw new Exception($"Generator received incorrect number of inputs: {files.Length} : {string.Join(",", files)}");
}
var modelAsJson = (await ReadFile(files[0])).EnsureYamlIsJson();
var codeModelT = new ModelSerializer<CodeModel>().Load(modelAsJson);
// build settings
var altNamespace = (await GetValue<string[]>("input-file") ?? new[] { "" }).FirstOrDefault()?.Split('/').Last().Split('\\').Last().Split('.').First();
new Settings
{
Namespace = await GetValue("namespace"),
ClientName = GetXmsCodeGenSetting<string>(codeModelT, "name") ?? await GetValue("override-client-name"),
PayloadFlatteningThreshold = GetXmsCodeGenSetting<int?>(codeModelT, "ft") ?? await GetValue<int?>("payload-flattening-threshold") ?? 0,
AddCredentials = await GetValue<bool?>("add-credentials") ?? false,
Host = this
};
var header = await GetValue("license-header");
if (header != null)
{
Settings.Instance.Header = header;
}
Settings.Instance.CustomSettings.Add("InternalConstructors", GetXmsCodeGenSetting<bool?>(codeModelT, "internalConstructors") ?? await GetValue<bool?>("use-internal-constructors") ?? false);
Settings.Instance.CustomSettings.Add("SyncMethods", GetXmsCodeGenSetting<string>(codeModelT, "syncMethods") ?? await GetValue("sync-methods") ?? "essential");
Settings.Instance.CustomSettings.Add("UseDateTimeOffset", GetXmsCodeGenSetting<bool?>(codeModelT, "useDateTimeOffset") ?? await GetValue<bool?>("use-datetimeoffset") ?? false);
Settings.Instance.CustomSettings["ClientSideValidation"] = await GetValue<bool?>("client-side-validation") ?? false;
Settings.Instance.MaximumCommentColumns = await GetValue<int?>("max-comment-columns") ?? Settings.DefaultMaximumCommentColumns;
Settings.Instance.OutputFileName = await GetValue<string>("output-file");
// process
var plugin = ExtensionsLoader.GetPlugin(
await GetValue<bool?>("azure-arm") ?? false,
await GetValue<bool?>("fluent") ?? false);
Settings.PopulateSettings(plugin.Settings, Settings.Instance.CustomSettings);
using (plugin.Activate())
{
Settings.Instance.Namespace = Settings.Instance.Namespace ?? CodeNamer.Instance.GetNamespaceName(altNamespace);
var codeModel = plugin.Serializer.Load(modelAsJson);
codeModel = plugin.Transformer.TransformCodeModel(codeModel);
if (await GetValue<bool?>("sample-generation") ?? false)
{
plugin.CodeGenerator.GenerateSamples(codeModel).GetAwaiter().GetResult();
}
else
{
plugin.CodeGenerator.Generate(codeModel).GetAwaiter().GetResult();
}
}
// write out files
var outFS = Settings.Instance.FileSystemOutput;
var outFiles = outFS.GetFiles("", "*", System.IO.SearchOption.AllDirectories);
foreach (var outFile in outFiles)
{
WriteFile(outFile, outFS.ReadAllText(outFile), null);
}
return true;
}
}
}