Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4267 Add --exact flag to the list command #6618

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1624,6 +1624,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 @@ -1632,6 +1633,7 @@ defaultListFlags :: ListFlags
defaultListFlags = ListFlags {
listInstalled = Flag False,
listSimpleOutput = Flag False,
listExactMatch = Flag False,
listVerbosity = toFlag normal,
listPackageDBs = []
}
Expand Down Expand Up @@ -1667,6 +1669,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 == unPackageName pname
else lsearchterm `isInfixOf` lowercase (unPackageName pname)
]
where
lsearchterm = lowercase searchterm
3 changes: 3 additions & 0 deletions cabal-install/changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
-*-change-log-*-

3.2.0.0 Herbert Valerio Riedel <[email protected]> April 2020
3.2.0.0 Someone <mailto:[email protected]> February 2020
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add fine to changelog.d directory, see the issue-* files for an examples

It's recent change, so we won't introduce conflicts to changelog files.

* 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
4 changes: 4 additions & 0 deletions changelog.d/issue-4267
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
synopsis: Add --exact flag to the list command
packages: cabal-install
prs: #6618
issues: #4267