{-# LANGUAGE NoImplicitPrelude   #-}
{-# LANGUAGE OverloadedStrings   #-}

-- | Functions related to Stack's @unpack@ command.

module Stack.Unpack
  ( unpackCmd
  , unpackPackages
  ) where

import           Path ( (</>), parseRelDir )
import           Path.IO ( doesDirExist, resolveDir' )
import           Pantry ( loadSnapshot )
import qualified RIO.Map as Map
import           RIO.Process ( HasProcessContext )
import qualified RIO.Set as Set
import qualified RIO.Text as T
import           Stack.Config ( makeConcreteResolver )
import           Stack.Prelude
import           Stack.Runners ( ShouldReexec (..), withConfig )
import           Stack.Types.GlobalOpts ( GlobalOpts (..) )
import           Stack.Types.Runner ( Runner, globalOptsL )

-- | Type representing \'pretty\' exceptions thrown by functions exported by the

-- "Stack.Unpack" module.

data UnpackPrettyException
  = UnpackDirectoryAlreadyExists (Set (Path Abs Dir))
  | CouldNotParsePackageSelectors [StyleDoc]
  deriving (Int -> UnpackPrettyException -> ShowS
[UnpackPrettyException] -> ShowS
UnpackPrettyException -> String
(Int -> UnpackPrettyException -> ShowS)
-> (UnpackPrettyException -> String)
-> ([UnpackPrettyException] -> ShowS)
-> Show UnpackPrettyException
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UnpackPrettyException -> ShowS
showsPrec :: Int -> UnpackPrettyException -> ShowS
$cshow :: UnpackPrettyException -> String
show :: UnpackPrettyException -> String
$cshowList :: [UnpackPrettyException] -> ShowS
showList :: [UnpackPrettyException] -> ShowS
Show, Typeable)

instance Pretty UnpackPrettyException where
  pretty :: UnpackPrettyException -> StyleDoc
pretty (UnpackDirectoryAlreadyExists Set (Path Abs Dir)
dirs) =
    StyleDoc
"[S-3515]"
    StyleDoc -> StyleDoc -> StyleDoc
forall a. Semigroup a => a -> a -> a
<> StyleDoc
line
    StyleDoc -> StyleDoc -> StyleDoc
forall a. Semigroup a => a -> a -> a
<> String -> StyleDoc
flow String
"Stack was unable to unpack due to directories already being \
            \present:"
    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 ((Path Abs Dir -> StyleDoc) -> [Path Abs Dir] -> [StyleDoc]
forall a b. (a -> b) -> [a] -> [b]
map Path Abs Dir -> StyleDoc
forall a. Pretty a => a -> StyleDoc
pretty ([Path Abs Dir] -> [StyleDoc]) -> [Path Abs Dir] -> [StyleDoc]
forall a b. (a -> b) -> a -> b
$ Set (Path Abs Dir) -> [Path Abs Dir]
forall a. Set a -> [a]
Set.toList Set (Path Abs Dir)
dirs)
  pretty (CouldNotParsePackageSelectors [StyleDoc]
errs) =
    StyleDoc
"[S-2628]"
    StyleDoc -> StyleDoc -> StyleDoc
forall a. Semigroup a => a -> a -> a
<> StyleDoc
line
    StyleDoc -> StyleDoc -> StyleDoc
forall a. Semigroup a => a -> a -> a
<> String -> StyleDoc
flow String
"The following package selectors are not valid package names or \
            \identifiers:"
    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 UnpackPrettyException

-- | Function underlying the @stack unpack@ command. Unpack packages to the

-- filesystem.

unpackCmd ::
     ([String], Maybe Text)
     -- ^ A pair of a list of names or identifiers and an optional destination

     -- path.

  -> RIO Runner ()
unpackCmd :: ([String], Maybe Text) -> RIO Runner ()
unpackCmd ([String]
names, Maybe Text
Nothing) = ([String], Maybe Text) -> RIO Runner ()
unpackCmd ([String]
names, Text -> Maybe Text
forall a. a -> Maybe a
Just Text
".")
unpackCmd ([String]
names, Just Text
dstPath) = 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
  Path Abs Dir
dstPath' <- String -> RIO Config (Path Abs Dir)
forall (m :: * -> *). MonadIO m => String -> m (Path Abs Dir)
resolveDir' (String -> RIO Config (Path Abs Dir))
-> String -> RIO Config (Path Abs Dir)
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
dstPath
  Maybe RawSnapshot -> Path Abs Dir -> [String] -> RIO Config ()
forall env.
(HasPantryConfig env, HasProcessContext env, HasTerm env) =>
Maybe RawSnapshot -> Path Abs Dir -> [String] -> RIO env ()
unpackPackages Maybe RawSnapshot
mSnapshot Path Abs Dir
dstPath' [String]
names

-- | Intended to work for the command line command.

unpackPackages ::
     forall env. (HasPantryConfig env, HasProcessContext env, HasTerm env)
  => Maybe RawSnapshot -- ^ When looking up by name, take from this build plan.

  -> Path Abs Dir -- ^ Destination.

  -> [String] -- ^ Names or identifiers.

  -> RIO env ()
unpackPackages :: forall env.
(HasPantryConfig env, HasProcessContext env, HasTerm env) =>
Maybe RawSnapshot -> Path Abs Dir -> [String] -> RIO env ()
unpackPackages Maybe RawSnapshot
mSnapshot Path Abs Dir
dest [String]
input = do
  let ([StyleDoc]
errs1, ([PackageName]
names, [PackageIdentifierRevision]
pirs1)) =
        ([Either PackageName PackageIdentifierRevision]
 -> ([PackageName], [PackageIdentifierRevision]))
-> ([StyleDoc], [Either PackageName PackageIdentifierRevision])
-> ([StyleDoc], ([PackageName], [PackageIdentifierRevision]))
forall a b. (a -> b) -> ([StyleDoc], a) -> ([StyleDoc], b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Either PackageName PackageIdentifierRevision]
-> ([PackageName], [PackageIdentifierRevision])
forall a b. [Either a b] -> ([a], [b])
partitionEithers (([StyleDoc], [Either PackageName PackageIdentifierRevision])
 -> ([StyleDoc], ([PackageName], [PackageIdentifierRevision])))
-> ([StyleDoc], [Either PackageName PackageIdentifierRevision])
-> ([StyleDoc], ([PackageName], [PackageIdentifierRevision]))
forall a b. (a -> b) -> a -> b
$ [Either StyleDoc (Either PackageName PackageIdentifierRevision)]
-> ([StyleDoc], [Either PackageName PackageIdentifierRevision])
forall a b. [Either a b] -> ([a], [b])
partitionEithers ([Either StyleDoc (Either PackageName PackageIdentifierRevision)]
 -> ([StyleDoc], [Either PackageName PackageIdentifierRevision]))
-> [Either StyleDoc (Either PackageName PackageIdentifierRevision)]
-> ([StyleDoc], [Either PackageName PackageIdentifierRevision])
forall a b. (a -> b) -> a -> b
$ (String
 -> Either StyleDoc (Either PackageName PackageIdentifierRevision))
-> [String]
-> [Either StyleDoc (Either PackageName PackageIdentifierRevision)]
forall a b. (a -> b) -> [a] -> [b]
map String
-> Either StyleDoc (Either PackageName PackageIdentifierRevision)
parse [String]
input
  [(PackageLocationImmutable, PackageIdentifier)]
locs1 <- [PackageIdentifierRevision]
-> (PackageIdentifierRevision
    -> RIO env (PackageLocationImmutable, PackageIdentifier))
-> RIO env [(PackageLocationImmutable, PackageIdentifier)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [PackageIdentifierRevision]
pirs1 ((PackageIdentifierRevision
  -> RIO env (PackageLocationImmutable, PackageIdentifier))
 -> RIO env [(PackageLocationImmutable, PackageIdentifier)])
-> (PackageIdentifierRevision
    -> RIO env (PackageLocationImmutable, PackageIdentifier))
-> RIO env [(PackageLocationImmutable, PackageIdentifier)]
forall a b. (a -> b) -> a -> b
$ \PackageIdentifierRevision
pir -> do
    PackageLocationImmutable
loc <- (CompletePackageLocation -> PackageLocationImmutable)
-> RIO env CompletePackageLocation
-> RIO env PackageLocationImmutable
forall a b. (a -> b) -> RIO env a -> RIO env b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CompletePackageLocation -> PackageLocationImmutable
cplComplete (RIO env CompletePackageLocation
 -> RIO env PackageLocationImmutable)
-> RIO env CompletePackageLocation
-> RIO env PackageLocationImmutable
forall a b. (a -> b) -> a -> b
$ RawPackageLocationImmutable -> RIO env CompletePackageLocation
forall env.
(HasPantryConfig env, HasLogFunc env, HasProcessContext env) =>
RawPackageLocationImmutable -> RIO env CompletePackageLocation
completePackageLocation (RawPackageLocationImmutable -> RIO env CompletePackageLocation)
-> RawPackageLocationImmutable -> RIO env CompletePackageLocation
forall a b. (a -> b) -> a -> b
$ PackageIdentifierRevision
-> Maybe TreeKey -> RawPackageLocationImmutable
RPLIHackage PackageIdentifierRevision
pir Maybe TreeKey
forall a. Maybe a
Nothing
    (PackageLocationImmutable, PackageIdentifier)
-> RIO env (PackageLocationImmutable, PackageIdentifier)
forall a. a -> RIO env a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PackageLocationImmutable
loc, PackageLocationImmutable -> PackageIdentifier
packageLocationIdent PackageLocationImmutable
loc)
  ([StyleDoc]
errs2, [(PackageLocationImmutable, PackageIdentifier)]
locs2) <- [Either StyleDoc (PackageLocationImmutable, PackageIdentifier)]
-> ([StyleDoc], [(PackageLocationImmutable, PackageIdentifier)])
forall a b. [Either a b] -> ([a], [b])
partitionEithers ([Either StyleDoc (PackageLocationImmutable, PackageIdentifier)]
 -> ([StyleDoc], [(PackageLocationImmutable, PackageIdentifier)]))
-> RIO
     env [Either StyleDoc (PackageLocationImmutable, PackageIdentifier)]
-> RIO
     env ([StyleDoc], [(PackageLocationImmutable, PackageIdentifier)])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (PackageName
 -> RIO
      env
      (Either StyleDoc (PackageLocationImmutable, PackageIdentifier)))
-> [PackageName]
-> RIO
     env [Either StyleDoc (PackageLocationImmutable, 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 (PackageLocationImmutable, 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 -> UnpackPrettyException -> RIO env ()
forall e (m :: * -> *) a.
(Exception e, MonadThrow m, Pretty e) =>
e -> m a
prettyThrowM (UnpackPrettyException -> RIO env ())
-> UnpackPrettyException -> RIO env ()
forall a b. (a -> b) -> a -> b
$ [StyleDoc] -> UnpackPrettyException
CouldNotParsePackageSelectors [StyleDoc]
errs
  Map PackageLocationImmutable (Path Abs Dir)
locs <- [(PackageLocationImmutable, Path Abs Dir)]
-> Map PackageLocationImmutable (Path Abs Dir)
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(PackageLocationImmutable, Path Abs Dir)]
 -> Map PackageLocationImmutable (Path Abs Dir))
-> RIO env [(PackageLocationImmutable, Path Abs Dir)]
-> RIO env (Map PackageLocationImmutable (Path Abs Dir))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((PackageLocationImmutable, PackageIdentifier)
 -> RIO env (PackageLocationImmutable, Path Abs Dir))
-> [(PackageLocationImmutable, PackageIdentifier)]
-> RIO env [(PackageLocationImmutable, Path Abs Dir)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM
    (\(PackageLocationImmutable
pir, PackageIdentifier
ident) -> do
        Path Rel Dir
suffix <- String -> RIO env (Path Rel Dir)
forall (m :: * -> *). MonadThrow m => String -> m (Path Rel Dir)
parseRelDir (String -> RIO env (Path Rel Dir))
-> String -> RIO env (Path Rel Dir)
forall a b. (a -> b) -> a -> b
$ PackageIdentifier -> String
packageIdentifierString PackageIdentifier
ident
        (PackageLocationImmutable, Path Abs Dir)
-> RIO env (PackageLocationImmutable, Path Abs Dir)
forall a. a -> RIO env a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PackageLocationImmutable
pir, Path Abs Dir
dest Path Abs Dir -> Path Rel Dir -> Path Abs Dir
forall b t. Path b Dir -> Path Rel t -> Path b t
</> Path Rel Dir
suffix)
    )
    ([(PackageLocationImmutable, PackageIdentifier)]
locs1 [(PackageLocationImmutable, PackageIdentifier)]
-> [(PackageLocationImmutable, PackageIdentifier)]
-> [(PackageLocationImmutable, PackageIdentifier)]
forall a. [a] -> [a] -> [a]
++ [(PackageLocationImmutable, PackageIdentifier)]
locs2)

  [Path Abs Dir]
alreadyUnpacked <- (Path Abs Dir -> RIO env Bool)
-> [Path Abs Dir] -> RIO env [Path Abs Dir]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM Path Abs Dir -> RIO env Bool
forall (m :: * -> *) b. MonadIO m => Path b Dir -> m Bool
doesDirExist ([Path Abs Dir] -> RIO env [Path Abs Dir])
-> [Path Abs Dir] -> RIO env [Path Abs Dir]
forall a b. (a -> b) -> a -> b
$ Map PackageLocationImmutable (Path Abs Dir) -> [Path Abs Dir]
forall k a. Map k a -> [a]
Map.elems Map PackageLocationImmutable (Path Abs Dir)
locs

  Bool -> RIO env () -> RIO env ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Path Abs Dir] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Path Abs Dir]
alreadyUnpacked) (RIO env () -> RIO env ()) -> RIO env () -> RIO env ()
forall a b. (a -> b) -> a -> b
$
    UnpackPrettyException -> RIO env ()
forall e (m :: * -> *) a.
(Exception e, MonadThrow m, Pretty e) =>
e -> m a
prettyThrowM (UnpackPrettyException -> RIO env ())
-> UnpackPrettyException -> RIO env ()
forall a b. (a -> b) -> a -> b
$ Set (Path Abs Dir) -> UnpackPrettyException
UnpackDirectoryAlreadyExists (Set (Path Abs Dir) -> UnpackPrettyException)
-> Set (Path Abs Dir) -> UnpackPrettyException
forall a b. (a -> b) -> a -> b
$ [Path Abs Dir] -> Set (Path Abs Dir)
forall a. Ord a => [a] -> Set a
Set.fromList [Path Abs Dir]
alreadyUnpacked

  [(PackageLocationImmutable, Path Abs Dir)]
-> ((PackageLocationImmutable, Path Abs Dir) -> RIO env ())
-> RIO env ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Map PackageLocationImmutable (Path Abs Dir)
-> [(PackageLocationImmutable, Path Abs Dir)]
forall k a. Map k a -> [(k, a)]
Map.toList Map PackageLocationImmutable (Path Abs Dir)
locs) (((PackageLocationImmutable, Path Abs Dir) -> RIO env ())
 -> RIO env ())
