@@ -15,12 +15,15 @@ import System.Process
1515import System.FilePath
1616import System.Directory
1717import Control.Monad (filterM )
18+ import Control.Exception
1819import Types
1920
2021-- | This module adds support for `stack`, as follows:
2122-- 1. Figure out if the target-file is in a stack project,
22- -- 2. Run `stack exec` to extract `StackConfig`
23- -- 3. The `StackConfig` is used to suitably alter the cabal ConfigFlags in Cabal.hs
23+ -- 2. If `stack` in available in PATH, run `stack exec` to extract
24+ -- `StackConfig`
25+ -- 3. The `StackConfig` is used to suitably alter the cabal ConfigFlags in
26+ -- Cabal.hs
2427
2528
2629-- TODO: Move into Types?
@@ -41,9 +44,9 @@ getStackConfig' p = do
4144 mbYaml <- getStackYaml p
4245 case mbYaml of
4346 Nothing -> return Nothing
44- Just _ -> jStackConfig <$> getStackDist p <*> getStackDbs p
45- where
46- jStackConfig x y = Just ( StackConfig x y)
47+ Just _ -> do mdbs <- getStackDbs p
48+ mdst <- getStackDist p
49+ return $ StackConfig <$> mdst <*> mdbs
4750
4851--------------------------------------------------------------------------------
4952getStackYaml :: FilePath -> IO (Maybe FilePath )
@@ -61,18 +64,21 @@ pathsToRoot p
6164 parent = takeDirectory p
6265
6366--------------------------------------------------------------------------------
64- getStackDist :: FilePath -> IO FilePath
67+ getStackDist :: FilePath -> IO ( Maybe FilePath )
6568--------------------------------------------------------------------------------
66- getStackDist p = trim <$> execInPath cmd p
69+ getStackDist p = ( trim <$> ) <$> execInPath cmd p
6770 where
6871 cmd = " stack path --dist-dir"
6972 -- dir = takeDirectory p
7073 -- splice = (dir </>) . trim
7174
7275--------------------------------------------------------------------------------
73- getStackDbs :: FilePath -> IO [FilePath ]
76+ getStackDbs :: FilePath -> IO ( Maybe [FilePath ])
7477--------------------------------------------------------------------------------
75- getStackDbs p = execInPath cmd p >>= extractDbs
78+ getStackDbs p = do mpp <- execInPath cmd p
79+ case mpp of
80+ Just pp -> Just <$> extractDbs pp
81+ Nothing -> return Nothing
7682 where
7783 cmd = " stack --verbosity quiet exec printenv GHC_PACKAGE_PATH"
7884
@@ -98,7 +104,14 @@ trim = f . f
98104 where
99105 f = reverse . dropWhile isSpace
100106
101- execInPath :: String -> FilePath -> IO String
102- execInPath cmd p = readCreateProcess prc " "
107+ execInPath :: String -> FilePath -> IO (Maybe String )
108+ execInPath cmd p = do
109+ eIOEstr <- (try $ readCreateProcess prc " " :: IO (Either IOError String ))
110+ return $ case eIOEstr of
111+ Right s -> Just s
112+ -- This error is most likely "/bin/sh: stack: command not found"
113+ -- which is caused by the package containing a stack.yaml file but
114+ -- no stack command is in the PATH.
115+ Left _ -> Nothing
103116 where
104117 prc = (shell cmd) { cwd = Just $ takeDirectory p }
0 commit comments