{-# LANGUAGE NoImplicitPrelude     #-}
{-# LANGUAGE ConstraintKinds       #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}

module Stack.Upgrade
  ( upgrade
  , UpgradeOpts
  , upgradeOpts
  ) where

import qualified Data.Text as T
import           Options.Applicative
import           Path
import           RIO.Process
import           Stack.Build
import           Stack.Build.Target ( NeedTargets (..) )
import           Stack.Constants
import           Stack.Prelude hiding ( force, Display (..) )
import           Stack.Runners
import           Stack.Setup
import           Stack.Types.Config
import           System.Console.ANSI ( hSupportsANSIWithoutEmulation )
import           System.Process ( rawSystem, readProcess )

-- | Type representing exceptions thrown by functions exported by the

-- "Stack.Upgrade" module.

data UpgradeException
    = NeitherBinaryOrSourceSpecified
    | ExecutableFailure
    | CommitsNotFound String String
    | StackInPackageIndexNotFound
    | VersionWithNoRevision
    deriving (Int -> UpgradeException -> ShowS
[UpgradeException] -> ShowS
UpgradeException -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UpgradeException] -> ShowS
$cshowList :: [UpgradeException] -> ShowS
show :: UpgradeException -> String
$cshow :: UpgradeException -> String
showsPrec :: Int -> UpgradeException -> ShowS
$cshowsPrec :: Int -> UpgradeException -> ShowS
Show, Typeable)

instance Exception UpgradeException where
    displayException :: UpgradeException -> String
displayException UpgradeException
NeitherBinaryOrSourceSpecified =
        String
"Error: [S-3642]\n"
        forall a. [a] -> [a] -> [a]
++ String
"You must allow either binary or source upgrade paths."
    displayException UpgradeException
ExecutableFailure =
        String
"Error: [S-8716]\n"
        forall a. [a] -> [a] -> [a]
++ String
"Non-success exit code from running newly downloaded executable."
    displayException (CommitsNotFound String
branch String
repo) = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
        [ String
"Error: [S-7114]\n"
        , String
"No commits found for branch "
        , String
branch
        , String
" on repo "
        , String
repo
        ]
    displayException UpgradeException
StackInPackageIndexNotFound =
        String
"Error: [S-9668]\n"
        forall a. [a] -> [a] -> [a]
++ String
"No Stack version found in package indices."
    displayException UpgradeException
VersionWithNoRevision =
        String
"Error: [S-6648]\n"
        forall a. [a] -> [a] -> [a]
++ String
"Latest version with no revision."

upgradeOpts :: Parser UpgradeOpts
upgradeOpts :: Parser UpgradeOpts
upgradeOpts = Maybe BinaryOpts -> Maybe SourceOpts -> UpgradeOpts
UpgradeOpts
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall {a}. Parser (Maybe a)
sourceOnly forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser BinaryOpts
binaryOpts)
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall {a}. Parser (Maybe a)
binaryOnly forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser SourceOpts
sourceOpts)
  where
    binaryOnly :: Parser (Maybe a)
binaryOnly = forall a. a -> Mod FlagFields a -> Parser a
flag' forall a. Maybe a
Nothing (forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"binary-only" forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Do not use a source upgrade path")
    sourceOnly :: Parser (Maybe a)
sourceOnly = forall a. a -> Mod FlagFields a -> Parser a
flag' forall a. Maybe a
Nothing (forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"source-only" forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Do not use a binary upgrade path")

    binaryOpts :: Parser BinaryOpts
binaryOpts = Maybe String
-> Bool
-> Maybe String
-> Maybe String
-> Maybe String
-> BinaryOpts
BinaryOpts
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (forall s. IsString s => Mod OptionFields s -> Parser s
strOption
              ( forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"binary-platform"
             forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Platform type for archive to download"
             forall a. Semigroup a => a -> a -> a
<> forall a (f :: * -> *). Show a => Mod f a
showDefault))
        forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch
         (forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"force-download" forall a. Semigroup a => a -> a -> a
<>
          forall (f :: * -> *) a. String -> Mod f a
help String
"Download the latest available Stack executable")
        forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (forall s. IsString s => Mod OptionFields s -> Parser s
strOption
         (forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"binary-version" forall a. Semigroup a => a -> a -> a
<>
          forall (f :: * -> *) a. String -> Mod f a
help String
"Download a specific Stack version"))
        forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (forall s. IsString s => Mod OptionFields s -> Parser s
strOption
         (forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"github-org" forall a. Semigroup a => a -> a -> a
<>
          forall (f :: * -> *) a. String -> Mod f a
help String
"GitHub organization name"))
        forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (forall s. IsString s => Mod OptionFields s -> Parser s
strOption
         (forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"github-repo" forall a. Semigroup a => a -> a -> a
<>
          forall (f :: * -> *) a. String -> Mod f a
help String
"GitHub repository name"))

    sourceOpts :: Parser SourceOpts
sourceOpts = Maybe (String, String) -> SourceOpts
SourceOpts
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((\Bool
fromGit String
repo String
branch -> if Bool
fromGit then forall a. a -> Maybe a
Just (String
repo, String
branch) else forall a. Maybe a
Nothing)
                forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Mod FlagFields Bool -> Parser Bool
switch
                    ( forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"git"
                    forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Clone from Git instead of downloading from Hackage (more dangerous)" )
                forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s. IsString s => Mod OptionFields s -> Parser s
strOption
                    ( forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"git-repo"
                    forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Clone from specified git repository"
                    forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasValue f => a -> Mod f a
value String
"https://github.com/commercialhaskell/stack"
                    forall a. Semigroup a => a -> a -> a
<> forall a (f :: * -> *). Show a => Mod f a
showDefault )
                forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s. IsString s => Mod OptionFields s -> Parser s
strOption
                    ( forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"git-branch"
                   forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Clone from this git branch"
                   forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasValue f => a -> Mod f a
value String
"master"
                   forall a. Semigroup a => a -> a -> a
<> forall a (f :: * -> *). Show a => Mod f a
showDefault ))

data BinaryOpts = BinaryOpts
    { BinaryOpts -> Maybe String
_boPlatform :: !(Maybe String)
    , BinaryOpts -> Bool
_boForce :: !Bool
    -- ^ force a download, even if the downloaded version is older

    -- than what we are

    , BinaryOpts -> Maybe String
_boVersion :: !(Maybe String)
    -- ^ specific version to download

    , BinaryOpts -> Maybe String
_boGitHubOrg :: !(Maybe String)
    , BinaryOpts -> Maybe String
_boGitHubRepo :: !(Maybe String)
    }
    deriving Int -> BinaryOpts -> ShowS
[BinaryOpts] -> ShowS
BinaryOpts -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BinaryOpts] -> ShowS
$cshowList :: [BinaryOpts] -> ShowS
show :: BinaryOpts -> String
$cshow :: BinaryOpts -> String
showsPrec :: Int -> BinaryOpts -> ShowS
$cshowsPrec :: Int -> BinaryOpts -> ShowS
Show
newtype SourceOpts = SourceOpts (Maybe (String, String)) -- repo and branch

    deriving Int -> SourceOpts -> ShowS
[SourceOpts] -> ShowS
SourceOpts -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SourceOpts] -> ShowS
$cshowList :: [SourceOpts] -> ShowS
show :: SourceOpts -> String
$cshow :: SourceOpts -> String
showsPrec :: Int -> SourceOpts -> ShowS
$cshowsPrec :: Int -> SourceOpts -> ShowS
Show

data UpgradeOpts = UpgradeOpts
    { UpgradeOpts -> Maybe BinaryOpts
_uoBinary :: !(Maybe BinaryOpts)
    , UpgradeOpts -> Maybe SourceOpts
_uoSource :: !(Maybe SourceOpts)
    }
    deriving Int -> UpgradeOpts -> ShowS
[UpgradeOpts] -> ShowS
UpgradeOpts -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UpgradeOpts] -> ShowS
$cshowList :: [UpgradeOpts] -> ShowS
show :: UpgradeOpts -> String
$cshow :: UpgradeOpts -> String
showsPrec :: Int -> UpgradeOpts -> ShowS
$cshowsPrec :: Int -> UpgradeOpts -> ShowS
Show

upgrade :: Maybe String -- ^ git hash at time of building, if known

        -> UpgradeOpts
        -> RIO Runner ()
