From da3b51c78f07a64f123c7ac47b620c609fc941e8 Mon Sep 17 00:00:00 2001 From: Graham Knop Date: Mon, 29 Mar 2021 08:37:14 +0200 Subject: [PATCH] fix used_plugins to only return real traits using proper format Rather than filtering the traits list to ones not generated by MooX::Traits::Util, filter for %INC entries that came from their own files by checking that the file matches the trait name. Also ensure traits in subdirectories are returned with double colon separators, rather than slashes. --- lib/DBIx/Class/Migration/RunScript.pm | 31 ++++++++++++--------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/lib/DBIx/Class/Migration/RunScript.pm b/lib/DBIx/Class/Migration/RunScript.pm index b0bac69..5709157 100644 --- a/lib/DBIx/Class/Migration/RunScript.pm +++ b/lib/DBIx/Class/Migration/RunScript.pm @@ -111,23 +111,20 @@ sub used_plugins { #We find them by looking for packages loaded in %INC that have #the key matching the namespace defined by $path my $path = 'DBIx/Class/Migration/RunScript/Trait/'; - my $match = "$path(.+).pm"; - - #however, there's one danger: if there's already been a - #migration object created, which will have MANY matches for - #$path in it, along with __AND and other things that - #Moox::Traits::Util names a package with dynamic roles, we - #shouldn't try to include that. Packages made that way get the - #same %INC value as MooX::Traits::Util, so let's save that. - my $traits_path = $INC{'MooX/Traits/Util.pm'}; - - #so we get the list of packages loaded that match $path - my @traits = grep { m[$path]x } keys %INC; - #filter out any that were made via MooX::Traits::Util, if it was - #loaded at all, yet - @traits = grep { $INC{$_} ne $traits_path } @traits if $traits_path; - #and return the last part of the path, the name of the plugin - return map { m[$match]x } @traits; + + # there may be entries in %INC for traits that are generated, and not + # originals. search our traits, filtering out anything that wasn't sourced + # from a regular file. + my @traits = grep { m[$path]x && $INC{$_} =~ /\Q$_\E\z/ } keys %INC; + + # and return the last part of the path, the name of the plugin + for (@traits) { + s/\.pm\z//; + s/\A\Q$path//; + s{/}{::}g; + } + + return @traits; } sub builder(&) {