Beispiel #1
0
        private void GenerateDeltas(VersionManifest manifest, string sourceDirectory, string destionationDirectory, string newVersion, int numberOfVersionsAsDelta)
        {
            string newVersionDirectory = Path.Combine(destionationDirectory, newVersion);
            var publishedVersions = Directory.EnumerateDirectories(destionationDirectory)
                                             .Select(x => x.Remove(0, destionationDirectory.Length))
                                             .Except(new[] { newVersion })
                                             .OrderByDescending(x => x)
                                             .Take(numberOfVersionsAsDelta);

            foreach (var version in publishedVersions)
            {
                string versionDirectory = Path.Combine(destionationDirectory, version);
                string manifestFile = Path.Combine(versionDirectory, "manifest.xml");
                VersionManifest versionManifest = VersionManifest.LoadVersionFile(version, manifestFile);
                foreach (var file in manifest.Files)
                {
                    VersionManifestFile fileInVersion = versionManifest.Files.FirstOrDefault(x => x.Name.Equals(file.Name, StringComparison.CurrentCultureIgnoreCase));
                    if (fileInVersion != null && fileInVersion.Checksum != file.Checksum)
                    {
                        if (file.GetDeltaFrom(fileInVersion.Checksum) == null)
                        {
                            string oldFile = Path.Combine(versionDirectory, fileInVersion.Name + ".deploy");
                            string decompressedOldFile = Path.GetTempFileName();
                            byte[] data = File.ReadAllBytes(oldFile);
                            data = DataCompressor.Decompress(data);
                            File.WriteAllBytes(decompressedOldFile, data);

                            string decompressedNewFile = Path.Combine(sourceDirectory, fileInVersion.Name);
                            string deltaFilename = String.Format("deltas\\{0}.{1}.deploy", fileInVersion.Name, GetShortChecksum(fileInVersion.Checksum));
                            string deltaFile = Path.Combine(newVersionDirectory, deltaFilename);
                            Directory.CreateDirectory(Path.GetDirectoryName(deltaFile));
                            DeltaAPI.CreateDelta(decompressedOldFile, decompressedNewFile, deltaFile, true);
                            File.Delete(decompressedOldFile);

                            VersionManifestDeltaFile deltaInfo = new VersionManifestDeltaFile(deltaFilename, fileInVersion.Checksum, new FileInfo(deltaFile).Length);
                            file.Deltas.Add(deltaInfo);
                        }
                    }
                }
            }
        }
        public UpdateRecipe UpdateTo(VersionManifest newVersionManifest)
        {
            List<UpdateRecipeFile> recipeFiles = new List<UpdateRecipeFile>();
            foreach (var file in newVersionManifest.Files)
            {
                VersionManifestFile originalFile = this.Files.FirstOrDefault(x => x.Name.Equals(file.Name, StringComparison.CurrentCultureIgnoreCase));
                FileUpdateAction action = FileUpdateAction.Download;
                string fileToDownload = file.DeployedName;
                long size = file.Size;
                if (originalFile != null)
                {
                    if (originalFile.Checksum == file.Checksum)
                    {
                        action = FileUpdateAction.Copy;
                    }
                    else if (DeltaAPI.IsSupported())
                    {
                        VersionManifestDeltaFile delta = file.GetDeltaFrom(originalFile.Checksum);
                        if (delta != null)
                        {
                            action = FileUpdateAction.DownloadDelta;
                            fileToDownload = delta.Filename;
                            size = delta.Size;
                        }
                    }
                }

                recipeFiles.Add(new UpdateRecipeFile(file.Name, file.Checksum, size, action, fileToDownload));
            }

            return new UpdateRecipe(newVersionManifest.Version, this.Version, recipeFiles);
        }
        public void UpdateTo_WithAnIncorrectDelta_SetTheActionAsDownload()
        {
            VersionManifestFile fileOriginal = new VersionManifestFile("arquivo1.txt", "333", 1000);
            VersionManifestFile fileUpdate = new VersionManifestFile("arquivo1.txt", "123", 1000);
            fileUpdate.Deltas.Add(new VersionManifestDeltaFile("deltas\\arquivo1.txt", "444", 10));
            VersionManifest currentManifest = new VersionManifest("1.0.0", new VersionManifestFile[] { fileOriginal });
            VersionManifest updateManifest = new VersionManifest("2.0.0", new VersionManifestFile[] { fileUpdate });

            UpdateRecipe recipe = currentManifest.UpdateTo(updateManifest);

            Assert.That(recipe.Files, Has.Count.EqualTo(1));
            Assert.That(recipe.Files.First().Action, Is.EqualTo(FileUpdateAction.Download));
        }
        public void UpdateTo_VersionWithTheFileWithIncorrectChecksum_SetTheActionAsDownload()
        {
            VersionManifestFile fileOriginal = new VersionManifestFile("arquivo1.txt", "333", 1000);
            VersionManifestFile fileUpdate = new VersionManifestFile("arquivo1.txt", "123", 1000);
            VersionManifest currentManifest = new VersionManifest("1.0.0", new VersionManifestFile[] { fileOriginal });
            VersionManifest updateManifest = new VersionManifest("2.0.0", new VersionManifestFile[] { fileUpdate });

            UpdateRecipe recipe = currentManifest.UpdateTo(updateManifest);

            Assert.That(recipe.Files, Has.Count.EqualTo(1));
            Assert.That(recipe.Files.First().Action, Is.EqualTo(FileUpdateAction.Download));
        }
        public void UpdateTo_VersionWithEqualFile_SetTheActionAsCopy()
        {
            VersionManifestFile fileUpdate = new VersionManifestFile("arquivo1.txt", "123", 1000);
            VersionManifest currentManifest = new VersionManifest("1.0.0", new VersionManifestFile[] { fileUpdate });
            VersionManifest updateManifest = new VersionManifest("2.0.0", new VersionManifestFile[] { fileUpdate });

            UpdateRecipe recipe = currentManifest.UpdateTo(updateManifest);

            Assert.That(recipe.Files, Has.Count.EqualTo(1));
            Assert.That(recipe.Files.First().Action, Is.EqualTo(FileUpdateAction.Copy));
        }
        public void UpdateTo_ReturnsARecipe()
        {
            VersionManifestFile fileUpdate = new VersionManifestFile("arquivo1.txt", "123", 1000);
            VersionManifest currentManifest = new VersionManifest("1.0.0", new VersionManifestFile[] {  });
            VersionManifest updateManifest = new VersionManifest("2.0.0", new VersionManifestFile[] { fileUpdate });

            UpdateRecipe recipe = currentManifest.UpdateTo(updateManifest);

            Assert.That(recipe, Is.Not.Null);
            Assert.That(recipe.CurrentVersion, Is.EqualTo("1.0.0"));
            Assert.That(recipe.NewVersion, Is.EqualTo("2.0.0"));
            Assert.That(recipe.Files, Has.Count.EqualTo(1));
        }