Skip to content
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
130 changes: 60 additions & 70 deletions src/UniGetUI.PackageEngine.Managers.Scoop/Helpers/ScoopSourceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ protected override IReadOnlyList<IManagerSource> GetSources_UnSafe()
StartInfo = new ProcessStartInfo
{
FileName = Manager.Status.ExecutablePath,
Arguments = Manager.Status.ExecutableCallArgs + " bucket list",
Arguments =
Manager.Status.ExecutableCallArgs
+ " bucket list"
+ Scoop.UntruncatedTableOutput,
Comment thread
GabrielDuf marked this conversation as resolved.
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
Expand All @@ -74,81 +77,48 @@ protected override IReadOnlyList<IManagerSource> GetSources_UnSafe()
internal IReadOnlyList<IManagerSource> ParseSources(IEnumerable<string> lines)
{
List<ManagerSource> sources = [];
bool dashesPassed = false;
IReadOnlyList<int>? columns = null;

foreach (string line in lines)
foreach (string rawLine in lines)
{
string line = ScoopTable.StripAnsiSequences(rawLine);

if (columns is null)
{
columns = ScoopTable.ReadColumnStarts(line);
continue;
}

if (columns.Count < 4 || string.IsNullOrWhiteSpace(line))
{
continue;
}

string name = ScoopTable.ReadColumn(line, columns, 0);
string source = ScoopTable.ReadColumn(line, columns, 1);
string updated = ScoopTable.ReadColumn(line, columns, 2);
string manifests = ScoopTable.ReadColumn(line, columns, 3);

if (name.Length is 0 || source.Length is 0 || manifests.Length is 0)
{
continue;
}

try
{
if (!dashesPassed)
{
if (line.Contains("---"))
{
dashesPassed = true;
}

continue;
}

if (string.IsNullOrWhiteSpace(line))
{
continue;
}

string[] elements = Regex
.Replace(
Regex.Replace(line, "[1234567890 :.-][AaPp][Mm][\\W]", "").Trim(),
" {2,}",
" "
)
.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (elements.Length < 5)
{
continue;
}

if (
!elements[1].Contains("https://")
&& !elements[1].Contains("http://")
)
{
elements[1] = Path.Join(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"scoop",
"buckets",
elements[0].Trim()
);
}
else
{
elements[1] = Regex.Replace(elements[1], @"^(.*)\.git$", "$1");
}

try
{
sources.Add(
new ManagerSource(
Manager,
elements[0].Trim(),
new Uri(elements[1]),
int.Parse(elements[4].Trim()),
elements[2].Trim() + " " + elements[3].Trim()
)
);
}
catch (Exception ex)
{
Logger.Warn(ex);
sources.Add(
new ManagerSource(
Uri url = BuildSourceUrl(name, source);

sources.Add(
int.TryParse(manifests, out int packageCount)
? new ManagerSource(
Manager,
elements[0].Trim(),
new Uri(elements[1]),
-1,
"1/1/1970"
name,
url,
packageCount,
Regex.Replace(updated, @"\s+[AaPp][Mm]$", "")
)
);
}
: new ManagerSource(Manager, name, url, -1, "1/1/1970")
);
}
catch (Exception e)
{
Expand All @@ -158,5 +128,25 @@ internal IReadOnlyList<IManagerSource> ParseSources(IEnumerable<string> lines)

return sources;
}

private static Uri BuildSourceUrl(string name, string source)
{
if (source.Contains("https://") || source.Contains("http://"))
{
return new Uri(Regex.Replace(source, @"^(.*)\.git$", "$1"));
}

string userProfile = Environment.GetFolderPath(
Environment.SpecialFolder.UserProfile
);
string path =
source.StartsWith("~/") || source.StartsWith(@"~\")
? Path.Join(userProfile, source[2..])
: source;

return Path.IsPathFullyQualified(path)
? new Uri(path)
: new Uri(Path.Join(userProfile, "scoop", "buckets", name));
}
}
}
51 changes: 51 additions & 0 deletions src/UniGetUI.PackageEngine.Managers.Scoop/Helpers/ScoopTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Text.RegularExpressions;

namespace UniGetUI.PackageEngine.Managers.ScoopManager
{
internal static partial class ScoopTable
{
[GeneratedRegex(@"\x1b\[[0-9;]*m")]
private static partial Regex AnsiSequence();

public static string StripAnsiSequences(string line) =>
line.Contains('\x1b') ? AnsiSequence().Replace(line, "") : line;

public static IReadOnlyList<int>? ReadColumnStarts(string line)
{
if (!line.Contains("---"))
{
return null;
}

List<int> starts = [];
for (int i = 0; i < line.Length; i++)
{
if (line[i] is not '-')
{
continue;
}

starts.Add(i);
while (i < line.Length && line[i] is '-')
{
i++;
}
}

return starts;
}

public static string ReadColumn(string line, IReadOnlyList<int> starts, int index)
{
int start = starts[index];
if (start >= line.Length)
{
return "";
}

int end =
index + 1 < starts.Count ? Math.Min(starts[index + 1], line.Length) : line.Length;
return line[start..end].Trim();
}
}
}
Loading
Loading