-> ((PackageLocationImmutable, Path Abs Dir) -> RIO env ())
-> RIO env ()
forall a b. (a -> b) -> a -> b
$ \(PackageLocationImmutable
loc, Path Abs Dir
dest') -> do
    Path Abs Dir -> PackageLocationImmutable -> RIO env ()
forall env.
(HasPantryConfig env, HasLogFunc env, HasProcessContext env) =>
Path Abs Dir -> PackageLocationImmutable -> RIO env ()
unpackPackageLocation Path Abs Dir
dest' PackageLocationImmutable
loc
    [StyleDoc] -> RIO env ()
forall env (m :: * -> *).
(HasCallStack, HasTerm env, MonadReader env m, MonadIO m) =>
[StyleDoc] -> m ()
prettyInfoL
      [ StyleDoc
"Unpacked"
      , String -> StyleDoc
forall a. IsString a => String -> a
fromString (String -> StyleDoc) -> String -> StyleDoc
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ PackageLocationImmutable -> Text
forall a. Display a => a -> Text
textDisplay PackageLocationImmutable
loc
      , StyleDoc
"to"
      , Path Abs Dir -> StyleDoc
forall a. Pretty a => a -> StyleDoc
pretty Path Abs Dir
dest' StyleDoc -> StyleDoc -> StyleDoc
forall a. Semigroup a => a -> a -> a
<> StyleDoc
"."
      ]
 where
  toLoc :: PackageName
-> RIO
     env (Either StyleDoc (PackageLocationImmutable, PackageIdentifier))
toLoc | Just RawSnapshot
snapshot <- Maybe RawSnapshot
mSnapshot = RawSnapshot
-> PackageName
-> RIO
     env (Either StyleDoc (PackageLocationImmutable, PackageIdentifier))
toLocSnapshot RawSnapshot
snapshot
        | Bool
otherwise = PackageName
-> RIO
     env (Either StyleDoc (PackageLocationImmutable, PackageIdentifier))
toLocNoSnapshot

  toLocNoSnapshot ::
       PackageName
    -> RIO env (Either StyleDoc (PackageLocationImmutable, PackageIdentifier))
  toLocNoSnapshot :: PackageName
-> RIO
     env (Either StyleDoc (PackageLocationImmutable, 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 (PackageLocationImmutable, PackageIdentifier)
-> RIO
     env (Either StyleDoc (PackageLocationImmutable, PackageIdentifier))
forall a. a -> RIO env a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either StyleDoc (PackageLocationImmutable, PackageIdentifier)
 -> RIO
      env
      (Either StyleDoc (PackageLocationImmutable, PackageIdentifier)))
-> Either StyleDoc (PackageLocationImmutable, PackageIdentifier)
-> RIO
     env (Either StyleDoc (PackageLocationImmutable, PackageIdentifier))
forall a b. (a -> b) -> a -> b
$ StyleDoc
-> Either StyleDoc (PackageLocationImmutable, PackageIdentifier)
forall a b. a -> Either a b
Left (StyleDoc
 -> Either StyleDoc (PackageLocationImmutable, PackageIdentifier))
-> StyleDoc
-> Either StyleDoc (PackageLocationImmutable, 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 (PackageLocationImmutable, PackageIdentifier)
-> RIO
     env (Either StyleDoc (PackageLocationImmutable, PackageIdentifier))
forall a. a -> RIO env a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either StyleDoc (PackageLocationImmutable, PackageIdentifier)
 -> RIO
      env
      (Either StyleDoc (PackageLocationImmutable, PackageIdentifier)))
-> Either StyleDoc (PackageLocationImmutable, PackageIdentifier)
-> RIO
     env (Either StyleDoc (PackageLocationImmutable, PackageIdentifier))
forall a b. (a -> b) -> a -> b
$ (PackageLocationImmutable, PackageIdentifier)
-> Either StyleDoc (PackageLocationImmutable, PackageIdentifier)
forall a b. b -> Either a b
Right (PackageLocationImmutable
loc, PackageLocationImmutable -> PackageIdentifier
packageLocationIdent PackageLocationImmutable
loc)

  toLocSnapshot ::
       RawSnapshot
    -> PackageName
    -> RIO env (Either StyleDoc (PackageLocationImmutable, PackageIdentifier))
  toLocSnapshot :: RawSnapshot
-> PackageName
-> RIO
     env (Either StyleDoc (PackageLocationImmutable, 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 (PackageLocationImmutable, PackageIdentifier)
-> RIO
     env (Either StyleDoc (PackageLocationImmutable, PackageIdentifier))
forall a. a -> RIO env a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either StyleDoc (PackageLocationImmutable, PackageIdentifier)
 -> RIO
      env
      (Either StyleDoc (PackageLocationImmutable, PackageIdentifier)))
-> Either StyleDoc (PackageLocationImmutable, PackageIdentifier)
-> RIO
     env (Either StyleDoc (PackageLocationImmutable, PackageIdentifier))
forall a b. (a -> b) -> a -> b
$ StyleDoc
-> Either StyleDoc (PackageLocationImmutable, PackageIdentifier)
forall a b. a -> Either a b
Left (StyleDoc
 -> Either StyleDoc (PackageLocationImmutable, PackageIdentifier))
-> StyleDoc
-> Either StyleDoc (PackageLocationImmutable, 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 (PackageLocationImmutable, PackageIdentifier)
-> RIO
     env (Either StyleDoc (PackageLocationImmutable, PackageIdentifier))
forall a. a -> RIO env a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either StyleDoc (PackageLocationImmutable, PackageIdentifier)
 -> RIO
      env
      (Either StyleDoc (PackageLocationImmutable, PackageIdentifier)))
-> Either StyleDoc (PackageLocationImmutable, PackageIdentifier)
-> RIO
     env (Either StyleDoc (PackageLocationImmutable, PackageIdentifier))
forall a b. (a -> b) -> a -> b
$ (PackageLocationImmutable, PackageIdentifier)
-> Either StyleDoc (PackageLocationImmutable, PackageIdentifier)
forall a b. b -> Either a b
Right (PackageLocationImmutable
loc, PackageLocationImmutable -> PackageIdentifier
packageLocationIdent PackageLocationImmutable
loc)

  -- Possible future enhancement: parse names as name + version range

  parse :: String
-> Either StyleDoc (Either PackageName PackageIdentifierRevision)
parse String
s =
    case String -> Maybe PackageName
parsePackageName String
s of
      Just PackageName
x -> Either PackageName PackageIdentifierRevision
-> Either StyleDoc (Either PackageName PackageIdentifierRevision)
forall a b. b -> Either a b
Right (Either PackageName PackageIdentifierRevision
 -> Either StyleDoc (Either PackageName PackageIdentifierRevision))
-> Either PackageName PackageIdentifierRevision
-> Either StyleDoc (Either PackageName PackageIdentifierRevision)
forall a b. (a -> b) -> a -> b
$ PackageName -> Either PackageName PackageIdentifierRevision
forall a b. a -> Either a b
Left PackageName
x
      Maybe PackageName
Nothing ->
        case Text -> Either PantryException PackageIdentifierRevision
parsePackageIdentifierRevision (String -> Text
T.pack String
s) of
          Right PackageIdentifierRevision
x -> Either PackageName PackageIdentifierRevision
-> Either StyleDoc (Either PackageName PackageIdentifierRevision)
forall a b. b -> Either a b
Right (Either PackageName PackageIdentifierRevision
 -> Either StyleDoc (Either PackageName PackageIdentifierRevision))
-> Either PackageName PackageIdentifierRevision
-> Either StyleDoc (Either PackageName PackageIdentifierRevision)
forall a b. (a -> b) -> a -> b
$ PackageIdentifierRevision
-> Either PackageName PackageIdentifierRevision
forall a b. b -> Either a b
Right PackageIdentifierRevision
x
          Left PantryException
_ -> StyleDoc
-> Either StyleDoc (Either PackageName PackageIdentifierRevision)
forall a b. a -> Either a b
Left (StyleDoc
 -> Either StyleDoc (Either PackageName PackageIdentifierRevision))
-> StyleDoc
-> Either StyleDoc (Either PackageName PackageIdentifierRevision)
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
"."
            ]