From 55d993ec7af397399a56a3d7c8b6c6efa3fd4732 Mon Sep 17 00:00:00 2001 From: Fabien Chouteau Date: Thu, 11 Jun 2020 13:25:33 +0200 Subject: [PATCH] Alr.Platforms.Windows: use Alire.config in msys2 install (#443) * Alr.Platforms.Windows: use Alire.config in msys2 install * Update msys2 install script URL Now that the master version is updated with install dir feature. * Alire.Config.Edit: create directory for the config file in case it doesn't exists * Alire.Config.Edit: improve TOML value evaluation If first the value cannot be parsed, we directly create a TOML_String value instead of trying to parse it again between quotes. * Alr.Platforms.Windows: remove unecessary call to YAML_Stringify --- src/alire/alire-config-edit.adb | 18 +++--- src/alr/os_windows/alr-platforms-windows.adb | 66 +++++++++++++------- 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/src/alire/alire-config-edit.adb b/src/alire/alire-config-edit.adb index 10bcf256..d01f1497 100644 --- a/src/alire/alire-config-edit.adb +++ b/src/alire/alire-config-edit.adb @@ -1,4 +1,5 @@ with Ada.Text_IO; +with Ada.Directories; with TOML; use TOML; @@ -27,9 +28,13 @@ package body Alire.Config.Edit is procedure Write_Config_File (Table : TOML_Value; Path : Absolute_Path) is use Ada.Text_IO; - + use Ada.Directories; File : File_Type; begin + + -- Create the directory for the config file, in case it doesn't exists + Create_Path (Containing_Directory (Path)); + Create (File, Out_File, Path); Trace.Debug ("Write config: '" & TOML.Dump_As_String (Table) & "'"); Put (File, TOML.Dump_As_String (Table)); @@ -112,15 +117,8 @@ package body Alire.Config.Edit is -- Conversion failed - if Str (Str'First) /= '"' then - -- Try again with double quotes to interpret the value as a TOML - -- string. - return To_TOML_Value ('"' & Str & '"'); - else - - -- Invalid TOML value - return No_TOML_Value; - end if; + -- Interpret as a string + return Create_String (Str); else return Result.Value.Get ("key"); end if; diff --git a/src/alr/os_windows/alr-platforms-windows.adb b/src/alr/os_windows/alr-platforms-windows.adb index 77249a06..1c1b3004 100644 --- a/src/alr/os_windows/alr-platforms-windows.adb +++ b/src/alr/os_windows/alr-platforms-windows.adb @@ -8,11 +8,15 @@ with Alire.OS_Lib.Subprocess; with Alire.OS_Lib.Download; with Alire.Utils; with Alire.Utils.User_Input; +with Alire.Config; +with Alire.Config.Edit; with Alr.OS_Lib; use Alr.OS_Lib; package body Alr.Platforms.Windows is + package Cfg renames Alire.Config; + Msys2_Installer : constant String := "msys2-x86_64-latest.exe"; Msys2_Installer_URL : constant String := "http://repo.msys2.org/distrib/" & Msys2_Installer; @@ -20,8 +24,8 @@ package body Alr.Platforms.Windows is -- FIXME, temporary address for development Msys2_Installer_Script : constant String := "auto-install.js"; Msys2_Installer_Script_URL : constant String := - "https://raw.githubusercontent.com/Fabien-Chouteau/" & - "msys2-installer/patch-1/" & Msys2_Installer_Script; + "https://raw.githubusercontent.com/msys2/msys2-installer/master/" & + Msys2_Installer_Script; ------------------- -- Set_Msys2_Env -- @@ -50,22 +54,16 @@ package body Alr.Platforms.Windows is return Boolean is use Alire.Utils.User_Input; - - Config : constant Alire.Absolute_Path := - Config_Folder (Platforms.Windows.New_Platform); - - Do_Not_Install_Path : constant Alire.Absolute_Path := - Config / "do_not_install_msys2"; - begin - if Ada.Directories.Exists (Do_Not_Install_Path) then + if Cfg.Get ("msys2.do_not_install", False) then -- User already requested that msys2 should not be installed Alr.Trace.Detail ("Alire is configured not to install msys2."); - Alr.Trace.Detail ("Delete '" & Do_Not_Install_Path & "'" & - " if you want Alire to install msys2."); + Alr.Trace.Detail + ("Run 'alr config --global --set msys2.do_not_install false'" & + " if you want Alire to install msys2."); return False; end if; @@ -92,9 +90,10 @@ package body Alr.Platforms.Windows is Valid => (Yes | No => True, others => False), Default => No) = Yes then - - -- Create a directory to remember the user choice - Ada.Directories.Create_Path (Do_Not_Install_Path); + -- Save user choice in the global config + Cfg.Edit.Set (Path => Cfg.Filepath (Cfg.Global), + Key => "msys2.do_not_install", + Value => "true"); end if; -- We are not allowed to install @@ -149,6 +148,13 @@ package body Alr.Platforms.Windows is return Alire.Outcome_Failure ("Cannot setup msys2 environment"); end; + if not Cfg.Defined ("msys2.install_dir") then + -- Save msys2 install dir in the global config + Cfg.Edit.Set (Path => Cfg.Filepath (Cfg.Global), + Key => "msys2.install_dir", + Value => Install_Dir); + end if; + return Alire.Outcome_Success; end Install_Msys2; @@ -156,22 +162,40 @@ package body Alr.Platforms.Windows is -- Setup_Msys2 -- ----------------- - procedure Setup_Msys2 (Install_Dir : Alire.Absolute_Path) is + procedure Setup_Msys2 is Result : Alire.Outcome; + + Default_Install_Dir : constant Alire.Absolute_Path := + Cache_Folder (Platforms.Windows.New_Platform) / "msys64"; + + Cfg_Install_Dir : constant String := + Cfg.Get ("msys2.install_dir", Default_Install_Dir); begin + + if not Alire.Check_Absolute_Path (Cfg_Install_Dir) then + -- This error is recoverable as msys2 is not required for alr to + -- work. + Alire.Recoverable_Error + ("Invalid absolute install path for msys2 in configuration:" & + " '" & Cfg_Install_Dir & "'"); + return; + end if; + -- Check if msys2 is already installed for Alire - if not Ada.Directories.Exists (Install_Dir) then + if not Ada.Directories.Exists (Cfg_Install_Dir) then -- Msys2 is not installed yet - Result := Install_Msys2 (Install_Dir); + Result := Install_Msys2 (Cfg_Install_Dir); if not Result.Success then - Alr.Trace.Error (Result.Message); + -- This error is recoverable as msys2 is not required for alr to + -- work. + Alire.Recoverable_Error (Result.Message); return; end if; end if; -- Set the PATH and other environment variable for msys2 - Set_Msys2_Env (Install_Dir); + Set_Msys2_Env (Cfg_Install_Dir); end Setup_Msys2; ---------- @@ -207,5 +231,5 @@ package body Alr.Platforms.Windows is is (Alire.Platform.Distribution); begin - Setup_Msys2 (Cache_Folder (Platforms.Windows.New_Platform) / "msys64"); + Setup_Msys2; end Alr.Platforms.Windows; -- 2.39.5