Skip to content

Commit

Permalink
4267 Add --exact flag to the list command
Browse files Browse the repository at this point in the history
This PR adds `--exact` flag to the cabal list command which
enables exact match on the package name while searching.
  • Loading branch information
v0d1ch committed Mar 27, 2020
1 parent cf25742 commit b6cdb56
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 deletions.
16 changes: 13 additions & 3 deletions Cabal/Distribution/Simple/PackageIndex.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ module Distribution.Simple.PackageIndex (
searchByName,
SearchResult(..),
searchByNameSubstring,
searchByNameExact,

-- ** Bulk queries
allPackages,
Expand Down Expand Up @@ -526,16 +527,25 @@ data SearchResult a = None | Unambiguous a | Ambiguous [a]
-- That is, all packages that contain the given string in their name.
--
searchByNameSubstring :: PackageIndex a -> String -> [a]
searchByNameSubstring index searchterm =
searchByNameSubstring =
searchByNameInternal False

searchByNameExact :: PackageIndex a -> String -> [a]
searchByNameExact =
searchByNameInternal True

searchByNameInternal :: Bool -> PackageIndex a -> String -> [a]
searchByNameInternal exactMatch index searchterm =
[ pkg
-- Don't match internal packages
| ((pname, LMainLibName), pvers) <- Map.toList (packageIdIndex index)
, lsearchterm `isInfixOf` lowercase (unPackageName pname)
, if exactMatch
then searchterm == unPackageName pname
else lsearchterm `isInfixOf` lowercase (unPackageName pname)
, pkgs <- Map.elems pvers
, pkg <- pkgs ]
where lsearchterm = lowercase searchterm


--
-- * Special queries
--
Expand Down
9 changes: 7 additions & 2 deletions cabal-install/Distribution/Client/List.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ getPkgList verbosity packageDBs repoCtxt comp progdb listFlags pats = do
[(PackageName, [Installed.InstalledPackageInfo], [UnresolvedSourcePackage])]
pkgsInfoMatching =
let matchingInstalled = matchingPackages
InstalledPackageIndex.searchByNameSubstring
ipiSearch
installedPkgIndex
matchingSource = matchingPackages
(\ idx n ->
concatMap snd
(PackageIndex.searchByNameSubstring idx n))
(piSearch idx n))
sourcePkgIndex
in mergePackages matchingInstalled matchingSource

Expand All @@ -131,6 +131,11 @@ getPkgList verbosity packageDBs repoCtxt comp progdb listFlags pats = do
return matches
where
onlyInstalled = fromFlag (listInstalled listFlags)
exactMatch = fromFlag (listExactMatch listFlags)
ipiSearch | exactMatch = InstalledPackageIndex.searchByNameExact
| otherwise = InstalledPackageIndex.searchByNameSubstring
piSearch | exactMatch = PackageIndex.searchByNameExact
| otherwise = PackageIndex.searchByNameSubstring
matchingPackages search index =
[ pkg
| pat <- pats
Expand Down
6 changes: 6 additions & 0 deletions cabal-install/Distribution/Client/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,7 @@ instance Semigroup GetFlags where
data ListFlags = ListFlags {
listInstalled :: Flag Bool,
listSimpleOutput :: Flag Bool,
listExactMatch :: Flag Bool,
listVerbosity :: Flag Verbosity,
listPackageDBs :: [Maybe PackageDB]
} deriving Generic
Expand All @@ -1631,6 +1632,7 @@ defaultListFlags :: ListFlags
defaultListFlags = ListFlags {
listInstalled = Flag False,
listSimpleOutput = Flag False,
listExactMatch = Flag False,
listVerbosity = toFlag normal,
listPackageDBs = []
}
Expand Down Expand Up @@ -1666,6 +1668,10 @@ listCommand = CommandUI {
"Print in a easy-to-parse format"
listSimpleOutput (\v flags -> flags { listSimpleOutput = v })
trueArg
, option [] ["exact"]
"Print only exact match"
listExactMatch (\v flags -> flags { listExactMatch = v })
trueArg

, option "" ["package-db"]
( "Append the given package database to the list of package"
Expand Down
19 changes: 17 additions & 2 deletions cabal-install/Distribution/Solver/Types/PackageIndex.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module Distribution.Solver.Types.PackageIndex (
searchByName,
SearchResult(..),
searchByNameSubstring,
searchByNameExact,

-- ** Bulk queries
allPackages,
Expand Down Expand Up @@ -312,9 +313,23 @@ data SearchResult a = None | Unambiguous a | Ambiguous [a]
--
searchByNameSubstring :: PackageIndex pkg
-> String -> [(PackageName, [pkg])]
searchByNameSubstring (PackageIndex m) searchterm =
searchByNameSubstring =
searchByNameInternal False

searchByNameExact :: PackageIndex pkg
-> String -> [(PackageName, [pkg])]
searchByNameExact =
searchByNameInternal True

searchByNameInternal :: Bool
-> PackageIndex pkg
-> String -> [(PackageName, [pkg])]
searchByNameInternal exactMatch (PackageIndex m) searchterm =
[ pkgs
| pkgs@(pname, _) <- Map.toList m
, lsearchterm `isInfixOf` lowercase (unPackageName pname) ]
, if exactMatch
then searchterm `isInfixOf` unPackageName pname
else lsearchterm `isInfixOf` lowercase (unPackageName pname)
]
where
lsearchterm = lowercase searchterm
2 changes: 2 additions & 0 deletions cabal-install/changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
-*-change-log-*-

3.2.0.0 Someone <mailto:[email protected]> February 2020
* Add `--exact` flag to `cabal list` command that enables exact match on the
search package name (#4267)
* `v2-build` (and other `v2-`prefixed commands) now accept the
`--benchmark-option(s)` flags, which pass options to benchmark executables
(analogous to how `--test-option(s)` works). (#6209)
Expand Down
2 changes: 1 addition & 1 deletion solver-benchmarks/HackageBenchmark.hs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ hackageBenchmarkMain = do
if null argPackages
then do
putStrLn $ "Obtaining the package list (using " ++ argCabal1 ++ ") ..."
list <- readProcess argCabal1 ["list", "--simple-output"] ""
list <- readProcess argCabal1 ["list", "--simple-output", "--exact"] ""
return $ nub [mkPackageName $ head (words line) | line <- lines list]
else do
putStrLn "Using given package list ..."
Expand Down

0 comments on commit b6cdb56

Please sign in to comment.