upgrade :: Maybe String -> UpgradeOpts -> RIO Runner ()
upgrade Maybe String
builtHash (UpgradeOpts Maybe BinaryOpts
mbo Maybe SourceOpts
mso) =
    case (Maybe BinaryOpts
mbo, Maybe SourceOpts
mso) of
        -- FIXME It would be far nicer to capture this case in the

        -- options parser itself so we get better error messages, but

        -- I can't think of a way to make it happen.

        (Maybe BinaryOpts
Nothing, Maybe SourceOpts
Nothing) -> forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO UpgradeException
NeitherBinaryOrSourceSpecified
        (Just BinaryOpts
bo, Maybe SourceOpts
Nothing) -> BinaryOpts -> RIO Runner ()
binary BinaryOpts
bo
        (Maybe BinaryOpts
Nothing, Just SourceOpts
so) -> SourceOpts -> RIO Runner ()
source SourceOpts
so
        -- See #2977 - if --git or --git-repo is specified, do source upgrade.

        (Maybe BinaryOpts
_, Just so :: SourceOpts
so@(SourceOpts (Just (String, String)
_))) -> SourceOpts -> RIO Runner ()
source SourceOpts
so
        (Just BinaryOpts
bo, Just SourceOpts
so) -> BinaryOpts -> RIO Runner ()
binary BinaryOpts
bo forall (m :: * -> *) a.
MonadUnliftIO m =>
m a -> (SomeException -> m a) -> m a
`catchAny` \SomeException
e -> do
            forall env (m :: * -> *).
(HasCallStack, HasTerm env, MonadReader env m, MonadIO m) =>
[StyleDoc] -> m ()
prettyWarnL
               [ String -> StyleDoc
flow String
"Exception occurred when trying to perform binary upgrade:"
               , forall a. IsString a => String -> a
fromString forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ SomeException
e
               , StyleDoc
line forall a. Semigroup a => a -> a -> a
<> String -> StyleDoc
flow String
"Falling back to source upgrade"
               ]

            SourceOpts -> RIO Runner ()
source SourceOpts
so
  where
    binary :: BinaryOpts -> RIO Runner ()
binary BinaryOpts
bo = BinaryOpts -> RIO Runner ()
binaryUpgrade BinaryOpts
bo
    source :: SourceOpts -> RIO Runner ()
source SourceOpts
so = Maybe String -> SourceOpts -> RIO Runner ()
sourceUpgrade Maybe String
builtHash SourceOpts
so

binaryUpgrade :: BinaryOpts -> RIO Runner ()
binaryUpgrade :: BinaryOpts -> RIO Runner ()
binaryUpgrade (BinaryOpts Maybe String
mplatform Bool
force' Maybe String
mver Maybe String
morg Maybe String
mrepo) = forall a. ShouldReexec -> RIO Config a -> RIO Runner a
withConfig ShouldReexec
NoReexec forall a b. (a -> b) -> a -> b
$ do
    [(Bool, String)]
platforms0 <-
      case Maybe String
mplatform of
        Maybe String
Nothing -> forall env (m :: * -> *).
(MonadReader env m, HasPlatform env, MonadThrow m) =>
m [(Bool, String)]
preferredPlatforms
        Just String
p -> forall (f :: * -> *) a. Applicative f => a -> f a
pure [(Text
"windows" Text -> Text -> Bool
`T.isInfixOf` String -> Text
T.pack String
p, String
p)]
    StackReleaseInfo
archiveInfo <- forall env.
(HasPlatform env, HasLogFunc env) =>
Maybe String
-> Maybe String -> Maybe String -> RIO env StackReleaseInfo
downloadStackReleaseInfo Maybe String
morg Maybe String
mrepo Maybe String
mver

    let mdownloadVersion :: Maybe Version
mdownloadVersion = StackReleaseInfo -> Maybe Version
getDownloadVersion StackReleaseInfo
archiveInfo
        force :: Bool
force =
          case Maybe String
mver of
            Maybe String
Nothing -> Bool
force'
            Just String
_ -> Bool
True -- specifying a version implies we're forcing things

    Bool
isNewer <-
        case Maybe Version
mdownloadVersion of
            Maybe Version
Nothing -> do
                forall env (m :: * -> *).
(HasCallStack, HasTerm env, MonadReader env m, MonadIO m) =>
[StyleDoc] -> m ()
prettyErrorL forall a b. (a -> b) -> a -> b
$
                    String -> StyleDoc
