<commit_before>module TestLoop.Internal.Watcher (reloadTestSuite) where

--------------------

import           Control.Monad.Trans                 (MonadIO (..))
import           Data.Monoid                         (mconcat)
import qualified Filesystem.Path                     as FS
import qualified Filesystem.Path.CurrentOS           as FS

--------------------

import           Language.Haskell.Interpreter        (as, interpret,
                                                      loadModules, setImportsQ,
                                                      setTopLevelModules)
import           Language.Haskell.Interpreter.Unsafe (unsafeRunInterpreterWithArgs)

--------------------

import           Data.Time.LocalTime                 (getZonedTime)

--------------------

import           TestLoop.Internal.Types
import           TestLoop.Util

--------------------------------------------------------------------------------

reloadTestSuite :: MainModuleName
                -> MainModulePath
                -> HsSourcePaths
                -> FS.FilePath
                -> IO ()
reloadTestSuite moduleName modulePath sourcePaths modifiedFile
  | isNotEmacsFile = reloadTestSuite_
  | otherwise = return ()
  where
    isNotEmacsFile = not ('#' `elem` (FS.encodeString $ FS.filename modifiedFile))
    reloadTestSuite_ = do
      -- time <- getZonedTime
      -- putStrLn ""
      -- putStrLn $ replicate 80 '-'
      -- putStrLn $ mconcat [ show time
      --                    , " | "
      --                    , FS.encodeString  modifiedFile]
      -- putStrLn $ replicate 80 '-'
      r <- runInterpreter
      case r of
        Right () -> return ()
        Left e   -> print e

    runInterpreter = unsafeRunInterpreterWithArgs
                       [ "-i " ++ join ":" sourcePaths
                       , "-package-conf cabal-dev/packages-7.6.1.conf"]
                       interpreterAction

    interpreterAction = do
      loadModules [modulePath]
      setTopLevelModules [moduleName]
      setImportsQ [("Prelude", Nothing)]
      execution <- interpret "main" (as :: IO ())
      liftIO $ execution<commit_msg>Remove fixed database package from the Interpreter execution<commit_after>module TestLoop.Internal.Watcher (reloadTestSuite) where

--------------------

import           Control.Monad.Trans                 (MonadIO (..))
import           Data.List                           (isPrefixOf)
import           Data.Monoid                         (mconcat)
import qualified Filesystem.Path                     as FS
import qualified Filesystem.Path.CurrentOS           as FS

--------------------

import           Language.Haskell.Interpreter        (as, interpret,
                                                      loadModules, setImportsQ,
                                                      setTopLevelModules)
import           Language.Haskell.Interpreter.Unsafe (unsafeRunInterpreterWithArgs)

--------------------

import           Data.Time.LocalTime                 (getZonedTime)

--------------------

import           System.Directory                    (doesDirectoryExist,
                                                      getCurrentDirectory,
                                                      getDirectoryContents)
import           System.FilePath                     (joinPath)

--------------------

import           TestLoop.Internal.Types
import           TestLoop.Util

--------------------------------------------------------------------------------

getPackageDatabaseFile :: IO (Maybe FilePath)
getPackageDatabaseFile = do
  cabalDevExists <- doesDirectoryExist "cabal-dev"
  if (not cabalDevExists)
     then return Nothing
     else do
       dir <- getCurrentDirectory
       let cabalDevDir = joinPath [dir, "cabal-dev"]
       packages <- getDirectoryContents cabalDevDir
       case filter ("packages-" `isPrefixOf`) packages of
         (packagesFile:_) -> return $ Just (joinPath [cabalDevDir, packagesFile])
         _ -> return Nothing


reloadTestSuite :: MainModuleName
                -> MainModulePath
                -> HsSourcePaths
                -> FS.FilePath
                -> IO ()
reloadTestSuite moduleName modulePath sourcePaths modifiedFile
  | isNotEmacsFile = reloadTestSuite_
  | otherwise = return ()
  where
    isNotEmacsFile = not ('#' `elem` (FS.encodeString $ FS.filename modifiedFile))
    reloadTestSuite_ = do
      -- time <- getZonedTime
      -- putStrLn ""
      -- putStrLn $ replicate 80 '-'
      -- putStrLn $ mconcat [ show time
      --                    , " | "
      --                    , FS.encodeString  modifiedFile]
      -- putStrLn $ replicate 80 '-'
      r <- runInterpreter
      case r of
        Right () -> return ()
        Left e   -> print e

    runInterpreter = do
      mPackageDatabaseFile <- getPackageDatabaseFile
      let args = case mPackageDatabaseFile of
                   Just file -> [ "-i " ++ join ":" sourcePaths
                                , "-package-conf " ++ file]
                   Nothing -> [ "-i " ++ join ":" sourcePaths]
      unsafeRunInterpreterWithArgs args interpreterAction

    interpreterAction = do
      loadModules [modulePath]
      setTopLevelModules [moduleName]
      setImportsQ [("Prelude", Nothing)]
      execution <- interpret "main" (as :: IO ())
      liftIO $ execution
{'fraction_non_alphanumeric': 0.1368205510052122, 'fraction_numerical': 0.0020476545048399106, 'mean_word_length': 1.2971355280034202, 'pattern_counts': {'":': 3, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6064029', 'n_tokens_mistral': 1287, 'n_tokens_neox': 1236, 'n_words': 295}