From bcc9a209a89412410132539a737751d07682b078 Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Wed, 22 Jul 2020 09:45:23 +0100 Subject: [PATCH] Add and use Alire.Lockfiles.Contents type (#477) This type allows future generalization of what is stored in the lock file, where currently it could only store a Solution which was at top level. --- src/alire/alire-lockfiles.adb | 75 +++++++++++++++++++++---- src/alire/alire-lockfiles.ads | 40 +++++++++---- src/alire/alire-roots.adb | 2 +- src/alire/alire-solutions.adb | 6 -- src/alire/alire-workspace.adb | 8 +-- src/alr/alr-commands-init.adb | 2 +- src/alr/alr-commands.adb | 2 +- testsuite/tests/pin/downgrade/test.py | 2 +- testsuite/tests/pin/post-update/test.py | 2 +- 9 files changed, 103 insertions(+), 36 deletions(-) diff --git a/src/alire/alire-lockfiles.adb b/src/alire/alire-lockfiles.adb index 1111a067..cede133c 100644 --- a/src/alire/alire-lockfiles.adb +++ b/src/alire/alire-lockfiles.adb @@ -3,8 +3,6 @@ with Ada.Text_IO; with Alire.Directories; with Alire.Paths; -with Alire.Solutions; -with Alire.TOML_Adapters; with TOML.File_IO; @@ -12,6 +10,18 @@ package body Alire.Lockfiles is use Directories.Operators; + ---------- + -- Keys -- + ---------- + + package Keys is + + -- Key used internally for TOML serialization + + Solution : constant String := "solution"; + + end Keys; + --------------- -- File_Name -- --------------- @@ -20,27 +30,65 @@ package body Alire.Lockfiles is Root_Dir : Any_Path) return Any_Path is (Root_Dir / Paths.Working_Folder_Inside_Root / (+Name) & ".lock"); + --------------- + -- From_TOML -- + --------------- + + overriding + function From_TOML (This : in out Contents; + From : TOML_Adapters.Key_Queue) + return Outcome + is + begin + This.Solution := + Solutions.From_TOML + (TOML_Adapters.From + (From.Checked_Pop + (Key => Keys.Solution, + Kind => TOML.TOML_Table), + Keys.Solution)); + + From.Report_Extra_Keys; + + return Outcome_Success; + end From_TOML; + ---------- -- Read -- ---------- - function Read (Filename : Any_Path) return Solver.Solution is + function Read (Filename : Any_Path) return Contents is begin - Trace.Debug ("Reading solution from " & Filename); + Trace.Debug ("Reading persistent contents from " & Filename); declare Result : constant TOML.Read_Result := TOML.File_IO.Load_File (Filename); begin if Result.Success then - return Solutions.From_TOML - (TOML_Adapters.From (Result.Value, Filename & ":")); + return This : Contents do + Assert (This.From_TOML + (TOML_Adapters.From (Result.Value, Filename & ":"))); + end return; else Raise_Checked_Error (TOML.Format_Error (Result)); end if; end; end Read; + ------------- + -- To_TOML -- + ------------- + + overriding + function To_TOML (This : Contents) return TOML.TOML_Value + is + begin + return Table : constant TOML.TOML_Value := TOML.Create_Table do + Table.Set (Keys.Solution, This.Solution.To_TOML); + end return; + end To_TOML; + -------------- -- Validity -- -------------- @@ -54,7 +102,7 @@ package body Alire.Lockfiles is -- Try to load to assess validity declare - Unused : constant Solver.Solution := Read (File); + Unused : constant Contents := Read (File); begin return Valid; end; @@ -70,15 +118,20 @@ package body Alire.Lockfiles is -- Write -- ----------- - procedure Write (Solution : Solver.Solution; + procedure Write (Contents : Lockfiles.Contents; Filename : Any_Path) is use Ada.Text_IO; - File : File_Type; + File : File_Type; begin - Trace.Debug ("Dumping solution to " & Filename); + Trace.Debug ("Dumping lockfile contents to " & Filename); + Create (File, Out_File, Filename); - TOML.File_IO.Dump_To_File (Solution.To_TOML, File); + Put_Line (File, + "# THIS IS A MACHINE-GENERATED FILE. DO NOT EDIT MANUALLY."); + New_Line (File); + + TOML.File_IO.Dump_To_File (Contents.To_TOML, File); Close (File); exception when others => diff --git a/src/alire/alire-lockfiles.ads b/src/alire/alire-lockfiles.ads index e8177b9f..3694cb44 100644 --- a/src/alire/alire-lockfiles.ads +++ b/src/alire/alire-lockfiles.ads @@ -1,28 +1,48 @@ +with Alire.Interfaces; with Alire.Properties; -with Alire.Solver; +with Alire.Solutions; +with Alire.TOML_Adapters; + +with TOML; package Alire.Lockfiles is type Validities is (Missing, Invalid, Valid); - -- A crate lockfile stores the dependency solution in use. This permanent - -- storage, in /crate_name.lock, is the basis for orderly uploads - -- and pinning. This file is autogenerated and manipulated via alr - -- commands; the user should not modify it. + -- The lockfile stores persistent private information read/written by + -- Alire and not intended for human tinkering. This currently includes + -- the solution for the root dependencies (that itself includes any pin + -- overrides). + + type Contents is + new Interfaces.Detomifiable + and Interfaces.Tomifiable with + record + Solution : Solutions.Solution; + end record; + -- Information that goes in the lockfile function File_Name (Name : Crate_Name; Root_Dir : Any_Path) return Any_Path; -- Return the location /path/to/crate/dir/alire/crate.lock, filename -- included, given the root directory where the crate is deployed. - function Read (Filename : Any_Path) return Solver.Solution; - -- Read a solution from the given lockfile + function Read (Filename : Any_Path) return Contents; + -- Read contents from the given lockfile function Validity (File : Any_Path) return Validities; -- Check if given file is a valid lockfile - procedure Write (Solution : Solver.Solution; - Filename : Any_Path); - -- Write a solution to a file + procedure Write (Contents : Lockfiles.Contents; + Filename : Any_Path); + -- Write persistent contents to a file + + overriding + function From_TOML (This : in out Contents; + From : TOML_Adapters.Key_Queue) + return Outcome; + + overriding + function To_TOML (This : Contents) return TOML.TOML_Value; end Alire.Lockfiles; diff --git a/src/alire/alire-roots.adb b/src/alire/alire-roots.adb index 815b143e..7021919c 100644 --- a/src/alire/alire-roots.adb +++ b/src/alire/alire-roots.adb @@ -163,7 +163,7 @@ package body Alire.Roots is begin -- TODO: This probably is a good target for caching unless file -- timestamp has changed. - return Lockfiles.Read (This.Lock_File); + return Lockfiles.Read (This.Lock_File).Solution; end Solution; ----------------- diff --git a/src/alire/alire-solutions.adb b/src/alire/alire-solutions.adb index 7b458664..b1add982 100644 --- a/src/alire/alire-solutions.adb +++ b/src/alire/alire-solutions.adb @@ -728,7 +728,6 @@ package body Alire.Solutions is -- TOML keys used locally for loading and saving of solutions - Advisory : constant String := "advisory"; Context : constant String := "context"; Solved : constant String := "solved"; State : constant String := "state"; @@ -793,11 +792,6 @@ package body Alire.Solutions is begin Root.Set (Keys.Context, Context); - Context.Set - (Keys.Advisory, - Create_String - ("THIS IS AN AUTOGENERATED FILE. DO NOT EDIT MANUALLY")); - Context.Set (Keys.Solved, Create_Boolean (This.Solved)); end; diff --git a/src/alire/alire-workspace.adb b/src/alire/alire-workspace.adb index 0bd73346..1425baab 100644 --- a/src/alire/alire-workspace.adb +++ b/src/alire/alire-workspace.adb @@ -37,7 +37,7 @@ package body Alire.Workspace is -- Store given solution on disk to ensure consistency between deployed -- dependencies and stored lockfile. - Alire.Lockfiles.Write (Solution, Root.Lock_File); + Alire.Lockfiles.Write ((Solution => Solution), Root.Lock_File); -- Prepare environment for any post-fetch actions. This must be done -- after the lockfile on disk is written, since the root will read @@ -222,9 +222,9 @@ package body Alire.Workspace is -- will be replaced with the complete solution. Lockfiles.Write - (Solution => (if Release.Dependencies (Env).Is_Empty - then Alire.Solutions.Empty_Valid_Solution - else Alire.Solutions.Empty_Invalid_Solution), + ((Solution => (if Release.Dependencies (Env).Is_Empty + then Alire.Solutions.Empty_Valid_Solution + else Alire.Solutions.Empty_Invalid_Solution)), Filename => Root.Lock_File); end; end if; diff --git a/src/alr/alr-commands-init.adb b/src/alr/alr-commands-init.adb index 253138dc..7413130f 100644 --- a/src/alr/alr-commands-init.adb +++ b/src/alr/alr-commands-init.adb @@ -187,7 +187,7 @@ package body Alr.Commands.Init is Alire.Workspace.Generate_Manifest (Root.Release, Root); Alire.Lockfiles.Write - (Alire.Solutions.Empty_Valid_Solution, + ((Solution => Alire.Solutions.Empty_Valid_Solution), Root.Lock_File); end; end Generate; diff --git a/src/alr/alr-commands.adb b/src/alr/alr-commands.adb index 802c514f..ba6785b5 100644 --- a/src/alr/alr-commands.adb +++ b/src/alr/alr-commands.adb @@ -459,7 +459,7 @@ package body Alr.Commands is Platform.Properties, Alire.Solutions.Empty_Valid_Solution); begin - Alire.Lockfiles.Write (Solution, Checked.Lock_File); + Alire.Lockfiles.Write ((Solution => Solution), Checked.Lock_File); -- Ensure the solved releases are indeed on disk diff --git a/testsuite/tests/pin/downgrade/test.py b/testsuite/tests/pin/downgrade/test.py index d2fe1cb6..c48c6aca 100644 --- a/testsuite/tests/pin/downgrade/test.py +++ b/testsuite/tests/pin/downgrade/test.py @@ -22,7 +22,7 @@ def check_child(version, output, pinned): # Verify lockfile check_line_in('alire/xxx.lock', - '[state.release.libchild."' + version + '"]') + '[solution.state.release.libchild."' + version + '"]') # Verify dependency folders assert os.path.exists('alire/cache/dependencies/libchild_' + version + diff --git a/testsuite/tests/pin/post-update/test.py b/testsuite/tests/pin/post-update/test.py index 45777b15..c9e0d82c 100644 --- a/testsuite/tests/pin/post-update/test.py +++ b/testsuite/tests/pin/post-update/test.py @@ -23,7 +23,7 @@ def check_child(version, output, pinned): # Verify lockfile check_line_in('alire/xxx.lock', - '[state.release.libchild."' + version + '"]') + '[solution.state.release.libchild."' + version + '"]') # Create a new "xxx" program project -- 2.39.5