From 9b2b79aaf259369f6fd8258950c3faef7757faa1 Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Fri, 16 Feb 2024 11:56:15 +0100 Subject: [PATCH] Report detected system package manager in `alr version` (#1565) * Report the system package manager in `alr version` * Also report distro detection disabling * Use a separate system deployer in unknown systems Instead of defaulting to apt. * Distinguish undetected exec from unknown exec name --- .../alire-origins-deployers-system-apt.ads | 3 ++ ...lire-origins-deployers-system-homebrew.ads | 3 ++ ...lire-origins-deployers-system-macports.ads | 3 ++ .../alire-origins-deployers-system-pacman.ads | 3 ++ ...-origins-deployers-system-rpm_wrappers.adb | 8 ++++ ...-origins-deployers-system-rpm_wrappers.ads | 3 ++ ...alire-origins-deployers-system-unknown.ads | 23 ++++++++++ .../alire-origins-deployers-system-zypper.ads | 3 ++ src/alire/alire-origins-deployers-system.adb | 46 +++++++++++++++++-- src/alire/alire-origins-deployers-system.ads | 21 ++++++++- src/alire/alire-os_lib.ads | 3 +- src/alire/alire.ads | 5 ++ src/alr/alr-commands-version.adb | 20 ++++++++ 13 files changed, 137 insertions(+), 7 deletions(-) create mode 100644 src/alire/alire-origins-deployers-system-unknown.ads diff --git a/src/alire/alire-origins-deployers-system-apt.ads b/src/alire/alire-origins-deployers-system-apt.ads index 613762d0..3f382dd7 100644 --- a/src/alire/alire-origins-deployers-system-apt.ads +++ b/src/alire/alire-origins-deployers-system-apt.ads @@ -12,4 +12,7 @@ package Alire.Origins.Deployers.System.Apt is overriding function Install (This : Deployer) return Outcome; + overriding + function Executable_Name (This : Deployer) return String is ("apt"); + end Alire.Origins.Deployers.System.Apt; diff --git a/src/alire/alire-origins-deployers-system-homebrew.ads b/src/alire/alire-origins-deployers-system-homebrew.ads index 2cff8fae..2a7ca44d 100644 --- a/src/alire/alire-origins-deployers-system-homebrew.ads +++ b/src/alire/alire-origins-deployers-system-homebrew.ads @@ -12,4 +12,7 @@ package Alire.Origins.Deployers.System.Homebrew is overriding function Install (This : Deployer) return Outcome; + overriding + function Executable_Name (This : Deployer) return String is ("brew"); + end Alire.Origins.Deployers.System.Homebrew; diff --git a/src/alire/alire-origins-deployers-system-macports.ads b/src/alire/alire-origins-deployers-system-macports.ads index 7c7fc92d..ad9c50cb 100644 --- a/src/alire/alire-origins-deployers-system-macports.ads +++ b/src/alire/alire-origins-deployers-system-macports.ads @@ -12,4 +12,7 @@ package Alire.Origins.Deployers.System.Macports is overriding function Install (This : Deployer) return Outcome; + overriding + function Executable_Name (This : Deployer) return String is ("port"); + end Alire.Origins.Deployers.System.Macports; diff --git a/src/alire/alire-origins-deployers-system-pacman.ads b/src/alire/alire-origins-deployers-system-pacman.ads index d2fed362..273df11d 100644 --- a/src/alire/alire-origins-deployers-system-pacman.ads +++ b/src/alire/alire-origins-deployers-system-pacman.ads @@ -12,4 +12,7 @@ package Alire.Origins.Deployers.System.Pacman is overriding function Install (This : Deployer) return Outcome; + overriding + function Executable_Name (This : Deployer) return String is ("pacman"); + end Alire.Origins.Deployers.System.Pacman; diff --git a/src/alire/alire-origins-deployers-system-rpm_wrappers.adb b/src/alire/alire-origins-deployers-system-rpm_wrappers.adb index 9c615ed5..3ab389a9 100644 --- a/src/alire/alire-origins-deployers-system-rpm_wrappers.adb +++ b/src/alire/alire-origins-deployers-system-rpm_wrappers.adb @@ -18,6 +18,14 @@ package body Alire.Origins.Deployers.System.RPM_Wrappers is when Dnf => "dnf", when Yum => "yum"); + --------------------- + -- Executable_Name -- + --------------------- + + overriding + function Executable_Name (This : Deployer) return String + is (This.Wrapper_Command); + ------------------------------ -- Package_Name_With_Archit -- ------------------------------ diff --git a/src/alire/alire-origins-deployers-system-rpm_wrappers.ads b/src/alire/alire-origins-deployers-system-rpm_wrappers.ads index 2b4fc452..6b4871bf 100644 --- a/src/alire/alire-origins-deployers-system-rpm_wrappers.ads +++ b/src/alire/alire-origins-deployers-system-rpm_wrappers.ads @@ -16,4 +16,7 @@ package Alire.Origins.Deployers.System.RPM_Wrappers is overriding function Install (This : Deployer) return Outcome; + overriding + function Executable_Name (This : Deployer) return String; + end Alire.Origins.Deployers.System.RPM_Wrappers; diff --git a/src/alire/alire-origins-deployers-system-unknown.ads b/src/alire/alire-origins-deployers-system-unknown.ads new file mode 100644 index 00000000..65e93df0 --- /dev/null +++ b/src/alire/alire-origins-deployers-system-unknown.ads @@ -0,0 +1,23 @@ +package Alire.Origins.Deployers.System.Unknown is + + type Deployer is new Deployers.System.Deployer with null record; + + overriding + function Already_Installed (This : Deployer) return Boolean + is (raise Program_Error with "Should be unreachable"); + + overriding + function Detect (This : Deployer) + return Version_Outcomes.Outcome + is (Version_Outcomes.Outcome_Failure + ("unknown distro, cannot detect versions of system packages")); + + overriding + function Install (This : Deployer) return Outcome + is (Outcome_Failure ("unknown distro, no system deployer")); + + overriding + function Executable_Name (This : Deployer) return String is (""); + -- Must be "" so no detection is attempted + +end Alire.Origins.Deployers.System.Unknown; diff --git a/src/alire/alire-origins-deployers-system-zypper.ads b/src/alire/alire-origins-deployers-system-zypper.ads index 63ae43f3..cadc3ffb 100644 --- a/src/alire/alire-origins-deployers-system-zypper.ads +++ b/src/alire/alire-origins-deployers-system-zypper.ads @@ -12,4 +12,7 @@ package Alire.Origins.Deployers.System.Zypper is overriding function Install (This : Deployer) return Outcome; + overriding + function Executable_Name (This : Deployer) return String is ("zypper"); + end Alire.Origins.Deployers.System.Zypper; diff --git a/src/alire/alire-origins-deployers-system.adb b/src/alire/alire-origins-deployers-system.adb index efecf97c..8ef2db99 100644 --- a/src/alire/alire-origins-deployers-system.adb +++ b/src/alire/alire-origins-deployers-system.adb @@ -3,8 +3,9 @@ with Alire.Origins.Deployers.System.Homebrew; with Alire.Origins.Deployers.System.Macports; with Alire.Origins.Deployers.System.Pacman; with Alire.Origins.Deployers.System.RPM_Wrappers; +with Alire.Origins.Deployers.System.Unknown; with Alire.Origins.Deployers.System.Zypper; -with Alire.Platforms.Current; +with Alire.OS_Lib; with CLIC.User_Input; @@ -86,9 +87,12 @@ package body Alire.Origins.Deployers.System is -- Platform_Deployer -- ----------------------- - function Platform_Deployer (From : Origins.Origin) return Deployer'Class is - (case Platforms.Distro_Manager (Platforms.Current.Distribution) is - when Platforms.Apt | Platforms.Packager_Unknown => + function Platform_Deployer + (From : Origins.Origin; + Distro : Platforms.Distributions := Platforms.Current.Distribution) + return Deployer'Class + is (case Platforms.Distro_Manager (Distro) is + when Platforms.Apt => System.Apt.Deployer'(Deployers.Deployer'(Base => From) with others => <>), when Platforms.Pacman => @@ -112,7 +116,11 @@ package body Alire.Origins.Deployers.System is with others => <>), when Platforms.Macports => System.Macports.Deployer'(Deployers.Deployer'(Base => From) - with others => <>)); + with others => <>), + when Platforms.Packager_Unknown => + System.Unknown.Deployer'(Deployers.Deployer'(Base => From) + with others => <>) + ); -- NOTE: add here other native package managers as they get -- implemented. @@ -125,4 +133,32 @@ package body Alire.Origins.Deployers.System is This.Ask_Permission := False; end Dont_Ask_Permission; + --------------------- + -- Executable_Name -- + --------------------- + + function Executable_Name return String is + Make : constant Origin := New_System ("make"); + -- We use a mock system package to be able to obtain a deployer. It + -- doesn't matter if this system package doesn't exist. + begin + return Platform_Deployer + (Make, + Distro => Platforms.Current.Detected_Distribution) + .Executable_Name; + end Executable_Name; + + --------------------- + -- Executable_Path -- + --------------------- + + function Executable_Path return Optional_Absolute_Path is + begin + if Executable_Name /= "" then + return OS_Lib.Locate_Exec_On_Path (Executable_Name); + else + return ""; + end if; + end Executable_Path; + end Alire.Origins.Deployers.System; diff --git a/src/alire/alire-origins-deployers-system.ads b/src/alire/alire-origins-deployers-system.ads index 2be0ee58..c9d4c28c 100644 --- a/src/alire/alire-origins-deployers-system.ads +++ b/src/alire/alire-origins-deployers-system.ads @@ -1,4 +1,5 @@ with Alire.Outcomes.Definite; +with Alire.Platforms.Current; with Semantic_Versioning; @@ -46,16 +47,34 @@ package Alire.Origins.Deployers.System is -- This procedure tells the deployer not to ask user permission before -- deployment. + not overriding + function Executable_Name (This : Deployer) return String is abstract; + -- Must return the name of the executable used for installations (apt, + -- pacman, etc). For platforms that use several, the top-most wrapper + -- should be returned (i.e., apt instead of dpkg, yum instead of rpm...). + ------------- -- Factory -- ------------- - function Platform_Deployer (From : Origins.Origin) return Deployer'Class + function Platform_Deployer + (From : Origins.Origin; + Distro : Platforms.Distributions := Platforms.Current.Distribution) + return Deployer'Class with Pre => From.Is_System; + -- Returns the deployer for a current system. Defaults to current one, + -- unless distro detection is disabled. function Platform_Deployer (Package_Name : String) return Deployer'Class is (Platform_Deployer (Origins.New_System (Package_Name))); + function Executable_Name return String; + -- Returns the simple name of the executable package manager on the system + + function Executable_Path return Optional_Absolute_Path; + -- Identifies the full path to the package manager executable found in the + -- current platform, even if distro detection is disabled. + private type Deployer is abstract new Deployers.Deployer with record diff --git a/src/alire/alire-os_lib.ads b/src/alire/alire-os_lib.ads index 81f99d53..6d7d0f12 100644 --- a/src/alire/alire-os_lib.ads +++ b/src/alire/alire-os_lib.ads @@ -17,6 +17,7 @@ package Alire.OS_Lib with Preelaborate is procedure Setenv (Name : String; Value : String); function Locate_Exec_On_Path (Exec_Name : String) return String; - -- Return the location of an executable if found on PATH, or "" otherwise + -- Return the location of an executable if found on PATH, or "" otherwise. + -- On Windows, no need to append ".exe" as it will be found without it. end Alire.OS_Lib; diff --git a/src/alire/alire.ads b/src/alire/alire.ads index 26773475..d18789a0 100644 --- a/src/alire/alire.ads +++ b/src/alire/alire.ads @@ -149,6 +149,11 @@ package Alire with Preelaborate is function Absolute_Path_Image (Path : Absolute_Path) return String; -- Needed for later instantiations + subtype Optional_Absolute_Path is Any_Path + with Dynamic_Predicate => + Optional_Absolute_Path = "" or else + Check_Absolute_Path (Optional_Absolute_Path); + subtype Unbounded_Absolute_Path is UString with Dynamic_Predicate => +Unbounded_Absolute_Path = "" or else diff --git a/src/alr/alr-commands-version.adb b/src/alr/alr-commands-version.adb index 8c4e702f..16629f42 100644 --- a/src/alr/alr-commands-version.adb +++ b/src/alr/alr-commands-version.adb @@ -4,6 +4,7 @@ with Alire.Directories; with Alire.Index; with Alire.Index_On_Disk.Loading; with Alire.Milestones; +with Alire.Origins.Deployers.System; with Alire.Paths.Vault; with Alire.Platforms.Folders; with Alire.Properties; @@ -116,6 +117,25 @@ package body Alr.Commands.Version is end loop; end; + declare + System_Manager : constant String := + Origins.Deployers.System.Executable_Path; + begin + Table + .Append ("system package manager:") + .Append (if System_Manager /= "" + then System_Manager + else "not found: " + & (if Origins.Deployers.System.Executable_Name /= "" + then "`" & Origins.Deployers.System.Executable_Name & "`" + else "unknown package manager")) + .New_Row; + Table + .Append ("distro detection disabled:") + .Append (Platforms.Current.Disable_Distribution_Detection'Image) + .New_Row; + end; + Table.Append ("").New_Row; Table.Append ("WORKSPACE").New_Row; -- 2.39.5