{-# LANGUAGE NoImplicitPrelude #-}

module Stack.Types.Runner
  ( Runner (..)
  , HasRunner (..)
  , globalOptsL
  , stackYamlLocL
  , lockFileBehaviorL
  , terminalL
  , reExecL
  , rslInLogL
  ) where

import           RIO.Process ( HasProcessContext (..), ProcessContext )
import           Stack.Prelude
import           Stack.Types.GlobalOpts ( GlobalOpts (..) )
import           Stack.Types.LockFileBehavior ( LockFileBehavior )
import           Stack.Types.StackYamlLoc ( StackYamlLoc )

-- | The base environment that almost everything in Stack runs in,

-- based off of parsing command line options in 'GlobalOpts'. Provides

-- logging and process execution.

data Runner = Runner
  { Runner -> GlobalOpts
runnerGlobalOpts :: !GlobalOpts
  , Runner -> Bool
runnerUseColor   :: !Bool
  , Runner -> LogFunc
runnerLogFunc    :: !LogFunc
  , Runner -> Int
runnerTermWidth  :: !Int
  , Runner -> ProcessContext
runnerProcessContext :: !ProcessContext
  }

instance HasLogFunc Runner where
  logFuncL :: Lens' Runner LogFunc
logFuncL = (Runner -> LogFunc)
-> (Runner -> LogFunc -> Runner) -> Lens' Runner LogFunc
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Runner -> LogFunc
runnerLogFunc (\Runner
x LogFunc
y -> Runner
x { runnerLogFunc :: LogFunc
runnerLogFunc = LogFunc
y })

instance HasProcessContext Runner where
  processContextL :: Lens' Runner ProcessContext
processContextL =
    (Runner -> ProcessContext)
-> (Runner -> ProcessContext -> Runner)
-> Lens' Runner ProcessContext
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Runner -> ProcessContext
runnerProcessContext (\Runner
x ProcessContext
y -> Runner
x { runnerProcessContext :: ProcessContext
runnerProcessContext = ProcessContext
y })

instance HasRunner Runner where
  runnerL :: Lens' Runner Runner
runnerL = (Runner -> f Runner) -> Runner -> f Runner
forall a. a -> a
id

instance HasStylesUpdate Runner where
  stylesUpdateL :: Lens' Runner StylesUpdate
stylesUpdateL = (GlobalOpts -> f GlobalOpts) -> Runner -> f Runner
forall env. HasRunner env => Lens' env GlobalOpts
Lens' Runner GlobalOpts
globalOptsL((GlobalOpts -> f GlobalOpts) -> Runner -> f Runner)
-> ((StylesUpdate -> f StylesUpdate) -> GlobalOpts -> f GlobalOpts)
-> (StylesUpdate -> f StylesUpdate)
-> Runner
-> f Runner
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                  (GlobalOpts -> StylesUpdate)
-> (GlobalOpts -> StylesUpdate -> GlobalOpts)
-> Lens GlobalOpts GlobalOpts StylesUpdate StylesUpdate
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens GlobalOpts -> StylesUpdate
globalStylesUpdate (\GlobalOpts
x StylesUpdate
y -> GlobalOpts
x { globalStylesUpdate :: StylesUpdate
globalStylesUpdate = StylesUpdate
y })
instance HasTerm Runner where
  useColorL :: Lens' Runner Bool
useColorL = (Runner -> Bool) -> (Runner -> Bool -> Runner) -> Lens' Runner Bool
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Runner -> Bool
runnerUseColor (\Runner
x Bool
y -> Runner
x { runnerUseColor :: Bool
runnerUseColor = Bool
y })
  termWidthL :: Lens' Runner Int
termWidthL = (Runner -> Int) -> (Runner -> Int -> Runner) -> Lens' Runner Int
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Runner -> Int
runnerTermWidth (\Runner
x Int
y -> Runner
x { runnerTermWidth :: Int
runnerTermWidth = Int
y })

-- | Class for environment values which have a 'Runner'.

class (HasProcessContext env, HasLogFunc env) => HasRunner env where
  runnerL :: Lens' env Runner

stackYamlLocL :: HasRunner env => Lens' env StackYamlLoc
stackYamlLocL :: forall env. HasRunner env => Lens' env StackYamlLoc
stackYamlLocL =
  (GlobalOpts -> f GlobalOpts) -> env -> f env
forall env. HasRunner env => Lens' env GlobalOpts
Lens' env GlobalOpts
globalOptsL((GlobalOpts -> f GlobalOpts) -> env -> f env)
-> ((StackYamlLoc -> f StackYamlLoc) -> GlobalOpts -> f GlobalOpts)
-> (StackYamlLoc -> f StackYamlLoc)
-> env
-> f env
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(GlobalOpts -> StackYamlLoc)
-> (GlobalOpts -> StackYamlLoc -> GlobalOpts)
-> Lens GlobalOpts GlobalOpts StackYamlLoc StackYamlLoc
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens GlobalOpts -> StackYamlLoc
globalStackYaml (\GlobalOpts
x StackYamlLoc
y -> GlobalOpts
x { globalStackYaml :: StackYamlLoc
globalStackYaml = StackYamlLoc
y })

lockFileBehaviorL :: HasRunner env => SimpleGetter env LockFileBehavior
lockFileBehaviorL :: forall env. HasRunner env => SimpleGetter env LockFileBehavior
lockFileBehaviorL = (GlobalOpts -> Const r GlobalOpts) -> env -> Const r env
forall env. HasRunner env => Lens' env GlobalOpts
Lens' env GlobalOpts
globalOptsL((GlobalOpts -> Const r GlobalOpts) -> env -> Const r env)
-> ((LockFileBehavior -> Const r LockFileBehavior)
    -> GlobalOpts -> Const r GlobalOpts)
-> (LockFileBehavior -> Const r LockFileBehavior)
-> env
-> Const r env
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(GlobalOpts -> LockFileBehavior)
-> SimpleGetter GlobalOpts LockFileBehavior
forall s a. (s -> a) -> SimpleGetter s a
to GlobalOpts -> LockFileBehavior
globalLockFileBehavior

globalOptsL :: HasRunner env => Lens' env GlobalOpts
globalOptsL :: forall env. HasRunner env => Lens' env GlobalOpts
globalOptsL = (Runner -> f Runner) -> env -> f env
forall env. HasRunner env => Lens' env Runner
Lens' env Runner
runnerL((Runner -> f Runner) -> env -> f env)
-> ((GlobalOpts -> f GlobalOpts) -> Runner -> f Runner)
-> (GlobalOpts -> f GlobalOpts)
-> env
-> f env
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Runner -> GlobalOpts)
-> (Runner -> GlobalOpts -> Runner) -> Lens' Runner GlobalOpts
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Runner -> GlobalOpts
runnerGlobalOpts (\Runner
x GlobalOpts
y -> Runner
x { runnerGlobalOpts :: GlobalOpts
runnerGlobalOpts = GlobalOpts
y })