flow String
"Unable to determine upstream version from GitHub metadata"
                  forall a. a -> [a] -> [a]
:
                  [ StyleDoc
line forall a. Semigroup a => a -> a -> a
<> String -> StyleDoc
flow String
"Rerun with --force-download to force an upgrade"
                    | Bool -> Bool
not Bool
force]
                forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
            Just Version
downloadVersion -> do
                forall env (m :: * -> *).
(HasCallStack, HasTerm env, MonadReader env m, MonadIO m) =>
[StyleDoc] -> m ()
prettyInfoL
                    [ String -> StyleDoc
flow String
"Current Stack version:"
                    , forall a. IsString a => String -> a
fromString (Version -> String
versionString Version
stackVersion) forall a. Semigroup a => a -> a -> a
<> StyleDoc
","
                    , String -> StyleDoc
flow String
"available download version:"
                    , forall a. IsString a => String -> a
fromString (Version -> String
versionString Version
downloadVersion)
                    ]
                forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Version
downloadVersion forall a. Ord a => a -> a -> Bool
> Version
stackVersion

    Bool
toUpgrade <- case (Bool
force, Bool
isNewer) of
        (Bool
False, Bool
False) -> do
            forall env (m :: * -> *).
(HasCallStack, HasTerm env, MonadReader env m, MonadIO m) =>
String -> m ()
prettyInfoS String
"Skipping binary upgrade, you are already running the most recent version"
            forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
        (Bool
True, Bool
False) -> do
            forall env (m :: * -> *).
(HasCallStack, HasTerm env, MonadReader env m, MonadIO m) =>
String -> m ()
prettyInfoS String
"Forcing binary upgrade"
            forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
        (Bool
_, Bool
True) -> do
            forall env (m :: * -> *).
(HasCallStack, HasTerm env, MonadReader env m, MonadIO m) =>
String -> m ()
prettyInfoS String
"Newer version detected, downloading"
            forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
toUpgrade forall a b. (a -> b) -> a -> b
$ do
        Config
config <- forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall env. HasConfig env => Lens' env Config
configL
        forall env.
HasConfig env =>
[(Bool, String)]
-> StackReleaseInfo
-> Path Abs Dir
-> Bool
-> (Path Abs File -> IO ())
-> RIO env ()
downloadStackExe [(Bool, String)]
platforms0 StackReleaseInfo
archiveInfo (Config -> Path Abs Dir
configLocalBin Config
config) Bool
True forall a b. (a -> b) -> a -> b
$ \Path Abs File
tmpFile -> do
            -- Sanity check!

            ExitCode
ec <- String -> [String] -> IO ExitCode
rawSystem (forall b t. Path b t -> String
toFilePath Path Abs File
tmpFile) [String
"--version"]
            forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ExitCode
ec forall a. Eq a => a -> a -> Bool
== ExitCode
ExitSuccess) (forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO UpgradeException
ExecutableFailure)

sourceUpgrade
  :: Maybe String
  -> SourceOpts
  -> RIO Runner ()
sourceUpgrade :: Maybe String -> SourceOpts -> RIO Runner ()
sourceUpgrade Maybe String
builtHash (SourceOpts Maybe (String, String)
gitRepo) =
  forall (m :: * -> *) a.
MonadUnliftIO m =>
String -> (Path Abs Dir -> m a) -> m a
withSystemTempDir String
"stack-upgrade" forall a b. (a -> b) -> a -> b
$ \Path Abs Dir
tmp -> do
    Maybe (Path Abs Dir)
mdir <- case Maybe (String, String)
gitRepo of
      Just (String
repo, String
branch) -> do
        String
remote <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ String -> [String] -> String -> IO String
System.Process.readProcess String
"git" [String
"ls-remote", String
repo, String
branch] []
        String
latestCommit <-
          case String -> [String]
words String
remote of
            [] -> forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO forall a b. (a -> b) -> a -> b
$ String -> String -> UpgradeException
CommitsNotFound String
branch String
repo
            String
x:[String]
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure String
x
        forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall a. Maybe a -> Bool
isNothing Maybe String
builtHash) forall a b. (a -> b) -> a -> b
$
            forall env (m :: * -> *).
