diff --git a/src/alire/alire-directories.adb b/src/alire/alire-directories.adb index fffdb9c07..5d7905394 100644 --- a/src/alire/alire-directories.adb +++ b/src/alire/alire-directories.adb @@ -87,7 +87,7 @@ package body Alire.Directories is else File & ".prev"); begin if Exists (File) then - if not Exists (Base_Dir) then + if Base_Dir /= "" and then not Exists (Base_Dir) then Create_Directory (Base_Dir); end if; diff --git a/src/alire/alire-paths.ads b/src/alire/alire-paths.ads index 1798089f0..5b4a06824 100644 --- a/src/alire/alire-paths.ads +++ b/src/alire/alire-paths.ads @@ -5,6 +5,9 @@ package Alire.Paths with Preelaborate is Crate_File_Name : constant String := "alire.toml"; -- Name of the manifest file in a regular workspace + Settings_File_Name : constant String := "settings.toml"; + -- Storage of Alire settings in workspace or global location + Build_Folder_Inside_Working_Folder : constant Relative_Path := "builds"; Cache_Folder_Inside_Working_Folder : constant Relative_Path := "cache"; diff --git a/src/alire/alire-settings-edit.adb b/src/alire/alire-settings-edit.adb index 4a7d35952..6ce00c709 100644 --- a/src/alire/alire-settings-edit.adb +++ b/src/alire/alire-settings-edit.adb @@ -1,3 +1,4 @@ +with Ada.Directories; with Ada.Text_IO; with Alire.Environment; @@ -6,7 +7,7 @@ with Alire.Paths; with Alire.Platforms.Folders; with Alire.Platforms.Current; with Alire.Settings.Builtins; -with Alire.Utils; +with Alire.Utils.Text_Files; with Alire.Version.Semver; with Alire.Warnings; @@ -19,8 +20,10 @@ package body Alire.Settings.Edit is use AAA.Strings; use TOML; + package Adirs renames Ada.Directories; + type String_Access is access String; - Config_Path : String_Access; + Settings_Path : String_Access; ----------------- -- Set_Locally -- @@ -114,25 +117,67 @@ package body Alire.Settings.Edit is -------------- function Filepath (Lvl : Level) return Absolute_Path is + + type Old_New is (Old, Current); + + -------------- + -- Location -- + -------------- + + function Location (Which : Old_New) return Absolute_Path is + File : constant String := + (case Which is + when Old => "config.toml", + when Current => Paths.Settings_File_Name); + begin + case Lvl is + when Global => + return Alire.Settings.Edit.Path / File; + when Local => + declare + Candidate : constant String := + Directories.Detect_Root_Path; + begin + if Candidate /= "" then + return Candidate / "alire" / File; + else + Raise_Checked_Error + ("Can only be used in an Alire directory"); + end if; + end; + end case; + end Location; + begin - case Lvl is - when Global => - return Alire.Settings.Edit.Path / "config.toml"; - when Local => - declare - Candidate : constant String := - Directories.Detect_Root_Path; - begin - if Candidate /= "" then - -- This file cannot have a .toml extension or the root - -- detection will not work. - return Candidate / "alire" / "config.toml"; - else - Raise_Checked_Error - ("Can only be used in an Alire directory"); - end if; - end; - end case; + -- Migrate file on the spot if necessary. This file is transparent to + -- users so we do not emit visible messages. + + if Directories.Is_File (Location (Old)) and then + not Directories.Is_File (Location (Current)) + then + Trace.Debug ("Migrating settings file: " + & Location (Old) & " --> " & Location (Current)); + Adirs.Copy_File (Source_Name => Location (Old), + Target_Name => Location (Current)); + + begin + -- Insert a comment in the old config.toml + Utils.Text_Files.Replace_Lines + (Location (Old), + Empty_Vector + & String'("# config.toml has been replaced by settings.toml" + & " after alr 2.0") + & "" + & Utils.Text_Files.Lines (Location (Old))); + exception + -- Ensure we don't break anything trying to leaving the clues + when E : others => + Log_Exception (E); + Trace.Debug ("Failed when leaving clue about settings.toml"); + end; + end if; + + return Location (Current); end Filepath; ----------------- @@ -186,8 +231,8 @@ package body Alire.Settings.Edit is end if; end if; - if Config_Path /= null then -- Case with switch (TODO) - return Config_Path.all; + if Settings_Path /= null then -- Case with switch (TODO) + return Settings_Path.all; else return OS_Lib.Getenv (Environment.Settings, @@ -215,10 +260,10 @@ package body Alire.Settings.Edit is procedure Set_Path (Path : Absolute_Path) is begin - if Config_Path /= null then + if Settings_Path /= null then raise Constraint_Error with "Custom path already set"; else - Config_Path := new String'(Path); + Settings_Path := new String'(Path); end if; end Set_Path; diff --git a/src/alire/alire-utils-text_files.adb b/src/alire/alire-utils-text_files.adb index a014b6aa9..b060d2338 100644 --- a/src/alire/alire-utils-text_files.adb +++ b/src/alire/alire-utils-text_files.adb @@ -20,6 +20,20 @@ package body Alire.Utils.Text_Files is F.Lines.Append (Lines); end Append_Lines; + ------------------- + -- Replace_Lines -- + ------------------- + + procedure Replace_Lines (File : Any_Path; + Lines : AAA.Strings.Vector; + Backup : Boolean := True; + Backup_Dir : Any_Path := "") + is + F : Text_Files.File := Load (File, Backup, Backup_Dir); + begin + F.Lines := Lines; + end Replace_Lines; + -------------- -- Finalize -- -------------- @@ -31,6 +45,8 @@ package body Alire.Utils.Text_Files is if This.Lines = This.Orig then Trace.Debug ("No changes to save in " & This.Name); return; + else + Trace.Debug ("Replacing contents of " & This.Name); end if; declare @@ -60,6 +76,18 @@ package body Alire.Utils.Text_Files is return access AAA.Strings.Vector is (This.Lines'Access); + ----------- + -- Lines -- + ----------- + + function Lines (Filename : Any_Path) + return AAA.Strings.Vector + is + F : constant File := Load (Filename); + begin + return F.Lines; + end Lines; + ------------ -- Create -- ------------ diff --git a/src/alire/alire-utils-text_files.ads b/src/alire/alire-utils-text_files.ads index d1de4d3ca..abf43f687 100644 --- a/src/alire/alire-utils-text_files.ads +++ b/src/alire/alire-utils-text_files.ads @@ -21,12 +21,21 @@ package Alire.Utils.Text_Files is function Lines (This : aliased in out File) return access AAA.Strings.Vector; + function Lines (Filename : Any_Path) + return AAA.Strings.Vector; + procedure Append_Lines (File : Any_Path; Lines : AAA.Strings.Vector; Backup : Boolean := True; Backup_Dir : Any_Path := ""); -- Add the given lines to the end of the file + procedure Replace_Lines (File : Any_Path; + Lines : AAA.Strings.Vector; + Backup : Boolean := True; + Backup_Dir : Any_Path := ""); + -- Replace contents of File with the new lines. + private type File (Length, Backup_Len : Natural) is diff --git a/testsuite/tests/index/auto-update/test.py b/testsuite/tests/index/auto-update/test.py index 1d5889de1..907fa2bb4 100644 --- a/testsuite/tests/index/auto-update/test.py +++ b/testsuite/tests/index/auto-update/test.py @@ -6,7 +6,7 @@ from drivers.alr import run_alr from drivers.helpers import content_of, lines_of -CONFIG_FILE = os.path.join("alr-config", "config.toml") +CONFIG_FILE = os.path.join("alr-config", "settings.toml") # First, we check no related configuration exists diff --git a/testsuite/tests/settings/early-loading/test.py b/testsuite/tests/settings/early-loading/test.py index c2f78c2c2..c3bdfa9cb 100644 --- a/testsuite/tests/settings/early-loading/test.py +++ b/testsuite/tests/settings/early-loading/test.py @@ -10,7 +10,7 @@ # Create a custom configuration dir + file custom_config = "custom_config" os.mkdir(custom_config) -with open(os.path.join(custom_config, "config.toml"), "w") as f: +with open(os.path.join(custom_config, "settings.toml"), "w") as f: f.write("test_value = 42\n") expected = "test_value=42\n" diff --git a/testsuite/tests/settings/relative_config_path/test.py b/testsuite/tests/settings/relative_config_path/test.py index efb4e7a0a..6f3d8ff6a 100644 --- a/testsuite/tests/settings/relative_config_path/test.py +++ b/testsuite/tests/settings/relative_config_path/test.py @@ -14,7 +14,7 @@ "--set", "some_config_key", "true") -assert_eq("some_config_key = true\n", lines_of ("config.toml")[0]) +assert_eq("some_config_key = true\n", lines_of ("settings.toml")[0]) print('SUCCESS') diff --git a/testsuite/tests/workflows/init-options/test.py b/testsuite/tests/workflows/init-options/test.py index 3e3e95f11..a56e3a988 100644 --- a/testsuite/tests/workflows/init-options/test.py +++ b/testsuite/tests/workflows/init-options/test.py @@ -26,7 +26,7 @@ 'xxx/alire.toml', 'xxx/alire/alire.lock', 'xxx/alire/build_hash_inputs', - 'xxx/alire/config.toml', + 'xxx/alire/settings.toml', 'xxx/config', 'xxx/config/xxx_config.ads', 'xxx/config/xxx_config.gpr', @@ -46,7 +46,7 @@ 'aaa/alire.toml', 'aaa/alire/alire.lock', 'aaa/alire/build_hash_inputs', - 'aaa/alire/config.toml', + 'aaa/alire/settings.toml', 'aaa/config', 'aaa/config/aaa_config.ads', 'aaa/config/aaa_config.gpr', @@ -89,7 +89,7 @@ './alire.toml', './alire/alire.lock', './alire/build_hash_inputs', - './alire/config.toml', + './alire/settings.toml', './config', './config/zzz_config.ads', './config/zzz_config.gpr',