{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NoImplicitPrelude #-}

module HWM.CLI.Command
  ( Options (..),
    Command (..),
    currentVersion,
    defaultOptions,
    Bump (..),
    runCommand,
  )
where

import Data.Version (showVersion)
import HWM.CLI.Command.Init (InitOptions (..), initWorkspace)
import HWM.CLI.Command.Outdated (runOutdated)
import HWM.CLI.Command.Publish (publish)
import HWM.CLI.Command.Run (ScriptOptions, runScript)
import HWM.CLI.Command.Status (showStatus)
import HWM.CLI.Command.Sync (sync)
import HWM.CLI.Command.Version (runVersion)
import HWM.Core.Common (Name)
import HWM.Core.Options (Options (..), defaultOptions)
import HWM.Core.Version (Bump (..))
import HWM.Domain.ConfigT (ConfigT, runConfigT)
import qualified Paths_hwm as CLI
import Relude hiding (fix)

data Command
  = Sync {Command -> Maybe Name
tag :: Maybe Name}
  | Publish {Command -> Maybe Name
groupName :: Maybe Name}
  | Version {Command -> Maybe Bump
bump :: Maybe Bump}
  | Outdated {Command -> Bool
fix :: Bool}
  | Run {Command -> ScriptOptions
runOptions :: ScriptOptions}
  | Status
  | Init {Command -> InitOptions
initOptions :: InitOptions}
  deriving (Int -> Command -> ShowS
[Command] -> ShowS
Command -> String
(Int -> Command -> ShowS)
-> (Command -> String) -> ([Command] -> ShowS) -> Show Command
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Command -> ShowS
showsPrec :: Int -> Command -> ShowS
$cshow :: Command -> String
show :: Command -> String
$cshowList :: [Command] -> ShowS
showList :: [Command] -> ShowS
Show)

currentVersion :: String
currentVersion :: String
currentVersion = Version -> String
showVersion Version
CLI.version

command :: Command -> ConfigT ()
command :: Command -> ConfigT ()
command Publish {Maybe Name
groupName :: Command -> Maybe Name
groupName :: Maybe Name
groupName} = Maybe Name -> ConfigT ()
publish Maybe Name
groupName
command Version {Maybe Bump
bump :: Command -> Maybe Bump
bump :: Maybe Bump
bump} = Maybe Bump -> ConfigT ()
runVersion Maybe Bump
bump
command Outdated {Bool
fix :: Command -> Bool
fix :: Bool
fix} = Bool -> ConfigT ()
runOutdated Bool
fix
command Sync {Maybe Name
tag :: Command -> Maybe Name
tag :: Maybe Name
tag} = Maybe Name -> ConfigT ()
sync Maybe Name
tag
command Run {ScriptOptions
runOptions :: Command -> ScriptOptions
runOptions :: ScriptOptions
runOptions} = ScriptOptions -> ConfigT ()
runScript ScriptOptions
runOptions
command Command
Status = ConfigT ()
showStatus
command Init {} = () -> ConfigT ()
forall a. a -> ConfigT a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

runCommand :: Command -> Options -> IO ()
runCommand :: Command -> Options -> IO ()
runCommand Init {InitOptions
initOptions :: Command -> InitOptions
initOptions :: InitOptions
initOptions} Options
ops = InitOptions -> Options -> IO ()
initWorkspace InitOptions
initOptions Options
ops IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ConfigT () -> Options -> IO ()
runConfigT ConfigT ()
showStatus Options
ops
runCommand Command
cmd Options
ops = ConfigT () -> Options -> IO ()
runConfigT (Command -> ConfigT ()
command Command
cmd) Options
ops