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

Remove multi select #71

Merged
merged 9 commits into from
Aug 26, 2023
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RepoM will not compete with your favourite git clients, so keep them. It's not a
## Credits

RepoM is a fork of [RepoZ](https://github.com/awaescher/RepoZ), which was created by [Andreas Wäscher](https://github.com/awaescher).
RepoZ contains functionality that has been stripped in RepoM like supporting MacOS, releasing versions using chocolatey, and the commandline sidekick (grr).
RepoZ contains functionality that has been stripped in RepoM like supporting MacOS, releasing versions using chocolatey, the commandline sidekick (`grr``), and performing actions at multiple repostitories at once.

## The Hub

Expand Down
2 changes: 1 addition & 1 deletion README.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RepoM will not compete with your favourite git clients, so keep them. It's not a
## Credits

RepoM is a fork of [RepoZ](https://github.com/awaescher/RepoZ), which was created by [Andreas Wäscher](https://github.com/awaescher).
RepoZ contains functionality that has been stripped in RepoM like supporting MacOS, releasing versions using chocolatey, and the commandline sidekick (grr).
RepoZ contains functionality that has been stripped in RepoM like supporting MacOS, releasing versions using chocolatey, the commandline sidekick (`grr``), and performing actions at multiple repostitories at once.

## The Hub

Expand Down
2 changes: 1 addition & 1 deletion src/RepoM.Api/Git/IRepositoryActionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public interface IRepositoryActionProvider

RepositoryActionBase? GetSecondaryAction(Repository repository);

IEnumerable<RepositoryActionBase> GetContextMenuActions(IEnumerable<Repository> repositories);
IEnumerable<RepositoryActionBase> GetContextMenuActions(Repository repository);
}
22 changes: 7 additions & 15 deletions src/RepoM.Api/IO/DefaultRepositoryActionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,43 @@ namespace RepoM.Api.IO;

using System;
using System.Collections.Generic;
using System.IO.Abstractions;
using System.Linq;
using Microsoft.Extensions.Logging;
using RepoM.Api.Git;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider;

