module Config.Dyre.Compile ( customCompile ) where
import System.IO ( openFile, hClose, IOMode(..) )
import System.Exit ( ExitCode(..) )
import System.Process ( runProcess, waitForProcess )
import System.FilePath ( (</>) )
import System.Directory ( getCurrentDirectory, doesFileExist
, createDirectoryIfMissing )
import Control.Exception ( bracket )
import GHC.Paths ( ghc )
import Config.Dyre.Paths ( getPaths )
import Config.Dyre.Params ( Params(..) )
customCompile :: Params cfgType -> IO (Maybe String)
customCompile params@Params{statusOut = output} = do
(thisBinary, tempBinary, configFile, cacheDir) <- getPaths params
output $ "Configuration '" ++ configFile ++ "' changed. Recompiling."
createDirectoryIfMissing True cacheDir
let errFile = cacheDir </> "errors.log"
result <- bracket (openFile errFile WriteMode) hClose $ \errHandle -> do
ghcOpts <- makeFlags params configFile tempBinary cacheDir
ghcProc <- runProcess ghc ghcOpts (Just cacheDir) Nothing
Nothing Nothing (Just errHandle)
waitForProcess ghcProc
if result /= ExitSuccess
then output $ "Error occurred while loading configuration file."
else output $ "Program reconfiguration successful."
errorsExist <- doesFileExist errFile
if not errorsExist
then return Nothing
else do errors <- readFile errFile
if errors == ""
then return Nothing
else return . Just $ errors
makeFlags :: Params cfgType -> FilePath -> FilePath -> FilePath -> IO [String]
makeFlags Params{ghcOpts = flags, hidePackages = hides}
cfgFile tmpFile cacheDir = do
currentDir <- getCurrentDirectory
return . concat $ [ ["-v0", "-fforce-recomp", "-i" ++ currentDir]
, ["-outputdir", cacheDir]
, prefix "-hide-package" hides, flags
, ["--make", cfgFile, "-o", tmpFile]
]
where prefix y xs = concat . map (\x -> [y,x]) $ xs