using System; using System.Collections.Immutable; using System.Linq; using System.Reflection; using Foodsoft.Alpm; namespace Samples { [Example("installed")] internal class Installed : IExample { private static string ModToStr(ModType modType) { return modType switch { ModType.Any => "", ModType.Equal => "==", ModType.GreaterThanOrEqual => ">=", ModType.LessThanOrEqual => "<=", ModType.GreaterThan => ">", 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[] {"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 {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)] public class Example : Attribute { public string Name { get; } public Example(string name) { Name = name; } } public interface IExample { public int Run(string[] args); } }