public class DefaultRepositoryActionProvider : IRepositoryActionProvider
{
private readonly IFileSystem _fileSystem;
private readonly RepositorySpecificConfiguration _repoSpecificConfig;
private readonly ILogger _logger;

public DefaultRepositoryActionProvider(
IFileSystem fileSystem,
RepositorySpecificConfiguration repoSpecificConfig,
ILogger logger)
public DefaultRepositoryActionProvider(RepositorySpecificConfiguration repoSpecificConfig, ILogger logger)
{
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
_repoSpecificConfig = repoSpecificConfig ?? throw new ArgumentNullException(nameof(repoSpecificConfig));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public RepositoryActionBase? GetPrimaryAction(Repository repository)
{
return GetContextMenuActions(new[] { repository, }).FirstOrDefault();
return GetContextMenuActions(repository).FirstOrDefault();
}

public RepositoryActionBase? GetSecondaryAction(Repository repository)
{
RepositoryActionBase[] actions = GetContextMenuActions(new[] { repository, }).Take(2).ToArray();
RepositoryActionBase[] actions = GetContextMenuActions(repository).Take(2).ToArray();
return actions.Length > 1 ? actions[1] : null;
}

public IEnumerable<RepositoryActionBase> GetContextMenuActions(IEnumerable<Repository> repositories)
public IEnumerable<RepositoryActionBase> GetContextMenuActions(Repository repository)
{
return GetContextMenuActionsInternal(repositories.Where(r => _fileSystem.Directory.Exists(r.SafePath))).Where(a => a != null);
return GetContextMenuActionsInternal(repository);
}

private IEnumerable<RepositoryActionBase> GetContextMenuActionsInternal(IEnumerable<Repository> repos)
private IEnumerable<RepositoryActionBase> GetContextMenuActionsInternal(Repository repository)
{
Repository[] repositories = repos.ToArray();

try
{
return _repoSpecificConfig.CreateActions(repositories);
return _repoSpecificConfig.CreateActions(repository);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace RepoM.Api.IO.ExpressionEvaluator;

using System;
using System.Collections.Generic;
using System.Linq;
using ExpressionStringEvaluator.Methods;
using ExpressionStringEvaluator.Parser;
using ExpressionStringEvaluator.VariableProviders;
Expand All @@ -15,102 +14,87 @@ public class RepositoryExpressionEvaluator : IRepositoryExpressionEvaluator

public RepositoryExpressionEvaluator(IEnumerable<IVariableProvider> variableProviders, IEnumerable<IMethod> methods)
{
List<IVariableProvider> v = variableProviders.ToList() ?? throw new ArgumentNullException(nameof(variableProviders));
List<IMethod> m = methods.ToList() ?? throw new ArgumentNullException(nameof(methods));

_expressionExecutor = new ExpressionExecutor(v, m);
_expressionExecutor = new ExpressionExecutor(variableProviders, methods);
}

public string EvaluateStringExpression(string value, params IRepository[] repository)
{
return EvaluateStringExpression(value, repository.AsEnumerable());
}

public object? EvaluateValueExpression(string value, params IRepository[] repository)
public bool EvaluateBooleanExpression(string? value, IRepository? repository)
{
return EvaluateValueExpression(value, repository.AsEnumerable());
}
if (string.IsNullOrWhiteSpace(value))
{
return true;
}

private object? EvaluateValueExpression(string value, IEnumerable<IRepository> repository)
{
try
if ("true".Equals(value, StringComparison.InvariantCulture))
{
return _expressionExecutor.Execute(new RepositoryContext(repository), value);
return true;
}
catch (Exception)

if ("false".Equals(value, StringComparison.InvariantCulture))
{
return null;
return false;
}
}

private string EvaluateStringExpression(string value, IEnumerable<IRepository> repository)
{
try
{
object? result = _expressionExecutor.Execute(new RepositoryContext(repository), value);
object? result = _expressionExecutor.Execute(RepositoryContext.Create(repository), value!);

// seems to be possible
if (result == null)
if (result is null)
{
return string.Empty;
return false;
}

if (result is bool b)
{
return b;
}

if (result is string s)
{
return s;
return "true".Equals(s, StringComparison.CurrentCultureIgnoreCase);
}

return string.Empty;
return false;
}
catch (Exception)
{
return string.Empty;
return false;
}
}

public bool EvaluateBooleanExpression(string? value, IRepository? repository)
public object? EvaluateValueExpression(string value, IRepository? repository)
{
if (string.IsNullOrWhiteSpace(value))
{
return true;
}

if ("true".Equals(value, StringComparison.InvariantCulture))
try
{
return true;
return _expressionExecutor.Execute(RepositoryContext.Create(repository), value);
}

if ("false".Equals(value, StringComparison.InvariantCulture))
catch (Exception)
{
return false;
return null;
}
}

public string EvaluateStringExpression(string value, IRepository? repository)
{
try
{
IRepository[] repositories = repository == null ? Array.Empty<IRepository>() : new[] { repository, };
object? result = _expressionExecutor.Execute(RepositoryContext.Create(repository), value);

object? result = _expressionExecutor.Execute(new RepositoryContext(repositories), value!);

if (result is null)
{
return false;
}

if (result is bool b)
// seems to be possible
if (result == null)
{
return b;
return string.Empty;
}

if (result is string s)
{
return "true".Equals(s, StringComparison.CurrentCultureIgnoreCase);
return s;
}

return false;
return string.Empty;
}
catch (Exception)
{
return false;
return string.Empty;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,9 @@ public ActionDeserializerComposition(IEnumerable<IActionDeserializer> deserializ

public RepositoryAction? DeserializeSingleAction(string type, JToken jToken, JsonSerializer jsonSerializer)
{
RepositoryAction? result = DeserializeWithCustomDeserializers(type, jToken, jsonSerializer)
??
DeserializeWithDefaultDeserializers(type, jToken, jsonSerializer);

if (result == null)
{
return null;
}

JToken? multiSelectEnabledToken = jToken["multi-select-enabled"];

if (multiSelectEnabledToken != null)
{
result.MultiSelectEnabled = multiSelectEnabledToken.Value<string>();
}

return result;
return DeserializeWithCustomDeserializers(type, jToken, jsonSerializer)
??
DeserializeWithDefaultDeserializers(type, jToken, jsonSerializer);
}

private RepositoryAction? DeserializeWithCustomDeserializers(string type, JToken jToken, JsonSerializer jsonSerializer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ namespace RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Data;
[PropertyDefaultBoolValue(true)]
public string? Active { get; set; }

/// <summary>
/// Multiselect enabled.
/// </summary>
[EvaluatedProperty]
[PropertyType(typeof(bool))] // todo
[PropertyDefaultBoolValue(false)]
public string? MultiSelectEnabled { get; set; }

/// <summary>
/// A set of variables to be availabe within this action.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace RepoM.Api.IO.ModuleBasedRepositoryActionProvider.FileCache;

using System.Collections.Generic;
using System.Runtime.Caching;
using DotNetEnv;

internal class EnvFileStore : FileStore<Dictionary<string, string>>
{
public EnvFileStore(ObjectCache cache) : base(cache)
{
}

public IDictionary<string, string> TryGet(string filename)
{
Dictionary<string, string>? result = Get(filename);

if (result != null)
{
return result;
}

IEnumerable<KeyValuePair<string, string>>? envResult = Env.Load(filename, new LoadOptions(setEnvVars: false));

Dictionary<string, string>? fileContents = envResult == null ? new Dictionary<string, string>(0) : envResult.ToDictionary();

return AddOrGetExisting(filename, fileContents);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace RepoM.Api.IO.ModuleBasedRepositoryActionProvider.FileCache;

using System;
using System.Collections.Generic;
using System.Runtime.Caching;

internal abstract class FileStore<T> where T : class
{
private readonly ObjectCache _cache;

protected FileStore(ObjectCache cache)
{
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
}

internal T? Get(string filename)
{
if (_cache[filename] is T fileContents)
{
return fileContents;
}

return null;
}

internal T AddOrGetExisting(string filename, T value)
{
var policy = new CacheItemPolicy();
var filePaths = new List<string>(1) { filename, };
policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));
var cacheResult = _cache.AddOrGetExisting(filename, value, policy) as T;
return cacheResult ?? value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace RepoM.Api.IO.ModuleBasedRepositoryActionProvider.FileCache;

using System;
using System.IO.Abstractions;
using System.Runtime.Caching;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Data;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Deserialization;

internal class RepositoryActionsFileStore : FileStore<RepositoryActionConfiguration>
{
private readonly IFileSystem _fileSystem;
private readonly IRepositoryActionDeserializer _repositoryActionsDeserializer;


public RepositoryActionsFileStore(IFileSystem fileSystem, IRepositoryActionDeserializer repositoryActionsDeserializer, ObjectCache cache) : base(cache)
{
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
_repositoryActionsDeserializer = repositoryActionsDeserializer ?? throw new ArgumentNullException(nameof(repositoryActionsDeserializer));
}

public RepositoryActionConfiguration? TryGet(string filename)
{
RepositoryActionConfiguration? result = Get(filename);

if (result != null)
{
return result;
}

var payload = _fileSystem.File.ReadAllText(filename);
RepositoryActionConfiguration? fileContents = _repositoryActionsDeserializer.Deserialize(payload);

if (fileContents == null)
{
return null;
}

return AddOrGetExisting(filename, fileContents);
}
}
Loading
Loading