-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Homebrew as the system deployer for macOS. (#1185)
* Implement Homebrew as the system deployer for macOS. * src/alire/alire-origins-deployers-system-homebrew.adb: new. * src/alire/alire-origins-deployers-system-homebrew.ads: new. * src/alire/alire-origins-deployers-system.adb (Platform_Deployer): When the platform distro manager is Homebrew, return Homebrew.Deployer. * src/alire/alire-platforms.ads (Distributions): include macOS. (Package_Managers): add Homebrew. (Distro_Manager): for macOS, use Homebrew. * src/alire/alire-utils-tools.adb (System_Package_For_Tool): treat MacOS like other distributions. * src/alire/os_macos/alire-platforms-current__macos.adb (Detected_Distribution): return Platforms.MacOS. * testsuite/drivers/helpers.py (on_macos()): new. (distribution()): return 'MACOS' if on_macos() is True. ; Squashed commit of the following: ; commit 0110299ceb66f58cd4793fd78a69fc5b100b4ccf ; Author: Simon Wright <[email protected]> ; Date: Fri Sep 9 17:45:04 2022 +0100 ; Restore test to correct sense. ; commit d396b6b54a73bbbfd59071539bf6268bccce3cf0 ; Author: Simon Wright <[email protected]> ; Date: Fri Sep 9 17:34:54 2022 +0100 ; Replace assertions in Homebrew deployer with exceptions ; commit d7603110b9543bdaac9516fa844599e87fac70aa ; Author: Simon Wright <[email protected]> ; Date: Mon Sep 5 14:14:03 2022 +0100 ; Recognise macOS as a distribution. ; * testsuite/drivers/helpers.py (on_macos()): new. ; (distribution()): check on_macos(). Return 'MACOS' if true. ; commit 8703acfeb8fdf336d862cefc16706eac5d3f0758 ; Author: Simon Wright <[email protected]> ; Date: Sun Sep 4 17:33:13 2022 +0100 ; Use 'brew info' to get available, installed versions. ; commit 8b22b904e19d64fecb89233e6527c2faf655d982 ; Author: Simon Wright <[email protected]> ; Date: Wed Aug 31 15:54:51 2022 +0100 ; Continuing. ; commit a9821ed9a605e30b5699a49df6fffd11224ab931 ; Author: Simon Wright <[email protected]> ; Date: Mon Aug 22 17:09:48 2022 +0100 ; First changes for macOS Homebrew. * Implement Homebrew for MacOS: response to comments on PR#1185 * .github/workflows/ci-macos.yml (Run test script): renamed to "Run test script (without Homebrew)". (Run test script (with Homebrew)): added 'eval $(brew shellenv)' before running the test script, so that HOMEBREW_PREFIX gets defined. * src/alire/alire-origins-deployers-system-homebrew.adb (Homebrew_Prefix, Homebrew_Present): removed. (Get_Info): named the declare block. Removed commented-out exception handler. (Already_Installed): use Get_Info's returned Installed_Version's length to determine the result. (Detect): Get_Version_From_String uses Semantic_Versioning.Parse. Don't check for Homebrew_Present, it has to be or we wouldn't be called. * src/alire/alire-platforms.ads (Distributions): rename MacOS to Homebrew. (Distro_Manager): likewise. * src/alire/alire-utils-tools.adb (System_Package_For_Tool): MacOS (in 'case Distribution)' changed to Homebrew. * src/alire/os_macos/alire-platforms-current__macos.adb (Homebrew_Prefix): new, gets the environment variable "HOMEBREW_PREFIX". (Homebrew_Present): new, true if the environment variable is present. (Detected_Distribution): checks Homebrew_Present, returns Homebrew or Distro_Unknown accordingly. (Distribution_Root): returns Homebrew_Prefix if present, otherwise "/". * testsuite/drivers/helpers.py (distribution()): if on macOS, return HOMEBREW if HOMEBREW_PREFIX is found, DISTRO_UNKNOWN otherwise. * Remove improper pragma in response to comment on #1185 * src/alire/alire-origins-deployers-system-homebrew.adb: removed the innefective pragma Warnings (Off). * Merging with openSUSE changes in #1173. * src/alire/alire-origins-deployers-system.adb: added Homebrew option. * src/alire/alire-platforms.ads: likewise. * src/alire/alire-utils-tools.adb: likewise. Co-authored-by: John Serock <[email protected]> Co-authored-by: GHA <[email protected]>
- Loading branch information
1 parent
256a97d
commit 9842f32
Showing
8 changed files
with
276 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
with AAA.Strings; use AAA.Strings; | ||
|
||
with Alire.OS_Lib.Subprocess; | ||
with Alire.Errors; | ||
|
||
with GNATCOLL.JSON; | ||
|
||
package body Alire.Origins.Deployers.System.Homebrew is | ||
|
||
-- Ada.Strings.Unbounded is use-visible via Alire.Origins. | ||
use GNATCOLL.JSON; | ||
|
||
package Subprocess renames Alire.OS_Lib.Subprocess; | ||
|
||
procedure Get_Info (Package_Name : String; | ||
Available_Version : out Unbounded_String; | ||
Installed_Version : out Unbounded_String); | ||
-- Queries the versions using 'brew info'. | ||
|
||
procedure Get_Info (Package_Name : String; | ||
Available_Version : out Unbounded_String; | ||
Installed_Version : out Unbounded_String) | ||
is | ||
|
||
-- The format of the JSON returned by 'brew info --json=v1 {pkg}' is | ||
-- [ | ||
-- { | ||
-- "stuff": {}, | ||
-- "versions": { | ||
-- "stable": "1.2.4_0", | ||
-- "stuff": {} | ||
-- }, | ||
-- "stuff": {}, | ||
-- "installed": [ | ||
-- { | ||
-- "version": "1.2.3_1", | ||
-- "stuff": {} | ||
-- } | ||
-- ], | ||
-- "stuff": {} | ||
-- } | ||
-- ] | ||
|
||
Info : AAA.Strings.Vector; | ||
JSON_Issue : exception; | ||
begin | ||
if Subprocess.Unchecked_Spawn_And_Capture | ||
("brew", | ||
Empty_Vector & "info" & "--json=v1" & Package_Name, | ||
Output => Info, | ||
Err_To_Out => True) /= 0 | ||
then | ||
-- failed. | ||
Trace.Debug ("brew failed to find " & Package_Name); | ||
Available_Version := Null_Unbounded_String; | ||
Installed_Version := Null_Unbounded_String; | ||
return; | ||
end if; | ||
|
||
Homebrew_Found_Required_Package : | ||
declare | ||
Data : constant JSON_Value := Read (AAA.Strings.Flatten (Info)); | ||
|
||
procedure Get_Available_Version | ||
(From : JSON_Value; | ||
Version : out Ada.Strings.Unbounded.Unbounded_String) | ||
with Pre => Kind (From) = JSON_Object_Type; | ||
procedure Get_Installed_Version | ||
(From : JSON_Array; | ||
Version : out Ada.Strings.Unbounded.Unbounded_String); | ||
procedure Info_Callback (Name : UTF8_String; Value : JSON_Value); | ||
-- Called for the elements of the JSON data to find the | ||
-- relevant sections and use Get_Available_Version, | ||
-- Get_Installed_Version to extract the actual versions (if | ||
-- any). | ||
|
||
procedure Get_Available_Version | ||
(From : JSON_Value; | ||
Version : out Ada.Strings.Unbounded.Unbounded_String) | ||
is | ||
begin | ||
Version := Ada.Strings.Unbounded.To_Unbounded_String | ||
(String'(Get (From, "stable"))); | ||
end Get_Available_Version; | ||
|
||
procedure Get_Installed_Version | ||
(From : GNATCOLL.JSON.JSON_Array; | ||
Version : out Ada.Strings.Unbounded.Unbounded_String) | ||
is | ||
Result : Ada.Strings.Unbounded.Unbounded_String; | ||
procedure Installed_Callback (Name : UTF8_String; | ||
Value : JSON_Value); | ||
procedure Installed_Callback (Name : UTF8_String; | ||
Value : JSON_Value) | ||
is | ||
begin | ||
if Name = "version" then | ||
Result := Ada.Strings.Unbounded.To_Unbounded_String | ||
(String'(Get (Value))); | ||
end if; | ||
end Installed_Callback; | ||
begin | ||
if Length (From) /= 0 then | ||
Map_JSON_Object (Get (From, 1), Installed_Callback'Access); | ||
end if; | ||
Version := Result; | ||
end Get_Installed_Version; | ||
|
||
procedure Info_Callback (Name : UTF8_String; Value : JSON_Value) is | ||
function "+" | ||
(L : Ada.Strings.Unbounded.Unbounded_String) return String | ||
renames Ada.Strings.Unbounded.To_String; | ||
begin | ||
if Name = "versions" then | ||
if Kind (Value) /= JSON_Object_Type then | ||
raise JSON_Issue with "JSON 'versions' not JSON_Object"; | ||
end if; | ||
Get_Available_Version (Value, Available_Version); | ||
Trace.Debug ("available: " & (+Available_Version)); | ||
elsif Name = "installed" then | ||
if Kind (Value) /= JSON_Array_Type then | ||
raise JSON_Issue with "JSON 'installed' not JSON_Array"; | ||
end if; | ||
Get_Installed_Version (JSON_Array'(Get (Value)), | ||
Installed_Version); | ||
Trace.Debug ("installed: " & (+Installed_Version)); | ||
end if; | ||
end Info_Callback; | ||
|
||
Arr : JSON_Array; | ||
begin | ||
if Kind (Data) /= JSON_Array_Type then | ||
raise JSON_Issue with "JSON info not JSON_Array"; | ||
end if; | ||
Arr := Get (Data); | ||
if Length (Arr) /= 1 then | ||
raise JSON_Issue with "JSON info length /= 1"; | ||
end if; | ||
if Kind (Get (Arr, 1)) /= JSON_Object_Type then | ||
raise JSON_Issue with "JSON info (1) not JSON_Object"; | ||
end if; | ||
Map_JSON_Object (Get (Arr, 1), Info_Callback'Access); | ||
end Homebrew_Found_Required_Package; | ||
|
||
end Get_Info; | ||
|
||
----------------------- | ||
-- Already_Installed -- | ||
----------------------- | ||
|
||
overriding function Already_Installed (This : Deployer) return Boolean | ||
is | ||
Installed_Version : Unbounded_String; | ||
Available_Version : Unbounded_String; | ||
begin | ||
Trace.Debug ("already_installed? " & This.Base.Package_Name); | ||
|
||
Get_Info (Package_Name => This.Base.Package_Name, | ||
Available_Version => Available_Version, | ||
Installed_Version => Installed_Version); | ||
return Length (Installed_Version) > 0; | ||
end Already_Installed; | ||
|
||
------------ | ||
-- Detect -- | ||
------------ | ||
|
||
overriding | ||
function Detect (This : Deployer) return Version_Outcomes.Outcome | ||
is | ||
|
||
function Get_Version_From_String (Candidate : String) | ||
return Version_Outcomes.Outcome | ||
is (Version_Outcomes.New_Result (Semantic_Versioning.Parse | ||
(Candidate, | ||
Relaxed => True))); | ||
|
||
Installed_Version : Unbounded_String; | ||
Available_Version : Unbounded_String; | ||
begin | ||
Trace.Debug ("detect? " & This.Base.Package_Name); | ||
|
||
Get_Info (Package_Name => This.Base.Package_Name, | ||
Available_Version => Available_Version, | ||
Installed_Version => Installed_Version); | ||
|
||
if Length (Installed_Version) > 0 then | ||
return Get_Version_From_String (To_String (Installed_Version)); | ||
elsif Length (Available_Version) > 0 then | ||
return Get_Version_From_String (To_String (Available_Version)); | ||
else | ||
return Version_Outcomes.Outcome_Failure | ||
("no candidate version found", | ||
Report => False); | ||
end if; | ||
|
||
end Detect; | ||
|
||
------------- | ||
-- Install -- | ||
------------- | ||
|
||
overriding | ||
function Install (This : Deployer) return Outcome is | ||
begin | ||
Trace.Debug ("hoping to install: " & This.Base.Image); | ||
Subprocess.Checked_Spawn | ||
("brew", | ||
Empty_Vector & "install" & This.Base.Package_Name); | ||
|
||
return Outcome_Success; | ||
exception | ||
when E : others => | ||
return Alire.Errors.Get (E); | ||
end Install; | ||
|
||
end Alire.Origins.Deployers.System.Homebrew; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package Alire.Origins.Deployers.System.Homebrew is | ||
|
||
type Deployer is new Deployers.System.Deployer with null record; | ||
|
||
overriding | ||
function Already_Installed (This : Deployer) return Boolean; | ||
|
||
overriding | ||
function Detect (This : Deployer) | ||
return Version_Outcomes.Outcome; | ||
|
||
overriding | ||
function Install (This : Deployer) return Outcome; | ||
|
||
end Alire.Origins.Deployers.System.Homebrew; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters