A commande line interface

This commit is contained in:
2018-04-12 13:27:49 +02:00
parent 6b3302568f
commit fdb1c550e3
12 changed files with 14405 additions and 637 deletions

View File

@ -1,124 +1,108 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Identity;
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.AspNet.Builder;
using System.Runtime.Versioning;
using Microsoft.AspNet.Builder.Internal;
using Yavsc.Services;
using Google.Apis.Util.Store;
using cli_2;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNet.Hosting;
using Yavsc.Models;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Extensions.OptionsModel;
using Microsoft.AspNet.Hosting.Builder;
namespace cli
{
using Yavsc;
using Microsoft.AspNet.Hosting.Internal;
public class Program
public class Program
{
private readonly EventLog _log =
new EventLog("Application") { Source = "Application" };
private IServiceProvider serviceProvider;
private static Startup startup;
public Program()
{
ConfigureServices(new ServiceCollection());
}
private void ConfigureServices(IServiceCollection services, string environmentName="Development")
{
}
public static void Main(string[] args)
{
var prog = new Program();
// Program.exe <-g|--greeting|-$ <greeting>> [name <fullname>]
// [-?|-h|--help] [-u|--uppercase]
CommandLineApplication commandLineApplication =
new CommandLineApplication(throwOnUnexpectedArg: false);
CommandArgument names = null;
commandLineApplication.Command("name",
(target) =>
names = target.Argument(
"fullname",
"Enter the full name of the person to be greeted.",
multipleValues: true));
CommandOption greeting = commandLineApplication.Option(
"-$|-g |--greeting <greeting>",
"The greeting to display. The greeting supports"
+ " a format string where {fullname} will be "
+ "substituted with the full name.",
CommandOptionType.SingleValue);
CommandOption envNameOption = commandLineApplication.Option(
"-e |--environment <environment>","The environment to run against.",CommandOptionType.SingleValue);
CommandOption uppercase = commandLineApplication.Option(
"-u | --uppercase", "Display the greeting in uppercase.",
CommandOptionType.NoValue);
commandLineApplication.HelpOption("-? | -h | --help");
commandLineApplication.OnExecute(() =>
{
string greetingString = "Hello!";
string environmentName = "Production";
if (greeting.HasValue())
{
string greetingStringTemplate = greeting.Value();
var fullname = string.Join(" ", commandLineApplication.RemainingArguments);
greetingString = greetingStringTemplate.Replace("{fullname}", fullname);
}
if (envNameOption.HasValue()){
environmentName = envNameOption.Value();
}
Greet(greetingString, environmentName);
return 0;
});
commandLineApplication.Execute(args);
}
private static void Greet(
string greeting, string environmentName)
{
Console.WriteLine(greeting);
IServiceCollection services = new ServiceCollection();
// Startup.cs finally :)
//EntryPoint.Main(new string[] {});
IHostingEnvironment hosting = new HostingEnvironment{ EnvironmentName = environmentName };
services.AddLogging();
services.AddOptions();
// Add application services.
services.AddTransient<IEmailSender, MessageSender>();
services.AddTransient<IGoogleCloudMessageSender, MessageSender>();
services.AddTransient<IBillingService, BillingService>();
services.AddTransient<IDataStore, FileDataStore>( (sp) => new FileDataStore("googledatastore",false) );
services.AddTransient<ICalendarManager, CalendarManager>();
// TODO for SMS: services.AddTransient<ISmsSender, AuthMessageSender>();
services.AddLocalization(options =>
{
options.ResourcesPath = "Resources";
});
services.AddIdentity<ApplicationUser,IdentityRole>();
var basePath = AppDomain.CurrentDomain.BaseDirectory;
// FIXME null ref var appName = AppDomain.CurrentDomain.ApplicationIdentity.FullName;
// var rtdcontext = new System.Runtime.DesignerServices.WindowsRuntimeDesignerContext (new string { "." }, "nonname");
// hosting.Initialize("approot", config);
serviceProvider = services.BuildServiceProvider();
// ApplicationHostContext apphostcontext = new ApplicationHostContext ();
IServiceProvider serviceProvider = services.BuildServiceProvider();
IApplicationBuilder iappbuilder = new ApplicationBuilder(serviceProvider);
iappbuilder.ApplicationServices = serviceProvider;
var projectRoot = "/home/paul/workspace/yavsc/Yavsc";
hosting.Initialize (projectRoot, null);
var targetFramework = new FrameworkName ("dnx",new Version(4,5,1));
//
//
ApplicationEnvironment appEnv = new ApplicationEnvironment(targetFramework, projectRoot);
//configure console logging
// needs a logger factory ...
var loggerFactory = serviceProvider
.GetService<ILoggerFactory>()
.AddConsole(LogLevel.Debug);
startup = new Startup (hosting, appEnv);
startup.ConfigureServices(services);
Startup startup = new Startup(hosting, PlatformServices.Default.Application);
ApplicationBuilderFactory applicationBuilderFactory
= new ApplicationBuilderFactory(serviceProvider);
var builder = applicationBuilderFactory.CreateBuilder(null);
//configure console logging
serviceProvider
.GetService<ILoggerFactory>()
.AddConsole(LogLevel.Debug);
var logger = serviceProvider.GetService<ILoggerFactory>()
.CreateLogger<Program>();
logger.LogDebug("Logger is working!");
// Get Service and call method
var userManager = serviceProvider.GetService<UserManager<ApplicationUser> >();
var emailSender = serviceProvider.GetService<IEmailSender>();
foreach (var user in userManager?.Users)
{
Task.Run(async () =>
await emailSender.SendEmailAsync(Startup.SiteSetup, Startup.SmtpSettup, Startup.SiteSetup.Owner.Name, Startup.SiteSetup.Owner.EMail,
$"[{Startup.SiteSetup.Title}] Rappel de votre inscription ({user.UserName})", $"{user.Id}/{user.UserName}/{user.Email}"));
}
IOptions<SiteSettings> siteSettings = serviceProvider.GetService(typeof(IOptions<SiteSettings>)) as IOptions<SiteSettings>;
IOptions<SmtpSettings> smtpSettings = serviceProvider.GetService(typeof(IOptions<SmtpSettings>)) as IOptions<SmtpSettings>;
startup.Configure(builder,siteSettings,smtpSettings);
}
public static void Main(string[] args)
{
Console.WriteLine($"Hello world!" );
foreach (var str in args) {
Console.WriteLine($"*> {str}");
}
startup.Main(args);
}
}
}