module HsDev.Watcher (
watchProject, watchModule, watchPackageDb, watchPackageDbStack,
unwatchProject, unwatchModule, unwatchPackageDb,
isSource, isCabal, isConf,
module System.Directory.Watcher,
module HsDev.Watcher.Types
) where
import Control.Lens (view)
import Control.Monad (void)
import System.FilePath (takeDirectory, takeExtension, (</>))
import System.Directory.Watcher hiding (Watcher)
import HsDev.Project
import HsDev.Symbols
import HsDev.Watcher.Types
watchProject :: Watcher -> Project -> [String] -> IO ()
watchProject w proj opts = do
mapM_ (\dir -> watchTree w dir isSource (WatchedProject proj opts)) dirs
watchDir w projDir isCabal (WatchedProject proj opts)
where
dirs = map ((projDir </>) . view entity) $ maybe [] sourceDirs $ view projectDescription proj
projDir = view projectPath proj
watchModule :: Watcher -> ModuleLocation -> IO ()
watchModule w (FileModule f Nothing) = watchDir w (takeDirectory f) isSource WatchedModule
watchModule w (FileModule _ (Just proj)) = watchProject w proj []
watchModule _ _ = return ()
watchPackageDb :: Watcher -> PackageDbStack -> [String] -> IO ()
watchPackageDb w pdbs opts = case topPackageDb pdbs of
GlobalDb -> return ()
UserDb -> return ()
(PackageDb pdb) -> watchTree w pdb isConf (WatchedPackageDb pdbs opts)
watchPackageDbStack :: Watcher -> PackageDbStack -> [String] -> IO ()
watchPackageDbStack w pdbs opts = mapM_ (\pdbs' -> watchPackageDb w pdbs' opts) $ packageDbStacks pdbs
unwatchProject :: Watcher -> Project -> IO ()
unwatchProject w proj = do
mapM_ (unwatchTree w) dirs
void $ unwatchDir w projDir
where
dirs = map ((projDir </>) . view entity) $ maybe [] sourceDirs $ view projectDescription proj
projDir = view projectPath proj
unwatchModule :: Watcher -> ModuleLocation -> IO ()
unwatchModule w (FileModule f Nothing) = void $ unwatchDir w (takeDirectory f)
unwatchModule _ (FileModule _ (Just _)) = return ()
unwatchModule _ (InstalledModule _ _ _) = return ()
unwatchModule _ _ = return ()
unwatchPackageDb :: Watcher -> PackageDb -> IO ()
unwatchPackageDb _ GlobalDb = return ()
unwatchPackageDb _ UserDb = return ()
unwatchPackageDb w (PackageDb pdb) = void $ unwatchTree w pdb
isSource :: Event -> Bool
isSource (Event _ f _) = takeExtension f == ".hs"
isCabal :: Event -> Bool
isCabal (Event _ f _) = takeExtension f == ".cabal"
isConf :: Event -> Bool
isConf (Event _ f _) = takeExtension f == ".conf"