public async Task <(string description, string commitId)> AddSaveAsync(string savePath, string filename)
        {
            var fi = new FileInfo(savePath);

            if (fi.Length == 0)
            {
                throw new ArgumentException("Save is empty");
            }

            var historyDir = await HistoryDirFromSavePathAsync(savePath, filename);

            ZipFile.ExtractToDirectory(savePath, historyDir, true);

            string ReadSingleEntry(IReadOnlyCollection <object> col)
            => col.Cast <string>().Single().Trim('"');

            var    saveMeta        = Path.Combine(historyDir, "meta");
            var    sg              = new LibCK2.SaveGame(File.ReadAllText(saveMeta)).GameState;
            string gameDescription = $"[{ReadSingleEntry(sg["date"])}] {ReadSingleEntry(sg["player_name"])}";

            var corgit = new Corgit(GitPath, historyDir);
            await corgit.AddAsync(); //stage all

            var result = await corgit.CommitAsync(gameDescription);

            return(gameDescription, result.Output);
        }
Beispiel #2
0
        private async ValueTask <string> InitializeHistoryDirectoryAsync(string filename)
        {
            var saveName = Path.GetFileNameWithoutExtension(filename);
            var path     = Path.Join(BaseDirectory, Prefix + saveName);

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);

                var corgit = new Corgit(GitPath, path);
                await corgit.InitAsync();

                await corgit.ConfigAsync("user.name", value : "ironmunge");

                await corgit.ConfigAsync("user.email", value : "@v0.1.0");

                await corgit.ConfigAsync("push.default", value : "current");

                //unset text to disable eol conversions
                var gitattributesPath = Path.Join(path, ".gitattributes");
                await File.WriteAllLinesAsync(gitattributesPath, DefaultGitAttributes);

                await corgit.AddAsync();

                await corgit.CommitAsync("Initialize save history");
            }

            return(path);
        }
        private async Task InitializeHistoryDirectoryAsync(string path)
        {
            Directory.CreateDirectory(path);

            var corgit = new Corgit(GitPath, path);
            await corgit.InitAsync();

            await corgit.ConfigAsync("user.name", value : "ironmunge");

            await corgit.ConfigAsync("user.email", value : "@v0.1");
        }
Beispiel #4
0
        private async ValueTask AddGitSaveAsync(string gameDescription, string historyDir)
        {
            var corgit = new Corgit(GitPath, historyDir);
            await corgit.AddAsync(); //stage all

            var statuses = (await corgit.StatusAsync())
                           .Select(gfs => gfs.Path);

            if (statuses.Any())
            {
                var result = await corgit.CommitAsync(gameDescription);

                if (result.ExitCode == 0 && !string.IsNullOrEmpty(Remote))
                {
                    await GitPushToRemoteAsync(corgit);
                }
            }
        }
Beispiel #5
0
        //private async ValueTask<string> RunMungersAsync(string historyDir, string gameDescription, JsonDocument save, JsonDocument meta)
        //{
        //    var mungerTasks = (from munger in Mungers
        //                       let mungerProgress = new Progress<string>(s => Console.WriteLine($"[{munger.Name}] {s}"))
        //                       select munger.MungeAsync(historyDir, (save, meta), mungerProgress)).ToArray();

        //    foreach (var task in mungerTasks)
        //    {
        //        var extendedDescription = await task;
        //        if (!string.IsNullOrWhiteSpace(extendedDescription))
        //        {
        //            gameDescription += extendedDescription;
        //        }
        //    }

        //    return gameDescription;
        //}

        private async ValueTask GitPushToRemoteAsync(Corgit corgit)
        {
            var setUrl = await corgit.RunGitAsync($"remote set-url origin {Remote}");

            if (setUrl.ExitCode != 0)
            {
                var addRemote = await corgit.RunGitAsync($"remote add origin {Remote}");

                if (addRemote.ExitCode != 0)
                {
                    throw new InvalidOperationException($"Configuring remote failed: {setUrl} {addRemote}");
                }
            }

            var push = await corgit.RunGitAsync("push");

            if (push.ExitCode != 0)
            {
                throw new InvalidOperationException($"Pushing remote failed: {push}");
            }
        }