using Invercargill; using Invercargill.DataStructures; using Invercargill.Mapping; namespace Usm { /** * Errors raised while reading a {@link Configuration}. */ public errordomain ConfigurationError { /** * The "system_package_manager" section defined only one of its required argv arrays. */ INCOMPLETE_SYSTEM_PACKAGE_MANAGER } public class Configuration { public bool is_managed { get; set; } public ManagedConfiguration? managed_config { get; set; } public Paths? paths { get; set; } public SystemPackageManagerConfig? system_package_manager { get; set; } /** * Seconds a single repository transfer may run before it is * aborted; consumed by {@link CurlRepositoryClient} through * {@link Repository.get_client}. */ public int download_timeout { get; set; default = 300; } public static PropertyMapper get_mapper() { return PropertyMapper.build_for(cfg => { cfg.map("is_managed", o => o.is_managed, (o, v) => o.is_managed = v); cfg.map("download_timeout", o => o.download_timeout, (o, v) => o.download_timeout = v) .undefined_when(o => o.download_timeout == 0) .when_undefined(o => o.download_timeout = 300); cfg.map_properties_with("managed", o => o.managed_config, (o, v) => o.managed_config = v, ManagedConfiguration.get_mapper()) .undefined_when(o => o.managed_config == null) .when_undefined(o => o.managed_config = null); cfg.map_properties_with("paths", o => o.paths, (o, v) => o.paths = v, Paths.get_mapper()) .undefined_when(o => o.paths == null) .when_undefined(o => o.paths = new Paths.usm_environ()); cfg.map_properties_with("system_package_manager", o => o.system_package_manager, (o, v) => o.system_package_manager = v, SystemPackageManagerConfig.get_mapper()) .undefined_when(o => o.system_package_manager == null) .when_undefined(o => o.system_package_manager = null); cfg.set_constructor(() => new Configuration()); }); } public Configuration.from_paths(Paths paths) throws Error { var element = new InvercargillJson.JsonElement.from_file(Path.build_filename(paths.usm_config_dir, "usm.config")); get_mapper().map_into(this, element.as()); validate(); } private void validate() throws Error { if(system_package_manager == null) { return; } var query_defined = system_package_manager.query != null; var install_defined = system_package_manager.install != null; if(query_defined != install_defined) { throw new ConfigurationError.INCOMPLETE_SYSTEM_PACKAGE_MANAGER( @"The \"system_package_manager\" section of usm.config must define both \"query\" and \"install\" argv arrays, but only \"$(query_defined ? "query" : "install")\" was found." ); } } public static bool check_managed(Paths paths) { var config_path = Path.build_filename(paths.usm_config_dir, "usm.config"); if(!File.new_for_path(config_path).query_exists()) { return false; } try { return new Configuration.from_paths(paths).is_managed; } catch(Error e) { warning(@"Error reading configuration: $(e.message)"); return false; } } } public class ManagedConfiguration { public string state_path { get; set; } public static PropertyMapper get_mapper() { return PropertyMapper.build_for(cfg => { cfg.map("state_path", o => o.state_path, (o, v) => o.state_path = v); cfg.set_constructor(() => new ManagedConfiguration()); }); } } /** * argv arrays used to invoke the configured system package manager helper. * * Values are plain argv arrays (no shell parsing); see {@link SystemPackageManager} * for the helper contracts. Both {@link query} and {@link install} must be defined * together or omitted entirely, which {@link Configuration} validates on load. */ public class SystemPackageManagerConfig { public Vector? query { get; set; } public Vector? install { get; set; } public static PropertyMapper get_mapper() { return PropertyMapper.build_for(cfg => { cfg.map_many("query", o => o.query ?? new Vector(), (o, v) => o.query = v.to_vector()) .undefined_when(o => o.query == null) .when_undefined(o => o.query = null); cfg.map_many("install", o => o.install ?? new Vector(), (o, v) => o.install = v.to_vector()) .undefined_when(o => o.install == null) .when_undefined(o => o.install = null); cfg.set_constructor(() => new SystemPackageManagerConfig()); }); } } }