Skip to content

Commit

Permalink
Refactor configuration builtins
Browse files Browse the repository at this point in the history
Gives them a tagged type that simplifies use and aglutinates default,
description, checks, which were previously not formally required or scattered.
  • Loading branch information
mosteo committed Aug 23, 2023
1 parent b7e5d11 commit 823f4e4
Show file tree
Hide file tree
Showing 21 changed files with 486 additions and 309 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
149 changes: 149 additions & 0 deletions src/alire/alire-config-builtins.ads
Original file line number Diff line number Diff line change
@@ -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;
72 changes: 26 additions & 46 deletions src/alire/alire-config-edit.adb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
with Ada.Text_IO;

with Alire.Config.Builtins;
with Alire.Environment;
with Alire.Paths;
with Alire.Platforms.Folders;
Expand Down Expand Up @@ -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 --
-----------------
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -287,31 +280,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 +304,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
Loading

0 comments on commit 823f4e4

Please sign in to comment.