Skip to content

Commit

Permalink
WIP: refactor config builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
mosteo committed Aug 22, 2023
1 parent b7e5d11 commit b6beddf
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 113 deletions.
4 changes: 2 additions & 2 deletions src/alire/alire-builds.adb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
with AAA.Strings;

with Alire.Config.Builtins;
with Alire.Config.Edit;
with Alire.Directories;
with Alire.OS_Lib.Subprocess;
Expand All @@ -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 --
Expand Down
31 changes: 31 additions & 0 deletions src/alire/alire-config-builtins.ads
Original file line number Diff line number Diff line change
@@ -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;
53 changes: 8 additions & 45 deletions src/alire/alire-config-edit.adb
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
-----------------
Expand Down Expand Up @@ -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 =>
Expand Down Expand Up @@ -287,31 +263,17 @@ 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 --
-------------------

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;
Expand All @@ -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;
Expand Down
55 changes: 9 additions & 46 deletions src/alire/alire-config-edit.ads
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 " &
Expand Down Expand Up @@ -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 (^) "
Expand All @@ -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;
146 changes: 146 additions & 0 deletions src/alire/alire-config.adb
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit b6beddf

Please sign in to comment.