128 lines
3.7 KiB
C#
128 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Runtime.CompilerServices;
|
|
using Foodsoft.Alpm;
|
|
|
|
// ReSharper disable once UnusedType.Global
|
|
namespace Samples
|
|
{
|
|
[Example("installed")]
|
|
internal class Installed : IExample
|
|
{
|
|
private static string ModToStr(Depend.ModType modType)
|
|
{
|
|
return modType switch
|
|
{
|
|
Depend.ModType.Any => "",
|
|
Depend.ModType.Equal => "==",
|
|
Depend.ModType.GreaterThanOrEqual => ">=",
|
|
Depend.ModType.LessThanOrEqual => "<=",
|
|
Depend.ModType.GreaterThan => ">",
|
|
Depend.ModType.LessThan => "<",
|
|
_ => throw new ArgumentOutOfRangeException(nameof(modType), modType, null)
|
|
};
|
|
}
|
|
|
|
public int Run(string[] args)
|
|
{
|
|
using var h = new Handle("/", "/var/lib/pacman");
|
|
using var db = h.LocalDB;
|
|
|
|
foreach (var pkg in db.PackageCache)
|
|
using (pkg)
|
|
{
|
|
Console.WriteLine($"Size: {pkg.DownloadSize}");
|
|
foreach (var d in pkg.OptDepends)
|
|
{
|
|
if (d.Description.Length == 0) continue;
|
|
Console.WriteLine("dep({0}): {1} {2} {3} {4}", pkg.Name, d.Name, ModToStr(d.Mod), d.Version,
|
|
d.Description);
|
|
foreach (var f in pkg.Backup)
|
|
Console.WriteLine($"{f.Name} {f.Hash}");
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
[Example("search")]
|
|
internal class Search : IExample
|
|
{
|
|
public int Run(string[] args)
|
|
{
|
|
using var h = new Handle("/", "/var/lib/pacman");
|
|
using var db = h.RegisterSyncDB("core");
|
|
db.Servers = new[] {"http://www.google.com"};
|
|
|
|
|
|
//using var pkg = db.PackageCache.FindSatisfier("gcc=9.3.0-1");
|
|
var result = db.Search(new string[] {"gcc", "objc"});
|
|
foreach (var pkg in result)
|
|
using (pkg)
|
|
{
|
|
Console.WriteLine($"{pkg?.Name} {pkg?.Version}");
|
|
}
|
|
|
|
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace Samples
|
|
{
|
|
internal static class Program
|
|
{
|
|
private static int Main(string[] args)
|
|
{
|
|
var examples = (from t in Assembly.GetExecutingAssembly().GetTypes()
|
|
let attribute = (Example?) t.GetCustomAttribute(typeof(Example))
|
|
where attribute != null
|
|
select new {Name = attribute.Name, Type = t}).ToImmutableDictionary((e) => e.Name);
|
|
|
|
if (args.Length < 1)
|
|
{
|
|
Console.Error.WriteLine("Sample");
|
|
foreach (var example in examples.Values)
|
|
{
|
|
Console.Error.WriteLine("{0}", example.Name);
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
int ret;
|
|
try
|
|
{
|
|
ret = ((IExample) Activator.CreateInstance(examples[args[0]].Type)!).Run(args);
|
|
}
|
|
catch (AlpmException e)
|
|
{
|
|
Console.Error.WriteLine("{0}", e.Message);
|
|
return 1;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)]
|
|
public class Example : Attribute
|
|
{
|
|
public string Name { get; }
|
|
|
|
public Example(string name)
|
|
{
|
|
Name = name;
|
|
}
|
|
}
|
|
|
|
public interface IExample
|
|
{
|
|
public int Run(string[] args);
|
|
}
|
|
} |