-- | See 'globalTerminal'

terminalL :: HasRunner env => Lens' env Bool
terminalL :: forall env. HasRunner env => Lens' env Bool
terminalL = (GlobalOpts -> f GlobalOpts) -> env -> f env
forall env. HasRunner env => Lens' env GlobalOpts
Lens' env GlobalOpts
globalOptsL((GlobalOpts -> f GlobalOpts) -> env -> f env)
-> ((Bool -> f Bool) -> GlobalOpts -> f GlobalOpts)
-> (Bool -> f Bool)
-> env
-> f env
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(GlobalOpts -> Bool)
-> (GlobalOpts -> Bool -> GlobalOpts)
-> Lens GlobalOpts GlobalOpts Bool Bool
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens GlobalOpts -> Bool
globalTerminal (\GlobalOpts
x Bool
y -> GlobalOpts
x { globalTerminal :: Bool
globalTerminal = Bool
y })

-- | See 'globalReExecVersion'

reExecL :: HasRunner env => SimpleGetter env Bool
reExecL :: forall env. HasRunner env => SimpleGetter env Bool
reExecL = (GlobalOpts -> Const r GlobalOpts) -> env -> Const r env
forall env. HasRunner env => Lens' env GlobalOpts
Lens' env GlobalOpts
globalOptsL((GlobalOpts -> Const r GlobalOpts) -> env -> Const r env)
-> ((Bool -> Const r Bool) -> GlobalOpts -> Const r GlobalOpts)
-> (Bool -> Const r Bool)
-> env
-> Const r env
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(GlobalOpts -> Bool) -> SimpleGetter GlobalOpts Bool
forall s a. (s -> a) -> SimpleGetter s a
to (Maybe String -> Bool
forall a. Maybe a -> Bool
isJust (Maybe String -> Bool)
-> (GlobalOpts -> Maybe String) -> GlobalOpts -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GlobalOpts -> Maybe String
globalReExecVersion)

rslInLogL :: HasRunner env => SimpleGetter env Bool
rslInLogL :: forall env. HasRunner env => SimpleGetter env Bool
rslInLogL = (GlobalOpts -> Const r GlobalOpts) -> env -> Const r env
forall env. HasRunner env => Lens' env GlobalOpts
Lens' env GlobalOpts
globalOptsL((GlobalOpts -> Const r GlobalOpts) -> env -> Const r env)
-> ((Bool -> Const r Bool) -> GlobalOpts -> Const r GlobalOpts)
-> (Bool -> Const r Bool)
-> env
-> Const r env
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(GlobalOpts -> Bool) -> SimpleGetter GlobalOpts Bool
forall s a. (s -> a) -> SimpleGetter s a
to GlobalOpts -> Bool
globalRSLInLog