namespace QuiverLauncher.Core.Services
{
public static class RepositorySourceHelper
{
///
/// Normalizes a repository source id. Unknown/empty values become GitHub.
///
public static string Normalize(string? repositorySource, out bool wasUnsupported)
{
wasUnsupported = false;
if (string.IsNullOrWhiteSpace(repositorySource))
return RepositorySourceIds.GitHub;
var trimmed = repositorySource.Trim();
if (string.Equals(trimmed, RepositorySourceIds.GitHub, StringComparison.OrdinalIgnoreCase))
return RepositorySourceIds.GitHub;
if (string.Equals(trimmed, RepositorySourceIds.GitLab, StringComparison.OrdinalIgnoreCase))
return RepositorySourceIds.GitLab;
wasUnsupported = true;
return RepositorySourceIds.GitHub;
}
public static string Normalize(string? repositorySource) =>
Normalize(repositorySource, out _);
public static bool IsGitHub(string? repositorySource) =>
string.Equals(Normalize(repositorySource), RepositorySourceIds.GitHub, StringComparison.OrdinalIgnoreCase);
public static bool IsGitLab(string? repositorySource) =>
string.Equals(Normalize(repositorySource), RepositorySourceIds.GitLab, StringComparison.OrdinalIgnoreCase);
public static bool IsManuallyManaged(string? repository) =>
string.IsNullOrWhiteSpace(repository);
public static string GetManualIdentityKey(string? folderName)
{
var folder = folderName?.Trim() ?? string.Empty;
return $"{RepositorySourceIds.Manual}:{folder}";
}
///
/// Repository identity / API cache key: "{source}:{repository}". Missing source is treated as GitHub.
/// Apps with no repository use manual:{folderName}.
/// Multiple library tiles may share this key when they use the same hosted repository.
///
public static string GetIdentityKey(string? repositorySource, string? repository, string? folderName = null)
{
if (IsManuallyManaged(repository))
return GetManualIdentityKey(folderName);
var source = Normalize(repositorySource);
var repo = repository!.Trim();
return $"{source}:{repo}";
}
///
/// Unique library-tile key: "{source}:{repository}:{folderName}" for hosted apps,
/// or manual:{folderName} when there is no repository.
///
public static string GetInstanceKey(string? repositorySource, string? repository, string? folderName)
{
if (IsManuallyManaged(repository))
return GetManualIdentityKey(folderName);
var source = Normalize(repositorySource);
var repo = repository!.Trim();
var folder = folderName?.Trim() ?? string.Empty;
return $"{source}:{repo}:{folder}";
}
///
/// Case-insensitive substring match for a per-app release asset filter.
/// Empty/whitespace filters match every asset.
///
public static bool AssetNameMatchesFilter(string? assetName, string? releaseAssetFilter)
{
var filter = NormalizeReleaseAssetFilter(releaseAssetFilter);
if (filter == null)
return true;
return (assetName ?? string.Empty).Contains(filter, StringComparison.OrdinalIgnoreCase);
}
public static string? NormalizeReleaseAssetFilter(string? releaseAssetFilter)
{
var trimmed = releaseAssetFilter?.Trim();
return string.IsNullOrEmpty(trimmed) ? null : trimmed;
}
public static string GetRepositoryPageUrl(string? repositorySource, string? repository)
{
var repo = repository?.Trim() ?? string.Empty;
if (string.IsNullOrEmpty(repo))
return string.Empty;
return IsGitLab(repositorySource)
? $"https://gitlab.com/{repo}"
: $"https://github.com/{repo}";
}
public static string DisplayName(string? repositorySource) =>
IsGitLab(repositorySource) ? "GitLab" : "GitHub";
}
}