From fcb03eb22841890b3aa628bc282a9351c65c6d2a Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Mon, 4 Sep 2023 11:29:07 +0200 Subject: [PATCH] Refactor config builtins (#1431) * Refactor configuration builtins Gives them a tagged type that simplifies use and aglutinates default, description, checks, which were previously not formally required or scattered. * Colorize help output --- src/alire/alire-builds.adb | 4 +- src/alire/alire-config-builtins.ads | 149 +++++++++++++++++ src/alire/alire-config-edit.adb | 73 ++++----- src/alire/alire-config-edit.ads | 129 +-------------- src/alire/alire-config.adb | 151 ++++++++++++++++++ src/alire/alire-config.ads | 125 ++++++++------- src/alire/alire-index.ads | 11 +- src/alire/alire-index_on_disk-loading.adb | 6 +- src/alire/alire-publish.adb | 8 +- src/alire/alire-releases.adb | 6 +- src/alire/alire-solutions.adb | 4 +- src/alire/alire-toml_index.adb | 10 +- src/alire/alire-toolchains.adb | 4 +- src/alire/alire-toolchains.ads | 8 +- .../alire-config-builtins-windows.ads | 41 +++++ .../alire-platforms-current__windows.adb | 37 ++--- src/alr/alr-commands-edit.adb | 9 +- src/alr/alr-commands-get.adb | 8 +- src/alr/alr-commands-init.adb | 8 +- src/alr/alr-commands.adb | 4 +- 20 files changed, 487 insertions(+), 308 deletions(-) create mode 100644 src/alire/alire-config-builtins.ads create mode 100644 src/alire/alire-config.adb create mode 100644 src/alire/os_windows/alire-config-builtins-windows.ads diff --git a/src/alire/alire-builds.adb b/src/alire/alire-builds.adb index ad23981c..9ccbc34c 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.Paths.Vault; @@ -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); ---------- -- Sync -- diff --git a/src/alire/alire-config-builtins.ads b/src/alire/alire-config-builtins.ads new file mode 100644 index 00000000..b297ad28 --- /dev/null +++ b/src/alire/alire-config-builtins.ads @@ -0,0 +1,149 @@ +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."); + + Distribution_Disable_Detection : constant Builtin := New_Builtin + (Key => "distribution.disable_detection", + Def => False, + Help => + "If true, Alire will report an unknown distribution and will not" + & " attempt to use the system package manager."); + + -- EDITOR + + Editor_Cmd : constant Builtin := New_Builtin + (Key => "editor.cmd", + Kind => Cfg_String, + Def => "gnatstudio -P ${GPR_FILE}", + Help => + "Editor command and arguments for editing crate code (alr edit)." & + " The executables and arguments are separated by a single space" & + " character. The token ${GPR_FILE} is replaced by" & + " a path to the project file to open."); + + -- INDEX + + 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."); + + Index_Host : constant Builtin := New_Builtin + (Key => "index.host", + Kind => Cfg_String, + Def => "https://github.com", + Help => "URL of the community index host"); + + Index_Owner : constant Builtin := New_Builtin + (Key => "index.owner", + Kind => Cfg_String, + Def => "alire-project", + Help => "Owner of the index repository (GitHub user/org)."); + + Index_Repository_Name : constant Builtin := New_Builtin + (Key => "index.repository_name", + Kind => Cfg_String, + Def => "alire-index", + Help => "Name of the index repository."); + + -- SOLVER + + 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."); + + -- TOOLCHAIN + + Toolchain_Assistant : constant Builtin := New_Builtin + (Key => "toolchain.assistant", + Def => True, + Help => + "If true, and assistant to select the default toolchain will run " + & "when first needed."); + + Toolchain_External : constant Builtin := New_Builtin + (Key => "toolchain.external", + Def => True, + Public => False); + -- We use this key to store whether a tool in the toolchain requires + -- external detection. It stores a boolean per tool, e.g, for gprbuild: + -- toolchain.external.gprbuild + + Toolchain_Use : constant Builtin := New_Builtin + (Key => "toolchain.use", + Def => True, + Public => False); + -- We use this key internally to store the configured tools picked + -- up by the user. Not really intended to be set up by users, so + -- not listed as a built-in. Each tool is a child of this key, + -- e.g.: toolchain.use.gnat, toolchain.use.gprbuild + + -- UPDATE + + Update_Manually_Only : constant Builtin := New_Builtin + (Key => "update.manually_only", + Def => False, + Help => + "If true, Alire will not attempt to update dependencies even after " + & "the manifest is manually edited, or when no valid solution has " + & "been ever computed. All updates have to be manually requested " + & "through `alr update`"); + + -- USER + + User_Email : constant Builtin := New_Builtin + (Key => "user.email", + Kind => Cfg_Email, + Help => + "User email address. Used for the authors and" & + " maintainers field of a new crate."); + + User_Name : constant Builtin := New_Builtin + (Key => "user.name", + Kind => Cfg_String, + Help => + "User full name. Used for the authors and " & + "maintainers field of a new crate."); + + User_Github_Login : constant Builtin := New_Builtin + (Key => "user.github_login", + Kind => Cfg_GitHub_Login, + Help => + "User GitHub login/username. Used to for the maintainers-logins " & + "field of a new crate."); + + -- WARNINGS + + Warning_Caret : constant Builtin := New_Builtin + (Key => "warning.caret", + Def => True, + Help => + "If true, Alire will warn about the use of caret (^) for pre-1 " + & "dependencies, for which tilde (~) is recommended instead."); + + Warning_Old_Index : constant Builtin := New_Builtin + (Key => "warning.old_index", + Def => True, + Help => + "If unset or true, a warning will be emitted when " & + "using a compatible index with a lower version than the newest" & + " known."); + +end Alire.Config.Builtins; diff --git a/src/alire/alire-config-edit.adb b/src/alire/alire-config-edit.adb index b40517f2..6b99a28a 100644 --- a/src/alire/alire-config-edit.adb +++ b/src/alire/alire-config-edit.adb @@ -1,5 +1,6 @@ with Ada.Text_IO; +with Alire.Config.Builtins; with Alire.Environment; with Alire.Paths; with Alire.Platforms.Folders; @@ -131,30 +132,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 -- ----------------- @@ -175,7 +152,7 @@ package body Alire.Config.Edit is -- Set variables elsewhere Platforms.Current.Disable_Distribution_Detection := - DB.Get (Keys.Distribution_Disable_Detection, False); + Config.Builtins.Distribution_Disable_Detection.Get; if Platforms.Current.Disable_Distribution_Detection then Trace.Debug ("Distribution detection disabled by configuration"); end if; @@ -237,8 +214,11 @@ 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 + + -- Verify the type/specific constraints + case Ent.Kind is when Cfg_Int => Result := Value.Kind = TOML_Integer; @@ -273,6 +253,19 @@ package body Alire.Config.Edit is and then Utils.Is_Valid_GitHub_Username (Value.As_String); end case; + exit when not Result; + + -- Apply the own builtin check if any. + + if Result and then Ent.Check not in null then + if not Ent.Check (Key, Value) then + Trace.Error + ("Invalid value '" & CLIC.Config.Image (Value) & + "' for builtin configuration '" & Key & "'. " & + "Specific builtin check failed."); + end if; + end if; + exit; end if; end loop; @@ -287,21 +280,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 +287,11 @@ 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'("- " & TTY.Bold (To_String (Ent.Key)) + & " [" & TTY.Emph (Image (Ent.Kind)) & "]" + & "[Default:" & TTY.Terminal (To_String (Ent.Def)) + & "]")); Results.Append (To_String (Ent.Help)); Results.Append (""); end loop; @@ -325,9 +305,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 4780579c..5157573f 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. @@ -84,131 +86,4 @@ private -- 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 " & - "maintainers field of a new crate.")), - (+Keys.User_Email, - Cfg_Email, - +("User email address. Used for the authors and" & - " maintainers field of a new crate.")), - (+Keys.User_Github_Login, - Cfg_GitHub_Login, - +("User GitHub login/username. Used to for the maintainers-logins " & - "field of a new crate.")), - - (+Keys.Editor_Cmd, - Cfg_String, - +("Editor command and arguments for editing crate code (alr edit)." & - " The executables and arguments are separated by a single space" & - " character. The token ${GPR_FILE} is replaced by" & - " a path to the project file to open.")), - - (+Keys.Index_Host, - Cfg_String, - +("URL of the community index host, defaults to " - & Defaults.Index_Host)), - - (+Keys.Index_Owner, - Cfg_String, - +("Owner of the index repository (GitHub user/org), defaults to " - & Defaults.Index_Owner)), - - (+Keys.Index_Repo_Name, - Cfg_String, - +("Name of the index repository, defaults to " - & Defaults.Index_Repo_Name)), - - (+Keys.Msys2_Do_Not_Install, - Cfg_Bool, - +("If true, Alire will not try to automatically" & - " install msys2 system package manager. (Windows only)")), - - (+Keys.Msys2_Install_Dir, - Cfg_Absolute_Path, - +("Directory where Alire will detect and/or install" & - " msys2 system package manager. (Windows only)")), - - (+Keys.Msys2_Installer, - Cfg_String, - +("Filename of the executable msys2 installer, " & - "e.g. 'msys2-x86_64-20220319.exe'. (Windows only)")), - - (+Keys.Msys2_Installer_URL, - Cfg_String, - +("URL of the executable msys2 installer, " & - "e.g. 'https://github.com/msys2/msys2-installer/releases/" & - "download/2022-03-19/msys2-x86_64-20220319.exe'. (Windows only)")), - - (+Keys.Update_Manually, - Cfg_Bool, - +("If true, Alire will not attempt to update dependencies even after " - & "the manifest is manually edited, or when no valid solution has " - & "been ever computed. All updates have to be manually requested " - & "through `alr update`")), - - (+Keys.Distribution_Disable_Detection, - Cfg_Bool, - +("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 (^) " - & "for pre-1 dependencies.")), - - (+Keys.Warning_Old_Index, - Cfg_Bool, - +("When unset (default) or true, a warning will be emitted when " & - "using a compatible index with a lower version than the newest" & - " known.")), - - (+Keys.Toolchain_Assistant, - Cfg_Bool, - +("If true, and assistant to select the default toolchain will run " - & "when first needed.")) - - ); - end Alire.Config.Edit; diff --git a/src/alire/alire-config.adb b/src/alire/alire-config.adb new file mode 100644 index 00000000..faba1b59 --- /dev/null +++ b/src/alire/alire-config.adb @@ -0,0 +1,151 @@ +with Alire.Config.Edit; + +package body Alire.Config is + + --------- + -- Get -- + --------- + + function Get (This : Builtin_Option) return String + is (DB.Get_As_String (+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 := ""; + Public : Boolean := True; + 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 + if Public then + All_Builtins.Insert (Key, Result); + end if; + end return; + end New_Builtin; + + ----------------- + -- New_Builtin -- + ----------------- + + function New_Builtin (Key : CLIC.Config.Config_Key; + Def : Boolean; + Help : String := ""; + Public : Boolean := True; + Check : CLIC.Config.Check_Import := null) + return Builtin_Option + is (New_Builtin (Key => Key, + Kind => Cfg_Bool, + Def => Def'Image, + Help => Help, + Public => Public, + Check => Check)); + +end Alire.Config; diff --git a/src/alire/alire-config.ads b/src/alire/alire-config.ads index e5def09f..fd361de5 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,87 +19,89 @@ package Alire.Config with Preelaborate is -- Built-ins -- --------------- - package Keys is + type Builtin_Kind is (Cfg_Int, Cfg_Float, Cfg_Bool, + Cfg_String, Cfg_Absolute_Path, + Cfg_Existing_Absolute_Path, + Cfg_Email, Cfg_GitHub_Login); - use CLIC.Config; + function Image (Kind : Builtin_Kind) return String; - -- A few predefined keys that are used in several places. This list is - -- not exhaustive. + function Is_Builtin (Key : CLIC.Config.Config_Key) return Boolean; - Dependencies_Shared : constant Config_Key := "dependencies.shared"; + function Kind_Of_Builtin (Key : CLIC.Config.Config_Key) return Builtin_Kind + with Pre => Is_Builtin (Key); - Editor_Cmd : constant Config_Key := "editor.cmd"; + type Builtin_Option is tagged private; - Distribution_Disable_Detection : constant Config_Key := - "distribution.disable_detection"; - -- When set to True, distro will be reported as unknown, and in turn no - -- native package manager will be used. + function Key (This : Builtin_Option) return CLIC.Config.Config_Key; - 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. + function Is_Empty (This : Builtin_Option) return Boolean + with Post => Is_Empty'Result = (This.Get = ""); + -- True if undefined or equal to "" (considering its own default) - 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. + function Get (This : Builtin_Option) return String; + function Get (This : Builtin_Option) return Boolean; - Solver_Autonarrow : constant Config_Key := "solver.autonarrow"; - -- When true, `alr with` will substitute "any" dependencies by the - -- appropriate caret/tilde. + procedure Set_Locally (This : Builtin_Option; Value : String); - Toolchain_Assistant : constant Config_Key := "toolchain.assistant"; - -- When true (default), on first `Requires_Workspace`, the - -- assistant to select a gnat compiler and corresponding gprbuild - -- will be launched. + procedure Set_Globally (This : Builtin_Option; Value : String); - Toolchain_External : constant Config_Key := "toolchain.external"; - -- We use this key to store whether a tool in the toolchain requires - -- external detection. It stores a boolean per tool, e.g, for gprbuild: - -- toolchain.external.gprbuild + procedure Set (This : Builtin_Option; + Level : Config.Level; + Value : String); - Toolchain_Use : constant Config_Key := "toolchain.use"; - -- We use this key internally to store the configured tools picked - -- up by the user. Not really intended to be set up by users, so - -- not listed as a built-in. Each tool is a child of this key, - -- e.g.: toolchain.use.gnat, toolchain.use.gprbuild + procedure Set (This : Builtin_Option; + Level : Config.Level; + Value : Boolean); - Update_Manually : constant Config_Key := "update-manually-only"; - -- Used by `get --only` to flag a workspace to not autoupdate itself - -- despite having no solution in the lockfile. + procedure Unset (This : Builtin_Option; + Level : Config.Level); - User_Email : constant Config_Key := "user.email"; - User_Name : constant Config_Key := "user.name"; - User_Github_Login : constant Config_Key := "user.github_login"; + function New_Builtin (Key : CLIC.Config.Config_Key; + Kind : Builtin_Kind; + Def : String := ""; + Help : String := ""; + Public : Boolean := True; + Check : CLIC.Config.Check_Import := null) + return Builtin_Option + with Pre => Help /= "" or not Public; - Warning_Caret : constant Config_Key := "warning.caret"; - -- Set to false to disable warnings about caret/tilde use for ^0 deps. + function New_Builtin (Key : CLIC.Config.Config_Key; + Def : Boolean; + Help : String := ""; + Public : Boolean := True; + Check : CLIC.Config.Check_Import := null) + return Builtin_Option + with Pre => Help /= "" or not Public; - Warning_Old_Index : constant Config_Key := "warning.old_index"; - -- Warn about old but compatible index in use +private - Msys2_Do_Not_Install : constant Config_Key := "msys2.do_not_install"; - Msys2_Install_Dir : constant Config_Key := "msys2.install_dir"; - Msys2_Installer : constant Config_Key := "msys2.installer"; - Msys2_Installer_URL : constant Config_Key := "msys2.installer_url"; - end Keys; + 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; -------------- - -- Defaults -- + -- Is_Empty -- -------------- - package Defaults is + function Is_Empty (This : Builtin_Option) return Boolean + is (This.Get = ""); - Dependencies_Shared : constant Boolean := False; - -- TODO: enable it when hashing is complete + --------- + -- Key -- + --------- - Index_Host : constant String := "https://github.com"; - Index_Owner : constant String := "alire-project"; - Index_Repo_Name : constant String := "alire-index"; + function Key (This : Builtin_Option) return CLIC.Config.Config_Key + is (+This.Key); - Warning_Old_Index : constant Boolean := True; + package Builtin_Maps is new + Ada.Containers.Indefinite_Ordered_Maps (CLIC.Config.Config_Key, + Builtin_Option); - end Defaults; + All_Builtins : Builtin_Maps.Map; end Alire.Config; diff --git a/src/alire/alire-index.ads b/src/alire/alire-index.ads index 753b6962..b0be96a0 100644 --- a/src/alire/alire-index.ads +++ b/src/alire/alire-index.ads @@ -1,7 +1,7 @@ private with Alire_Early_Elaboration; pragma Unreferenced (Alire_Early_Elaboration); -with Alire.Config; pragma Unreferenced (Alire.Config); +with Alire.Config.Builtins; with Alire.Crates.Containers; with Alire.Dependencies; with Alire.Origins; @@ -14,15 +14,12 @@ with Semantic_Versioning.Extended; package Alire.Index is - Community_Host : constant String - := Config.DB.Get (Config.Keys.Index_Host, Config.Defaults.Index_Host); + Community_Host : constant String := Config.Builtins.Index_Host.Get; - Community_Organization : constant String - := Config.DB.Get (Config.Keys.Index_Owner, Config.Defaults.Index_Owner); + Community_Organization : constant String := Config.Builtins.Index_Owner.Get; Community_Repo_Name : constant String - := Config.DB.Get (Config.Keys.Index_Repo_Name, - Config.Defaults.Index_Repo_Name); + := Config.Builtins.Index_Repository_Name.Get; Community_Repo : constant URL := "git+" & Community_Host diff --git a/src/alire/alire-index_on_disk-loading.adb b/src/alire/alire-index_on_disk-loading.adb index 8ef736c7..3053ddb9 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-publish.adb b/src/alire/alire-publish.adb index f85aae6c..9b773bb7 100644 --- a/src/alire/alire-publish.adb +++ b/src/alire/alire-publish.adb @@ -3,7 +3,7 @@ with Ada.Text_IO; with AAA.Strings; -with Alire.Config; +with Alire.Config.Builtins; with Alire.Crates; with Alire.Environment; with Alire.Errors; @@ -524,12 +524,12 @@ package body Alire.Publish is then raise Early_Stop; end if; - elsif Config.DB.Defined (Config.Keys.User_Github_Login) then + elsif not Config.Builtins.User_Github_Login.Is_Empty then Put_Info ("Please upload this file to " & TTY.URL (Index.Community_Host & "/" - & Config.DB.Get (Config.Keys.User_Github_Login, "") & "/" + & Config.Builtins.User_Github_Login.Get & "/" & Index.Community_Repo_Name & "/upload/" & Index.Community_Branch & "/" @@ -758,7 +758,7 @@ package body Alire.Publish is -- User has an account - if not Config.DB.Defined (Config.Keys.User_Github_Login) then + if Config.Builtins.User_Github_Login.Is_Empty then Put_Info ("Publishing to the community index" & " requires a GitHub account."); else diff --git a/src/alire/alire-releases.adb b/src/alire/alire-releases.adb index 7376cddb..5d605074 100644 --- a/src/alire/alire-releases.adb +++ b/src/alire/alire-releases.adb @@ -1,7 +1,7 @@ with Ada.Directories; with Ada.Text_IO; -with Alire.Config; +with Alire.Config.Builtins; with Alire.Crates; with Alire.Directories; with Alire.Defaults; @@ -85,7 +85,7 @@ package body Alire.Releases is Newline : constant String := ASCII.LF & " "; begin for Dep of This.Flat_Dependencies loop - if Config.DB.Get (Config.Keys.Warning_Caret, Default => True) + if Config.Builtins.Warning_Caret.Get and then AAA.Strings.Contains (Dep.Versions.Image, "^0") then @@ -98,7 +98,7 @@ package body Alire.Releases is & "The suspicious dependency is: " & TTY.Version (Dep.Image) & Newline & "You can disable this warning by setting the option " - & TTY.Emph (Config.Keys.Warning_Caret) & " to false.", + & TTY.Emph (Config.Builtins.Warning_Caret.Key) & " to false.", Warnings.Caret_Or_Tilde); return True; end if; diff --git a/src/alire/alire-solutions.adb b/src/alire/alire-solutions.adb index df717b34..02797e69 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; diff --git a/src/alire/alire-toml_index.adb b/src/alire/alire-toml_index.adb index 216ae695..20a53b43 100644 --- a/src/alire/alire-toml_index.adb +++ b/src/alire/alire-toml_index.adb @@ -1,6 +1,6 @@ with Ada.Directories; -with Alire.Config; +with Alire.Config.Builtins; with Alire.Crates; with Alire.Directories; with Alire.TOML_Adapters; @@ -107,8 +107,7 @@ package body Alire.TOML_Index is use type Semantic_Versioning.Version; Warn_Of_Old_Compatible : constant Boolean := - Config.DB.Get (Config.Keys.Warning_Old_Index, - Config.Defaults.Warning_Old_Index); + Config.Builtins.Warning_Old_Index.Get; ---------------------- -- Compare_Branches -- @@ -125,7 +124,7 @@ package body Alire.TOML_Index is & TTY.Emph (Alire.Index.Branch_Kind) & "' but your community index branch is '" & TTY.Emph (Local) & "'", - Disable_Config => Config.Keys.Warning_Old_Index); + Disable_Config => Config.Builtins.Warning_Old_Index.Key); Suggest_Update := True; end if; end Compare_Branches; @@ -183,7 +182,8 @@ package body Alire.TOML_Index is & "' version (" & Version.Image & ") is older than the newest supported by alr (" & Alire.Index.Version.Image & ")", - Disable_Config => Config.Keys.Warning_Old_Index); + Disable_Config => + Config.Builtins.Warning_Old_Index.Key); Suggest_Update := True; elsif not Alire.Index.Valid_Versions.Contains (Version) then diff --git a/src/alire/alire-toolchains.adb b/src/alire/alire-toolchains.adb index 9fc3e7d8..5484e6d5 100644 --- a/src/alire/alire-toolchains.adb +++ b/src/alire/alire-toolchains.adb @@ -415,9 +415,7 @@ package body Alire.Toolchains is procedure Set_Automatic_Assistant (Enabled : Boolean; Level : Config.Level) is begin - Config.Edit.Set_Boolean (Level, - Config.Keys.Toolchain_Assistant, - Enabled); + Config.Builtins.Toolchain_Assistant.Set (Level, Enabled); end Set_Automatic_Assistant; ------------------------ diff --git a/src/alire/alire-toolchains.ads b/src/alire/alire-toolchains.ads index ac4ee2f4..4b232b45 100644 --- a/src/alire/alire-toolchains.ads +++ b/src/alire/alire-toolchains.ads @@ -2,7 +2,7 @@ with Ada.Containers.Indefinite_Ordered_Sets; with AAA.Strings; -with Alire.Config; +with Alire.Config.Builtins; with Alire.Dependencies; with Alire.Errors; with Alire.Milestones; @@ -153,7 +153,7 @@ private ----------------------- function Assistant_Enabled return Boolean - is (Config.DB.Get (Config.Keys.Toolchain_Assistant, Default => True)); + is (Config.Builtins.Toolchain_Assistant.Get); ---------------------- -- Tool_Is_External -- @@ -175,8 +175,8 @@ private then Tool_Key (GNAT_Crate, Kind) else CLIC.Config.Config_Key ((case Kind is - when For_Use => Config.Keys.Toolchain_Use, - when For_Is_External => Config.Keys.Toolchain_External) + when For_Use => Config.Builtins.Toolchain_Use.Key, + when For_Is_External => Config.Builtins.Toolchain_External.Key) & "." & Crate.As_String)); -------------------- diff --git a/src/alire/os_windows/alire-config-builtins-windows.ads b/src/alire/os_windows/alire-config-builtins-windows.ads new file mode 100644 index 00000000..c787506f --- /dev/null +++ b/src/alire/os_windows/alire-config-builtins-windows.ads @@ -0,0 +1,41 @@ +with Alire.Platforms.Folders; + +package Alire.Config.Builtins.Windows is + + Default_Msys2_Installer : constant String := "msys2-x86_64-20221216.exe"; + Default_Msys2_Installer_URL : constant String := + "https://github.com/msys2/msys2-installer/releases/download/2022-12-16/" + & Default_Msys2_Installer; + + -- MSYS2 + + Msys2_Do_Not_Install : constant Builtin := New_Builtin + (Key => "msys2.do_not_install", + Def => False, + Help => + "If true, Alire will not try to automatically" & + " install msys2 system package manager. (Windows only)"); + + Msys2_Install_Dir : constant Builtin := New_Builtin + (Key => "msys2.install_dir", + Kind => Cfg_Absolute_Path, + Def => Platforms.Folders.Cache / "msys64", + Help => + "Directory where Alire will detect and/or install" & + " msys2 system package manager. (Windows only)"); + + Msys2_Installer : constant Builtin := New_Builtin + (Key => "msys2.installer", + Kind => Cfg_String, + Def => Default_Msys2_Installer, + Help => + "Filename of the executable msys2 installer. (Windows only)"); + + Msys2_Installer_URL : constant Builtin := New_Builtin + (Key => "msys2.installer_url", + Kind => Cfg_String, + Def => Default_Msys2_Installer_URL, + Help => + "URL of the executable msys2 installer. (Windows only)"); + +end Alire.Config.Builtins.Windows; diff --git a/src/alire/os_windows/alire-platforms-current__windows.adb b/src/alire/os_windows/alire-platforms-current__windows.adb index d56796f4..0151159b 100644 --- a/src/alire/os_windows/alire-platforms-current__windows.adb +++ b/src/alire/os_windows/alire-platforms-current__windows.adb @@ -5,9 +5,7 @@ with AAA.Strings; with Alire.Environment; with Alire.OS_Lib; use Alire.OS_Lib; -with Alire.Config; -with Alire.Config.Edit; -with Alire.Platforms.Folders; +with Alire.Config.Builtins.Windows; with Alire.Errors; with GNATCOLL.VFS; @@ -16,13 +14,6 @@ with CLIC.User_Input; package body Alire.Platforms.Current is - package Cfg renames Config; - - Default_Msys2_Installer : constant String := "msys2-x86_64-20221216.exe"; - Default_Msys2_Installer_URL : constant String := - "https://github.com/msys2/msys2-installer/releases/download/2022-12-16/" - & Default_Msys2_Installer; - -- Windows implementation Distrib_Detected : Boolean := False; @@ -172,7 +163,7 @@ package body Alire.Platforms.Current is use CLIC.User_Input; begin - if Cfg.DB.Get (Cfg.Keys.Msys2_Do_Not_Install, False) then + if Config.Builtins.Windows.Msys2_Do_Not_Install.Get then -- User already requested that msys2 should not be installed @@ -207,8 +198,7 @@ package body Alire.Platforms.Current is Default => No) = Yes then -- Save user choice in the global config - Cfg.Edit.Set_Globally (Key => Cfg.Keys.Msys2_Do_Not_Install, - Value => "true"); + Config.Builtins.Windows.Msys2_Do_Not_Install.Set_Globally ("true"); end if; -- We are not allowed to install @@ -263,10 +253,10 @@ package body Alire.Platforms.Current is end Download_File; Msys2_Installer : constant String := - Cfg.DB.Get (Cfg.Keys.Msys2_Installer, Default_Msys2_Installer); + Config.Builtins.Windows.Msys2_Installer.Get; Msys2_Installer_URL : constant String := - Cfg.DB.Get (Cfg.Keys.Msys2_Installer_URL, Default_Msys2_Installer_URL); + Config.Builtins.Windows.Msys2_Installer_URL.Get; Result : Alire.Outcome; begin @@ -298,22 +288,17 @@ package body Alire.Platforms.Current is return Alire.Outcome_Failure ("Cannot setup msys2 environment"); end; - if not Cfg.DB.Defined (Cfg.Keys.Msys2_Install_Dir) then + if Config.Builtins.Windows.Msys2_Install_Dir.Is_Empty then -- Save msys2 install dir in the global config - Cfg.Edit.Set_Globally (Key => Cfg.Keys.Msys2_Install_Dir, - Value => Install_Dir); + Config.Builtins.Windows.Msys2_Install_Dir.Set_Globally (Install_Dir); end if; -- Load msys2 environment to attempt first full update according to -- official setup instructions at: -- https://www.msys2.org/wiki/MSYS2-installation/ declare - Default_Install_Dir : constant Alire.Absolute_Path := - Platforms.Folders.Cache / "msys64"; - Cfg_Install_Dir : constant String := - Cfg.DB.Get (Cfg.Keys.Msys2_Install_Dir, - Default_Install_Dir); + Config.Builtins.Windows.Msys2_Install_Dir.Get; begin Set_Msys2_Env (Cfg_Install_Dir); end; @@ -384,12 +369,8 @@ package body Alire.Platforms.Current is procedure Setup_Msys2 is Result : Alire.Outcome; - Default_Install_Dir : constant Alire.Absolute_Path := - Platforms.Folders.Cache / "msys64"; - Cfg_Install_Dir : constant String := - Cfg.DB.Get (Cfg.Keys.Msys2_Install_Dir, - Default_Install_Dir); + Config.Builtins.Windows.Msys2_Install_Dir.Get; Pacman : constant String := Alire.OS_Lib.Subprocess.Locate_In_Path ("pacman"); diff --git a/src/alr/alr-commands-edit.adb b/src/alr/alr-commands-edit.adb index b976c016..4dbdd425 100644 --- a/src/alr/alr-commands-edit.adb +++ b/src/alr/alr-commands-edit.adb @@ -1,7 +1,7 @@ with Ada.Containers; with Alire; use Alire; -with Alire.Config; +with Alire.Config.Builtins; with Alire.OS_Lib.Subprocess; with Alire.Platforms.Current; @@ -61,8 +61,10 @@ package body Alr.Commands.Edit is use GNAT.Strings; use Alire.Config; + package Builtins renames Alire.Config.Builtins; + Editor_Cmd : constant String := - Alire.Config.DB.Get (Keys.Editor_Cmd, "gnatstudio -P ${GPR_FILE}"); + Builtins.Editor_Cmd.Get; Edit_Args : AAA.Strings.Vector := AAA.Strings.Split (Editor_Cmd, ' '); begin @@ -72,7 +74,8 @@ package body Alr.Commands.Edit is if Edit_Args.Is_Empty then Reportaise_Command_Failed - ("No editor defined in config key '" & Keys.Editor_Cmd & "'."); + ("No editor defined in config key '" + & Builtins.Editor_Cmd.Key & "'."); end if; Cmd.Requires_Workspace; diff --git a/src/alr/alr-commands-get.adb b/src/alr/alr-commands-get.adb index 8998883e..0ecf8e37 100644 --- a/src/alr/alr-commands-get.adb +++ b/src/alr/alr-commands-get.adb @@ -1,6 +1,6 @@ with Ada.Directories; -with Alire.Config.Edit; +with Alire.Config.Builtins; with Alire.Dependencies; with Alire.Directories; with Alire.Index; @@ -161,10 +161,8 @@ package body Alr.Commands.Get is Trace.Info ("Because --only was used, automatic dependency" & " retrieval is disabled in this workspace:" & " use `alr update` to apply dependency changes"); - Alire.Config.Edit.Set_Boolean - (Alire.Config.Local, - Alire.Config.Keys.Update_Manually, - True); + Alire.Config.Builtins.Update_Manually_Only.Set (Alire.Config.Local, + True); if not CLIC.User_Input.Not_Interactive then Alire.Roots.Print_Nested_Crates (Cmd.Root.Path); diff --git a/src/alr/alr-commands-init.adb b/src/alr/alr-commands-init.adb index e0105901..30438c76 100644 --- a/src/alr/alr-commands-init.adb +++ b/src/alr/alr-commands-init.adb @@ -4,7 +4,7 @@ with Ada.Directories; with Ada.Wide_Wide_Text_IO; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; -with Alire.Config; +with Alire.Config.Builtins; with Alire.Utils.User_Input.Query_Config; with CLIC.User_Input; @@ -242,9 +242,9 @@ package body Alr.Commands.Init is procedure Generate_Manifest is use Alire.Config; begin - if not DB.Defined (Keys.User_Email) or else - not DB.Defined (Keys.User_Name) or else - not DB.Defined (Keys.User_Github_Login) + if Builtins.User_Email.Is_Empty or else + Builtins.User_Name.Is_Empty or else + Builtins.User_Github_Login.Is_Empty then AAA.Text_IO.Put_Paragraph ("Alire needs some user information to initialize the crate" diff --git a/src/alr/alr-commands.adb b/src/alr/alr-commands.adb index 87686a26..a4690fd5 100644 --- a/src/alr/alr-commands.adb +++ b/src/alr/alr-commands.adb @@ -9,6 +9,7 @@ with CLIC.User_Input; with Alire.Platforms; with Alire_Early_Elaboration; +with Alire.Config.Builtins; with Alire.Config.Edit; with Alire.Errors; with Alire.Index_On_Disk.Loading; @@ -288,8 +289,7 @@ package body Alr.Commands is Unchecked : Alire.Roots.Optional.Root renames Cmd.Optional_Root; Manual_Only : constant Boolean := - Alire.Config.DB.Get - (Alire.Config.Keys.Update_Manually, False); + Alire.Config.Builtins.Update_Manually_Only.Get; package Conf renames Alire.Config; begin -- 2.39.5