{-# LANGUAGE OverloadedStrings #-}

module Test.Tasty.AutoCollect (
  processFile,
) where

import Data.Text (Text)
import qualified Data.Text as Text

import Test.Tasty.AutoCollect.GenerateMain
import Test.Tasty.AutoCollect.ModuleType
import Test.Tasty.AutoCollect.Utils.Text

-- | Preprocess the given Haskell file. See Preprocessor.hs
processFile :: FilePath -> Text -> IO Text
processFile :: FilePath -> Text -> IO Text
processFile FilePath
path Text
file =
  case Text -> Maybe ModuleType
parseModuleType Text
file of
    Just (ModuleMain AutoCollectConfig
cfg) -> AutoCollectConfig -> FilePath -> IO Text
generateMainModule AutoCollectConfig
cfg FilePath
path
    Just ModuleType
ModuleTest ->
      Text -> IO Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> IO Text) -> ([Text] -> Text) -> [Text] -> IO Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Text
Text.unlines ([Text] -> IO Text) -> [Text] -> IO Text
forall a b. (a -> b) -> a -> b
$
        [ Text
"{-# OPTIONS_GHC -fplugin=Test.Tasty.AutoCollect.ConvertTest #-}"
        , Text
file'
        ]
    Maybe ModuleType
Nothing -> Text -> IO Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
file'
  where
    file' :: Text
file' =
      [Text] -> Text
Text.unlines
        [ Text
"{-# LINE 1 " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
quoted (FilePath -> Text
Text.pack FilePath
path) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" #-}"
        , Text
file
        ]