|
| 1 | +using System; |
| 2 | +using System.Net; |
| 3 | +using System.Threading; |
| 4 | +using System.Windows.Forms; |
| 5 | +using KFlearning.App.Resources; |
| 6 | +using KFlearning.App.Services; |
| 7 | +using KFlearning.App.Views; |
| 8 | +using KFlearning.Core.Extensions; |
| 9 | +using KFlearning.Core.Services; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using Serilog; |
| 12 | + |
| 13 | +namespace KFlearning.App |
| 14 | +{ |
| 15 | + internal static class Program |
| 16 | + { |
| 17 | + private const int MutexTimeout = 1000; |
| 18 | + private const string MutexName = "KFlearning.SingleInstanceGuard"; |
| 19 | + |
| 20 | + public static ServiceProvider Container { get; private set; } = null!; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// The main entry point for the application. |
| 24 | + /// </summary> |
| 25 | + [STAThread] |
| 26 | + static void Main() |
| 27 | + { |
| 28 | + //try |
| 29 | + //{ |
| 30 | + using var mutex = new Mutex(true, MutexName); |
| 31 | + if (!mutex.WaitOne(MutexTimeout)) |
| 32 | + { |
| 33 | + MessageBox.Show(MessagesText.SingleInstanceError, MessagesText.AppName, |
| 34 | + MessageBoxButtons.OK, MessageBoxIcon.Exclamation); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + // install services |
| 39 | + RegisterLogger(); |
| 40 | + RegisterServices(); |
| 41 | + |
| 42 | + // find vscode |
| 43 | + var path = Container.GetRequiredService<IPathManager>(); |
| 44 | + if (!path.IsVscodeInstalled) |
| 45 | + { |
| 46 | + Log.Debug("Visual Studio Code not found"); |
| 47 | + MessageBox.Show(MessagesText.VSCodeNotInstalled, MessagesText.AppName, |
| 48 | + MessageBoxButtons.OK, MessageBoxIcon.Exclamation); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + // find mingw |
| 53 | + if (!path.IsKfMingwInstalled) |
| 54 | + { |
| 55 | + Log.Debug("KF-MinGW not found"); |
| 56 | + MessageBox.Show(MessagesText.KFmingwNotInstalled, MessagesText.AppName, |
| 57 | + MessageBoxButtons.OK, MessageBoxIcon.Exclamation); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // enable TLS |
| 62 | + Log.Debug("Enabling TLS support"); |
| 63 | + NetworkHelpers.EnableTls(); |
| 64 | + |
| 65 | + // app exit handler |
| 66 | + Application.ApplicationExit += Application_ApplicationExit; |
| 67 | + |
| 68 | + // bootstrapper |
| 69 | + Log.Debug("Bootstrapping application"); |
| 70 | + |
| 71 | + ApplicationConfiguration.Initialize(); |
| 72 | + |
| 73 | + Application.EnableVisualStyles(); |
| 74 | + Application.SetCompatibleTextRenderingDefault(false); |
| 75 | + Application.Run(Container.GetRequiredService<KFlearningApplicationContext>()); |
| 76 | + //} |
| 77 | + //catch (Exception e) |
| 78 | + //{ |
| 79 | + // Log.Fatal("Application shutdown unexpectedly", e); |
| 80 | + // Log.CloseAndFlush(); |
| 81 | + |
| 82 | + // MessageBox.Show(MessagesText.FatalShutdown, MessagesText.AppName, |
| 83 | + // MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 84 | + //} |
| 85 | + } |
| 86 | + |
| 87 | + private static void RegisterLogger() |
| 88 | + { |
| 89 | + Log.Logger = new LoggerConfiguration() |
| 90 | + .MinimumLevel.Debug() |
| 91 | + .WriteTo.File("logs/kflearning-.txt", rollingInterval: RollingInterval.Day) |
| 92 | + .CreateLogger(); |
| 93 | + } |
| 94 | + |
| 95 | + private static void RegisterServices() |
| 96 | + { |
| 97 | + var services = new ServiceCollection(); |
| 98 | + |
| 99 | + // register views and presenters |
| 100 | + services.AddTransient<StartupView>(); |
| 101 | + services.AddTransient<StartupViewPresenter>(); |
| 102 | + services.AddTransient<AboutView>(); |
| 103 | + services.AddTransient<CreateProjectView>(); |
| 104 | + services.AddTransient<CreateProjectViewPresenter>(); |
| 105 | + services.AddTransient<FlutterInstallView>(); |
| 106 | + services.AddTransient<FlutterInstallViewPresenter>(); |
| 107 | + |
| 108 | + // register services |
| 109 | + services.AddSingleton<KFlearningApplicationContext>(); |
| 110 | + services.AddSingleton<IHistoryService, HistoryService>(); |
| 111 | + |
| 112 | + services.AddSingleton<IPathManager, PathManager>(); |
| 113 | + services.AddSingleton<IProcessManager, ProcessManager>(); |
| 114 | + services.AddSingleton<IProjectService, ProjectService>(); |
| 115 | + services.AddSingleton<ITemplateService, TemplateService>(); |
| 116 | + services.AddSingleton<IPersistanceStorage, PersistanceStorage>(); |
| 117 | + services.AddSingleton<IFlutterInstallService, FlutterInstallService>(); |
| 118 | + services.AddSingleton<IVisualStudioCodeService, VisualStudioCodeService>(); |
| 119 | + |
| 120 | + // register clients |
| 121 | + services.AddTransient<WebClient>(); |
| 122 | + |
| 123 | + // setup logging |
| 124 | + services.AddLogging(configure => |
| 125 | + { |
| 126 | + configure.AddSerilog(); |
| 127 | + }); |
| 128 | + |
| 129 | + Container = services.BuildServiceProvider(); |
| 130 | + } |
| 131 | + |
| 132 | + private static void Application_ApplicationExit(object? sender, EventArgs e) |
| 133 | + { |
| 134 | + try |
| 135 | + { |
| 136 | + foreach (var usesPersistence in Container.GetServices<IUsesPersistence>()) |
| 137 | + { |
| 138 | + usesPersistence.Save(); |
| 139 | + } |
| 140 | + } |
| 141 | + catch (Exception ex) |
| 142 | + { |
| 143 | + Log.Error("Cannot save persistence", ex); |
| 144 | + } |
| 145 | + |
| 146 | + Container.Dispose(); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments