{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections     #-}

module Stack.FileWatch
  ( WatchMode (WatchModePoll)
  , fileWatch
  , fileWatchPoll
  ) where

import           Control.Concurrent.STM ( check )
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import           GHC.IO.Exception
import           Path
import           Stack.Prelude
import           System.FSNotify
import           System.IO ( getLine )

fileWatch
  :: (HasLogFunc env, HasTerm env)
  => ((Set (Path Abs File) -> IO ()) -> RIO env ())
  -> RIO env ()
fileWatch :: forall env.
(HasLogFunc env, HasTerm env) =>
((Set (Path Abs File) -> IO ()) -> RIO env ()) -> RIO env ()
fileWatch = forall env.
(HasLogFunc env, HasTerm env) =>
WatchConfig
-> ((Set (Path Abs File) -> IO ()) -> RIO env ()) -> RIO env ()
fileWatchConf WatchConfig
defaultConfig

fileWatchPoll
  :: (HasLogFunc env, HasTerm env)
  => ((Set (Path Abs File) -> IO ()) -> RIO env ())
  -> RIO env ()
fileWatchPoll :: forall env.
(HasLogFunc env, HasTerm env) =>
((Set (Path Abs File) -> IO ()) -> RIO env ()) -> RIO env ()
fileWatchPoll =
  forall env.
(HasLogFunc env, HasTerm env) =>
WatchConfig
-> ((Set (Path Abs File) -> IO ()) -> RIO env ()) -> RIO env ()
fileWatchConf forall a b. (a -> b) -> a -> b
$ WatchConfig
defaultConfig { confWatchMode :: WatchMode
confWatchMode = Int -> WatchMode
WatchModePoll Int
1000000 }

-- | Run an action, watching for file changes

--

-- The action provided takes a callback that is used to set the files to be

-- watched. When any of those files are changed, we rerun the action again.

fileWatchConf
  :: (HasLogFunc env, HasTerm env)
  => WatchConfig
  -> ((Set (Path Abs File) -> IO ()) -> RIO env ())
  -> RIO env ()
fileWatchConf :: forall env.
(HasLogFunc env, HasTerm env) =>
WatchConfig
-> ((Set (Path Abs File) -> IO ()) -> RIO env ()) -> RIO env ()
fileWatchConf WatchConfig
cfg (Set (Path Abs File) -> IO ()) -> RIO env ()
inner = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. RIO env a -> IO a
run -> forall a. WatchConfig -> (WatchManager -> IO a) -> IO a
withManagerConf WatchConfig
cfg forall a b. (a -> b) -> a -> b
$ \WatchManager
manager -> do
    TVar (Set FilePath)
allFiles <- forall (m :: * -> *) a. MonadIO m => a -> m (TVar a)
newTVarIO forall a. Set a
Set.empty
    TVar Bool
dirtyVar <- forall (m :: * -> *) a. MonadIO m => a -> m (TVar a)
newTVarIO Bool
True
    TVar (Map (Path Abs Dir) (IO ()))
watchVar <- forall (m :: * -> *) a. MonadIO m => a -> m (TVar a)
newTVarIO forall k a. Map k a
Map.empty

    let onChange :: Event -> m ()
onChange Event
event = forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically forall a b. (a -> b) -> a -> b
$ do
            Set FilePath
files <- forall a. TVar a -> STM a
readTVar TVar (Set FilePath)
allFiles
            forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Event -> FilePath
eventPath Event
event forall a. Ord a => a -> Set a -> Bool
`Set.member` Set FilePath
files) (forall a. TVar a -> a -> STM ()
writeTVar TVar Bool
dirtyVar Bool
True)

        setWatched :: Set (Path Abs File) -> IO ()
        setWatched :: Set (Path Abs File) -> IO ()
setWatched Set (Path Abs File)
files = do
            forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically forall a b. (a -> b) -> a -> b
$ forall a. TVar a -> a -> STM ()
writeTVar TVar (Set FilePath)
allFiles forall a b. (a -> b) -> a -> b
$ forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map forall b t. Path b t -> FilePath
toFilePath Set (Path Abs File)
files
            Map (Path Abs Dir) (IO ())
watch0 <- forall (m :: * -> *) a. MonadIO m => TVar a -> m a
readTVarIO TVar (Map (Path Abs Dir) (IO ()))
watchVar
            let actions :: Map (Path Abs Dir) (IO (Maybe (IO ())))
actions = forall k a b c.
Ord k =>
(k -> a -> b -> Maybe c)
-> (Map k a -> Map k c)
-> (Map k b -> Map k c)
-> Map k a
-> Map k b
-> Map k c
Map.mergeWithKey
                    forall {f :: * -> *} {p} {a}.
Applicative f =>
p -> a -> () -> Maybe (f (Maybe a))
keepListening
                    forall {k} {a}. Map k (IO ()) -> Map k (IO (Maybe a))
stopListening
                    forall {b} {t}.
Map (Path b t) () -> Map (Path b t) (IO (Maybe (IO ())))
startListening
                    Map (Path Abs Dir) (IO ())
watch0
                    Map (Path Abs Dir) ()
newDirs
            [Map (Path Abs Dir) (IO ())]
watch1 <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM (forall k a. Map k a -> [(k, a)]
Map.toList Map (Path Abs Dir) (IO (Maybe (IO ())))
actions) forall a b. (a -> b) -> a -> b
$ \(Path Abs Dir
k, IO (Maybe (IO ()))
mmv) -> do
                Maybe (IO ())
mv <- IO (Maybe (IO ()))
mmv
                forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$
                    case Maybe (IO ())
mv of
                        Maybe (IO ())
Nothing -> forall k a. Map k a
Map.empty
                        Just IO ()
v -> forall k a. k -> a -> Map k a
Map.singleton Path Abs Dir
k IO ()
v
            forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically forall a b. (a -> b) -> a -> b
$ forall a. TVar a -> a -> STM ()
writeTVar TVar (Map (Path Abs Dir) (IO ()))
watchVar forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) k a.
(Foldable f, Ord k) =>
f (Map k a) -> Map k a
Map.unions [Map (Path Abs Dir) (IO ())]
watch1
          where
            newDirs :: Map (Path Abs Dir) ()
newDirs = forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (, ())
                    forall a b. (a -> b) -> a -> b
$ forall a. Set a -> [a]
Set.toList
                    forall a b. (a -> b) -> a -> b
$ forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map forall b t. Path b t -> Path b Dir
parent Set (Path Abs File)
files

            keepListening :: p -> a -> () -> Maybe (f (Maybe a))
keepListening p
_dir a
listen () = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just a
listen
            stopListening :: Map k (IO ()) -> Map k (IO (Maybe a))
stopListening = forall a b k. (a -> b) -> Map k a -> Map k b
Map.map forall a b. (a -> b) -> a -> b
$ \IO ()
f -> do
                () <- IO ()
f forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
`catch` \IOException
ioe ->
                    -- Ignore invalid argument error - it can happen if

                    -- the directory is removed.

                    case IOException -> IOErrorType
ioe_type IOException
ioe of
                        IOErrorType
InvalidArgument -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
                        IOErrorType
_ -> forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO IOException
ioe
                forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
            startListening :: Map (Path b t) () -> Map (Path b t) (IO (Maybe (IO ())))
startListening = forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey forall a b. (a -> b) -> a -> b
$ \Path b t
dir () -> do
                let dir' :: FilePath
dir' = forall a. IsString a => FilePath -> a
fromString forall a b. (a -> b) -> a -> b
$ forall b t. Path b t -> FilePath
toFilePath Path b t
dir
                IO ()
listen <- WatchManager -> FilePath -> ActionPredicate -> Action -> IO (IO ())
watchDir WatchManager
manager FilePath
dir' (forall a b. a -> b -> a
const Bool
True) forall {m :: * -> *}. MonadIO m => Event -> m ()
onChange
                forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just IO ()
listen

    let watchInput :: IO ()
watchInput = do
            FilePath
l <- IO FilePath
getLine
            forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (FilePath
l forall a. Eq a => a -> a -> Bool
== FilePath
"quit") forall a b. (a -> b) -> a -> b
$ do
                forall a. RIO env a -> IO a
run forall a b. (a -> b) -> a -> b
$ case FilePath
l of
                    FilePath
"help" -> do
                        forall (m :: * -> *) env.
(MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) =>
Utf8Builder -> m ()
logInfo Utf8Builder
""
                        forall (m :: * -> *) env.
(MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) =>
Utf8Builder -> m ()
logInfo Utf8Builder
"help: display this help"
                        forall (m :: * -> *) env.
(MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) =>
Utf8Builder -> m ()
logInfo Utf8Builder
"quit: exit"
                        forall (m :: * -> *) env.
(MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) =>
Utf8Builder -> m ()
logInfo Utf8Builder
"build: force a rebuild"
                        forall (m :: * -> *) env.
(MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) =>
Utf8Builder -> m ()
logInfo Utf8Builder
"watched: display watched files"
                    FilePath
"build" -> forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically forall a b. (a -> b) -> a -> b
$ forall a. TVar a -> a -> STM ()
writeTVar TVar Bool
dirtyVar Bool
True
                    FilePath
"watched" -> do
                        Set FilePath
watch <- forall (m :: * -> *) a. MonadIO m => TVar a -> m a
readTVarIO TVar (Set FilePath)
allFiles
                        forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (forall (m :: * -> *) env.
(MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) =>
Utf8Builder -> m ()
logInfo forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsString a => FilePath -> a
fromString) (forall a. Set a -> [a]
Set.toList Set FilePath
watch)
                    FilePath
"" -> forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically forall a b. (a -> b) -> a -> b
$ forall a. TVar a -> a -> STM ()
writeTVar TVar Bool
dirtyVar Bool
True
                    FilePath
_ -> forall (m :: * -> *) env.
(MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) =>
Utf8Builder -> m ()
logInfo forall a b. (a -> b) -> a -> b
$
                        Utf8Builder
"Unknown command: " forall a. Semigroup a => a -> a -> a
<>
                        forall a. Show a => a -> Utf8Builder
displayShow FilePath
l forall a. Semigroup a => a -> a -> a
<>
                        Utf8Builder
". Try 'help'"

                IO ()
watchInput

    forall (m :: * -> *) a b. MonadUnliftIO m => m a -> m b -> m ()
race_ IO ()
watchInput forall a b. (a -> b) -> a -> b
$ forall a. RIO env a -> IO a
run forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Applicative f => f a -> f b
forever forall a b. (a -> b) -> a -> b
$ do
        forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically forall a b. (a -> b) -> a -> b
$ do
            Bool
dirty <- forall a. TVar a -> STM a
readTVar TVar Bool
dirtyVar
            Bool -> STM ()
check Bool
dirty

        Either SomeException ()
eres <- forall (m :: * -> *) a.
MonadUnliftIO m =>
m a -> m (Either SomeException a)
tryAny forall a b. (a -> b) -> a -> b
$ (Set (Path Abs File) -> IO ()) -> RIO env ()
inner Set (Path Abs File) -> IO ()
setWatched

        -- Clear dirtiness flag after the build to avoid an infinite

        -- loop caused by the build itself triggering dirtiness. This

        -- could be viewed as a bug, since files changed during the

        -- build will not trigger an extra rebuild, but overall seems

        -- like better behavior. See

        -- https://github.com/commercialhaskell/stack/issues/822

        forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically forall a b. (a -> b) -> a -> b
$ forall a. TVar a -> a -> STM ()
writeTVar TVar Bool
dirtyVar Bool
False

        forall env (m :: * -> *).
(HasCallStack, HasTerm env, MonadReader env m, MonadIO m) =>
StyleDoc -> m ()
prettyInfo forall a b. (a -> b) -> a -> b
$
          case Either SomeException ()
eres of
            Left SomeException
e ->
                let theStyle :: Style
theStyle = case forall e. Exception e => SomeException -> Maybe e
fromException SomeException
e of
                        Just ExitCode
ExitSuccess -> Style
Good
                        Maybe ExitCode
_ -> Style
Error
                 in Style -> StyleDoc -> StyleDoc
style Style
theStyle forall a b. (a -> b) -> a -> b
$ forall a. IsString a => FilePath -> a
fromString forall a b. (a -> b) -> a -> b
$ forall e. Exception e => e -> FilePath
displayException SomeException
e
            Either SomeException ()
_ -> Style -> StyleDoc -> StyleDoc
style Style
Good StyleDoc
"Success! Waiting for next file change."

        forall (m :: * -> *) env.
(MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) =>
Utf8Builder -> m ()
logInfo Utf8Builder
"Type help for available commands. Press enter to force a rebuild."