Exemple #1
0
        protected override async Task <bool> OnUpgrade(ZipArchive archive)
        {
            var archiveEntries = archive.Entries.ToArray();

            foreach (var zipArchiveEntry in archiveEntries)
            {
                if (zipArchiveEntry.Name != "save_log.json" || zipArchiveEntry.FullName.Contains("-backup-"))
                {
                    continue;
                }

                SaveLogEntry[] saveLogs;
                await using (var saveLogStream = zipArchiveEntry.Open())
                    saveLogs = await JsonSerializer.DeserializeAsync <SaveLogEntry[]>(saveLogStream);

                var changelogEntries = new ChangelogEntry[saveLogs.Length];
                for (var i = 0; i < saveLogs.Length; i++)
                {
                    changelogEntries[i] = new ChangelogEntry
                    {
                        Username     = saveLogs[i].Username,
                        ComputerName = saveLogs[i].ComputerName,
                        Time         = saveLogs[i].Time,
                        Type         = saveLogs[i].AutoSave == true ? ChangelogItemType.AutoSave : ChangelogItemType.Save
                    };
                }

                var basePath       = zipArchiveEntry.FullName.Replace(zipArchiveEntry.Name, "");
                var changelogEntry = archive.CreateEntry(basePath + "changelog.json");
                await using (var saveLogStream = changelogEntry.Open())
                {
                    await JsonSerializer.SerializeAsync(saveLogStream, changelogEntries);
                }

                zipArchiveEntry.Delete();
            }

            var fileVersionFile = archiveEntries.FirstOrDefault(f => f.FullName == "file_version");

            fileVersionFile?.Delete();

            return(true);
        }
Exemple #2
0
        public void  SyncChangelogs()
        {
            var imageLock = GetImageLock();

            _logger.LogInformation("Updating the package cache...");
            _aptGetService.Update();

            // Download the packages.
            _logger.LogInformation("Downloading the debs...");
            var debFolder = Path.Combine(_workspaceConfig.RootDirectory, ".debs");

            debFolder.EnsureDirectoryExists();
            _aptGetService.Download(imageLock.InstalledPackages.ToDictionary(x => x.Key, x => x.Value.Version), debFolder);

            var tmpChangelogDirectory = Path.Combine(_workspaceConfig.RootDirectory, ".tmp-changelog");

            tmpChangelogDirectory.CleanOrCreateDirectory();

            var tmpExtractionDirectory = Path.Combine(tmpChangelogDirectory, "tmp");

            Directory.CreateDirectory(tmpExtractionDirectory);

            foreach (var package in imageLock.InstalledPackages.Keys)
            {
                var installedPackage = imageLock.InstalledPackages[package];
                var debFile          = Path.Combine(debFolder, $"{package}_{installedPackage.Version.Version.Replace(":", "%3a")}_{installedPackage.Version.Architecture}.deb");
                if (!File.Exists(debFile))
                {
                    throw new Exception($"The deb file {debFile} doesn't exist.");
                }

                _logger.LogInformation("Extracting: {package}", imageLock.InstalledPackages[package].Version.ToCommandParameter(package));

                _dpkgService.Extract(debFile, tmpExtractionDirectory, false);
            }

            _logger.LogInformation("Finding changelogs...");
            var docsDirectory    = Path.Combine(tmpExtractionDirectory, "usr", "share", "doc");
            var changelogEntries = new List <ChangelogEntry>();

            foreach (var packageDirectoryPath in Directory.GetDirectories(docsDirectory))
            {
                var packageDirectoryName = Path.GetFileName(packageDirectoryPath);

                _logger.LogInformation("Finding changelog for {package}...", packageDirectoryName);

                string FindChangelog(string baseName)
                {
                    var debianName = baseName + ".Debian";

                    if (File.Exists(debianName))
                    {
                        return(debianName);
                    }
                    debianName += ".gz";
                    if (File.Exists(debianName))
                    {
                        return(debianName);
                    }
                    if (File.Exists(baseName))
                    {
                        return(baseName);
                    }
                    baseName += ".gz";
                    if (File.Exists(baseName))
                    {
                        return(baseName);
                    }
                    return(null);
                }

                var changelogPath = FindChangelog(Path.Combine(packageDirectoryPath, "changelog"));
                if (string.IsNullOrEmpty(changelogPath))
                {
                    _logger.LogWarning("Couldn't find changelog in {package} doc directory.", packageDirectoryName);
                    continue;
                }

                var changelogEntry = new ChangelogEntry();
                changelogEntry.MD5           = _processRunner.ReadShell($"md5sum {changelogPath.Quoted()}");
                changelogEntry.MD5           = changelogEntry.MD5.Substring(0, changelogEntry.MD5.IndexOf(" ", StringComparison.Ordinal));
                changelogEntry.SourcePackage = _processRunner.ReadShell($"dpkg-parsechangelog -S Source -l {changelogPath.Quoted()}").TrimEnd(Environment.NewLine.ToCharArray());

                var existing = changelogEntries.SingleOrDefault(x =>
                                                                x.MD5 == changelogEntry.MD5 && x.SourcePackage == changelogEntry.SourcePackage);
                if (existing != null)
                {
                    _logger.LogWarning("Changelog already exists for {packge} via {sourcePackage}", packageDirectoryName, changelogEntry.SourcePackage);
                    continue;
                }

                var destination = Path.Combine(tmpChangelogDirectory, changelogEntry.SourcePackage);
                _processRunner.RunShell($"dpkg-parsechangelog -S Changes --all -l {changelogPath.Quoted()} > {destination.Quoted()}");

                _logger.LogInformation("Saved changelog for {package}.", packageDirectoryName);
            }

            if (Directory.Exists(tmpExtractionDirectory))
            {
                Directory.Delete(tmpExtractionDirectory, true);
            }
            var changelogDirectory = Path.Combine(_workspaceConfig.RootDirectory, "changelogs");

            if (Directory.Exists(changelogDirectory))
            {
                Directory.Delete(changelogDirectory, true);
            }
            Directory.Move(tmpChangelogDirectory, changelogDirectory);

            _logger.LogInformation("Done!");
        }