Skip to content

Commit

Permalink
Don't always create a new stack.yaml file
Browse files Browse the repository at this point in the history
Not my intention originally, but this paves the way quite nicely for
implementing #59
  • Loading branch information
snoyberg committed Jun 5, 2015
1 parent c388a9a commit 36a6714
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
13 changes: 10 additions & 3 deletions src/Stack/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,20 @@ loadConfig configArgs = do
loadBuildConfig :: (MonadLogger m,MonadIO m,MonadCatch m,MonadReader env m,HasHttpManager env,MonadBaseControl IO m)
=> Maybe (Project, Path Abs File, ConfigMonoid)
-> Config
-> NoBuildConfigStrategy
-> m BuildConfig
loadBuildConfig mproject config = do
loadBuildConfig mproject config noConfigStrat = do
env <- ask
let miniConfig = MiniConfig (getHttpManager env) config
(project, stackYamlFP) <- case mproject of
Just (project, fp, _) -> return (project, fp)
Nothing -> do
Just (project, fp, _) -> return (project, fp)
Nothing -> case noConfigStrat of
ThrowException -> do
currDir <- getWorkingDir
throwM $ NoProjectConfigFound currDir
ExecStrategy ->
error "You do not have a stack.yaml. This will be handled in the future, see https://github.com/fpco/stack/issues/59"
CreateConfig -> do
currDir <- getWorkingDir
(r, flags, cabalFileExists) <- runReaderT (getDefaultResolver currDir) miniConfig
let dest = currDir </> stackDotYaml
Expand Down
33 changes: 22 additions & 11 deletions src/Stack/Types/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,18 @@ data BuildConfig = BuildConfig
data LoadConfig m = LoadConfig
{ lcConfig :: !Config
-- ^ Top-level Stack configuration.
, lcLoadBuildConfig :: !(m BuildConfig)
-- ^ Action to load the remaining 'BuildConfig'. This action will create a project if one
-- does not already exist.
, lcLoadBuildConfig :: !(NoBuildConfigStrategy -> m BuildConfig)
-- ^ Action to load the remaining 'BuildConfig'.
, lcProjectRoot :: !(Maybe (Path Abs Dir))
-- ^ The project root directory, if in a project.
}

data NoBuildConfigStrategy
= ThrowException
| CreateConfig
| ExecStrategy
deriving (Show, Eq, Ord)

-- | A project is a collection of packages. We can have multiple stack.yaml
-- files, but only one of them may contain project information.
data Project = Project
Expand Down Expand Up @@ -240,10 +245,6 @@ parseResolver t =
where
parseGhc = T.stripPrefix "ghc-" t >>= parseMajorVersionFromString . T.unpack

data ParseResolverException = ParseResolverException Text
deriving (Show, Typeable)
instance Exception ParseResolverException

-- | Class for environment values which have access to the stack root
class HasStackRoot env where
getStackRoot :: env -> Path Abs Dir
Expand Down Expand Up @@ -327,10 +328,20 @@ instance FromJSON ConfigMonoid where
return ConfigMonoid {..}

data ConfigException
= ConfigInvalidYaml String
| ConfigNoFile
| ConfigNoDockerConfig
deriving (Typeable,Show)
= ParseResolverException Text
| NoProjectConfigFound (Path Abs Dir)
deriving Typeable
instance Show ConfigException where
show (ParseResolverException t) = concat
[ "Invalid resolver value: "
, T.unpack t
, ". Possible valid values include lts-2.12, nightly-2015-01-01, and ghc-7.10."
]
show (NoProjectConfigFound dir) = concat
[ "Unable to find a stack.yaml file in the current directory ("
, toFilePath dir
, ") or its ancestors"
]
instance Exception ConfigException

-- | Helper function to ask the environment and apply getConfig
Expand Down
12 changes: 6 additions & 6 deletions src/main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ main =
pathCmd :: PathArg -> GlobalOpts -> IO ()
pathCmd pathArg go@GlobalOpts{..} = do
(manager,lc) <- loadConfigWithOpts go
buildConfig <- runStackLoggingT manager globalLogLevel (lcLoadBuildConfig lc)
buildConfig <- runStackLoggingT manager globalLogLevel (lcLoadBuildConfig lc ExecStrategy)
runStackT manager globalLogLevel buildConfig (pathString pathArg) >>= putStrLn


Expand Down Expand Up @@ -226,7 +226,7 @@ setupCmd SetupCmdOpts{..} go@GlobalOpts{..} = do
case scoGhcVersion of
Just v -> return (v, Nothing)
Nothing -> do
bc <- lcLoadBuildConfig lc
bc <- lcLoadBuildConfig lc ThrowException
return (bcGhcVersion bc, Just $ bcStackYaml bc)
mpaths <- ensureGHC manager (lcConfig lc) SetupOpts
{ soptsInstallIfMissing = True
Expand All @@ -249,7 +249,7 @@ cleanCmd _ go@GlobalOpts{..} = do
(lcProjectRoot lc)
(do config <- runStackLoggingT manager
globalLogLevel
(lcLoadBuildConfig lc >>= setupEnv globalSystemGhc manager)
(lcLoadBuildConfig lc ThrowException >>= setupEnv globalSystemGhc manager)
runStackT manager globalLogLevel config clean)

-- | Install dependencies
Expand All @@ -258,7 +258,7 @@ depsCmd (names, dryRun) go@GlobalOpts{..} = do
(manager,lc) <- loadConfigWithOpts go
Docker.rerunWithOptionalContainer (lcConfig lc) (lcProjectRoot lc) $ do
config <- runStackLoggingT manager globalLogLevel
(lcLoadBuildConfig lc >>= setupEnv globalSystemGhc manager)
(lcLoadBuildConfig lc ExecStrategy >>= setupEnv globalSystemGhc manager)
runStackT manager globalLogLevel config $ Stack.Build.build BuildOpts
{ boptsTargets = Right names
, boptsLibProfile = False
Expand Down Expand Up @@ -309,7 +309,7 @@ buildCmd finalAction opts go@GlobalOpts{..} =
(lcProjectRoot lc)
(do config <- runStackLoggingT manager
globalLogLevel
(lcLoadBuildConfig lc >>= setupEnv globalSystemGhc manager)
(lcLoadBuildConfig lc CreateConfig >>= setupEnv globalSystemGhc manager)
runStackT manager globalLogLevel config $
Stack.Build.build opts { boptsFinalAction = finalAction}))
(error . printBuildException)
Expand Down Expand Up @@ -427,7 +427,7 @@ execCmd (cmd, args) go@GlobalOpts{..} = do
(lcProjectRoot lc)
(do config <- runStackLoggingT manager
globalLogLevel
(lcLoadBuildConfig lc >>= setupEnv globalSystemGhc manager)
(lcLoadBuildConfig lc ExecStrategy >>= setupEnv globalSystemGhc manager)
menv <- configEnvOverride (bcConfig config)
EnvSettings
{ esIncludeLocals = True
Expand Down

0 comments on commit 36a6714

Please sign in to comment.