protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         db.Dispose();
     }
     base.Dispose(disposing);
 }
Esempio n. 2
0
        private async Task OnExecuteAsync()
        {
            var toolsOption = GetToolsOptions(ConfigFilePath);

            LoadPlugins(toolsOption.RequiredPlugins);
            LoadPatchs(toolsOption.RequiredPatchs);
            toolPlugins     = GetInstalledPlugins(toolsOption.RequiredPlugins);
            portalPatches   = GetInstalledPortalPatchs(toolsOption.RequiredPatchs);
            identityPatches = GetInstalledIdentityPatchs(toolsOption.RequiredPatchs);
            bool isRunningPlugin = !IsPortal() && !IsIdentity();

            // Check if run with mode patch
            bool isPatchRunning = false;

            if (!isRunningPlugin && !string.IsNullOrEmpty(PatchName) && Command != "install")
            {
                throw new NotSupportedException("Sorry, we only support -pn with install command.");
            }
            else if (!isRunningPlugin && !string.IsNullOrEmpty(PatchName) && Command == "install")
            {
                isPatchRunning = true;
            }


            IToolPlugin toolPlugin = isRunningPlugin ? toolPlugins.First(a => a.AppName == App) : null;
            var         services   = new ServiceCollection();

            ConventionPackDefault.Register();
            MongoDbRegistry.RegisterEntities();
            var dbType         = DatabseType.ToEnum <ConnectionType>(true);
            var databaseOption =
                new DatabaseOptions
            {
                ConnectionString = !string.IsNullOrEmpty(Connection)
                        ? Connection
                            : (isRunningPlugin ?
                               toolPlugin.LoadDefaultDatabase().ConnectionString
                                    : GetDefaultConnectionString(dbType, App, toolsOption)),
                ConnectionType = dbType
            };

            var runningCommand = GetAvailableCommands().FirstOrDefault(a => a.CommandName.ToLower() == Command.ToLower());

            if (runningCommand != null)
            {
                ToolsContext toolsContext       = null;
                var          storingConnections = toolsOption.GetByDatabaseType(dbType);
                switch (dbType)
                {
                case ConnectionType.MongoDB:
                    var mongoConnection        = new MongoConnection(databaseOption);
                    var mongoVersionContext    = new MongoVersionContext(databaseOption);
                    var versionMongoRepository = new VersionMongoRepository(mongoConnection);
                    var patchMongoRepository   = new PatchVersionMongoRepository(mongoConnection);
                    mongoVersionContext.ConnectionType           = ConnectionType.MongoDB;
                    mongoVersionContext.DatabaseOptions          = databaseOption;
                    mongoVersionContext.PortalDatabaseOptions    = toolsOption.MongoStoringConnections.PortalConnection;
                    mongoVersionContext.ServiceManagementOptions = storingConnections.ServiceManagementConnection;
                    mongoVersionContext.IdentityDbOptions        = storingConnections.IdentityConnection;
                    var latestVersion = await versionMongoRepository.GetLastestVersion(App);

                    var allVersions = Enumerable.Empty <IVersion>();
                    IServiceProvider          serviceProvider          = null;
                    PatchVersion              latestPatchVersion       = null;
                    IPortalPatchFeatureTool   portalPatchFeatureTool   = null;
                    IIdentityPatchFeatureTool identityPatchFeatureTool = null;
                    if (IsPortal())
                    {
                        RegisterServicesForPortal(services, databaseOption);
                        if (isPatchRunning)
                        {
                            latestPatchVersion = await patchMongoRepository.GetLatestAsync(App, PatchName);

                            portalPatchFeatureTool = portalPatches.First(a => a.PatchName == PatchName);
                            portalPatchFeatureTool.RegisterServices(services, configuration);
                        }
                        serviceProvider = services.BuildServiceProvider();
                        allVersions     = Scanner.GetAllPortalVersions(serviceProvider);
                    }
                    else if (IsIdentity())
                    {
                        RegisterServicesForIdentity(services, databaseOption);
                        if (isPatchRunning)
                        {
                            latestPatchVersion = await patchMongoRepository.GetLatestAsync(App, PatchName);

                            identityPatchFeatureTool = identityPatches.First(a => a.PatchName == PatchName);
                            identityPatchFeatureTool.RegisterServices(services, configuration);
                        }
                        serviceProvider = services.BuildServiceProvider();
                        allVersions     = Scanner.GetAllIdentityVersions(serviceProvider);
                    }
                    else
                    {
                        var foundPlugin = toolPlugins.First(a => a.AppName.Equals(App, StringComparison.OrdinalIgnoreCase));
                        foundPlugin.RegisterServices(services, configuration);
                        serviceProvider = services.BuildServiceProvider();
                        allVersions     = foundPlugin.GetAllVersions(serviceProvider);
                    }
                    toolsContext = new ToolsContext
                    {
                        LatestVersion          = latestVersion,
                        LatestPatchVersion     = latestPatchVersion,
                        VersionContext         = mongoVersionContext,
                        VersionNumber          = VersionNumber,
                        Versions               = allVersions,
                        VersionRepository      = versionMongoRepository,
                        PatchesFolder          = PatchesFolder,
                        AllowPatch             = !string.IsNullOrEmpty(PatchesFolder),
                        Services               = serviceProvider,
                        PatchVersionRepository = patchMongoRepository,
                        CurrentPatchPortal     = portalPatchFeatureTool,
                        CurrentIdentityPortal  = identityPatchFeatureTool,
                        Arguments              = CombineArguments()
                    };

                    break;

                case ConnectionType.PostgreSQL:
                case ConnectionType.MySQL:
                case ConnectionType.SQLServer:

#pragma warning disable CA2000 // Dispose objects before losing scope
                    var letportalContext = new SaturnFullContext(databaseOption);
#pragma warning restore CA2000 // Dispose objects before losing scope
                    letportalContext.Database.EnsureCreated();

#pragma warning disable CA2000 // Dispose objects before losing scope
                    var letportalContextForRepo = new SaturnFullContext(databaseOption);
#pragma warning restore CA2000 // Dispose objects before losing scope
                    var sqlEFVersionContext = new EFVersionContext(letportalContext)
                    {
                        ConnectionType  = dbType,
                        DatabaseOptions = databaseOption
                    };
                    var patchEFRepository       = new PatchVersionEFRepository(letportalContextForRepo);
                    var portalVersionRepository = new VersionEFRepository(letportalContextForRepo);
                    var latestVersionEF         = await portalVersionRepository.GetLastestVersion(App);

                    sqlEFVersionContext.ServiceManagementOptions = storingConnections.ServiceManagementConnection;
                    sqlEFVersionContext.IdentityDbOptions        = storingConnections.IdentityConnection;
                    sqlEFVersionContext.PortalDatabaseOptions    = storingConnections.PortalConnection;
                    var sqlAllVersions = Enumerable.Empty <IVersion>();
                    IServiceProvider          sqlServiceProvider         = null;
                    PatchVersion              latestEFPatchVersion       = null;
                    IPortalPatchFeatureTool   portalEFPatchFeatureTool   = null;
                    IIdentityPatchFeatureTool identityEFPatchFeatureTool = null;

                    if (IsPortal())
                    {
                        RegisterServicesForPortal(services, databaseOption);
                        if (isPatchRunning)
                        {
                            latestEFPatchVersion = await patchEFRepository.GetLatestAsync(App, PatchName);

                            portalEFPatchFeatureTool = portalPatches.First(a => a.PatchName == PatchName);
                            portalEFPatchFeatureTool.RegisterServices(services, configuration);
                        }
                        sqlServiceProvider = services.BuildServiceProvider();
                        sqlAllVersions     = Scanner.GetAllPortalVersions(sqlServiceProvider);
                    }
                    else if (IsIdentity())
                    {
                        RegisterServicesForIdentity(services, databaseOption);
                        if (isPatchRunning)
                        {
                            latestEFPatchVersion = await patchEFRepository.GetLatestAsync(App, PatchName);

                            identityEFPatchFeatureTool = identityPatches.First(a => a.PatchName == PatchName);
                            identityEFPatchFeatureTool.RegisterServices(services, configuration);
                        }
                        sqlServiceProvider = services.BuildServiceProvider();
                        sqlAllVersions     = Scanner.GetAllIdentityVersions(sqlServiceProvider);
                    }
                    else
                    {
                        var foundPlugin = toolPlugins.First(a => a.AppName.Equals(App, StringComparison.OrdinalIgnoreCase));
                        foundPlugin.RegisterServices(services, configuration);
                        sqlServiceProvider = services.BuildServiceProvider();
                        sqlAllVersions     = foundPlugin.GetAllVersions(sqlServiceProvider);
                    }
                    toolsContext = new ToolsContext
                    {
                        LatestVersion          = latestVersionEF,
                        VersionContext         = sqlEFVersionContext,
                        VersionNumber          = VersionNumber,
                        Versions               = sqlAllVersions,
                        VersionRepository      = portalVersionRepository,
                        AllowPatch             = !string.IsNullOrEmpty(PatchesFolder),
                        Services               = sqlServiceProvider,
                        Arguments              = CombineArguments(),
                        PatchesFolder          = PatchesFolder,
                        PatchVersionRepository = patchEFRepository,
                        CurrentPatchPortal     = portalEFPatchFeatureTool,
                        CurrentIdentityPortal  = identityEFPatchFeatureTool
                    };

                    break;
                }

                if (toolsContext != null)
                {
                    Console.WriteLine("");
                    await runningCommand.RunAsync(toolsContext);

                    toolsContext.Dispose();
                }

                Console.WriteLine("-----------------------++++++DONE++++++-------------------------");
            }
            else
            {
                Console.WriteLine("Oops! We don't find any matching command to execute. If you don't know how to run, please type '--help'");
            }

            Console.ReadLine();
        }