public void Initialize(Stream stream)
        {
            _zipStream      = stream;
            _tempFolderName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{ Guid.NewGuid().ToString()}");
            ZipTool archive = new ZipTool(_zipStream, ZipArchiveMode.Read);

            archive.ExtractToDirectory(_tempFolderName);

            DirectoryInfo folder = new DirectoryInfo(_tempFolderName);

            FileInfo[] files = folder.GetFiles();
            var        entryAssemblyFileName = Path.GetFileNameWithoutExtension(_packagePath).ToLower() + ".dll";
            FileInfo   configFile            = files.SingleOrDefault(p => p.Name.ToLower() == entryAssemblyFileName);

            if (configFile == null)
            {
                throw new QStack.Framework.Core.ServiceFrameworkException("can not find the entry assembly.the package name must be same as the entry assembly.");
            }
            else
            {
                FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(configFile.FullName);

                _pluginInfo = new PluginInfoDto
                {
                    DisplayName = fileVersionInfo.FileDescription,
                    Version     = fileVersionInfo.FileVersion,
                    Name        = Path.GetFileNameWithoutExtension(fileVersionInfo.FileName)
                };
            }
        }
        private async Task InitializePlugin(PluginInfoDto pluginInfo)
        {
            PluginInfo plugin = Mapper.Map <PluginInfo>(pluginInfo);

            _mvcModuleSetup.LoadModule(pluginInfo.Name);
            await Daos.CurrentDao.AddAsync(plugin);;
        }
        public async Task UpgradePlugin(PluginInfoDto newPlugin, PluginInfoDto oldPlugin)
        {
            if (oldPlugin.IsEnable)
            {
                throw new InvalidOperationException("the plugin is running.please stop it before upgradePlugin");
            }

            _mvcModuleSetup.ReLoadModule(newPlugin.Name);
            var model = await Daos.CurrentDao.Get <PluginInfo>(oldPlugin.Id);

            model.Version = newPlugin.Version;
            await Daos.CurrentDao.Flush();

            //await Daos.CurrentDao.Update<PluginInfo>(p => p.Id == oldPlugin.Id, p => new PluginInfo { Version = newPlugin.Version });
        }
        public bool UnZipPackage(string zipFile, out PluginInfoDto pluginInfoDto)
        {
            pluginInfoDto = null;
            using (FileStream fs = new FileStream(zipFile, FileMode.Open))
            {
                DirectoryInfo folder = null;
                try
                {
                    ZipTool archive = new ZipTool(fs, ZipArchiveMode.Read);
                    fs.Position = 0;
                    var pluginName = Path.GetFileNameWithoutExtension(zipFile);
                    var destFolder = Path.Combine(_baseDirectory, _pluginOptions.InstallBasePath, $"{pluginName}");


                    archive.ExtractToDirectory(destFolder, true);

                    folder = new DirectoryInfo(destFolder);

                    FileInfo[] files = folder.GetFiles();
                    var        entryAssemblyFileName = Path.GetFileNameWithoutExtension(zipFile).ToLower() + ".dll";
                    FileInfo   configFile            = files.SingleOrDefault(p => p.Name.ToLower() == entryAssemblyFileName);

                    if (configFile == null)
                    {
                        throw new QStack.Framework.Core.ServiceFrameworkException("can not find the entry assembly.the package name must be same as the entry assembly.");
                    }
                    else
                    {
                        FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(configFile.FullName);

                        pluginInfoDto = new PluginInfoDto
                        {
                            DisplayName = fileVersionInfo.FileDescription,
                            Version     = fileVersionInfo.FileVersion,
                            Name        = Path.GetFileNameWithoutExtension(fileVersionInfo.FileName)
                        };
                    }
                    pluginInfoDto.IntallPath = Path.GetRelativePath(_baseDirectory, destFolder);

                    return(true);
                }
                catch
                {
                    folder?.Delete(true);
                    return(false);
                }
            }
        }
        public async Task AddPlugins(PluginInfoDto plugininfo)
        {
            PluginInfoDto existedPlugin = await this.Get <PluginInfoDto>(p => p.Name == plugininfo.Name);

            if (existedPlugin == null)
            {
                await InitializePlugin(plugininfo);
            }
            else if (new Version(plugininfo.Version) > new Version(existedPlugin.Version))
            {
                await UpgradePlugin(plugininfo, existedPlugin);
            }
            else if (new Version(plugininfo.Version) == new Version(existedPlugin.Version))
            {
                throw new ArgumentException("The package version is same as the current plugin version.");
            }
            else
            {
                await DegradePlugin(plugininfo, existedPlugin);
            }
            if (plugininfo.IsMigration)
            {
                _autoMigration.GenerateMigrations();
            }
            var pluginContext = _puginsAssemblyLoadContexts.Get(plugininfo.Name).PluginContext;

            if (pluginContext.TestUrl?.Any() == true)
            {
                var model = await Daos.CurrentDao.SingleAsync <PluginInfo>(p => p.Name == plugininfo.Name);

                model.TestUrl   = pluginContext.TestUrl;
                model.RouteArea = pluginContext.RouteArea;
                await Daos.CurrentDao.Flush();

                //await Daos.CurrentDao.Update<PluginInfo>(p => p.Name == plugininfo.Name, p => new PluginInfo { TestUrl = pluginContext.TestUrl, RouteArea = pluginContext.RouteArea });
            }
        }