Skip to content

Commit

Permalink
Override distro via config key
Browse files Browse the repository at this point in the history
  • Loading branch information
mosteo committed Mar 3, 2024
1 parent 2bf0f9e commit acb8cb2
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 6 deletions.
16 changes: 15 additions & 1 deletion src/alire/alire-config-builtins.ads
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
with Alire.Config.Checks;

package Alire.Config.Builtins is

subtype Builtin is Builtin_Option;
Expand Down Expand Up @@ -32,12 +34,24 @@ package Alire.Config.Builtins is
& "location inside the global cache. When false, "
& "dependencies are sandboxed in each workspace.");

-- DISTRIBUTION

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.");
& " attempt to use the system package manager. Takes precedence over "
& " distribution.override.");

Distribution_Override : constant Builtin := New_Builtin
(Key => "distribution.override",
Kind => Cfg_String,
Check => Checks.Valid_Distro'Access,
Def => "",
Help =>
"Distribution name to be used instead of autodetection. No effect if "
& "distribution.disable_detection is True.");

-- EDITOR

Expand Down
20 changes: 20 additions & 0 deletions src/alire/alire-config-checks.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
with AAA.Enum_Tools;

with Alire.Platforms;

package body Alire.Config.Checks is

function Is_Valid is
new AAA.Enum_Tools.Is_Valid (Alire.Platforms.Known_Distributions);

------------------
-- Valid_Distro --
------------------

function Valid_Distro (Key : CLIC.Config.Config_Key;
Value : TOML.TOML_Value)
return Boolean
is (Value.Kind in TOML.TOML_String
and then Is_Valid (Value.As_String));

end Alire.Config.Checks;
9 changes: 9 additions & 0 deletions src/alire/alire-config-checks.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
with TOML;

package Alire.Config.Checks is

function Valid_Distro (Key : CLIC.Config.Config_Key;
Value : TOML.TOML_Value)
return Boolean;

end Alire.Config.Checks;
36 changes: 31 additions & 5 deletions src/alire/alire-platforms-current.ads
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
private with AAA.Enum_Tools;

private with Alire.Config.Builtins;
limited with Alire.Environment;
private with Alire.OS_Lib.Subprocess;
private with Alire.Platforms.Common;
with Alire.Properties;
private with Alire.Properties.Platform;
private with Alire.Warnings;

private with System;

Expand Down Expand Up @@ -50,8 +54,9 @@ package Alire.Platforms.Current is
Disable_Distribution_Detection : Boolean := False with Atomic;

function Distribution return Platforms.Distributions;
-- Cooked distribution that may return Unknown if detection was disabled
-- via config.
-- Cooked distribution that may return Unknown if detection was
-- disabled via config, or a different distribution if config key
-- distribution.override is set.

function Distribution_Is_Known return Boolean is
(Platforms."/=" (Distribution, Platforms.Distribution_Unknown));
Expand All @@ -70,14 +75,35 @@ package Alire.Platforms.Current is

private

function Is_Valid_Distro is
new AAA.Enum_Tools.Is_Valid (Platforms.Known_Distributions);

function Return_With_Warning is
new Warnings.Warn_With_Result (Platforms.Distributions);

------------------
-- Distribution --
------------------

function Distribution return Platforms.Distributions
is (if Disable_Distribution_Detection
then Platforms.Distribution_Unknown
else Detected_Distribution);
is (
-- Disabled detection
if Disable_Distribution_Detection then
Platforms.Distribution_Unknown

-- Overridden detection
elsif Config.Builtins.Distribution_Override.Get /= "" then
(if Is_Valid_Distro (Config.Builtins.Distribution_Override.Get) then
Distributions'Value (Config.Builtins.Distribution_Override.Get)
else
Return_With_Warning
("Invalid distribution override: "
& Config.Builtins.Distribution_Override.Get,
Result => Platforms.Distribution_Unknown))

-- Regular detection
else
Detected_Distribution);

-----------------------
-- Host_Architecture --
Expand Down
14 changes: 14 additions & 0 deletions src/alire/alire-warnings.adb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,18 @@ package body Alire.Warnings is
function Already_Warned (Id : Warning_Id) return Boolean
is (Already_Emitted.Contains (String (Id)));

----------------------
-- Warn_With_Result --
----------------------

function Warn_With_Result (Text : String;
Result : Returned;
Level : Trace.Levels := Trace.Warning)
return Returned
is
begin
Trace.Log (Text, Level);
return Result;
end Warn_With_Result;

end Alire.Warnings;
9 changes: 9 additions & 0 deletions src/alire/alire-warnings.ads
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ package Alire.Warnings with Preelaborate is
function Already_Warned (Id : Warning_Id) return Boolean;
-- Says if a warning has been already emitted in the current run

generic
type Returned (<>) is private;
function Warn_With_Result (Text : String;
Result : Returned;
Level : Trace.Levels := Trace.Warning)
return Returned;
-- Return Result after printing Text; for use in expressions. See instances
-- in Alire.Warnings.Typed.

------------------
-- Defined Ids --
------------------
Expand Down
23 changes: 23 additions & 0 deletions testsuite/tests/config/distro-override/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Verify that distro overridding works as intended
"""

from drivers.alr import run_alr, distro_is_known
from drivers.asserts import assert_match

# Overriding distro detection. We force Debian as our tests run in Ubuntu and
# many other distros so it is only giving a false positive on Debian.
run_alr("config", "--global",
"--set", "distribution.override", "debian")

assert_match(".*distribution:[^\n]*DEBIAN",
run_alr("version").out)

# Disabling distro detection takes precedence
run_alr("config", "--global",
"--set", "distribution.disable_detection", "true")

assert_match(".*distribution:[^\n]*DISTRIBUTION_UNKNOWN",
run_alr("version").out)

print('SUCCESS')
3 changes: 3 additions & 0 deletions testsuite/tests/config/distro-override/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
driver: python-script
indexes:
basic_index: {}

0 comments on commit acb8cb2

Please sign in to comment.