module Coffee.Bindings
( Coffee(..)
, coffeeCompile
, coffeeVersion
, coffeePrint
) where
import Data.Maybe (fromMaybe)
import System.Process (rawSystem, readProcess)
import System.Exit (ExitCode(..))
data Coffee = Coffee
{ customCompiler :: Maybe FilePath
, bare :: Bool
}
coffeeCompile :: [FilePath]
-> Maybe FilePath
-> Coffee
-> IO ExitCode
coffeeCompile [] _ _ = return $ ExitFailure 1
coffeeCompile files output coffee =
rawSystem (getCompiler coffee) args
where args = outputPath output ++ ["-c"] ++ files
coffeeVersion :: Coffee -> IO String
coffeeVersion c = coffeeRead c ["-v"]
coffeePrint :: FilePath -> Coffee -> IO String
coffeePrint file c = coffeeRead c $ ["-v", file]
outputPath :: Maybe FilePath -> [FilePath]
outputPath (Just path) = ["-o", path]
outputPath Nothing = []
getCompiler :: Coffee -> FilePath
getCompiler = fromMaybe "coffee" . customCompiler
coffeeRead :: Coffee -> [String] -> IO String
coffeeRead coffee args =
readProcess (getCompiler coffee) args []