-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathSmartCodeApp.cs
More file actions
112 lines (106 loc) · 4.26 KB
/
SmartCodeApp.cs
File metadata and controls
112 lines (106 loc) · 4.26 KB
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
using System;
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;
namespace SmartCode.App
{
public class SmartCodeApp
{
public SmartCodeOptions SmartCodeOptions { get; }
public String AppDirectory { get { return AppDomain.CurrentDomain.BaseDirectory; } }
public IConfigBuilder ConfigBuilder { get; private set; }
public IServiceCollection Services { get { return SmartCodeOptions.Services; } }
public IServiceProvider ServiceProvider { get; private set; }
public Project Project { get; private set; }
public string ConfigPath { get { return 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>();
Services.AddSingleton<IProjectBuilder, ProjectBuilder>();
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>();
Logger.LogInformation($"------- Build ConfigPath:{ConfigPath} Start! --------");
await projectBuilder.Build();
Logger.LogInformation($"-------- Build ConfigPath:{ConfigPath},Output:{Project.Output?.Path} End! --------");
}
catch (SmartCodeException scEx)
{
Logger.LogError(new EventId(scEx.HResult), scEx, scEx.Message);
}
}
public class NullTextEncoder : ITextEncoder
{
public static NullTextEncoder Instance = new NullTextEncoder();
public string Encode(string value)
{
return value;
}
}
}
}