Skip to content

Commit

Permalink
init: use global --resolver option
Browse files Browse the repository at this point in the history
stack init now uses the global --resolver option instead of its own
implementation of the same.

This changes the CLI behavior: you will have to use
`stack --resolver lts-4.1 init` instead of `stack init --resolver lts-4.1`

Fixes commercialhaskell#1588
  • Loading branch information
harendra-kumar committed Jan 19, 2016
1 parent 5d7b66d commit f569a16
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 26 deletions.
22 changes: 8 additions & 14 deletions src/Stack/Init.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
module Stack.Init
( initProject
, InitOpts (..)
, Method (..)
) where

import Control.Exception (assert)
Expand Down Expand Up @@ -52,8 +51,9 @@ initProject
, HasTerminal env)
=> Path Abs Dir
-> InitOpts
-> Maybe AbstractResolver
-> m ()
initProject currDir initOpts = do
initProject currDir initOpts mresolver = do
let dest = currDir </> stackDotYaml
dest' = toFilePath dest

Expand All @@ -71,7 +71,8 @@ initProject currDir initOpts = do
cabalfps <- findCabalFiles (includeSubDirs initOpts) currDir
(bundle, dupPkgs) <- cabalPackagesCheck cabalfps noPkgMsg Nothing

(r, flags, extraDeps, rbundle) <- getDefaultResolver dest initOpts bundle
(r, flags, extraDeps, rbundle) <- getDefaultResolver dest initOpts
mresolver bundle

let ignored = Map.difference bundle rbundle
dupPkgMsg
Expand Down Expand Up @@ -261,6 +262,7 @@ getDefaultResolver
, HasTerminal env)
=> Path Abs File -- ^ stack.yaml
-> InitOpts
-> Maybe AbstractResolver
-> Map PackageName (Path Abs File, C.GenericPackageDescription)
-- ^ Src package name: cabal dir, cabal package description
-> m ( Resolver
Expand All @@ -271,14 +273,11 @@ getDefaultResolver
-- , Flags for src packages and extra deps
-- , Extra dependencies
-- , Src packages actually considered)
getDefaultResolver stackYaml initOpts bundle =
getResolver (ioMethod initOpts)
getDefaultResolver stackYaml initOpts mresolver bundle =
maybe selectSnapResolver makeConcreteResolver mresolver
>>= getWorkingResolverPlan stackYaml initOpts bundle
where
-- TODO support selecting best across regular and custom snapshots
getResolver (MethodAutoSelect) = selectSnapResolver
getResolver (MethodResolver aresolver) = makeConcreteResolver aresolver

selectSnapResolver = do
let gpds = Map.elems (fmap snd bundle)
snaps <- getSnapshots' >>= getRecommendedSnapshots
Expand Down Expand Up @@ -430,17 +429,12 @@ getRecommendedSnapshots snapshots = do
]

data InitOpts = InitOpts
{ ioMethod :: !Method
{ useSolver :: Bool
-- ^ Use solver
, useSolver :: Bool
-- ^ Preferred snapshots
, omitPackages :: Bool
-- ^ Exclude conflicting or incompatible user packages
, forceOverwrite :: Bool
-- ^ Overwrite existing files
, includeSubDirs :: Bool
-- ^ If True, include all .cabal files found in any sub directories
}

-- | Method of initializing
data Method = MethodAutoSelect | MethodResolver AbstractResolver
10 changes: 1 addition & 9 deletions src/Stack/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ globalOptsFromMonoid defaultTerminal GlobalOptsMonoid{..} = GlobalOpts

initOptsParser :: Parser InitOpts
initOptsParser =
InitOpts <$> method <*> solver <*> omitPackages
InitOpts <$> solver <*> omitPackages
<*> overwrite <*> fmap not ignoreSubDirs
where
ignoreSubDirs = switch (long "ignore-subdirs" <>
Expand All @@ -682,14 +682,6 @@ initOptsParser =
solver = switch (long "solver" <>
help "Use a dependency solver to determine extra dependencies")

method = (MethodResolver <$> resolver)
<|> (pure MethodAutoSelect)

resolver = option readAbstractResolver
(long "resolver" <>
metavar "RESOLVER" <>
help "Use the specified resolver")

-- | Parser for a logging level.
logLevelOptsParser :: Bool -> Maybe LogLevel -> Parser (Maybe LogLevel)
logLevelOptsParser hide defLogLevel =
Expand Down
5 changes: 2 additions & 3 deletions src/main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1183,15 +1183,14 @@ withMiniConfigAndLock go inner =
initCmd :: InitOpts -> GlobalOpts -> IO ()
initCmd initOpts go = do
pwd <- getWorkingDir
withMiniConfigAndLock go (initProject pwd initOpts)
withMiniConfigAndLock go (initProject pwd initOpts (globalResolver go))

-- | Create a project directory structure and initialize the stack config.
newCmd :: (NewOpts,InitOpts) -> GlobalOpts -> IO ()
newCmd (newOpts,initOpts) go@GlobalOpts{..} = do
withMiniConfigAndLock go $ do
dir <- new newOpts
initProject dir initOpts

initProject dir initOpts globalResolver

-- | List the available templates.
templatesCmd :: () -> GlobalOpts -> IO ()
Expand Down

3 comments on commit f569a16

@mgsloan
Copy link

Choose a reason for hiding this comment

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

Actually, happily this does not change CLI behavior. The order thing was fixed a long while back - commercialhaskell#519

@harendra-kumar
Copy link
Owner Author

Choose a reason for hiding this comment

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

Actually it did change the behavior and I fixed it via da82ce9 . There was a special case for stack init for fixing another problem due to conflict between global and local args.

@mgsloan
Copy link

Choose a reason for hiding this comment

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

Ah gotcha, nice!

Please sign in to comment.