Skip to content

Commit

Permalink
Merge pull request #6488 from commercialhaskell/getRawSnapshot
Browse files Browse the repository at this point in the history
Eliminate some code duplication
  • Loading branch information
mpilgrem authored Feb 17, 2024
2 parents 12005d6 + 4bba686 commit cc1f6df
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
15 changes: 13 additions & 2 deletions src/Stack/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Stack.Config
, getImplicitGlobalProjectDir
, getSnapshots
, makeConcreteSnapshot
, getRawSnapshot
, checkOwnership
, getInContainer
, getInNixShell
Expand Down Expand Up @@ -56,6 +57,7 @@ import GHC.Conc ( getNumProcessors )
import Network.HTTP.StackClient
( httpJSON, parseUrlThrow, getResponseBody )
import Options.Applicative ( Parser, help, long, metavar, strOption )
import Pantry ( loadSnapshot )
import Path
( PathException (..), (</>), parent, parseAbsDir
, parseAbsFile, parseRelDir, stripProperPrefix
Expand Down Expand Up @@ -247,6 +249,15 @@ makeConcreteSnapshot as = do
]
pure s

-- | Get the raw snapshot from the global options.
getRawSnapshot :: HasConfig env => RIO env (Maybe RawSnapshot)
getRawSnapshot = do
mASnapshot <- view $ globalOptsL . to (.snapshot)
forM mASnapshot $ \aSnapshot -> do
concrete <- makeConcreteSnapshot aSnapshot
loc <- completeSnapshotLocation concrete
loadSnapshot loc

-- | Get the latest snapshot available.
getLatestSnapshot :: HasConfig env => RIO env RawSnapshotLocation
getLatestSnapshot = do
Expand Down Expand Up @@ -670,7 +681,7 @@ loadConfig ::
loadConfig inner = do
mstackYaml <- view $ globalOptsL . to (.stackYaml)
mproject <- loadProjectConfig mstackYaml
mSnapshot <- view $ globalOptsL . to (.snapshot)
mASnapshot <- view $ globalOptsL . to (.snapshot)
configArgs <- view $ globalOptsL . to (.configMonoid)
(configRoot, stackRoot, userOwnsStackRoot) <-
determineStackRootAndOwnership configArgs
Expand All @@ -695,7 +706,7 @@ loadConfig inner = do
configFromConfigMonoid
stackRoot
userConfigPath
mSnapshot
mASnapshot
mproject'
(mconcat $ configArgs : addConfigMonoid extraConfigs)

Expand Down
13 changes: 3 additions & 10 deletions src/Stack/List.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}

-- | Types and functions related to Stack's @list@ command.
Expand All @@ -8,15 +7,13 @@ module Stack.List
, 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 ( makeConcreteSnapshot )
import Stack.Config ( getRawSnapshot )
import Stack.Prelude
import Stack.Runners ( ShouldReexec (..), withConfig )
import Stack.Types.GlobalOpts ( GlobalOpts (..) )
import Stack.Types.Runner ( Runner, globalOptsL )
import Stack.Types.Runner ( Runner )

-- | Type representing exceptions thrown by functions exported by the
-- "Stack.List" module.
Expand All @@ -35,11 +32,7 @@ instance Exception ListPrettyException
-- | Function underlying the @stack list@ command. List packages.
listCmd :: [String] -> RIO Runner ()
listCmd names = withConfig NoReexec $ do
mASnapshot <- view $ globalOptsL . to (.snapshot)
mSnapshot <- forM mASnapshot $ \aSnapshot -> do
concrete <- makeConcreteSnapshot aSnapshot
loc <- completeSnapshotLocation concrete
loadSnapshot loc
mSnapshot <- getRawSnapshot
listPackages mSnapshot names

-- | Intended to work for the command line command.
Expand Down
12 changes: 3 additions & 9 deletions src/Stack/Unpack.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ module Stack.Unpack
import Data.List.Extra ( notNull )
import Path ( SomeBase (..), (</>), parseRelDir )
import Path.IO ( doesDirExist, getCurrentDir )
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 ( makeConcreteSnapshot )
import Stack.Config ( getRawSnapshot )
import Stack.Constants ( relDirRoot )
import Stack.Prelude
import Stack.Runners ( ShouldReexec (..), withConfig )
import Stack.Types.Config ( Config (..), HasConfig, configL )
import Stack.Types.GlobalOpts ( GlobalOpts (..) )
import Stack.Types.Runner ( Runner, globalOptsL )
import Stack.Types.Runner ( Runner )

-- | Type representing \'pretty\' exceptions thrown by functions exported by the
-- "Stack.Unpack" module.
Expand Down Expand Up @@ -94,11 +92,7 @@ unpackCmd (UnpackOpts targets areCandidates Nothing) =
unpackCmd (UnpackOpts targets areCandidates (Just $ Rel relDirRoot))
unpackCmd (UnpackOpts targets areCandidates (Just dstPath)) =
withConfig NoReexec $ do
mASnapshot <- view $ globalOptsL . to (.snapshot)
mSnapshot <- forM mASnapshot $ \aSnapshot -> do
concrete <- makeConcreteSnapshot aSnapshot
loc <- completeSnapshotLocation concrete
loadSnapshot loc
mSnapshot <- getRawSnapshot
dstPath' <- case dstPath of
Abs path -> pure path
Rel path -> do
Expand Down

0 comments on commit cc1f6df

Please sign in to comment.