From 625c906a9ba753cddaf567fb58dcb3aed351b371 Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Wed, 24 Mar 2021 12:01:52 +0100 Subject: [PATCH] Dead/duplicated code cleanup (allowed milestones, manual hashing) (#714) * Remove dead code obsolescent since `alr publish` * Remove Alire.Milestones.Allowed_Milestones This was redundant, being in essence the same thing as a dependency. --- src/alire/alire-dependencies-states.ads | 13 ++----- src/alire/alire-dependencies.adb | 41 +++++++++++++++++++++ src/alire/alire-dependencies.ads | 12 +++---- src/alire/alire-features-index.adb | 47 ------------------------- src/alire/alire-features-index.ads | 15 -------- src/alire/alire-milestones.adb | 47 ------------------------- src/alire/alire-milestones.ads | 38 +------------------- src/alr/alr-commands-get.adb | 4 +-- src/alr/alr-commands-init.adb | 5 ++- src/alr/alr-commands-show.adb | 6 ++-- src/alr/alr-commands-test.adb | 9 ++--- src/alr/alr-commands-withing.adb | 14 ++++---- 12 files changed, 67 insertions(+), 184 deletions(-) delete mode 100644 src/alire/alire-milestones.adb diff --git a/src/alire/alire-dependencies-states.ads b/src/alire/alire-dependencies-states.ads index 874ac3fa..8f6d8df9 100644 --- a/src/alire/alire-dependencies-states.ads +++ b/src/alire/alire-dependencies-states.ads @@ -150,8 +150,8 @@ private -- Base overridings overriding - function From_Milestones (Unused : Milestones.Allowed_Milestones) - return State; + function From_String (Unused_Spec : String) return State + is (raise Program_Error with "Not intended for use"); overriding function From_TOML (Unused_Key : String; @@ -210,15 +210,6 @@ private function As_Dependency (This : State) return Dependencies.Dependency is (Dependencies.Dependency (This)); - --------------------- - -- From_Milestones -- - --------------------- - - overriding - function From_Milestones (Unused : Milestones.Allowed_Milestones) - return State - is (raise Unimplemented); -- not needed - --------------- -- From_TOML -- --------------- diff --git a/src/alire/alire-dependencies.adb b/src/alire/alire-dependencies.adb index 192bae14..b027e0f8 100644 --- a/src/alire/alire-dependencies.adb +++ b/src/alire/alire-dependencies.adb @@ -1,7 +1,48 @@ +with Ada.Strings.Fixed; +with Ada.Strings.Maps; + with Alire.TOML_Adapters; +with Semantic_Versioning; + package body Alire.Dependencies is + package Semver renames Semantic_Versioning; + + ----------------- + -- From_String -- + ----------------- + + function From_String (Spec : String) return Dependency + is + -- Locate and identify the version operator + use Ada.Strings; + use Ada.Strings.Fixed; + use Ada.Strings.Maps; + + Op_Pos : constant Natural := Index (Spec, To_Set ("*=^~<>/("), Inside); + Name : constant String := (if Op_Pos > Spec'First + then Spec (Spec'First .. Op_Pos - 1) + else Spec); + Result : constant Semver.Extended.Result := + (if Op_Pos > Spec'First + then Semver.Extended.Parse (Spec (Op_Pos .. Spec'Last)) + else Semver.Extended.Parse ("*")); + begin + if Result.Valid then + return New_Dependency (+Name, Result.Set); + else + Raise_Checked_Error ("Invalid version set expression: " + & Spec (Op_Pos .. Spec'Last)); + end if; + exception + when Alire.Checked_Error => + raise; + when E : others => + Log_Exception (E); + Raise_Checked_Error ("A crate/version string was invalid"); + end From_String; + --------------- -- From_TOML -- --------------- diff --git a/src/alire/alire-dependencies.ads b/src/alire/alire-dependencies.ads index 729f6c03..76635305 100644 --- a/src/alire/alire-dependencies.ads +++ b/src/alire/alire-dependencies.ads @@ -30,6 +30,11 @@ package Alire.Dependencies with Preelaborate is Version : Semantic_Versioning.Version) return Dependency; + function From_String (Spec : String) return Dependency; + -- Intended to parse command-line dependencies given as crate[subset]: + -- alr^1.0, alr=1.0, alr~0.7, etc. If no subset is specified, Any version + -- is returned. May raise Checked_Error if parsing cannot succeed. + function Crate (Dep : Dependency) return Crate_Name; function Versions (Dep : Dependency) @@ -48,9 +53,6 @@ package Alire.Dependencies with Preelaborate is overriding function Key (Dep : Dependency) return String; - function From_Milestones (Allowed : Milestones.Allowed_Milestones) - return Dependency; - function From_TOML (Key : String; Value : TOML.TOML_Value) return Dependency with Pre => @@ -105,10 +107,6 @@ private return Semantic_Versioning.Extended.Version_Set is (Dep.Versions); - function From_Milestones (Allowed : Milestones.Allowed_Milestones) - return Dependency is - (New_Dependency (Allowed.Crate, Allowed.Versions)); - function Image (Dep : Dependency) return String is ((+Dep.Crate) & Dep.Versions.Image); diff --git a/src/alire/alire-features-index.adb b/src/alire/alire-features-index.adb index 28a45772..6d513cb7 100644 --- a/src/alire/alire-features-index.adb +++ b/src/alire/alire-features-index.adb @@ -1,8 +1,6 @@ with Ada.Directories; with Alire.Config.Edit; -with Alire.Directories; -with Alire.Errors; with Alire.Index; with Alire.Origins.Deployers; with Alire.OS_Lib; @@ -382,49 +380,4 @@ package body Alire.Features.Index is return Outcome_Success; end Update_All; - ----------------- - -- Hash_Origin -- - ----------------- - - function Hash_Origin (Kind : Hashes.Kinds; - Origin_Img : URL) - return Hashing_Outcomes.Outcome - is - Origin : constant Origins.Origin := Origins.From_String (Origin_Img); - begin - - -- Retrieve the given origin and compute its hash: - - declare - Depl : constant Origins.Deployers.Deployer'Class := - Origins.Deployers.New_Deployer (Origin); - Tmp : Alire.Directories.Temp_File; - begin - if not Depl.Supports_Hashing then - return Hashing_Outcomes.Outcome_Failure - ("The supplied origin does not support integrity verification"); - end if; - - declare - Result : constant Outcome := Depl.Fetch (Tmp.Filename); - begin - Result.Assert; - end; - - declare - Hash : constant Hashes.Any_Hash := - Hashes.New_Hash (Kind, - Depl.Compute_Hash (Tmp.Filename, Kind)); - begin - return Hashing_Outcomes.New_Result (Hash); - end; - end; - - exception - when E : Checked_Error => - return Hashing_Outcomes.Outcome_Failure (Errors.Get (E)); - when E : others => - return Hashing_Outcomes.Outcome_From_Exception (E); - end Hash_Origin; - end Alire.Features.Index; diff --git a/src/alire/alire-features-index.ads b/src/alire/alire-features-index.ads index a03bfbb3..853843c0 100644 --- a/src/alire/alire-features-index.ads +++ b/src/alire/alire-features-index.ads @@ -1,8 +1,6 @@ with Ada.Containers.Indefinite_Ordered_Sets; -with Alire.Hashes; with Alire.Index_On_Disk; -with Alire.Outcomes.Indefinite; package Alire.Features.Index is @@ -54,17 +52,4 @@ package Alire.Features.Index is -- re-adds it at the required branch by Index.Community_Branch with the -- same priority (i.e., maintaining the relative ordering); - ------------------------ - -- Hashing utilities -- - ------------------------ - - package Hashing_Outcomes is new Outcomes.Indefinite (Hashes.Any_Hash); - - function Hash_Origin (Kind : Hashes.Kinds; - Origin_Img : URL) - return Hashing_Outcomes.Outcome; - -- Given a valid image of an origin, compute its hash as it would appear - -- in the index. The origin is retrieved into a temporary folder that is - -- deleted after use. - end Alire.Features.Index; diff --git a/src/alire/alire-milestones.adb b/src/alire/alire-milestones.adb deleted file mode 100644 index f2774382..00000000 --- a/src/alire/alire-milestones.adb +++ /dev/null @@ -1,47 +0,0 @@ -with Ada.Strings.Fixed; -with Ada.Strings.Maps; - -package body Alire.Milestones is - - package Semver renames Semantic_Versioning; - - -------------------- - -- Crate_Versions -- - -------------------- - - function Crate_Versions (Spec : String) return Allowed_Milestones - is - -- Locate and identify the version operator - use Ada.Strings; - use Ada.Strings.Fixed; - use Ada.Strings.Maps; - - Op_Pos : constant Natural := Index (Spec, To_Set ("*=^~<>/("), Inside); - Name : constant String := (if Op_Pos > Spec'First - then Spec (Spec'First .. Op_Pos - 1) - else Spec); - Result : constant Semver.Extended.Result := - (if Op_Pos > Spec'First - then Semver.Extended.Parse (Spec (Op_Pos .. Spec'Last)) - else Semver.Extended.Parse ("*")); - begin - if Result.Valid then - return M : Allowed_Milestones (Name'Length) do - M.Crate := +Name; - M.Versions := Result.Set; - end return; - else - Trace.Error ("Invalid version set expression: " - & Spec (Op_Pos .. Spec'Last)); - Trace.Error (Result.Error); - raise Checked_Error with "Invalid version set expression"; - end if; - exception - when Alire.Checked_Error => - raise; - when others => - Trace.Error ("A crate/version string was invalid"); - raise Checked_Error; - end Crate_Versions; - -end Alire.Milestones; diff --git a/src/alire/alire-milestones.ads b/src/alire/alire-milestones.ads index 9ed0bdbb..4d03a3c3 100644 --- a/src/alire/alire-milestones.ads +++ b/src/alire/alire-milestones.ads @@ -1,6 +1,6 @@ with Alire.Interfaces; -with Semantic_Versioning.Extended; +with Semantic_Versioning; private with Alire.Utils.TTY; @@ -23,38 +23,8 @@ package Alire.Milestones with Preelaborate is overriding function TTY_Image (M : Milestone) return String; - ----------------------- - -- Milestone parsing -- - ----------------------- - - type Allowed_Milestones (<>) is tagged private; - - function Crate_Versions (Spec : String) return Allowed_Milestones; - -- Either valid set or Constraint_Error - -- If no version was specified, Any version is returned - -- Syntax: name[extended version set expression] - - function Crate (This : Allowed_Milestones) return Crate_Name; - function Versions (This : Allowed_Milestones) - return Semantic_Versioning.Extended.Version_Set; - - function Image (This : Allowed_Milestones) return String; - function TTY_Image (This : Allowed_Milestones) return String; - private - type Allowed_Milestones (Len : Positive) is tagged record - Crate : Alire.Crate_Name (Len); - Versions : Semantic_Versioning.Extended.Version_Set; - end record; - - function Crate (This : Allowed_Milestones) return Crate_Name - is (This.Crate); - - function Versions (This : Allowed_Milestones) - return Semantic_Versioning.Extended.Version_Set - is (This.Versions); - package TTY renames Utils.TTY; type Milestone (Name_Len : Natural) is new Interfaces.Colorable with record @@ -90,10 +60,4 @@ private & "=" & TTY.Version (Image (M.Version))); - function Image (This : Allowed_Milestones) return String - is ((+This.Crate) & This.Versions.Image); - - function TTY_Image (This : Allowed_Milestones) return String - is (TTY.Name (This.Crate) & TTY.Version (This.Versions.Image)); - end Alire.Milestones; diff --git a/src/alr/alr-commands-get.adb b/src/alr/alr-commands-get.adb index 94279567..5df8dc35 100644 --- a/src/alr/alr-commands-get.adb +++ b/src/alr/alr-commands-get.adb @@ -301,8 +301,8 @@ package body Alr.Commands.Get is end if; declare - Allowed : constant Alire.Milestones.Allowed_Milestones := - Alire.Milestones.Crate_Versions (Argument (1)); + Allowed : constant Alire.Dependencies.Dependency := + Alire.Dependencies.From_String (Argument (1)); begin if Cmd.Build and Cmd.Only then Reportaise_Wrong_Arguments diff --git a/src/alr/alr-commands-init.adb b/src/alr/alr-commands-init.adb index e2f77e51..fab8a3a1 100644 --- a/src/alr/alr-commands-init.adb +++ b/src/alr/alr-commands-init.adb @@ -4,6 +4,7 @@ with Ada.Directories; with Ada.Text_IO; with Alire.Config; +with Alire.Dependencies; with Alire.Lockfiles; with Alire.Milestones; with Alire.Paths; @@ -401,9 +402,7 @@ package body Alr.Commands.Init is declare Name : constant String := Argument (1); - Check : constant Alire.Milestones.Allowed_Milestones := - Alire.Milestones.Crate_Versions (Name) - with Unreferenced; + Check : constant Alire.Crate_Name := +Name with Unreferenced; begin if Utils.To_Lower_Case (Name) = Utils.To_Lower_Case (Sed_Pattern) then diff --git a/src/alr/alr-commands-show.adb b/src/alr/alr-commands-show.adb index 768dc407..734c4221 100644 --- a/src/alr/alr-commands-show.adb +++ b/src/alr/alr-commands-show.adb @@ -230,10 +230,10 @@ package body Alr.Commands.Show is end if; declare - Allowed : constant Alire.Milestones.Allowed_Milestones := + Allowed : constant Alire.Dependencies.Dependency := (if Num_Arguments = 1 - then Alire.Milestones.Crate_Versions (Argument (1)) - else Alire.Milestones.Crate_Versions + then Alire.Dependencies.From_String (Argument (1)) + else Alire.Dependencies.From_String (Cmd.Root.Release.Milestone.Image)); begin if Num_Arguments = 1 and not Alire.Index.Exists (Allowed.Crate) then diff --git a/src/alr/alr-commands-test.adb b/src/alr/alr-commands-test.adb index 6099899b..95d9d91c 100644 --- a/src/alr/alr-commands-test.adb +++ b/src/alr/alr-commands-test.adb @@ -5,6 +5,7 @@ with Ada.Exceptions; with Alire.Containers; with Alire.Crates; with Alire.Defaults; +with Alire.Dependencies; with Alire.Directories; with Alire.Index; with Alire.Milestones; @@ -408,8 +409,8 @@ package body Alr.Commands.Test is else for J in 1 .. Num_Arguments loop declare - Allowed : constant Alire.Milestones.Allowed_Milestones := - Alire.Milestones.Crate_Versions (Argument (J)); + Allowed : constant Alire.Dependencies.Dependency := + Alire.Dependencies.From_String (Argument (J)); Crate : constant Alire.Crates.Crate := Alire.Index.Crate (Allowed.Crate); Releases : constant Alire.Containers.Release_Set := @@ -469,8 +470,8 @@ package body Alr.Commands.Test is if not Cmd.Search then for I in 1 .. Num_Arguments loop declare - Cry_Me_A_River : constant Alire.Milestones.Allowed_Milestones := - Alire.Milestones.Crate_Versions + Cry_Me_A_River : constant Alire.Dependencies.Dependency := + Alire.Dependencies.From_String (Argument (I)) with Unreferenced; begin null; -- Just check that no exception is raised diff --git a/src/alr/alr-commands-withing.adb b/src/alr/alr-commands-withing.adb index 0e4c71f7..78550c66 100644 --- a/src/alr/alr-commands-withing.adb +++ b/src/alr/alr-commands-withing.adb @@ -58,8 +58,8 @@ package body Alr.Commands.Withing is return Alire.Conditional.Dependencies is use all type Alire.Conditional.Dependencies; - Requested : constant Alire.Milestones.Allowed_Milestones := - Alire.Milestones.Crate_Versions (New_Dep); + Requested : constant Alire.Dependencies.Dependency := + Alire.Dependencies.From_String (New_Dep); begin -- Check that the requested dependency exists @@ -95,10 +95,8 @@ package body Alr.Commands.Withing is procedure Add_Softlink (Cmd : in out Command; Dep_Spec : String; Path : String) is - Requested : constant Alire.Milestones.Allowed_Milestones := - Alire.Milestones.Crate_Versions (Dep_Spec); - New_Dep : constant Alire.Dependencies.Dependency := - Alire.Dependencies.From_Milestones (Requested); + New_Dep : constant Alire.Dependencies.Dependency := + Alire.Dependencies.From_String (Dep_Spec); begin -- Confirm target dir @@ -193,8 +191,8 @@ package body Alr.Commands.Withing is is use all type Alire.Conditional.Dependencies; use all type Semantic_Versioning.Extended.Version_Set; - Requested : constant Alire.Milestones.Allowed_Milestones := - Alire.Milestones.Crate_Versions (Old_Dep); + Requested : constant Alire.Dependencies.Dependency := + Alire.Dependencies.From_String (Old_Dep); Found : Boolean := False; begin if Requested.Versions /= Semantic_Versioning.Extended.Any then -- 2.39.5