(HasCallStack, HasTerm env, MonadReader env m, MonadIO m) =>
String -> m ()
prettyWarnS forall a b. (a -> b) -> a -> b
$
                       String
"Information about the commit this version of Stack was "
                    forall a. Semigroup a => a -> a -> a
<> String
"built from is not available due to how it was built. "
                    forall a. Semigroup a => a -> a -> a
<> String
"Will continue by assuming an upgrade is needed "
                    forall a. Semigroup a => a -> a -> a
<> String
"because we have no information to the contrary."
        if Maybe String
builtHash forall a. Eq a => a -> a -> Bool
== forall a. a -> Maybe a
Just String
latestCommit
            then do
                forall env (m :: * -> *).
(HasCallStack, HasTerm env, MonadReader env m, MonadIO m) =>
String -> m ()
prettyInfoS String
"Already up-to-date, no upgrade required"
                forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
            else do
                forall env (m :: * -> *).
(HasCallStack, HasTerm env, MonadReader env m, MonadIO m) =>
String -> m ()
prettyInfoS String
"Cloning stack"
                -- NOTE: "--recursive" was added after v1.0.0 (and before the

                -- next release).  This means that we can't use submodules in

                -- the Stack repo until we're comfortable with "stack upgrade

                -- --git" not working for earlier versions.

                let args :: [String]
args = [ String
"clone", String
repo , String
"stack", String
"--depth", String
"1", String
"--recursive", String
"--branch", String
branch]
                forall env (m :: * -> *) a.
(HasProcessContext env, MonadReader env m, MonadIO m) =>
String -> m a -> m a
withWorkingDir (forall b t. Path b t -> String
toFilePath Path Abs Dir
tmp) forall a b. (a -> b) -> a -> b
$ forall env (m :: * -> *) a.
(HasProcessContext env, HasLogFunc env, MonadReader env m,
 MonadIO m, HasCallStack) =>
String -> [String] -> (ProcessConfig () () () -> m a) -> m a
proc String
"git" [String]
args forall (m :: * -> *) stdin stdout stderr.
MonadIO m =>
ProcessConfig stdin stdout stderr -> m ()
runProcess_
                -- On Windows 10, an upstream issue with the `git clone` command

                -- means that command clears, but does not then restore, the

                -- ENABLE_VIRTUAL_TERMINAL_PROCESSING flag for native terminals.

                -- The following hack re-enables the lost ANSI-capability.

                forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
osIsWindows forall a b. (a -> b) -> a -> b
$
                  forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Handle -> IO (Maybe Bool)
hSupportsANSIWithoutEmulation Handle
stdout
                forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Path Abs Dir
tmp forall b t. Path b Dir -> Path Rel t -> Path b t
</> Path Rel Dir
relDirStackProgName
      -- We need to access the Pantry database to find out about the

      -- latest Stack available on Hackage. We first use a standard

      -- Config to do this, and once we have the source load up the

      -- stack.yaml from inside that source.

      Maybe (String, String)
Nothing -> forall a. ShouldReexec -> RIO Config a -> RIO Runner a
withConfig ShouldReexec
NoReexec forall a b. (a -> b) -> a -> b
$ do
        forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall env.
(HasPantryConfig env, HasLogFunc env) =>
Maybe Utf8Builder -> RIO env DidUpdateOccur
updateHackageIndex
             forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just Utf8Builder
"Updating index to make sure we find the latest Stack version"
        Maybe PackageIdentifierRevision
mversion <- forall env.
(HasPantryConfig env, HasLogFunc env) =>
RequireHackageIndex
-> PackageName
-> UsePreferredVersions
-> RIO env (Maybe PackageIdentifierRevision)
getLatestHackageVersion RequireHackageIndex
YesRequireHackageIndex PackageName
"stack" UsePreferredVersions
UsePreferredVersions
        (PackageIdentifierRevision PackageName
_ Version
version CabalFileInfo
_) <-
          case Maybe PackageIdentifierRevision
mversion of
            Maybe PackageIdentifierRevision
Nothing -> forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO UpgradeException
StackInPackageIndexNotFound
            Just PackageIdentifierRevision
version -> forall (f :: * -> *) a. Applicative f => a -> f a
pure PackageIdentifierRevision
version

        if Version
