yavsc/src/cli/Program.cs

142 lines
5.1 KiB
C#


using System;
7 years ago
using System.Runtime.Versioning;
7 years ago
using cli.Commands;
using Microsoft.AspNetCore.Builder.Internal;
using Microsoft.AspNetCore.Hosting;
7 years ago
using Microsoft.Dnx.Runtime;
using Microsoft.Extensions.CommandLineUtils;
7 years ago
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel;
using Microsoft.Extensions.PlatformAbstractions;
using Newtonsoft.Json;
using Yavsc;
namespace cli
{
7 years ago
public class CliAppEnv : IApplicationEnvironment
{
public CliAppEnv(IApplicationEnvironment defaultEnv)
{
var envAppVar = Environment.GetEnvironmentVariable("ASPNET_ENV");
Configuration = envAppVar ?? defaultEnv.Configuration;
ApplicationName = defaultEnv.ApplicationName;
ApplicationVersion = defaultEnv.ApplicationVersion;
ApplicationBasePath = defaultEnv.ApplicationBasePath;
RuntimeFramework = defaultEnv.RuntimeFramework;
}
public string ApplicationName { get; private set; }
public string ApplicationVersion { get; private set; }
public string ApplicationBasePath { get; private set; }
public string Configuration { get; private set; }
public FrameworkName RuntimeFramework { get; private set; }
public object GetData(string name)
{
throw new NotImplementedException();
}
public void SetData(string name, object value)
{
throw new NotImplementedException();
}
}
public partial class Program
{
7 years ago
private static IApplicationEnvironment appEnv;
public static IHostingEnvironment HostingEnvironment { get; private set; }
public Program()
{
7 years ago
appEnv = new CliAppEnv(PlatformServices.Default.Application);
}
7 years ago
/// <summary>
/// Initializes the application by
/// </summary>
private static ApplicationBuilder ConfigureApplication()
{
AppDomain.CurrentDomain.UnhandledException += OnUnHandledException;
var services = new ServiceCollection();
// create a service provider with the HostEnvironment.
HostingEnvironment = new HostingEnvironment
{
EnvironmentName = appEnv.Configuration
};
7 years ago
var startup = new Startup(HostingEnvironment, appEnv);
startup.ConfigureServices(services);
services.AddInstance<IHostingEnvironment>(HostingEnvironment);
var serviceProvider = services.BuildServiceProvider();
var app = new ApplicationBuilder(serviceProvider)
{
ApplicationServices = serviceProvider
};
7 years ago
var siteSettings = serviceProvider.GetRequiredService<IOptions<SiteSettings>>();
var cxSettings = serviceProvider.GetRequiredService<IOptions<ConnectionSettings>>();
var userCxSettings = serviceProvider.GetRequiredService<IOptions<UserConnectionSettings>>();
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
startup.Configure(cxSettings, userCxSettings, loggerFactory);
7 years ago
return app;
}
private static void OnUnHandledException(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine("Unhandled Exception occured:");
Console.WriteLine(JsonConvert.SerializeObject(e.ExceptionObject));
}
[STAThread]
public static int Main(string[] args)
{
CommandLineApplication cliapp = new CommandLineApplication(false)
{
Name = "cli",
FullName = "Yavsc command line interface",
Description = "Dnx console app for yavsc server side",
ShortVersionGetter = () => "v1.0",
LongVersionGetter = () => "version 1.0 (stable)"
};
7 years ago
// calling a Startup sequence
var appBuilder = ConfigureApplication();
var loggerFactory = appBuilder.ApplicationServices.GetRequiredService<ILoggerFactory>();
var cxSettings = appBuilder.ApplicationServices.GetRequiredService<IOptions<ConnectionSettings>>();
var usercxSettings = appBuilder.ApplicationServices.GetRequiredService<IOptions<UserConnectionSettings>>();
7 years ago
CommandOption rootCommandHelpOption = cliapp.HelpOption("-? | -h | --help");
7 years ago
new SendMailCommandProvider().Integrate(cliapp);
new GenerateJsonSchema().Integrate(cliapp);
new AuthCommander(loggerFactory).Integrate(cliapp);
new GenerationCommander().Integrate(cliapp);
new Streamer(loggerFactory, cxSettings, usercxSettings ).Integrate(cliapp);
new UserListCleanUp().Integrate(cliapp);
if (args.Length == 0)
{
cliapp.ShowHint();
7 years ago
return -1;
}
7 years ago
var result = cliapp.Execute(args);
if (cliapp.RemainingArguments.Count > 0)
{
cliapp.ShowHint();
7 years ago
return -1;
}
7 years ago
return result;
}
}
}