2017-03-06 23:19:57 +00:00
|
|
|
-- ghc-mod: Happy Haskell Hacking
|
2015-03-03 19:28:34 +00:00
|
|
|
-- 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/>.
|
2015-03-03 20:12:43 +00:00
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
2014-05-03 14:08:28 +00:00
|
|
|
module Language.Haskell.GhcMod.Monad (
|
2015-09-01 08:27:12 +00:00
|
|
|
runGmOutT
|
2015-09-14 07:42:45 +00:00
|
|
|
, runGmOutT'
|
2015-09-01 08:27:12 +00:00
|
|
|
, runGhcModT
|
2014-07-17 05:30:42 +00:00
|
|
|
, runGhcModT'
|
2014-08-28 09:54:01 +00:00
|
|
|
, hoistGhcModT
|
2015-03-09 21:04:04 +00:00
|
|
|
, runGmlT
|
|
|
|
, runGmlT'
|
|
|
|
, runGmlTWith
|
2015-03-03 20:12:43 +00:00
|
|
|
, runGmPkgGhc
|
|
|
|
, withGhcModEnv
|
2015-09-14 03:59:01 +00:00
|
|
|
, withGhcModEnv'
|
2015-03-03 20:12:43 +00:00
|
|
|
, module Language.Haskell.GhcMod.Monad.Types
|
2014-07-17 05:30:42 +00:00
|
|
|
) where
|
2014-05-03 14:08:28 +00:00
|
|
|
|
2014-07-03 05:22:43 +00:00
|
|
|
import Language.Haskell.GhcMod.Types
|
2015-02-07 22:55:57 +00:00
|
|
|
import Language.Haskell.GhcMod.Monad.Types
|
2014-08-28 09:54:01 +00:00
|
|
|
import Language.Haskell.GhcMod.Error
|
2015-03-03 20:12:43 +00:00
|
|
|
import Language.Haskell.GhcMod.Logging
|
2014-07-12 01:30:06 +00:00
|
|
|
import Language.Haskell.GhcMod.Cradle
|
2015-03-03 20:12:43 +00:00
|
|
|
import Language.Haskell.GhcMod.Target
|
2015-08-13 07:01:58 +00:00
|
|
|
import Language.Haskell.GhcMod.Output
|
2014-07-03 05:26:39 +00:00
|
|
|
|
2014-07-22 17:45:48 +00:00
|
|
|
import Control.Arrow (first)
|
2015-03-03 20:12:43 +00:00
|
|
|
import Control.Applicative
|
2014-07-11 02:51:11 +00:00
|
|
|
|
2015-08-13 04:47:12 +00:00
|
|
|
import Control.Concurrent
|
|
|
|
|
2015-02-07 22:55:57 +00:00
|
|
|
import Control.Monad.Reader (runReaderT)
|
|
|
|
import Control.Monad.State.Strict (runStateT)
|
|
|
|
import Control.Monad.Trans.Journal (runJournalT)
|
2014-05-03 14:08:28 +00:00
|
|
|
|
2015-09-14 07:44:16 +00:00
|
|
|
import Exception
|
2015-01-16 14:47:56 +00:00
|
|
|
|
2015-03-03 20:12:43 +00:00
|
|
|
import System.Directory
|
2016-01-04 18:05:15 +00:00
|
|
|
import System.IO.Unsafe
|
2015-08-03 01:09:56 +00:00
|
|
|
import Prelude
|
2015-01-16 14:47:56 +00:00
|
|
|
|
2016-05-14 18:18:06 +00:00
|
|
|
withGhcModEnv :: (IOish m, GmOut m) => FilePath -> Options -> ((GhcModEnv, GhcModLog) -> m a) -> m a
|
|
|
|
withGhcModEnv dir opts f = withGhcModEnv' withCradle dir opts f
|
2015-09-14 03:59:01 +00:00
|
|
|
where
|
2016-05-14 18:18:06 +00:00
|
|
|
withCradle dir' =
|
|
|
|
gbracket
|
|
|
|
(runJournalT $ do
|
|
|
|
gmSetLogLevel $ ooptLogLevel $ optOutput opts
|
|
|
|
findCradle' (optPrograms opts) dir')
|
|
|
|
(liftIO . cleanupCradle . fst)
|
|
|
|
|
|
|
|
|
2016-01-04 18:05:15 +00:00
|
|
|
|
|
|
|
cwdLock :: MVar ThreadId
|
|
|
|
cwdLock = unsafePerformIO $ newEmptyMVar
|
|
|
|
{-# NOINLINE cwdLock #-}
|
|
|
|
|
2015-10-30 18:05:41 +00:00
|
|
|
withGhcModEnv' :: (IOish m, GmOut m) => (FilePath -> ((Cradle, GhcModLog) -> m a) -> m a) -> FilePath -> Options -> ((GhcModEnv, GhcModLog) -> m a) -> m a
|
2015-09-14 03:59:01 +00:00
|
|
|
withGhcModEnv' withCradle dir opts f =
|
2015-10-30 18:05:41 +00:00
|
|
|
withCradle dir $ \(crdl,lg) ->
|
2015-09-14 07:42:45 +00:00
|
|
|
withCradleRootDir crdl $
|
2015-10-30 18:05:41 +00:00
|
|
|
f (GhcModEnv opts crdl, lg)
|
2015-03-03 20:12:43 +00:00
|
|
|
where
|
2016-01-04 18:05:15 +00:00
|
|
|
swapCurrentDirectory ndir = do
|
|
|
|
odir <- canonicalizePath =<< getCurrentDirectory
|
|
|
|
setCurrentDirectory ndir
|
|
|
|
return odir
|
|
|
|
|
2015-09-14 07:42:45 +00:00
|
|
|
withCradleRootDir (cradleRootDir -> projdir) a = do
|
2016-01-04 18:05:15 +00:00
|
|
|
success <- liftIO $ tryPutMVar cwdLock =<< myThreadId
|
|
|
|
if not success
|
|
|
|
then error "withGhcModEnv': using ghc-mod from multiple threads is not supported!"
|
|
|
|
else gbracket setup teardown (const a)
|
|
|
|
where
|
|
|
|
setup = liftIO $ swapCurrentDirectory projdir
|
2015-09-14 07:42:45 +00:00
|
|
|
|
2016-01-04 18:05:15 +00:00
|
|
|
teardown odir = liftIO $ do
|
|
|
|
setCurrentDirectory odir
|
|
|
|
void $ takeMVar cwdLock
|
2015-09-14 07:42:45 +00:00
|
|
|
|
|
|
|
runGmOutT :: IOish m => Options -> GmOutT m a -> m a
|
|
|
|
runGmOutT opts ma = do
|
2015-09-16 03:09:35 +00:00
|
|
|
gmo@GhcModOut{..} <- GhcModOut (optOutput opts) <$> liftIO newChan
|
|
|
|
let action = runGmOutT' gmo ma
|
|
|
|
case ooptLinePrefix $ optOutput opts of
|
|
|
|
Nothing -> action
|
|
|
|
Just pfxs ->
|
|
|
|
gbracket_ (liftIO $ forkIO $ stdoutGateway pfxs gmoChan)
|
|
|
|
(const $ liftIO $ flushStdoutGateway gmoChan)
|
|
|
|
action
|
2015-09-14 07:42:45 +00:00
|
|
|
|
2016-05-22 00:53:51 +00:00
|
|
|
runGmOutT' :: GhcModOut -> GmOutT m a -> m a
|
2015-09-16 03:09:35 +00:00
|
|
|
runGmOutT' gmo ma = flip runReaderT gmo $ unGmOutT ma
|
2014-10-14 17:52:58 +00:00
|
|
|
|
2014-07-22 17:45:48 +00:00
|
|
|
-- | Run a @GhcModT m@ computation.
|
2015-10-26 22:27:03 +00:00
|
|
|
runGhcModT :: IOish m
|
2014-07-22 17:45:48 +00:00
|
|
|
=> Options
|
|
|
|
-> GhcModT m a
|
|
|
|
-> m (Either GhcModError a, GhcModLog)
|
2015-09-14 07:42:45 +00:00
|
|
|
runGhcModT opt action = liftIO (getCurrentDirectory >>= canonicalizePath) >>= \dir' -> do
|
|
|
|
runGmOutT opt $
|
2015-10-30 18:05:41 +00:00
|
|
|
withGhcModEnv dir' opt $ \(env,lg) ->
|
2016-01-09 22:22:27 +00:00
|
|
|
first (fst <$>) <$> runGhcModT' env defaultGhcModState (do
|
|
|
|
gmSetLogLevel (ooptLogLevel $ optOutput opt)
|
|
|
|
gmAppendLogQuiet lg
|
|
|
|
action)
|
2014-12-17 16:52:50 +00:00
|
|
|
|
2014-08-28 09:54:01 +00:00
|
|
|
-- | @hoistGhcModT result@. Embed a GhcModT computation's result into a GhcModT
|
|
|
|
-- computation. Note that if the computation that returned @result@ modified the
|
|
|
|
-- state part of GhcModT this cannot be restored.
|
|
|
|
hoistGhcModT :: IOish m
|
|
|
|
=> (Either GhcModError a, GhcModLog)
|
|
|
|
-> GhcModT m a
|
|
|
|
hoistGhcModT (r,l) = do
|
2015-03-03 20:12:43 +00:00
|
|
|
gmlJournal l >> case r of
|
2014-08-28 09:54:01 +00:00
|
|
|
Left e -> throwError e
|
|
|
|
Right a -> return a
|
|
|
|
|
2015-09-01 08:27:12 +00:00
|
|
|
|
2014-07-14 22:51:22 +00:00
|
|
|
-- | Run a computation inside @GhcModT@ providing the RWST environment and
|
|
|
|
-- initial state. This is a low level function, use it only if you know what to
|
|
|
|
-- do with 'GhcModEnv' and 'GhcModState'.
|
|
|
|
--
|
|
|
|
-- You should probably look at 'runGhcModT' instead.
|
2015-09-14 07:42:45 +00:00
|
|
|
runGhcModT' :: IOish m
|
2015-03-03 20:12:43 +00:00
|
|
|
=> GhcModEnv
|
|
|
|
-> GhcModState
|
|
|
|
-> GhcModT m a
|
2015-09-01 08:27:12 +00:00
|
|
|
-> GmOutT m (Either GhcModError (a, GhcModState), GhcModLog)
|
2015-09-14 07:42:45 +00:00
|
|
|
runGhcModT' r s a = do
|
2015-09-01 08:27:12 +00:00
|
|
|
flip runReaderT r $ runJournalT $ runErrorT $ runStateT (unGmT a) s
|
|
|
|
|
2015-09-14 07:42:45 +00:00
|
|
|
gbracket_ :: ExceptionMonad m => m a -> (a -> m b) -> m c -> m c
|
|
|
|
gbracket_ ma mb mc = gbracket ma mb (const mc)
|