module SHC.Stack
where
import Data.Version
#if __GLASGOW_HASKELL__ < 710
import Control.Applicative ((<$>), (<*>))
#endif
import Control.Monad (forM, guard)
import System.Directory (makeRelativeToCurrentDirectory)
import System.FilePath ((</>), equalFilePath)
import qualified Data.ByteString.Char8 as BS8
import qualified Data.Yaml as Y
import SHC.Types
import SHC.Utils
stack :: [String] -> IO String
stack = readP "stack"
checkStackVersion :: IO Bool
checkStackVersion = do
let lowerBound = Version [0,1,7,0] []
stackVersion <- stack ["--numeric-version"]
return $ verifyVersion stackVersion lowerBound
getHpcDir :: String -> IO FilePath
getHpcDir package = (</> package) <$> stack ["path", "--local-hpc-root"]
getBaseMixDir :: IO FilePath
getBaseMixDir = (</> "hpc") <$> stack ["path", "--dist-dir"]
getStackQuery :: IO StackQuery
getStackQuery = (Y.decodeEither' . BS8.pack <$> stack ["query"]) >>= either err return
where err = fail . (++) "getStackQuery: Couldn't decode the result of 'stack query' as YAML: " . show
getProjectKey :: String -> IO String
getProjectKey pkgName = stack ["exec", "--", "ghc-pkg", "field", pkgName, "key", "--simple-output"]
getStackProjects :: IO [StackProject]
getStackProjects = do
sq <- getStackQuery
baseMixDir <- getBaseMixDir
forM (stackQueryLocals sq) $ \(pkgName, filepath) -> do
relfp <- makeRelativeToCurrentDirectory filepath
let mpath = guard (not $ relfp `equalFilePath` ".") >> Just relfp
key <- getProjectKey pkgName
return
StackProject
{ stackProjectName = pkgName
, stackProjectPath = mpath
, stackProjectKey = key
, stackProjectMixDir = maybe id (</>) mpath baseMixDir
}