module Hix where

import Hix.Bootstrap (bootstrapProject)
import Hix.Data.Error (
  Error (..),
  printBootstrapError,
  printEnvError,
  printFatalError,
  printGhciError,
  printNewError,
  printPreprocError,
  )
import Hix.Env (printEnvRunner)
import Hix.Ghci (printGhciCmdline, printGhcidCmdline)
import Hix.Monad (M, runM)
import Hix.New (newProject)
import qualified Hix.Options as Options
import Hix.Options (
  Command (BootstrapCmd, EnvRunner, GhciCmd, GhcidCmd, NewCmd, Preproc),
  GlobalOptions (GlobalOptions),
  Options (Options),
  parseCli,
  )
import Hix.Preproc (preprocess)

handleError ::
  MonadIO m =>
  GlobalOptions ->
  Error ->
  m ()
handleError :: forall (m :: * -> *). MonadIO m => GlobalOptions -> Error -> m ()
handleError GlobalOptions {Maybe Bool
verbose :: Maybe Bool
$sel:verbose:GlobalOptions :: GlobalOptions -> Maybe Bool
verbose} = \case
  PreprocError Text
err -> Text -> m ()
forall (m :: * -> *). MonadIO m => Text -> m ()
printPreprocError Text
err
  EnvError Text
err -> Text -> m ()
forall (m :: * -> *). MonadIO m => Text -> m ()
printEnvError Text
err
  GhciError Text
err -> Text -> m ()
forall (m :: * -> *). MonadIO m => Text -> m ()
printGhciError Text
err
  NewError Text
err -> Text -> m ()
forall (m :: * -> *). MonadIO m => Text -> m ()
printNewError Text
err
  BootstrapError Text
err -> Text -> m ()
forall (m :: * -> *). MonadIO m => Text -> m ()
printBootstrapError Text
err
  NoMatch Text
msg | Bool -> Maybe Bool -> Bool
forall a. a -> Maybe a -> a
fromMaybe Bool
False Maybe Bool
verbose -> Text -> m ()
forall (m :: * -> *). MonadIO m => Text -> m ()
printPreprocError Text
msg
  NoMatch Text
_ -> m ()
forall (f :: * -> *). Applicative f => f ()
unit
  Fatal Text
err -> Text -> m ()
forall (m :: * -> *). MonadIO m => Text -> m ()
printFatalError Text
err

runCommand :: Command -> M ()
runCommand :: Command -> M ()
runCommand = \case
  Preproc PreprocOptions
opts -> PreprocOptions -> M ()
preprocess PreprocOptions
opts
  EnvRunner EnvRunnerCommandOptions
opts -> EnvRunnerOptions -> M ()
printEnvRunner EnvRunnerCommandOptions
opts.options
  GhcidCmd GhcidOptions
opts -> GhcidOptions -> M ()
printGhcidCmdline GhcidOptions
opts
  GhciCmd GhciOptions
opts -> GhciOptions -> M ()
printGhciCmdline GhciOptions
opts
  NewCmd NewOptions
opts -> NewProjectConfig -> M ()
newProject NewOptions
opts.config
  BootstrapCmd BootstrapOptions
opts -> BootstrapProjectConfig -> M ()
bootstrapProject BootstrapOptions
opts.config

main :: IO ()
main :: IO ()
main = do
  Options GlobalOptions
global Command
cmd <- IO Options
parseCli
  (Error -> IO ()) -> Either Error () -> IO ()
forall (m :: * -> *) a b.
Applicative m =>
(a -> m b) -> Either a b -> m b
leftA (GlobalOptions -> Error -> IO ()
forall (m :: * -> *). MonadIO m => GlobalOptions -> Error -> m ()
handleError GlobalOptions
global) (Either Error () -> IO ()) -> IO (Either Error ()) -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Path Abs Dir -> M () -> IO (Either Error ())
forall a. Path Abs Dir -> M a -> IO (Either Error a)
runM GlobalOptions
global.cwd (Command -> M ()
runCommand Command
cmd)