From a25b42ef45ead2e04dfbfe96b3713bfbe902fc8e Mon Sep 17 00:00:00 2001 From: Fabien Chouteau Date: Mon, 18 Mar 2024 18:55:29 +0100 Subject: [PATCH] Enforce usage of Sementic_Versioning when printing Alire/Alr version (#1643) (#1652) * Enforce usage of Sementic_Versioning when printing Alire/Alr version To ensure a consistent formatting whatever the version. The version string is now private, only the semver type is visible. * Enforce usage of Sementic_Versioning for index versions --- src/alire/alire-github.adb | 4 +-- src/alire/alire-index.ads | 39 +++++++++++++++++++-------- src/alire/alire-publish-submit.adb | 2 +- src/alire/alire-settings-edit.adb | 4 +-- src/alire/alire-toml_index.adb | 5 ++-- src/alire/alire-version-semver.ads | 14 ---------- src/alire/alire-version.ads | 16 +++++++++-- src/alire/alire_early_elaboration.adb | 4 +-- src/alr/alr-commands-version.adb | 8 +++--- src/alr/alr-commands.adb | 3 +-- src/alr/alr-commands.ads | 2 +- 11 files changed, 58 insertions(+), 43 deletions(-) delete mode 100644 src/alire/alire-version-semver.ads diff --git a/src/alire/alire-github.adb b/src/alire/alire-github.adb index 20da5062..e9ab5f20 100644 --- a/src/alire/alire-github.adb +++ b/src/alire/alire-github.adb @@ -304,7 +304,7 @@ package body Alire.GitHub is Args => "state" = "closed"); begin Comment (Number, - "Closed using `alr " & Version.Current & "` with reason: " + "Closed using `alr " & Version.Current.Image & "` with reason: " & Reason); end Close; @@ -423,7 +423,7 @@ package body Alire.GitHub is Mutation : constant String := "mutation { markPullRequestReadyForReview (input: { " - & "clientMutationId: ""alr-" & Version.Current & """, " + & "clientMutationId: ""alr-" & Version.Current.Image & """, " & "pullRequestId: ""PRID"" }) {clientMutationId}}"; Response : constant Minirest.Response diff --git a/src/alire/alire-index.ads b/src/alire/alire-index.ads index 2e581d03..485dc550 100644 --- a/src/alire/alire-index.ads +++ b/src/alire/alire-index.ads @@ -49,21 +49,14 @@ package Alire.Index is -- The branch used for the community index. Must be updated when new index -- features are introduced. - Min_Compatible_Version : constant String := "1.1"; - -- Update as needed in case of backward-incompatible changes - - Max_Compatible_Version : constant String := - AAA.Strings.Tail (Community_Branch, '-'); + Min_Compatible_Version : constant Semantic_Versioning.Version; + -- Based on the constant defined in private section -- We store here the indexes we are able to load. As long as we do not -- break back compatibility, we can keep on simply updating the minor value - Valid_Versions : constant Semantic_Versioning.Extended.Version_Set := - Semantic_Versioning.Extended.Value - ("^" & Min_Compatible_Version - & " & <=" & Max_Compatible_Version); + Valid_Versions : constant Semantic_Versioning.Extended.Version_Set; - Version : constant Semantic_Versioning.Version := - Semantic_Versioning.New_Version (Max_Compatible_Version); + Version : constant Semantic_Versioning.Version; -- The index version understood by alire must match the one in the indexes -- being loaded. @@ -173,4 +166,28 @@ package Alire.Index is -- applied during load: -- * Whether some origin is not in our allowed hosting sites. +private + + -- The string constants for versions are kept in the private section to + -- avoid using them in command output or other message. The only option + -- is to use the Sementic_Versioning.Image function which will provide a + -- consistant output. + + Min_Compatible_Version_Str : constant String := "1.1"; + -- Update as needed in case of backward-incompatible changes + + Max_Compatible_Version_Str : constant String := + AAA.Strings.Tail (Community_Branch, '-'); + + Min_Compatible_Version : constant Semantic_Versioning.Version := + Semantic_Versioning.New_Version (Min_Compatible_Version_Str); + + Valid_Versions : constant Semantic_Versioning.Extended.Version_Set := + Semantic_Versioning.Extended.Value + ("^" & Min_Compatible_Version_Str + & " & <=" & Max_Compatible_Version_Str); + + Version : constant Semantic_Versioning.Version := + Semantic_Versioning.New_Version (Max_Compatible_Version_Str); + end Alire.Index; diff --git a/src/alire/alire-publish-submit.adb b/src/alire/alire-publish-submit.adb index 27cae24a..122a5ec0 100644 --- a/src/alire/alire-publish-submit.adb +++ b/src/alire/alire-publish-submit.adb @@ -386,7 +386,7 @@ package body Alire.Publish.Submit is Title => Context.PR_Name, Message => "Created via `alr publish` with `alr " - & Version.Current & "`"); + & Version.Current.Image & "`"); begin Put_Success ("Pull request created successfully"); Put_Info ("Visit " & TTY.URL (States.Webpage (Number)) diff --git a/src/alire/alire-settings-edit.adb b/src/alire/alire-settings-edit.adb index 0962c134..61e5a9c3 100644 --- a/src/alire/alire-settings-edit.adb +++ b/src/alire/alire-settings-edit.adb @@ -8,7 +8,7 @@ with Alire.Platforms.Folders; with Alire.Platforms.Current; with Alire.Settings.Builtins; with Alire.Utils.Text_Files; -with Alire.Version.Semver; +with Alire.Version; with Alire.Warnings; with CLIC.Config.Edit; @@ -224,7 +224,7 @@ package body Alire.Settings.Edit is begin -- Warn or fail depending on version if OS_Lib.Getenv (Environment.Config, Unset) /= Unset then - if Version.Semver.Current < Features.Config_Deprecated then + if Version.Current < Features.Config_Deprecated then Warnings.Warn_Once (Msg, Level => Warning); else Raise_Checked_Error (Msg); diff --git a/src/alire/alire-toml_index.adb b/src/alire/alire-toml_index.adb index 07c3aac6..b60e714b 100644 --- a/src/alire/alire-toml_index.adb +++ b/src/alire/alire-toml_index.adb @@ -203,14 +203,13 @@ package body Alire.TOML_Index is & ") is newer than that expected by alr (" & Alire.Index.Version.Image & ")." & " You may have to update alr"); - elsif Loading_Index_Version < Semver.Parse - (Alire.Index.Min_Compatible_Version) + elsif Loading_Index_Version < Alire.Index.Min_Compatible_Version then Set_Error (Result, Filename, "index version (" & Loading_Index_Version.Image & ") is too old. The minimum compatible version is " - & Alire.Index.Min_Compatible_Version & ASCII.LF + & Alire.Index.Min_Compatible_Version.Image & ASCII.LF & (if Index.Name = Alire.Index.Community_Name then " Resetting the community index (" & TTY.Terminal ("alr index --reset-community") diff --git a/src/alire/alire-version-semver.ads b/src/alire/alire-version-semver.ads deleted file mode 100644 index aeead5a9..00000000 --- a/src/alire/alire-version-semver.ads +++ /dev/null @@ -1,14 +0,0 @@ -with Semantic_Versioning; - -package Alire.Version.Semver is - - -- Convenience to be able to use the current version directly as a - -- comparable proper semantic version version. - - package Semver renames Semantic_Versioning; - - subtype Version is Semver.Version; - - Current : constant Version := Semver.New_Version (Alire.Version.Current); - -end Alire.Version.Semver; diff --git a/src/alire/alire-version.ads b/src/alire/alire-version.ads index e1447f45..4ae30c25 100644 --- a/src/alire/alire-version.ads +++ b/src/alire/alire-version.ads @@ -1,8 +1,18 @@ -package Alire.Version with Preelaborate is +with Semantic_Versioning; + +package Alire.Version is + + package Semver renames Semantic_Versioning; + + subtype Version is Semver.Version; + + Current : constant Version; + +private -- Remember to update Alire.Index branch if needed too - Current : constant String := "2.0"; + Current_Str : constant String := "2.0"; -- 2.0.0: alr settings refactor and minor fixes -- 2.0.0-rc1: release candidate for 2.0 -- 2.0.0-b1: first public release on the 2.0 branch @@ -23,4 +33,6 @@ package Alire.Version with Preelaborate is -- 0.8.1-dev: update to devel-0.5 index branch -- 0.8.0-dev: post-0.7-beta changes + Current : constant Version := Semver.New_Version (Current_Str); + end Alire.Version; diff --git a/src/alire/alire_early_elaboration.adb b/src/alire/alire_early_elaboration.adb index d819982c..0f92508f 100644 --- a/src/alire/alire_early_elaboration.adb +++ b/src/alire/alire_early_elaboration.adb @@ -4,7 +4,7 @@ with Ada.Directories; with Alire.Features; with Alire.Settings.Edit.Early_Load; -with Alire.Version.Semver; +with Alire.Version; with GNAT.Command_Line; with GNAT.OS_Lib; @@ -110,7 +110,7 @@ package body Alire_Early_Elaboration is use type Alire.Version.Semver.Version; Config_Deprecated : constant Boolean - := Alire.Version.Semver.Current >= Alire.Features.Config_Deprecated; + := Alire.Version.Current >= Alire.Features.Config_Deprecated; procedure Check_Config_Deprecated is begin diff --git a/src/alr/alr-commands-version.adb b/src/alr/alr-commands-version.adb index bd145df5..f4fd73a3 100644 --- a/src/alr/alr-commands-version.adb +++ b/src/alr/alr-commands-version.adb @@ -11,6 +11,7 @@ with Alire.Properties; with Alire.Roots.Optional; with Alire.Toolchains; with Alire.Utils.Tables; +with Alire.Version; with Alr.Bootstrap; @@ -61,9 +62,10 @@ package body Alr.Commands.Version is end if; Table.Append ("APPLICATION").Append ("").New_Row; - Table.Append ("alr version:").Append (Alire.Version.Current).New_Row; + Table.Append ("alr version:") + .Append (Alire.Version.Current.Image).New_Row; Table.Append ("libalire version:") - .Append (Alire.Version.Current).New_Row; + .Append (Alire.Version.Current.Image).New_Row; Table.Append ("compilation date:") .Append (GNAT.Source_Info.Compilation_ISO_Date & " " & GNAT.Source_Info.Compilation_Time).New_Row; @@ -193,7 +195,7 @@ package body Alr.Commands.Version is procedure Print_Version is begin - Trace.Always ("alr " & Alire.Version.Current); + Trace.Always ("alr " & Alire.Version.Current.Image); end Print_Version; end Alr.Commands.Version; diff --git a/src/alr/alr-commands.adb b/src/alr/alr-commands.adb index 64d9155f..ef758482 100644 --- a/src/alr/alr-commands.adb +++ b/src/alr/alr-commands.adb @@ -20,7 +20,6 @@ with Alire.Platforms.Current; with Alire.Root; with Alire.Solutions; with Alire.Toolchains; -with Alire.Version.Semver; with Alr.Commands.Action; with Alr.Commands.Build; @@ -143,7 +142,7 @@ package body Alr.Commands is use CLIC.Subcommand; use type Alire.Version.Semver.Version; begin - if Alire.Version.Semver.Current < Features.Config_Deprecated then + if Alire.Version.Current < Features.Config_Deprecated then Define_Switch (Config, Command_Line_Config_Path'Access, "-c=", "--config=", diff --git a/src/alr/alr-commands.ads b/src/alr/alr-commands.ads index 54cfcf24..824db39f 100644 --- a/src/alr/alr-commands.ads +++ b/src/alr/alr-commands.ads @@ -139,7 +139,7 @@ private package Sub_Cmd is new CLIC.Subcommand.Instance (Main_Command_Name => "alr", - Version => Alire.Version.Current, + Version => Alire.Version.Current.Image, Put => GNAT.IO.Put, Put_Line => GNAT.IO.Put_Line, Put_Error => Put_Error, -- 2.39.5