version forall a. Ord a => a -> a -> Bool
<= Version
stackVersion
            then do
                forall env (m :: * -> *).
(HasCallStack, HasTerm env, MonadReader env m, MonadIO m) =>
String -> m ()
prettyInfoS String
"Already at latest version, no upgrade required"
                forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
            else do
                Path Rel Dir
suffix <- forall (m :: * -> *). MonadThrow m => String -> m (Path Rel Dir)
parseRelDir forall a b. (a -> b) -> a -> b
$ String
"stack-" forall a. [a] -> [a] -> [a]
++ Version -> String
versionString Version
version
                let dir :: Path Abs Dir
dir = Path Abs Dir
tmp forall b t. Path b Dir -> Path Rel t -> Path b t
</> Path Rel Dir
suffix
                Maybe (Revision, BlobKey, TreeKey)
mrev <- forall env.
(HasPantryConfig env, HasLogFunc env, HasProcessContext env) =>
RequireHackageIndex
-> PackageName
-> Version
-> RIO env (Maybe (Revision, BlobKey, TreeKey))
getLatestHackageRevision RequireHackageIndex
YesRequireHackageIndex PackageName
"stack" Version
version
                case Maybe (Revision, BlobKey, TreeKey)
mrev of
                  Maybe (Revision, BlobKey, TreeKey)
Nothing -> forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO UpgradeException
VersionWithNoRevision
                  Just (Revision
_rev, BlobKey
cfKey, TreeKey
treeKey) -> do
                    let ident :: PackageIdentifier
ident = PackageName -> Version -> PackageIdentifier
PackageIdentifier PackageName
"stack" Version
version
                    forall env.
(HasPantryConfig env, HasLogFunc env, HasProcessContext env) =>
Path Abs Dir -> PackageLocationImmutable -> RIO env ()
unpackPackageLocation Path Abs Dir
dir forall a b. (a -> b) -> a -> b
$ PackageIdentifier -> BlobKey -> TreeKey -> PackageLocationImmutable
PLIHackage PackageIdentifier
ident BlobKey
cfKey TreeKey
treeKey
                    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just Path Abs Dir
dir

    let modifyGO :: Path Abs Dir -> GlobalOpts -> GlobalOpts
modifyGO Path Abs Dir
dir GlobalOpts
go = GlobalOpts
go
          { globalResolver :: Maybe AbstractResolver
globalResolver = forall a. Maybe a
Nothing -- always use the resolver settings in the stack.yaml file

          , globalStackYaml :: StackYamlLoc
globalStackYaml = Path Abs File -> StackYamlLoc
SYLOverride forall a b. (a -> b) -> a -> b
$ Path Abs Dir
dir forall b t. Path b Dir -> Path Rel t -> Path b t
</> Path Rel File
stackDotYaml
          }
        boptsCLI :: BuildOptsCLI
boptsCLI = BuildOptsCLI
defaultBuildOptsCLI
          { boptsCLITargets :: [Text]
boptsCLITargets = [Text
"stack"]
          }
    forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe (Path Abs Dir)
mdir forall a b. (a -> b) -> a -> b
$ \Path Abs Dir
dir ->
      forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall env. HasRunner env => Lens' env GlobalOpts
globalOptsL (Path Abs Dir -> GlobalOpts -> GlobalOpts
modifyGO Path Abs Dir
dir)) forall a b. (a -> b) -> a -> b
$
      forall a. ShouldReexec -> RIO Config a -> RIO Runner a
withConfig ShouldReexec
NoReexec forall a b. (a -> b) -> a -> b
$ forall a.
NeedTargets -> BuildOptsCLI -> RIO EnvConfig a -> RIO Config a
withEnvConfig NeedTargets
AllowNoTargets BuildOptsCLI
boptsCLI forall a b. (a -> b) -> a -> b
$
      forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (forall s t a b. ASetter s t a b -> b -> s -> t
set (forall s. HasConfig s => Lens' s BuildOpts
buildOptsLforall b c a. (b -> c) -> (a -> b) -> a -> c
.Lens' BuildOpts Bool
buildOptsInstallExesL) Bool
True) forall a b. (a -> b) -> a -> b
$
      forall env.
HasEnvConfig env =>
Maybe (Set (Path Abs File) -> IO ()) -> RIO env ()
build forall a. Maybe a
Nothing