Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/generated values #97

Merged
merged 2 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}


}
4 changes: 4 additions & 0 deletions src/ScriptRunner/ScriptRunner.GUI/ParamsPanelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,17 @@ public ParamsPanel Create(ScriptConfig action, Dictionary<string, string> values
};
generateButton.Click += async(sender, args) =>
{
generateButton.IsEnabled = false;
generateButton.Classes.Add("spinning");
var result = await commandExecutor($"Generate parameter for '{param.Name}'", param.ValueGeneratorCommand);
Dispatcher.UIThread.Post(() =>
{
//TODO: Handle other controls
if (controlRecord is { Control: TextBox tb })
{
tb.Text = result?.Trim() ?? string.Empty;
generateButton.Classes.Remove("spinning");
generateButton.IsEnabled = true;
}
});
};
Expand Down
18 changes: 16 additions & 2 deletions src/ScriptRunner/ScriptRunner.GUI/Themes/StyleClasses.axaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:ScriptRunner.GUI.Views">
xmlns:views="clr-namespace:ScriptRunner.GUI.Views"
xmlns:tanker="https://github.com/projektanker/icons.avalonia">
<Design.PreviewWith>
<StackPanel>
<TextBlock Classes="h1" Text="Heading 1"></TextBlock>
Expand Down Expand Up @@ -120,5 +121,18 @@
<Setter Property="Background" Value="Black"></Setter>
<Setter Property="Padding" Value="10"></Setter>
</Style>

<Style Selector="Button.spinning tanker|Icon">
<Style.Animations>
<Animation Duration="0:0:2" IterationCount="INFINITE">
<KeyFrame Cue="0%">
<Setter Property="Opacity" Value="0.0"/>
<Setter Property="RotateTransform.Angle" Value="0.0"/>
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="Opacity" Value="1.0"/>
<Setter Property="RotateTransform.Angle" Value="360.0"/>
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
</Styles>
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
Loading