Skip to content

Commit

Permalink
Add insight into git repo update
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Sep 1, 2024
1 parent 775ffde commit 8244da5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using System.Text;
using System.Threading.Tasks;
using CliWrap;
using LibGit2Sharp;
using LibGit2Sharp.Handlers;
using ScriptRunner.GUI.Settings;

namespace ScriptRunner.GUI.BackgroundTasks;
Expand All @@ -17,8 +15,20 @@ public interface IRepositoryClient
Task<bool> PullRepository(string path);
}

public record CliCommand(string Command, string Parameters, string WorkingDirectory);

public delegate Task<CliCommandOutputs> CliCommandExecutor(CliCommand command);

public record CliCommandOutputs(string StandardOutput, string StandardError);
class CliRepositoryClient : IRepositoryClient
{
private readonly CliCommandExecutor _cliCommandExecutor;

public CliRepositoryClient(CliCommandExecutor cliCommandExecutor)
{
_cliCommandExecutor = cliCommandExecutor;
}

public async Task<bool> IsOutdated(string repoPath)
{
_ = await ExecuteCommand(repoPath, "git", "fetch --prune origin --verbose");
Expand All @@ -28,8 +38,8 @@ public async Task<bool> IsOutdated(string repoPath)

public async Task<bool> PullRepository(string path)
{
var (success,_) = await ExecuteCommand(path, "git", "pull --rebase=true origin --prune --verbose");
return success;
var result = await _cliCommandExecutor.Invoke(new CliCommand("git", "pull --rebase=true origin --prune --verbose", path));
return result.StandardError.Contains("error", StringComparison.InvariantCultureIgnoreCase) == false;
}

private static async Task<(bool, string)> ExecuteCommand(string repoPath, string command, string parameters)
Expand All @@ -56,11 +66,17 @@ await Cli.Wrap(command)



public static class ConfigRepositoryUpdater
public class ConfigRepositoryUpdater
{
private static readonly IRepositoryClient repositoryClient = new CliRepositoryClient();
private readonly IRepositoryClient repositoryClient;

public static async Task<List<OutdatedRepositoryModel>> CheckAllRepositories()

public ConfigRepositoryUpdater(IRepositoryClient repositoryClient)
{
this.repositoryClient = repositoryClient;
}

public async Task<List<OutdatedRepositoryModel>> CheckAllRepositories()
{
var outOfDateRepos = new List<OutdatedRepositoryModel>();
var entries = AppSettingsService.Load().ConfigScripts?.Where(e => e.Type == ConfigScriptType.Directory) ??
Expand All @@ -87,10 +103,8 @@ public static async Task<List<OutdatedRepositoryModel>> CheckAllRepositories()



public static Task<bool> PullRepository(string path)
public Task<bool> RefreshRepository(string path)
{
return repositoryClient.PullRepository(path);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,25 @@ public bool ShowNewVersionAvailable

public MainWindowViewModel(ParamsPanelFactory paramsPanelFactory, VaultProvider vaultProvider)
{
this._configRepositoryUpdater = new ConfigRepositoryUpdater(new CliRepositoryClient(command =>
{
var tcs = new TaskCompletionSource<CliCommandOutputs>();

var job = new RunningJobViewModel
{
Tile = $"Update repository",
ExecutedCommand = $"{command.Command} {command.Parameters}",
};
this.RunningJobs.Add(job);
SelectedRunningJob = job;

job.ExecutionCompleted += (sender, args) =>
{
tcs.SetResult(new(job.RawOutput, job.RawErrorOutput));
};
job.RunJob(command.Command, command.Parameters, command.WorkingDirectory, Array.Empty<InteractiveInputDescription>(), Array.Empty<TroubleshootingItem>());
return tcs.Task;
}));
IsScriptListVisible = true;
SaveAsPredefinedCommand = ReactiveCommand.Create(() => { });
_paramsPanelFactory = paramsPanelFactory;
Expand Down Expand Up @@ -300,7 +319,7 @@ public MainWindowViewModel(ParamsPanelFactory paramsPanelFactory, VaultProvider
_outdatedRepoCheckingScheduler = new RealTimeScheduler(TimeSpan.FromHours(
4), TimeSpan.FromHours(1), async () =>
{
var outOfDateRepos = await ConfigRepositoryUpdater.CheckAllRepositories();
var outOfDateRepos = await _configRepositoryUpdater.CheckAllRepositories();
Dispatcher.UIThread.Post(() =>
{
OutOfDateConfigRepositories.Clear();
Expand Down Expand Up @@ -555,7 +574,7 @@ public async void PullRepoChanges(object arg)
if (arg is OutdatedRepositoryModel record)
{
var result = false;
await Task.Run(async () => result = await ConfigRepositoryUpdater.PullRepository(record.Path));
await Task.Run(async () => result = await _configRepositoryUpdater.RefreshRepository(record.Path));
if (result)
{
OutOfDateConfigRepositories.Remove(record);
Expand Down Expand Up @@ -830,10 +849,9 @@ public ExecutionLogAction SelectedRecentExecution
}

private ExecutionLogAction _selectedRecentExecution;
private readonly ConfigRepositoryUpdater _configRepositoryUpdater;




private void AddExecutionAudit(ScriptConfig selectedAction)
{
AppSettingsService.UpdateRecent(recent =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public void RunJob(string commandPath, string args, string? workingDirectory,
var stopWatch = new Stopwatch();
stopWatch.Start();
var rawOutput = new StringBuilder();
var rawErrorOutput = new StringBuilder();
try
{
await using var inputStream = new MultiplexerStream();
Expand All @@ -113,9 +114,13 @@ await Cli.Wrap(commandPath)
rawOutput.Append(s);
AppendToOutput(s, ConsoleOutputLevel.Normal);
}))
.WithStandardErrorPipe(PipeTarget.ToDelegate(s => AppendToOutput(s, ConsoleOutputLevel.Error)))
.WithStandardErrorPipe(PipeTarget.ToDelegate(s =>
{
rawErrorOutput.Append(s);
AppendToOutput(s, ConsoleOutputLevel.Error);
}))
.WithValidation(CommandResultValidation.None)
.WithEnvironmentVariables(EnvironmentVariables)
.WithEnvironmentVariables(EnvironmentVariables ?? new())
.ExecuteAsync(ExecutionCancellation.Token);
ChangeStatus(RunningJobStatus.Finished);
}
Expand Down Expand Up @@ -146,6 +151,7 @@ await Cli.Wrap(commandPath)
{
ExecutionPending = false;
RawOutput = rawOutput.ToString();
RawErrorOutput = rawErrorOutput.ToString();
RaiseExecutionCompleted();
});
_logForwarder.Finish();
Expand Down Expand Up @@ -698,6 +704,7 @@ public bool ExecutionPending
}

public string RawOutput { get; set; }
public string RawErrorOutput { get; set; }

private int _outputIndex;
private bool _executionPending;
Expand Down

0 comments on commit 8244da5

Please sign in to comment.