diff --git a/src/alire/alire-builds.adb b/src/alire/alire-builds.adb index 4ed939c95..c565af035 100644 --- a/src/alire/alire-builds.adb +++ b/src/alire/alire-builds.adb @@ -1,5 +1,6 @@ with AAA.Strings; +with Alire.Config.Builtins; with Alire.Config.Edit; with Alire.Directories; with Alire.OS_Lib.Subprocess; @@ -18,8 +19,7 @@ package body Alire.Builds is ---------------------------- function Sandboxed_Dependencies return Boolean - is (not Config.DB.Get (Config.Keys.Dependencies_Shared, - Config.Defaults.Dependencies_Shared)); + is (not Config.Builtins.Dependencies_Shared.Get); ------------------- -- To_Msys2_Path -- diff --git a/src/alire/alire-config-builtins.ads b/src/alire/alire-config-builtins.ads new file mode 100644 index 000000000..0daff5ebd --- /dev/null +++ b/src/alire/alire-config-builtins.ads @@ -0,0 +1,31 @@ +package Alire.Config.Builtins is + + subtype Builtin is Builtin_Option; + + -------------- + -- Builtins -- + -------------- + + Dependencies_Shared : constant Builtin := New_Builtin + (Key => "dependencies.shared", + Def => False, + Help => + "When true, dependencies are downloaded and built in a shared " + & "location inside the global cache. When false (default), " + & "dependencies are sandboxed in each workspace."); + + Index_Auto_Community : constant Builtin := New_Builtin + (Key => "index.auto_community", + Def => True, + Help => + "When unset or true, the community index will be added " & + "automatically when required if no other index is configured."); + + Solver_Autonarrow : constant Builtin := New_Builtin + (Key => "solver.autonarrow", + Def => True, + Help => + "If true, `alr with` will replace 'any' dependencies with the" + & " appropriate caret/tilde dependency."); + +end Alire.Config.Builtins; diff --git a/src/alire/alire-config-edit.adb b/src/alire/alire-config-edit.adb index b40517f2d..8acdfb7e3 100644 --- a/src/alire/alire-config-edit.adb +++ b/src/alire/alire-config-edit.adb @@ -131,30 +131,6 @@ package body Alire.Config.Edit is end case; end Filepath; - ---------------- - -- Is_Builtin -- - ---------------- - - function Is_Builtin (Key : CLIC.Config.Config_Key) return Boolean - is (for some Cfg of Builtins => To_String (Cfg.Key) = Key); - - --------------------- - -- Kind_Of_Builtin -- - --------------------- - - function Kind_Of_Builtin (Key : CLIC.Config.Config_Key) - return Builtin_Kind - is - begin - for Ent of Builtins loop - if To_String (Ent.Key) = Key then - return Ent.Kind; - end if; - end loop; - - Raise_Checked_Error ("Kind is only valid for builtin config key"); - end Kind_Of_Builtin; - ----------------- -- Load_Config -- ----------------- @@ -237,7 +213,7 @@ package body Alire.Config.Edit is is Result : Boolean := True; begin - for Ent of Builtins loop + for Ent of All_Builtins loop if To_String (Ent.Key) = Key then case Ent.Kind is when Cfg_Int => @@ -287,21 +263,6 @@ package body Alire.Config.Edit is return Result; end Valid_Builtin; - ----------- - -- Image -- - ----------- - - function Image (Kind : Builtin_Kind) return String - is (case Kind is - when Cfg_Int => "Integer", - when Cfg_Float => "Float", - when Cfg_Bool => "Boolean", - when Cfg_String => "String", - when Cfg_Absolute_Path => "Absolute path", - when Cfg_Existing_Absolute_Path => "Absolute path already existing", - when Cfg_Email => "Email address", - when Cfg_GitHub_Login => "GitHub login"); - ------------------- -- Builtins_Info -- ------------------- @@ -309,9 +270,10 @@ package body Alire.Config.Edit is function Builtins_Info return AAA.Strings.Vector is Results : AAA.Strings.Vector; begin - for Ent of Builtins loop - Results.Append (String'("- " & To_String (Ent.Key) & - " [" & Image (Ent.Kind) & "]")); + for Ent of All_Builtins loop + Results.Append (String'("- " & To_String (Ent.Key) + & " [" & Image (Ent.Kind) & "]" + & "[Default:" & To_String (Ent.Def) & "]")); Results.Append (To_String (Ent.Help)); Results.Append (""); end loop; @@ -325,9 +287,10 @@ package body Alire.Config.Edit is procedure Print_Builtins_Doc is use Ada.Text_IO; begin - for Ent of Builtins loop + for Ent of All_Builtins loop Put (" - **`" & To_String (Ent.Key) & "`** "); - Put_Line ("[" & Image (Ent.Kind) & "]:"); + Put ("[" & Image (Ent.Kind) & "]"); + Put_Line ("[Default:" & To_String (Ent.Def) & "]:"); Put_Line (" " & To_String (Ent.Help)); New_Line; end loop; diff --git a/src/alire/alire-config-edit.ads b/src/alire/alire-config-edit.ads index 4780579ce..4f0f00dc0 100644 --- a/src/alire/alire-config-edit.ads +++ b/src/alire/alire-config-edit.ads @@ -65,6 +65,8 @@ package Alire.Config.Edit is -- Return path of the configuration file corresponding to the given -- configuration level. + -- Support for built-in config variables. See Alire.Config.Builtins also. + function Builtins_Info return AAA.Strings.Vector; -- Return a String_Vector with the documentation of builtin configuration -- options in text format. @@ -77,49 +79,8 @@ package Alire.Config.Edit is return Boolean; -- Check that the combination satisfies builtin rules -private - - procedure Load_Config; - -- Clear and reload all configuration. Also set some values elsewhere - -- used to break circularities. Bottom line, this procedure must leave - -- the program-wide configuration ready. - - type Builtin_Kind is (Cfg_Int, Cfg_Float, Cfg_Bool, - Cfg_String, Cfg_Absolute_Path, - Cfg_Existing_Absolute_Path, - Cfg_Email, Cfg_GitHub_Login); - - type Builtin_Entry is record - Key : Ada.Strings.Unbounded.Unbounded_String; - Kind : Builtin_Kind; - Help : Ada.Strings.Unbounded.Unbounded_String; - end record; - - function Image (Kind : Builtin_Kind) return String; - - function Is_Builtin (Key : CLIC.Config.Config_Key) return Boolean; - - function Kind_Of_Builtin (Key : CLIC.Config.Config_Key) return Builtin_Kind - with Pre => Is_Builtin (Key); - - -------------- - -- Builtins -- - -------------- - Builtins : constant array (Natural range <>) of Builtin_Entry := ( - (+Keys.Dependencies_Shared, - Cfg_Bool, - +("When true, dependencies are downloaded and built in a shared " - & "location inside the global cache. When false (default), " - & "dependencies are sandboxed in each workspace." - )), - - (+Keys.Index_Auto_Community, - Cfg_Bool, - +("When unset (default) or true, the community index will be added " & - "automatically when required if no other index is configured.")), - (+Keys.User_Name, Cfg_String, +("User full name. Used for the authors and " & @@ -188,11 +149,6 @@ private +("If true, Alire will report an unknown distribution and will not" & " attempt to use the system package manager.")), - (+Keys.Solver_Autonarrow, - Cfg_Bool, - +("If true, `alr with` will replace 'any' dependencies with the" - & " appropriate caret/tilde dependency.")), - (+Keys.Warning_Caret, Cfg_Bool, +("If true, Alire will warn about the use of caret (^) " @@ -211,4 +167,11 @@ private ); +private + + procedure Load_Config; + -- Clear and reload all configuration. Also set some values elsewhere + -- used to break circularities. Bottom line, this procedure must leave + -- the program-wide configuration ready. + end Alire.Config.Edit; diff --git a/src/alire/alire-config.adb b/src/alire/alire-config.adb new file mode 100644 index 000000000..a7f3d1c9b --- /dev/null +++ b/src/alire/alire-config.adb @@ -0,0 +1,146 @@ +with Alire.Config.Edit; + +package body Alire.Config is + + --------- + -- Get -- + --------- + + function Get (This : Builtin_Option) return String + is (DB.Get (+This.Key, +This.Def)); + + --------- + -- Get -- + --------- + + function Get (This : Builtin_Option) return Boolean + is (DB.Get (+This.Key, Boolean'Value (+This.Def))); + + ----------------- + -- Set_Locally -- + ----------------- + + procedure Set_Locally (This : Builtin_Option; Value : String) is + begin + Edit.Set_Locally (+This.Key, Value, This.Check); + end Set_Locally; + + ------------------ + -- Set_Globally -- + ------------------ + + procedure Set_Globally (This : Builtin_Option; Value : String) is + begin + Edit.Set_Globally (+This.Key, Value, This.Check); + end Set_Globally; + + --------- + -- Set -- + --------- + + procedure Set (This : Builtin_Option; + Level : Config.Level; + Value : String) + is + begin + Edit.Set (Level, +This.Key, Value, This.Check); + end Set; + + --------- + -- Set -- + --------- + + procedure Set (This : Builtin_Option; + Level : Config.Level; + Value : Boolean) + is + begin + Edit.Set_Boolean (Level, +This.Key, Value); + end Set; + + ----------- + -- Unset -- + ----------- + + procedure Unset (This : Builtin_Option; + Level : Config.Level) + is + begin + Edit.Unset (Level, +This.Key); + end Unset; + + ----------- + -- Image -- + ----------- + + function Image (Kind : Builtin_Kind) return String + is (case Kind is + when Cfg_Int => "Integer", + when Cfg_Float => "Float", + when Cfg_Bool => "Boolean", + when Cfg_String => "String", + when Cfg_Absolute_Path => "Absolute path", + when Cfg_Existing_Absolute_Path => "Absolute path already existing", + when Cfg_Email => "Email address", + when Cfg_GitHub_Login => "GitHub login"); + + ---------------- + -- Is_Builtin -- + ---------------- + + function Is_Builtin (Key : CLIC.Config.Config_Key) return Boolean + is (All_Builtins.Contains (Key)); + + --------------------- + -- Kind_Of_Builtin -- + --------------------- + + function Kind_Of_Builtin (Key : CLIC.Config.Config_Key) + return Builtin_Kind + is + begin + if All_Builtins.Contains (Key) then + return All_Builtins (Key).Kind; + end if; + + Raise_Checked_Error ("Kind is only valid for builtin config key"); + end Kind_Of_Builtin; + + ----------------- + -- New_Builtin -- + ----------------- + + function New_Builtin (Key : CLIC.Config.Config_Key; + Kind : Builtin_Kind; + Def : String; + Help : String; + Check : CLIC.Config.Check_Import := null) + return Builtin_Option + is + begin + return Result : constant Builtin_Option := (Key => +Key, + Kind => Kind, + Def => +Def, + Help => +Help, + Check => Check) + do + All_Builtins.Insert (Key, Result); + end return; + end New_Builtin; + + ----------------- + -- New_Builtin -- + ----------------- + + function New_Builtin (Key : CLIC.Config.Config_Key; + Def : Boolean; + Help : String; + Check : CLIC.Config.Check_Import := null) + return Builtin_Option + is (New_Builtin (Key => Key, + Kind => Cfg_Bool, + Def => Def'Image, + Help => Help, + Check => Check)); + +end Alire.Config; diff --git a/src/alire/alire-config.ads b/src/alire/alire-config.ads index e5def09fc..924ebffc9 100644 --- a/src/alire/alire-config.ads +++ b/src/alire/alire-config.ads @@ -1,9 +1,12 @@ -with Alire.OS_Lib; use Alire.OS_Lib.Operators; with AAA.Strings; +private with Ada.Containers.Indefinite_Ordered_Maps; + +with Alire.OS_Lib; use Alire.OS_Lib.Operators; + with CLIC.Config; -package Alire.Config with Preelaborate is +package Alire.Config is DB : CLIC.Config.Instance; -- The Alire user configuration database @@ -16,6 +19,62 @@ package Alire.Config with Preelaborate is -- Built-ins -- --------------- + type Builtin_Kind is (Cfg_Int, Cfg_Float, Cfg_Bool, + Cfg_String, Cfg_Absolute_Path, + Cfg_Existing_Absolute_Path, + Cfg_Email, Cfg_GitHub_Login); + + function Image (Kind : Builtin_Kind) return String; + + function Is_Builtin (Key : CLIC.Config.Config_Key) return Boolean; + + function Kind_Of_Builtin (Key : CLIC.Config.Config_Key) return Builtin_Kind + with Pre => Is_Builtin (Key); + + -- type Builtin_Entry is tagged private; + + type Builtin_Entry is tagged record + Key : Ada.Strings.Unbounded.Unbounded_String; + Kind : Builtin_Kind; + Help : Ada.Strings.Unbounded.Unbounded_String; + -- Check : CLIC.Config.Check_Import := null; + end record; + + type Builtin_Option is tagged private; + + function Key (This : Builtin_Option) return CLIC.Config.Config_Key; + + function Get (This : Builtin_Option) return String; + function Get (This : Builtin_Option) return Boolean; + + procedure Set_Locally (This : Builtin_Option; Value : String); + + procedure Set_Globally (This : Builtin_Option; Value : String); + + procedure Set (This : Builtin_Option; + Level : Config.Level; + Value : String); + + procedure Set (This : Builtin_Option; + Level : Config.Level; + Value : Boolean); + + procedure Unset (This : Builtin_Option; + Level : Config.Level); + + function New_Builtin (Key : CLIC.Config.Config_Key; + Kind : Builtin_Kind; + Def : String; + Help : String; + Check : CLIC.Config.Check_Import := null) + return Builtin_Option; + + function New_Builtin (Key : CLIC.Config.Config_Key; + Def : Boolean; + Help : String; + Check : CLIC.Config.Check_Import := null) + return Builtin_Option; + package Keys is use CLIC.Config; @@ -23,8 +82,6 @@ package Alire.Config with Preelaborate is -- A few predefined keys that are used in several places. This list is -- not exhaustive. - Dependencies_Shared : constant Config_Key := "dependencies.shared"; - Editor_Cmd : constant Config_Key := "editor.cmd"; Distribution_Disable_Detection : constant Config_Key := @@ -32,20 +89,12 @@ package Alire.Config with Preelaborate is -- When set to True, distro will be reported as unknown, and in turn no -- native package manager will be used. - Index_Auto_Community : constant Config_Key := "index.auto_community"; - -- When unset (default) or true, add the community index if no other - -- index is already configured. - Index_Host : constant Config_Key := "index.host"; Index_Owner : constant Config_Key := "index.owner"; Index_Repo_Name : constant Config_Key := "index.repository_name"; -- These three conform the URL where the community index is hosted, -- allowing to override the default. - Solver_Autonarrow : constant Config_Key := "solver.autonarrow"; - -- When true, `alr with` will substitute "any" dependencies by the - -- appropriate caret/tilde. - Toolchain_Assistant : constant Config_Key := "toolchain.assistant"; -- When true (default), on first `Requires_Workspace`, the -- assistant to select a gnat compiler and corresponding gprbuild @@ -88,9 +137,6 @@ package Alire.Config with Preelaborate is package Defaults is - Dependencies_Shared : constant Boolean := False; - -- TODO: enable it when hashing is complete - Index_Host : constant String := "https://github.com"; Index_Owner : constant String := "alire-project"; Index_Repo_Name : constant String := "alire-index"; @@ -99,4 +145,27 @@ package Alire.Config with Preelaborate is end Defaults; +private + + type Builtin_Option is tagged record + Key : Ada.Strings.Unbounded.Unbounded_String; + Kind : Builtin_Kind; + Def : Ada.Strings.Unbounded.Unbounded_String; + Help : Ada.Strings.Unbounded.Unbounded_String; + Check : CLIC.Config.Check_Import := null; + end record; + + --------- + -- Key -- + --------- + + function Key (This : Builtin_Option) return CLIC.Config.Config_Key + is (+This.Key); + + package Builtin_Maps is new + Ada.Containers.Indefinite_Ordered_Maps (CLIC.Config.Config_Key, + Builtin_Option); + + All_Builtins : Builtin_Maps.Map; + end Alire.Config; diff --git a/src/alire/alire-index_on_disk-loading.adb b/src/alire/alire-index_on_disk-loading.adb index 8ef736c7c..3053ddb9d 100644 --- a/src/alire/alire-index_on_disk-loading.adb +++ b/src/alire/alire-index_on_disk-loading.adb @@ -1,6 +1,7 @@ with Ada.Directories; with Ada.Text_IO; +with Alire.Config.Builtins; with Alire.Config.Edit; with Alire.Containers; with Alire.Index; @@ -196,11 +197,10 @@ package body Alire.Index_On_Disk.Loading is Cached => False); use Sets; begin - if not Config.DB.Get (Config.Keys.Index_Auto_Community, Default => True) - then + if not Config.Builtins.Index_Auto_Community.Get then Warnings.Warn_Once ("Not configuring the community index, disabled via " - & Config.Keys.Index_Auto_Community); + & Config.Builtins.Index_Auto_Community.Key); return Outcome_Success; end if; diff --git a/src/alire/alire-solutions.adb b/src/alire/alire-solutions.adb index df717b34d..02797e698 100644 --- a/src/alire/alire-solutions.adb +++ b/src/alire/alire-solutions.adb @@ -1,6 +1,6 @@ with Ada.Containers; -with Alire.Config; +with Alire.Config.Builtins; with Alire.Crates; with Alire.Dependencies.Diffs; with Alire.Dependencies.Graphs; @@ -1386,7 +1386,7 @@ package body Alire.Solutions is -- Do nothing when deps are being removed. - if not Config.DB.Get (Config.Keys.Solver_Autonarrow, True) or else + if not Config.Builtins.Solver_Autonarrow.Get or else not Diff.Removed.Is_Empty then return New_Deps;