| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- 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<Configuration> get_mapper() {
- return PropertyMapper.build_for<Configuration>(cfg => {
- cfg.map<bool>("is_managed", o => o.is_managed, (o, v) => o.is_managed = v);
- cfg.map<int>("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<ManagedConfiguration>("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>("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<SystemPackageManagerConfig>("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<Invercargill.Properties>());
- 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<ManagedConfiguration> get_mapper() {
- return PropertyMapper.build_for<ManagedConfiguration>(cfg => {
- cfg.map<string>("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<string>? query { get; set; }
- public Vector<string>? install { get; set; }
- public static PropertyMapper<SystemPackageManagerConfig> get_mapper() {
- return PropertyMapper.build_for<SystemPackageManagerConfig>(cfg => {
- cfg.map_many<string>("query", o => o.query ?? new Vector<string>(), (o, v) => o.query = v.to_vector())
- .undefined_when(o => o.query == null)
- .when_undefined(o => o.query = null);
- cfg.map_many<string>("install", o => o.install ?? new Vector<string>(), (o, v) => o.install = v.to_vector())
- .undefined_when(o => o.install == null)
- .when_undefined(o => o.install = null);
- cfg.set_constructor(() => new SystemPackageManagerConfig());
- });
- }
- }
- }
|