-
Notifications
You must be signed in to change notification settings - Fork 164
/
SmartCodeApp.cs
141 lines (127 loc) · 4.88 KB
/
SmartCodeApp.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
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using SmartCode.Configuration;
using System.IO;
using SmartCode.Configuration.ConfigBuilders;
using System.Reflection;
using HandlebarsDotNet;
using SmartCode.ETL;
using SmartCode.Generator;
namespace SmartCode.App
{
public class SmartCodeApp
{
public SmartCodeOptions SmartCodeOptions { get; }
public String AppDirectory => AppDomain.CurrentDomain.BaseDirectory;
public IConfigBuilder ConfigBuilder { get; private set; }
public IServiceCollection Services => SmartCodeOptions.Services;
public IServiceProvider ServiceProvider { get; private set; }
public Project Project { get; private set; }
public string ConfigPath => SmartCodeOptions.ConfigPath;
public ILogger<SmartCodeApp> Logger { get; private set; }
public SmartCodeApp(SmartCodeOptions smartCodeOptions)
{
SmartCodeOptions = smartCodeOptions;
BuildProject();
RegisterServices();
}
private void BuildProject()
{
var pathExtension = Path.GetExtension(ConfigPath).ToUpper();
switch (pathExtension)
{
case ".JSON":
{
ConfigBuilder = new JsonBuilder(ConfigPath);
break;
}
case ".YML":
{
ConfigBuilder = new YamlBuilder(ConfigPath);
break;
}
default:
{
throw new SmartCodeException($"ConfigPath:{ConfigPath},未知扩展名:{pathExtension}");
}
}
Project = ConfigBuilder.Build();
Project.ConfigPath = ConfigPath;
}
private void RegisterServices()
{
Services.AddSingleton<SmartCodeOptions>(SmartCodeOptions);
Services.AddSingleton<Project>(Project);
RegisterPlugins();
Services.AddSingleton<IPluginManager, PluginManager>();
if (Project.Mode == Project.ProjectMode.ETL)
{
Services.AddSingleton<IProjectBuilder, ETLProjectBuilder>();
}
else
{
if (Project.DataSource.Parameters.ContainsKey("Query"))
{
Services.AddSingleton<IProjectBuilder, ETLProjectBuilder>();
}
else
{
Services.AddSingleton<IProjectBuilder, GeneratorProjectBuilder>();
}
}
ServiceProvider = Services.BuildServiceProvider();
Logger = ServiceProvider.GetRequiredService<ILogger<SmartCodeApp>>();
}
private void RegisterPlugins()
{
foreach (var plugin in SmartCodeOptions.Plugins)
{
var pluginType = Assembly.Load(plugin.AssemblyName).GetType(plugin.TypeName);
if (pluginType == null)
{
throw new SmartCodeException($"Plugin.Type:{plugin.TypeName} can not find!");
}
var implType = Assembly.Load(plugin.ImplAssemblyName).GetType(plugin.ImplTypeName);
if (implType == null)
{
throw new SmartCodeException($"Plugin.ImplType:{plugin.ImplTypeName} can not find!");
}
if (!pluginType.IsAssignableFrom(implType))
{
throw new SmartCodeException(
$"Plugin.ImplType:{implType.FullName} can not Impl Plugin.Type:{pluginType.FullName}!");
}
Services.AddSingleton(pluginType, implType);
}
}
public async Task Run()
{
try
{
Handlebars.Configuration.TextEncoder = NullTextEncoder.Instance;
var projectBuilder = ServiceProvider.GetRequiredService<IProjectBuilder>();
Stopwatch stopwatch = Stopwatch.StartNew();
Logger.LogInformation($"------- Build ConfigPath:{ConfigPath} Start! --------");
await projectBuilder.Build();
Logger.LogInformation(
$"-------- Build ConfigPath:[{ConfigPath}],Output:[{Project.Output?.Path}],Taken:[{stopwatch.ElapsedMilliseconds}ms] End! --------");
}
catch (SmartCodeException scEx)
{
Logger.LogError(new EventId(scEx.HResult), scEx, scEx.Message);
throw;
}
}
public class NullTextEncoder : ITextEncoder
{
public static NullTextEncoder Instance = new NullTextEncoder();
public string Encode(string value)
{
return value;
}
}
}
}