yavsc/src/cli/Program.cs

135 lines
5.1 KiB
C#
Raw Normal View History

2018-07-28 01:55:13 +02:00

using System;
2019-06-25 01:51:23 +01:00
using System.Runtime.Versioning;
2018-08-05 04:16:21 +02:00
using cli.Commands;
2019-06-25 01:51:23 +01:00
using Microsoft.AspNet.Builder.Internal;
using Microsoft.AspNet.Hosting;
using Microsoft.Dnx.Runtime;
2018-07-28 01:55:13 +02:00
using Microsoft.Extensions.CommandLineUtils;
2019-06-25 01:51:23 +01:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel;
using Microsoft.Extensions.PlatformAbstractions;
using Newtonsoft.Json;
using Yavsc;
2018-07-28 01:55:13 +02:00
namespace cli
{
2019-06-25 01:51:23 +01:00
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();
}
}
2018-07-28 01:55:13 +02:00
public partial class Program
{
2019-06-25 01:51:23 +01:00
private static IApplicationEnvironment appEnv;
public static IHostingEnvironment HostingEnvironment { get; private set; }
public Program()
2018-07-28 01:55:13 +02:00
{
2019-06-25 01:51:23 +01:00
appEnv = new CliAppEnv(PlatformServices.Default.Application);
}
2018-07-28 01:55:13 +02:00
2019-06-25 01:51:23 +01:00
/// <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();
HostingEnvironment.EnvironmentName = appEnv.Configuration;
var startup = new Startup(HostingEnvironment, appEnv);
startup.ConfigureServices(services);
services.AddInstance<IHostingEnvironment>(HostingEnvironment);
var serviceProvider = services.BuildServiceProvider();
var app = new ApplicationBuilder(serviceProvider);
app.ApplicationServices = serviceProvider;
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(app, HostingEnvironment, siteSettings, cxSettings, userCxSettings, loggerFactory);
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)
{
2018-07-28 01:55:13 +02:00
CommandLineApplication cliapp = new CommandLineApplication(false);
cliapp.Name = "cli";
cliapp.FullName = "Yavsc command line interface";
2018-07-30 11:21:57 +02:00
cliapp.Description = "Dnx console app for yavsc server side";
2018-07-28 01:55:13 +02:00
cliapp.ShortVersionGetter = () => "v1.0";
cliapp.LongVersionGetter = () => "version 1.0 (stable)";
2019-06-25 01:51:23 +01:00
// calling a Startup sequence
var appBuilder = ConfigureApplication();
var loggerFactory = appBuilder.ApplicationServices.GetRequiredService<ILoggerFactory>();
2019-06-25 17:11:16 +01:00
var cxSettings = appBuilder.ApplicationServices.GetRequiredService<IOptions<ConnectionSettings>>();
var usercxSettings = appBuilder.ApplicationServices.GetRequiredService<IOptions<UserConnectionSettings>>();
2019-06-25 01:51:23 +01:00
CommandOption rootCommandHelpOption = cliapp.HelpOption("-? | -h | --help");
2018-08-05 04:16:21 +02:00
(new SendMailCommandProvider()).Integrate(cliapp);
(new GenerateJsonSchema()).Integrate(cliapp);
2019-06-25 01:51:23 +01:00
(new AuthCommander(loggerFactory)).Integrate(cliapp);
2018-08-05 04:16:21 +02:00
(new CiBuildCommand()).Integrate(cliapp);
2019-01-08 15:00:58 +00:00
(new GenerationCommander()).Integrate(cliapp);
2019-06-25 17:11:16 +01:00
(new Streamer(loggerFactory, cxSettings, usercxSettings )).Integrate(cliapp);
2018-07-28 01:55:13 +02:00
if (args.Length == 0)
{
cliapp.ShowHint();
2019-06-25 01:51:23 +01:00
return -1;
2018-07-28 01:55:13 +02:00
}
2019-06-25 01:51:23 +01:00
var result = cliapp.Execute(args);
2018-07-28 01:55:13 +02:00
if (cliapp.RemainingArguments.Count > 0)
{
cliapp.ShowHint();
2019-06-25 01:51:23 +01:00
return -1;
2018-07-28 01:55:13 +02:00
}
2019-06-25 01:51:23 +01:00
return result;
2018-07-28 01:55:13 +02:00
}
}
}