Exemplo n.º 1
0
        private async Task <ProductVersion> InitilizeVersionAsync(Product product, PublishVersionModel versionModel)
        {
            var productVersion = product.ProductVersions.FirstOrDefault(x => x.Version.Equals(versionModel.Version, StringComparison.CurrentCultureIgnoreCase));

            if (productVersion == null)
            {
                productVersion = new ProductVersion()
                {
                    PublishTime = DateTime.Now,
                    Version     = versionModel.Version,
                    ChangeLog   = versionModel.ChangeLog
                };
                product.ProductVersions.Add(productVersion);
                await productRepository.SaveChangesAsync();
            }
            else
            {
                var prevVersion = product.ProductVersions.OrderByDescending(x => x.Id).FirstOrDefault(x => x.Id < productVersion.Id);
                productVersion.ExportedTypes.Clear();
                if (prevVersion != null)
                {
                    productVersion.QueryStartSteps   = prevVersion.QueryStartSteps.ToList();
                    productVersion.ComponentVersions = prevVersion.ComponentVersions.Select(x => new ComponentVersion()
                    {
                        Component      = x.Component,
                        ProductVersion = productVersion
                    }).ToList();
                }
                productVersion.ChangeLog = versionModel.ChangeLog;
                await productRepository.SaveChangesAsync();
            }

            return(productVersion);
        }
Exemplo n.º 2
0
        public async Task PrePublishAsync(PublishVersionModel versionModel)
        {
            try
            {
                var folder    = Path.GetRandomFileName();
                var directory = Path.Combine(AppContext.BaseDirectory, folder);
                Directory.CreateDirectory(directory);
                ZipFile.ExtractToDirectory(versionModel.FilePackage.FirstOrDefault().Id, directory);
                var subDirectories = Directory.GetDirectories(directory);
                var product        = await productRepository.GetFullProductAsync(versionModel.ProductId);

                if (product == null)
                {
                    throw new OperationException("当前产品不存在,产品ID为:" + versionModel.ProductId);
                }
                using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    var productVersion = await InitilizeVersionAsync(product, versionModel);

                    var oldComponents = productVersion.ComponentVersions.Select(x => new Repository.Model.Component()
                    {
                        ExportedTypes = x.Component.ExportedTypes
                    }).ToList();
                    var files = Directory.GetFiles(directory, "*.dll");
                    InitilizeAssemblies(productVersion, files);
                    var newExportedTypes = productVersion.ComponentVersions.SelectMany(x => x.Component.ExportedTypes).ToList();
                    foreach (var oldComponent in oldComponents)
                    {
                        var newComponent = productVersion.ComponentVersions.FirstOrDefault(x => x.Component.Id == oldComponent.Id)?.Component;
                        if (newComponent == null)
                        {
                            continue;
                        }
                        foreach (var oldExportedType in oldComponent.ExportedTypes)
                        {
                            var newExportedType = newComponent.ExportedTypes.Where(x => x.Name == oldExportedType.Name)
                                                  .FirstOrDefault(x => x.Namespace == oldExportedType.Namespace);
                            newExportedType.Component = newComponent;
                        }
                    }
                    await productRepository.SaveChangesAsync();

                    scope.Complete();
                }
            }
            finally
            {
                File.Delete(versionModel.FilePackage[0].Id);
            }
        }