{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}

module Ema.CLI where

import Options.Applicative hiding (action)

data Cli = Cli
  { Cli -> Action
action :: Action
  }
  deriving (Cli -> Cli -> Bool
(Cli -> Cli -> Bool) -> (Cli -> Cli -> Bool) -> Eq Cli
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Cli -> Cli -> Bool
$c/= :: Cli -> Cli -> Bool
== :: Cli -> Cli -> Bool
$c== :: Cli -> Cli -> Bool
Eq, Int -> Cli -> ShowS
[Cli] -> ShowS
Cli -> String
(Int -> Cli -> ShowS)
-> (Cli -> String) -> ([Cli] -> ShowS) -> Show Cli
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Cli] -> ShowS
$cshowList :: [Cli] -> ShowS
show :: Cli -> String
$cshow :: Cli -> String
showsPrec :: Int -> Cli -> ShowS
$cshowsPrec :: Int -> Cli -> ShowS
Show)

data Action
  = Generate FilePath
  | Run
  deriving (Action -> Action -> Bool
(Action -> Action -> Bool)
-> (Action -> Action -> Bool) -> Eq Action
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Action -> Action -> Bool
$c/= :: Action -> Action -> Bool
== :: Action -> Action -> Bool
$c== :: Action -> Action -> Bool
Eq, Int -> Action -> ShowS
[Action] -> ShowS
Action -> String
(Int -> Action -> ShowS)
-> (Action -> String) -> ([Action] -> ShowS) -> Show Action
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Action] -> ShowS
$cshowList :: [Action] -> ShowS
show :: Action -> String
$cshow :: Action -> String
showsPrec :: Int -> Action -> ShowS
$cshowsPrec :: Int -> Action -> ShowS
Show)

cliParser :: Parser Cli
cliParser :: Parser Cli
cliParser = do
  Action
action <-
    Mod CommandFields Action -> Parser Action
forall a. Mod CommandFields a -> Parser a
subparser
      (String -> ParserInfo Action -> Mod CommandFields Action
forall a. String -> ParserInfo a -> Mod CommandFields a
command String
"gen" (Parser Action -> InfoMod Action -> ParserInfo Action
forall a. Parser a -> InfoMod a -> ParserInfo a
info Parser Action
generate (String -> InfoMod Action
forall a. String -> InfoMod a
progDesc String
"Generate static HTML files")))
      Parser Action -> Parser Action -> Parser Action
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Action -> Parser Action
forall (f :: * -> *) a. Applicative f => a -> f a
pure Action
Run
  pure Cli :: Action -> Cli
Cli {Action
action :: Action
action :: Action
..}
  where
    generate :: Parser Action
    generate :: Parser Action
generate =
      String -> Action
Generate (String -> Action) -> Parser String -> Parser Action
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM String -> Mod ArgumentFields String -> Parser String
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM String
forall s. IsString s => ReadM s
str (String -> Mod ArgumentFields String
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"DEST...")

cliAction :: IO Cli
cliAction :: IO Cli
cliAction = do
  ParserInfo Cli -> IO Cli
forall a. ParserInfo a -> IO a
execParser ParserInfo Cli
opts
  where
    opts :: ParserInfo Cli
opts =
      Parser Cli -> InfoMod Cli -> ParserInfo Cli
forall a. Parser a -> InfoMod a -> ParserInfo a
info
        (Parser Cli
cliParser Parser Cli -> Parser (Cli -> Cli) -> Parser Cli
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> Parser (Cli -> Cli)
forall a. Parser (a -> a)
helper)
        ( InfoMod Cli
forall a. InfoMod a
fullDesc
            InfoMod Cli -> InfoMod Cli -> InfoMod Cli
forall a. Semigroup a => a -> a -> a
<> String -> InfoMod Cli
forall a. String -> InfoMod a
progDesc String
"Ema - static site generator"
            InfoMod Cli -> InfoMod Cli -> InfoMod Cli
forall a. Semigroup a => a -> a -> a
<> String -> InfoMod Cli
forall a. String -> InfoMod a
header String
"Ema"
        )