{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DataKinds         #-}

module Stack.Options.ResolverParser
  ( abstractResolverOptsParser
  , compilerOptsParser
  , readCompilerVersion
  ) where

import qualified Data.Text as T
import           Options.Applicative
                   ( Parser, ReadM, help, long, metavar, option, readerError )
import           Options.Applicative.Types ( readerAsk )
import           Stack.Options.Utils ( hideMods )
import           Stack.Prelude
import           Stack.Types.Resolver ( AbstractResolver, readAbstractResolver )

-- | Parser for the resolver

abstractResolverOptsParser :: Bool -> Parser (Unresolved AbstractResolver)
abstractResolverOptsParser :: Bool -> Parser (Unresolved AbstractResolver)
abstractResolverOptsParser Bool
hide = forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM (Unresolved AbstractResolver)
readAbstractResolver
  (  forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"resolver"
  forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"RESOLVER"
  forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Override resolver in project file."
  forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. Bool -> Mod f a
hideMods Bool
hide
  )

compilerOptsParser :: Bool -> Parser WantedCompiler
compilerOptsParser :: Bool -> Parser WantedCompiler
compilerOptsParser Bool
hide = forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM WantedCompiler
readCompilerVersion
  (  forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"compiler"
  forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"COMPILER"
  forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Use the specified compiler."
  forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. Bool -> Mod f a
hideMods Bool
hide
  )

readCompilerVersion :: ReadM WantedCompiler
readCompilerVersion :: ReadM WantedCompiler
readCompilerVersion = do
  String
s <- ReadM String
readerAsk
  case Text -> Either PantryException WantedCompiler
parseWantedCompiler (String -> Text
T.pack String
s) of
    Left{} -> forall a. String -> ReadM a
readerError forall a b. (a -> b) -> a -> b
$ String
"Failed to parse compiler: " forall a. [a] -> [a] -> [a]
++ String
s
    Right WantedCompiler
x -> forall (f :: * -> *) a. Applicative f => a -> f a
pure WantedCompiler
x