2015-09-15 03:25:00 +00:00
|
|
|
-- ghc-mod: Making Haskell development *more* fun
|
|
|
|
-- Copyright (C) 2015 Daniel Gröber <dxld ÄT darkboxed DOT org>
|
|
|
|
--
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU Affero General Public License as published by
|
|
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
--
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU Affero General Public License for more details.
|
|
|
|
--
|
|
|
|
-- You should have received a copy of the GNU Affero General Public License
|
|
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
module Language.Haskell.GhcMod.Stack where
|
|
|
|
|
2016-01-13 03:49:38 +00:00
|
|
|
import Safe
|
2015-09-15 03:25:00 +00:00
|
|
|
import Control.Applicative
|
|
|
|
import Control.Exception as E
|
|
|
|
import Control.Monad
|
|
|
|
import Control.Monad.Trans.Maybe
|
|
|
|
import Control.Monad.Trans.Class
|
|
|
|
import Data.List
|
|
|
|
import Data.List.Split
|
|
|
|
import Data.Maybe
|
|
|
|
import System.Directory
|
|
|
|
import System.FilePath
|
|
|
|
import System.Info.Extra
|
|
|
|
import Exception
|
|
|
|
|
|
|
|
import Language.Haskell.GhcMod.Types
|
|
|
|
import Language.Haskell.GhcMod.Monad.Types
|
|
|
|
import Language.Haskell.GhcMod.Output
|
2016-01-09 22:21:59 +00:00
|
|
|
import Language.Haskell.GhcMod.Logging
|
|
|
|
import Language.Haskell.GhcMod.Error
|
2015-09-15 03:25:00 +00:00
|
|
|
import qualified Language.Haskell.GhcMod.Utils as U
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
patchStackPrograms :: (IOish m, GmOut m) => Cradle -> Programs -> m Programs
|
|
|
|
patchStackPrograms Cradle { cradleProject = (StackProject senv) } progs = do
|
|
|
|
Just ghc <- getStackGhcPath senv
|
|
|
|
Just ghcPkg <- getStackGhcPkgPath senv
|
|
|
|
return $ progs {
|
|
|
|
ghcProgram = ghc
|
|
|
|
, ghcPkgProgram = ghcPkg
|
|
|
|
}
|
|
|
|
patchStackPrograms _crdl progs = return progs
|
|
|
|
|
2016-08-27 12:05:44 +00:00
|
|
|
getStackEnv :: (IOish m, GmOut m, GmLog m)
|
|
|
|
=> FilePath -> FilePath -> m (Maybe StackEnv)
|
|
|
|
getStackEnv projdir stackProg = U.withDirectory_ projdir $ runMaybeT $ do
|
|
|
|
env <- map (liToTup . splitOn ": ") . lines <$> readStack stackProg ["path"]
|
2016-01-13 03:49:38 +00:00
|
|
|
let look k = fromJustNote "getStackEnv" $ lookup k env
|
2015-09-15 03:25:00 +00:00
|
|
|
return StackEnv {
|
|
|
|
seDistDir = look "dist-dir"
|
|
|
|
, seBinPath = splitSearchPath $ look "bin-path"
|
|
|
|
, seSnapshotPkgDb = look "snapshot-pkg-db"
|
|
|
|
, seLocalPkgDb = look "local-pkg-db"
|
|
|
|
}
|
|
|
|
where
|
|
|
|
liToTup [k,v] = (k,v)
|
2015-09-16 03:18:53 +00:00
|
|
|
liToTup [k] = (k, error "getStackEnv: missing key '"++k++"'")
|
2015-09-15 03:25:00 +00:00
|
|
|
liToTup _ = error "getStackEnv"
|
|
|
|
|
|
|
|
getStackGhcPath :: IOish m => StackEnv -> m (Maybe FilePath)
|
|
|
|
getStackGhcPath = findExecutablesInStackBinPath "ghc"
|
|
|
|
|
|
|
|
getStackGhcPkgPath :: IOish m => StackEnv -> m (Maybe FilePath)
|
|
|
|
getStackGhcPkgPath = findExecutablesInStackBinPath "ghc-pkg"
|
|
|
|
|
|
|
|
findExecutablesInStackBinPath :: IOish m => String -> StackEnv -> m (Maybe FilePath)
|
|
|
|
findExecutablesInStackBinPath exe StackEnv {..} =
|
|
|
|
liftIO $ listToMaybe <$> findExecutablesInDirectories' seBinPath exe
|
|
|
|
|
|
|
|
findExecutablesInDirectories' :: [FilePath] -> String -> IO [FilePath]
|
|
|
|
findExecutablesInDirectories' path binary =
|
2016-02-14 11:35:57 +00:00
|
|
|
U.findFilesWith' isExecutable path (binary <.> exeExtension')
|
2015-09-15 03:25:00 +00:00
|
|
|
where isExecutable file = do
|
|
|
|
perms <- getPermissions file
|
|
|
|
return $ executable perms
|
|
|
|
|
2016-02-14 11:35:57 +00:00
|
|
|
exeExtension' = if isWindows then "exe" else ""
|
2015-09-15 03:25:00 +00:00
|
|
|
|
2016-08-27 12:05:44 +00:00
|
|
|
readStack :: (IOish m, GmOut m, GmLog m)
|
|
|
|
=> FilePath -> [String] -> MaybeT m String
|
|
|
|
readStack exe args = do
|
|
|
|
stack <- MaybeT $ liftIO $ findExecutable exe
|
2015-09-15 03:25:00 +00:00
|
|
|
readProc <- lift gmReadProcess
|
2016-01-09 22:21:59 +00:00
|
|
|
flip gcatch handler $ do
|
2015-09-15 03:25:00 +00:00
|
|
|
liftIO $ evaluate =<< readProc stack args ""
|
|
|
|
where
|
2016-01-09 22:21:59 +00:00
|
|
|
handler (e :: IOError) = do
|
|
|
|
gmLog GmWarning "readStack" $ gmeDoc $ exToErr e
|
|
|
|
mzero
|
|
|
|
exToErr = GMEStackBootstrap . GMEString . show
|