{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
module Stack.List
( listCmd
, listPackages
) where
import Pantry ( loadSnapshot )
import qualified RIO.ByteString.Lazy as Lazy
import qualified RIO.Map as Map
import RIO.Process ( HasProcessContext )
import Stack.Config ( makeConcreteResolver )
import Stack.Prelude
import Stack.Runners ( ShouldReexec (..), withConfig )
import Stack.Types.GlobalOpts ( GlobalOpts (..) )
import Stack.Types.Runner ( Runner, globalOptsL )
newtype ListPrettyException
= CouldNotParsePackageSelectors [StyleDoc]
deriving (Int -> ListPrettyException -> ShowS
[ListPrettyException] -> ShowS
ListPrettyException -> String
(Int -> ListPrettyException -> ShowS)
-> (ListPrettyException -> String)
-> ([ListPrettyException] -> ShowS)
-> Show ListPrettyException
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ListPrettyException -> ShowS
showsPrec :: Int -> ListPrettyException -> ShowS
$cshow :: ListPrettyException -> String
show :: ListPrettyException -> String
$cshowList :: [ListPrettyException] -> ShowS
showList :: [ListPrettyException] -> ShowS
Show, Typeable)
instance Pretty ListPrettyException where
pretty :: ListPrettyException -> StyleDoc
pretty (CouldNotParsePackageSelectors [StyleDoc]
errs) =
StyleDoc
"[S-4926]"
StyleDoc -> StyleDoc -> StyleDoc
forall a. Semigroup a => a -> a -> a
<> StyleDoc
line
StyleDoc -> StyleDoc -> StyleDoc
forall a. Semigroup a => a -> a -> a
<> [StyleDoc] -> StyleDoc
bulletedList [StyleDoc]
errs
instance Exception ListPrettyException
listCmd :: [String] -> RIO Runner ()
listCmd :: [String] -> RIO Runner ()
listCmd [String]
names = ShouldReexec -> RIO Config () -> RIO Runner ()
forall a. ShouldReexec -> RIO Config a -> RIO Runner a
withConfig ShouldReexec
NoReexec (RIO Config () -> RIO Runner ()) -> RIO Config () -> RIO Runner ()
forall a b. (a -> b) -> a -> b
$ do
Maybe AbstractResolver
mresolver <- Getting (Maybe AbstractResolver) Config (Maybe AbstractResolver)
-> RIO Config (Maybe AbstractResolver)
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (Getting (Maybe AbstractResolver) Config (Maybe AbstractResolver)
-> RIO Config (Maybe AbstractResolver))
-> Getting (Maybe AbstractResolver) Config (Maybe AbstractResolver)
-> RIO Config (Maybe AbstractResolver)
forall a b. (a -> b) -> a -> b
$ (GlobalOpts -> Const (Maybe AbstractResolver) GlobalOpts)
-> Config -> Const (Maybe AbstractResolver) Config
forall env. HasRunner env => Lens' env GlobalOpts
Lens' Config GlobalOpts
globalOptsL((GlobalOpts -> Const (Maybe AbstractResolver) GlobalOpts)
-> Config -> Const (Maybe AbstractResolver) Config)
-> ((Maybe AbstractResolver
-> Const (Maybe AbstractResolver) (Maybe AbstractResolver))
-> GlobalOpts -> Const (Maybe AbstractResolver) GlobalOpts)
-> Getting (Maybe AbstractResolver) Config (Maybe AbstractResolver)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(GlobalOpts -> Maybe AbstractResolver)
-> SimpleGetter GlobalOpts (Maybe AbstractResolver)
forall s a. (s -> a) -> SimpleGetter s a
to GlobalOpts -> Maybe AbstractResolver
globalResolver
Maybe RawSnapshot
mSnapshot <- Maybe AbstractResolver
-> (AbstractResolver -> RIO Config RawSnapshot)
-> RIO Config (Maybe RawSnapshot)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM Maybe AbstractResolver
mresolver ((AbstractResolver -> RIO Config RawSnapshot)
-> RIO Config (Maybe RawSnapshot))
-> (AbstractResolver -> RIO Config RawSnapshot)
-> RIO Config (Maybe RawSnapshot)
forall a b. (a -> b) -> a -> b
$ \AbstractResolver
resolver -> do
RawSnapshotLocation
concrete <- AbstractResolver -> RIO Config RawSnapshotLocation
forall env.
HasConfig env =>
AbstractResolver -> RIO env RawSnapshotLocation
makeConcreteResolver AbstractResolver
resolver
SnapshotLocation
loc <- RawSnapshotLocation -> RIO Config SnapshotLocation
forall env.
(HasPantryConfig env, HasLogFunc env) =>
RawSnapshotLocation -> RIO env SnapshotLocation
completeSnapshotLocation RawSnapshotLocation
concrete
SnapshotLocation -> RIO Config RawSnapshot
forall env.
(HasPantryConfig env, HasLogFunc env, HasProcessContext env) =>
SnapshotLocation -> RIO env RawSnapshot
loadSnapshot SnapshotLocation
loc
Maybe RawSnapshot -> [String] -> RIO Config ()
forall env.
(HasPantryConfig env, HasProcessContext env, HasTerm env) =>
Maybe RawSnapshot -> [String] -> RIO env ()
listPackages Maybe RawSnapshot
mSnapshot [String]
names
listPackages ::
forall env. (HasPantryConfig env, HasProcessContext env, HasTerm env)
=> Maybe RawSnapshot
-> [String]
-> RIO env ()
listPackages :: forall env.
(HasPantryConfig env, HasProcessContext env, HasTerm env) =>
Maybe RawSnapshot -> [String] -> RIO env ()
listPackages Maybe RawSnapshot
mSnapshot [String]
input = do
let ([StyleDoc]
errs1, [PackageName]
names) = case Maybe RawSnapshot
mSnapshot of
Just RawSnapshot
snapshot | [String] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
input -> ([], Map PackageName RawSnapshotPackage -> [PackageName]
forall k a. Map k a -> [k]
Map.keys (RawSnapshot -> Map PackageName RawSnapshotPackage
rsPackages RawSnapshot
snapshot))
Maybe RawSnapshot
_ -> [Either StyleDoc PackageName] -> ([StyleDoc], [PackageName])
forall a b. [Either a b] -> ([a], [b])
partitionEithers ([Either StyleDoc PackageName] -> ([StyleDoc], [PackageName]))
-> [Either StyleDoc PackageName] -> ([StyleDoc], [PackageName])
forall a b. (a -> b) -> a -> b
$ (String -> Either StyleDoc PackageName)
-> [String] -> [Either StyleDoc PackageName]
forall a b. (a -> b) -> [a] -> [b]
map String -> Either StyleDoc PackageName
parse [String]
input
([StyleDoc]
errs2, [PackageIdentifier]
locs) <- [Either StyleDoc PackageIdentifier]
-> ([StyleDoc], [PackageIdentifier])
forall a b. [Either a b] -> ([a], [b])
partitionEithers ([Either StyleDoc PackageIdentifier]
-> ([StyleDoc], [PackageIdentifier]))
-> RIO env [Either StyleDoc PackageIdentifier]
-> RIO env ([StyleDoc], [PackageIdentifier])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (PackageName -> RIO env (Either StyleDoc PackageIdentifier))
-> [PackageName] -> RIO env [Either StyleDoc PackageIdentifier]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse PackageName -> RIO env (Either StyleDoc PackageIdentifier)
toLoc [PackageName]
names
case [StyleDoc]
errs1 [StyleDoc] -> [StyleDoc] -> [StyleDoc]
forall a. [a] -> [a] -> [a]
++ [StyleDoc]
errs2 of
[] -> () -> RIO env ()
forall a. a -> RIO env a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
[StyleDoc]
errs -> ListPrettyException -> RIO env ()
forall e (m :: * -> *) a.
(Exception e, MonadThrow m, Pretty e) =>
e -> m a
prettyThrowM (ListPrettyException -> RIO env ())
-> ListPrettyException -> RIO env ()
forall a b. (a -> b) -> a -> b
$ [StyleDoc] -> ListPrettyException
CouldNotParsePackageSelectors [StyleDoc]
errs
(PackageIdentifier -> RIO env ())
-> [PackageIdentifier] -> RIO env ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (LByteString -> RIO env ()
forall (m :: * -> *). MonadIO m => LByteString -> m ()
Lazy.putStrLn (LByteString -> RIO env ())
-> (PackageIdentifier -> LByteString)
-> PackageIdentifier
-> RIO env ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> LByteString
forall a. IsString a => String -> a
fromString (String -> LByteString)
-> (PackageIdentifier -> String)
-> PackageIdentifier
-> LByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackageIdentifier -> String
packageIdentifierString) [PackageIdentifier]
locs
where
toLoc :: PackageName -> RIO env (Either StyleDoc PackageIdentifier)
toLoc | Just RawSnapshot
snapshot <- Maybe RawSnapshot
mSnapshot = RawSnapshot
-> PackageName -> RIO env (Either StyleDoc PackageIdentifier)
toLocSnapshot RawSnapshot
snapshot
| Bool
otherwise = PackageName -> RIO env (Either StyleDoc PackageIdentifier)
toLocNoSnapshot
toLocNoSnapshot :: PackageName -> RIO env (Either StyleDoc PackageIdentifier)
toLocNoSnapshot :: PackageName -> RIO env (Either StyleDoc PackageIdentifier)
toLocNoSnapshot PackageName
name = do
Maybe PackageLocationImmutable
mloc1 <-
RequireHackageIndex
-> PackageName
-> UsePreferredVersions
-> RIO env (Maybe PackageLocationImmutable)
forall env.
(HasPantryConfig env, HasLogFunc env, HasProcessContext env) =>
RequireHackageIndex
-> PackageName
-> UsePreferredVersions
-> RIO env (Maybe PackageLocationImmutable)
getLatestHackageLocation RequireHackageIndex
YesRequireHackageIndex PackageName
name UsePreferredVersions
UsePreferredVersions
Maybe PackageLocationImmutable
mloc <-
case Maybe PackageLocationImmutable
mloc1 of
Just PackageLocationImmutable
_ -> Maybe PackageLocationImmutable
-> RIO env (Maybe PackageLocationImmutable)
forall a. a -> RIO env a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe PackageLocationImmutable
mloc1
Maybe PackageLocationImmutable
Nothing -> do
DidUpdateOccur
updated <-
Maybe Utf8Builder -> RIO env DidUpdateOccur
forall env.
(HasPantryConfig env, HasLogFunc env) =>
Maybe Utf8Builder -> RIO env DidUpdateOccur
updateHackageIndex (Maybe Utf8Builder -> RIO env DidUpdateOccur)
-> Maybe Utf8Builder -> RIO env DidUpdateOccur
forall a b. (a -> b) -> a -> b
$ Utf8Builder -> Maybe Utf8Builder
forall a. a -> Maybe a
Just (Utf8Builder -> Maybe Utf8Builder)
-> Utf8Builder -> Maybe Utf8Builder
forall a b. (a -> b) -> a -> b
$
Utf8Builder
"Could not find package "
Utf8Builder -> Utf8Builder -> Utf8Builder
forall a. Semigroup a => a -> a -> a
<> String -> Utf8Builder
forall a. IsString a => String -> a
fromString (PackageName -> String
packageNameString PackageName
name)
Utf8Builder -> Utf8Builder -> Utf8Builder
forall a. Semigroup a => a -> a -> a
<> Utf8Builder
", updating"
case DidUpdateOccur
updated of
DidUpdateOccur
UpdateOccurred ->
RequireHackageIndex
-> PackageName
-> UsePreferredVersions
-> RIO env (Maybe PackageLocationImmutable)
forall env.
(HasPantryConfig env, HasLogFunc env, HasProcessContext env) =>
RequireHackageIndex
-> PackageName
-> UsePreferredVersions
-> RIO env (Maybe PackageLocationImmutable)
getLatestHackageLocation
RequireHackageIndex
YesRequireHackageIndex
PackageName
name
UsePreferredVersions
UsePreferredVersions
DidUpdateOccur
NoUpdateOccurred -> Maybe PackageLocationImmutable
-> RIO env (Maybe PackageLocationImmutable)
forall a. a -> RIO env a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe PackageLocationImmutable
forall a. Maybe a
Nothing
case Maybe PackageLocationImmutable
mloc of
Maybe PackageLocationImmutable
Nothing -> do
[PackageName]
candidates <- PackageName -> RIO env [PackageName]
forall env.
(HasPantryConfig env, HasLogFunc env) =>
PackageName -> RIO env [PackageName]
getHackageTypoCorrections PackageName
name
Either StyleDoc PackageIdentifier
-> RIO env (Either StyleDoc PackageIdentifier)
forall a. a -> RIO env a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either StyleDoc PackageIdentifier
-> RIO env (Either StyleDoc PackageIdentifier))
-> Either StyleDoc PackageIdentifier
-> RIO env (Either StyleDoc PackageIdentifier)
forall a b. (a -> b) -> a -> b
$ StyleDoc -> Either StyleDoc PackageIdentifier
forall a b. a -> Either a b
Left (StyleDoc -> Either StyleDoc PackageIdentifier)
-> StyleDoc -> Either StyleDoc PackageIdentifier
forall a b. (a -> b) -> a -> b
$ [StyleDoc] -> StyleDoc
fillSep
[ String -> StyleDoc
flow String
"Could not find package"
, Style -> StyleDoc -> StyleDoc
style Style
Current (String -> StyleDoc
forall a. IsString a => String -> a
fromString (String -> StyleDoc) -> String -> StyleDoc
forall a b. (a -> b) -> a -> b
$ PackageName -> String
packageNameString PackageName
name)
, String -> StyleDoc
flow String
"on Hackage."
, if [PackageName] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [PackageName]
candidates
then StyleDoc
forall a. Monoid a => a
mempty
else [StyleDoc] -> StyleDoc
fillSep ([StyleDoc] -> StyleDoc) -> [StyleDoc] -> StyleDoc
forall a b. (a -> b) -> a -> b
$
String -> StyleDoc
flow String
"Perhaps you meant one of:"
StyleDoc -> [StyleDoc] -> [StyleDoc]
forall a. a -> [a] -> [a]
: Maybe Style -> Bool -> [StyleDoc] -> [StyleDoc]
forall a. Pretty a => Maybe Style -> Bool -> [a] -> [StyleDoc]
mkNarrativeList (Style -> Maybe Style
forall a. a -> Maybe a
Just Style
Good) Bool
False
((PackageName -> StyleDoc) -> [PackageName] -> [StyleDoc]
forall a b. (a -> b) -> [a] -> [b]
map (String -> StyleDoc
forall a. IsString a => String -> a
fromString (String -> StyleDoc)
-> (PackageName -> String) -> PackageName -> StyleDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackageName -> String
packageNameString) [PackageName]
candidates :: [StyleDoc])
]
Just PackageLocationImmutable
loc -> Either StyleDoc PackageIdentifier
-> RIO env (Either StyleDoc PackageIdentifier)
forall a. a -> RIO env a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either StyleDoc PackageIdentifier
-> RIO env (Either StyleDoc PackageIdentifier))
-> Either StyleDoc PackageIdentifier
-> RIO env (Either StyleDoc PackageIdentifier)
forall a b. (a -> b) -> a -> b
$ PackageIdentifier -> Either StyleDoc PackageIdentifier
forall a b. b -> Either a b
Right (PackageLocationImmutable -> PackageIdentifier
packageLocationIdent PackageLocationImmutable
loc)
toLocSnapshot ::
RawSnapshot
-> PackageName
-> RIO env (Either StyleDoc PackageIdentifier)
toLocSnapshot :: RawSnapshot
-> PackageName -> RIO env (Either StyleDoc PackageIdentifier)
toLocSnapshot RawSnapshot
snapshot PackageName
name =
case PackageName
-> Map PackageName RawSnapshotPackage -> Maybe RawSnapshotPackage
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup PackageName
name (RawSnapshot -> Map PackageName RawSnapshotPackage
rsPackages RawSnapshot
snapshot) of
Maybe RawSnapshotPackage
Nothing ->
Either StyleDoc PackageIdentifier
-> RIO env (Either StyleDoc PackageIdentifier)
forall a. a -> RIO env a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either StyleDoc PackageIdentifier
-> RIO env (Either StyleDoc PackageIdentifier))
-> Either StyleDoc PackageIdentifier
-> RIO env (Either StyleDoc PackageIdentifier)
forall a b. (a -> b) -> a -> b
$ StyleDoc -> Either StyleDoc PackageIdentifier
forall a b. a -> Either a b
Left (StyleDoc -> Either StyleDoc PackageIdentifier)
-> StyleDoc -> Either StyleDoc PackageIdentifier
forall a b. (a -> b) -> a -> b
$ [StyleDoc] -> StyleDoc
fillSep
[ String -> StyleDoc
flow String
"Package does not appear in snapshot:"
, Style -> StyleDoc -> StyleDoc
style Style
Current (String -> StyleDoc
forall a. IsString a => String -> a
fromString (String -> StyleDoc) -> String -> StyleDoc
forall a b. (a -> b) -> a -> b
$ PackageName -> String
packageNameString PackageName
name) StyleDoc -> StyleDoc -> StyleDoc
forall a. Semigroup a => a -> a -> a
<> StyleDoc
"."
]
Just RawSnapshotPackage
sp -> do
PackageLocationImmutable
loc <- CompletePackageLocation -> PackageLocationImmutable
cplComplete (CompletePackageLocation -> PackageLocationImmutable)
-> RIO env CompletePackageLocation
-> RIO env PackageLocationImmutable
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RawPackageLocationImmutable -> RIO env CompletePackageLocation
forall env.
(HasPantryConfig env, HasLogFunc env, HasProcessContext env) =>
RawPackageLocationImmutable -> RIO env CompletePackageLocation
completePackageLocation (RawSnapshotPackage -> RawPackageLocationImmutable
rspLocation RawSnapshotPackage
sp)
Either StyleDoc PackageIdentifier
-> RIO env (Either StyleDoc PackageIdentifier)
forall a. a -> RIO env a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either StyleDoc PackageIdentifier
-> RIO env (Either StyleDoc PackageIdentifier))
-> Either StyleDoc PackageIdentifier
-> RIO env (Either StyleDoc PackageIdentifier)
forall a b. (a -> b) -> a -> b
$ PackageIdentifier -> Either StyleDoc PackageIdentifier
forall a b. b -> Either a b
Right (PackageLocationImmutable -> PackageIdentifier
packageLocationIdent PackageLocationImmutable
loc)
parse :: String -> Either StyleDoc PackageName
parse String
s =
case String -> Maybe PackageName
parsePackageName String
s of
Just PackageName
x -> PackageName -> Either StyleDoc PackageName
forall a b. b -> Either a b
Right PackageName
x
Maybe PackageName
Nothing -> StyleDoc -> Either StyleDoc PackageName
forall a b. a -> Either a b
Left (StyleDoc -> Either StyleDoc PackageName)
-> StyleDoc -> Either StyleDoc PackageName
forall a b. (a -> b) -> a -> b
$ [StyleDoc] -> StyleDoc
fillSep
[ String -> StyleDoc
flow String
"Could not parse as package name or identifier:"
, Style -> StyleDoc -> StyleDoc
style Style
Current (String -> StyleDoc
forall a. IsString a => String -> a
fromString String
s) StyleDoc -> StyleDoc -> StyleDoc
forall a. Semigroup a => a -> a -> a
<> StyleDoc
"."
]