-- | The Haxible library entrypoint.
module Haxible where

import Haxible.Codegen
import Haxible.Import
import Haxible.Normalize
import Haxible.Prelude
import Haxible.Syntax
import System.Process.Typed (proc, runProcess_)

-- | Transform an inventory and playbook into a haskell script.
compile :: FilePath -> FilePath -> IO Text
compile :: FilePath -> FilePath -> IO Text
compile FilePath
inventory FilePath
playPath = do
  [PlaySyntax]
basePlays <- FilePath -> IO [PlaySyntax]
forall a (m :: * -> *).
(Show a, FromJSON a, MonadIO m) =>
FilePath -> m a
Haxible.Syntax.decodeFile FilePath
playPath
  [Play]
plays <- (PlaySyntax -> IO Play) -> [PlaySyntax] -> IO [Play]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (FilePath -> PlaySyntax -> IO Play
Haxible.Import.resolveImport FilePath
playPath) [PlaySyntax]
basePlays
  let exprs :: [Definition]
exprs = [Play] -> [Definition]
Haxible.Normalize.normalizePlaybook [Play]
plays
  Text -> IO Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> IO Text) -> Text -> IO Text
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> [Definition] -> Text
renderScript FilePath
inventory FilePath
playPath [Definition]
exprs

-- | Execute the haskell script with cabal.
execute :: FilePath -> IO ()
execute :: FilePath -> IO ()
execute FilePath
script = ProcessConfig () () () -> IO ()
forall (m :: * -> *) stdin stdout stderr.
MonadIO m =>
ProcessConfig stdin stdout stderr -> m ()
runProcess_ (FilePath -> [FilePath] -> ProcessConfig () () ()
proc FilePath
"cabal" [FilePath
"run", FilePath
"-O0", FilePath
script])