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

Repository specific configuration, extending configuration #162

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 10 additions & 6 deletions RepoZ.Api.Common/Git/FileRepositoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ protected FileRepositoryStore(IErrorHandler errorHandler)

public abstract string GetFileName();

public IEnumerable<string> Get()
public IEnumerable<string> Get(string file)
{
if (!UseFilePersistence)
return new string[0];

string file = GetFileName();

return Array.Empty<string>();

if (File.Exists(file))
{
try
Expand All @@ -39,7 +37,13 @@ public IEnumerable<string> Get()
}
}

return new string[0];
return Array.Empty<string>();
}

public IEnumerable<string> Get()
{
string file = GetFileName();
return Get(file);
}

public void Set(IEnumerable<string> paths)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,52 @@
using System.IO;
using System.Linq;
using System.Reflection;
using RepoZ.Api.Git;

namespace RepoZ.Api.Common.Git
{
public class DefaultRepositoryActionConfigurationStore : FileRepositoryStore, IRepositoryActionConfigurationStore
{
private const string REPOSITORY_ACTIONS_FILENAME = "RepositoryActions.json";
private object _lock = new object();
private readonly IAppDataPathProvider _appDataPathProvider;

public DefaultRepositoryActionConfigurationStore(IErrorHandler errorHandler, IAppDataPathProvider appDataPathProvider)
public DefaultRepositoryActionConfigurationStore(IErrorHandler errorHandler,
IAppDataPathProvider appDataPathProvider)
: base(errorHandler)
{
AppDataPathProvider = appDataPathProvider ?? throw new ArgumentNullException(nameof(appDataPathProvider));
_appDataPathProvider = appDataPathProvider ?? throw new ArgumentNullException(nameof(appDataPathProvider));
}

public override string GetFileName() => Path.Combine(AppDataPathProvider.GetAppDataPath(), "RepositoryActions.json");
public override string GetFileName()
{
return Path.Combine(_appDataPathProvider.GetAppDataPath(), REPOSITORY_ACTIONS_FILENAME);
}

public RepositoryActionConfiguration LoadRepositoryConfiguration(Repository repo)
{
var file = Path.Combine(repo.Path, ".git", REPOSITORY_ACTIONS_FILENAME);
if (File.Exists(file))
{
var result = LoadRepositoryActionConfiguration(file);
if (result.State == RepositoryActionConfiguration.LoadState.Ok)
{
return result;
}
}

file = Path.Combine(repo.Path, REPOSITORY_ACTIONS_FILENAME);
if (File.Exists(file))
{
var result = LoadRepositoryActionConfiguration(file);
if (result.State == RepositoryActionConfiguration.LoadState.Ok)
{
return result;
}
}

return null;
}

public void Preload()
{
Expand All @@ -34,32 +66,43 @@ public void Preload()
}
}

try
{
var lines = Get()?.ToList() ?? new List<string>();
var json = string.Join(Environment.NewLine, lines.Select(RemoveComment));
RepositoryActionConfiguration = JsonConvert.DeserializeObject<RepositoryActionConfiguration>(json) ?? new RepositoryActionConfiguration();
RepositoryActionConfiguration.State = RepositoryActionConfiguration.LoadState.Ok;
}
catch (Exception ex)
RepositoryActionConfiguration = LoadRepositoryActionConfiguration(GetFileName());
}
}

public RepositoryActionConfiguration LoadRepositoryActionConfiguration(string filename)
{
try
{
var lines = Get(filename)?.ToList() ?? new List<string>();
var json = string.Join(Environment.NewLine, lines.Select(RemoveComment));
var repositoryActionConfiguration = JsonConvert.DeserializeObject<RepositoryActionConfiguration>(json) ?? new RepositoryActionConfiguration();
repositoryActionConfiguration.State = RepositoryActionConfiguration.LoadState.Ok;
return repositoryActionConfiguration;
}
catch (Exception ex)
{
return new RepositoryActionConfiguration
{
RepositoryActionConfiguration = new RepositoryActionConfiguration();
RepositoryActionConfiguration.State = RepositoryActionConfiguration.LoadState.Error;
RepositoryActionConfiguration.LoadError = ex.Message;
}
State = RepositoryActionConfiguration.LoadState.Error,
LoadError = ex.Message
};
}
}

private bool TryCopyDefaultJsonFile()
{
var defaultFile = Path.Combine(AppDataPathProvider.GetAppResourcesPath(), "RepositoryActions.json");
var defaultFile = Path.Combine(_appDataPathProvider.GetAppResourcesPath(), REPOSITORY_ACTIONS_FILENAME);
var targetFile = GetFileName();

try
{
File.Copy(defaultFile, targetFile);
}
catch { /* lets ignore errors here, we just want to know if if worked or not by checking the file existence */ }
catch
{
/* lets ignore errors here, we just want to know if if worked or not by checking the file existence */
}

return File.Exists(targetFile);
}
Expand All @@ -71,7 +114,5 @@ private string RemoveComment(string line)
}

public RepositoryActionConfiguration RepositoryActionConfiguration { get; private set; }

public IAppDataPathProvider AppDataPathProvider { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
namespace RepoZ.Api.Common.Git
using RepoZ.Api.Git;

namespace RepoZ.Api.Common.Git
{
public interface IRepositoryActionConfigurationStore
{
void Preload();

RepositoryActionConfiguration RepositoryActionConfiguration { get; }

RepositoryActionConfiguration LoadRepositoryConfiguration(Repository repo);

RepositoryActionConfiguration LoadRepositoryActionConfiguration(string filename);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace RepoZ.Api.Common.Git
{
public class RepositoryActionConfiguration
{
[JsonProperty("redirect")]
public string RedirectFile { get; set; }

[JsonProperty("repository-actions")]
public List<RepositoryAction> RepositoryActions { get; set; } = new List<RepositoryAction>();

Expand Down Expand Up @@ -37,6 +40,9 @@ public class RepositoryAction

[JsonProperty("active")]
public bool Active { get; set; }

[JsonProperty("subfolder")]
public List<RepositoryAction> Subfolder { get; set; } = new List<RepositoryAction>();
}

[System.Diagnostics.DebuggerDisplay("{Name}")]
Expand Down
Loading