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

compilerOptsParser :: Bool -> Parser WantedCompiler
compilerOptsParser :: Bool -> Parser WantedCompiler
compilerOptsParser Bool
hide = ReadM WantedCompiler
-> Mod OptionFields WantedCompiler -> Parser WantedCompiler
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM WantedCompiler
readCompilerVersion
  (  String -> Mod OptionFields WantedCompiler
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"compiler"
  Mod OptionFields WantedCompiler
-> Mod OptionFields WantedCompiler
-> Mod OptionFields WantedCompiler
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields WantedCompiler
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"COMPILER"
  Mod OptionFields WantedCompiler
-> Mod OptionFields WantedCompiler
-> Mod OptionFields WantedCompiler
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields WantedCompiler
forall (f :: * -> *) a. String -> Mod f a
help String
"Use the specified compiler."
  Mod OptionFields WantedCompiler
-> Mod OptionFields WantedCompiler
-> Mod OptionFields WantedCompiler
forall a. Semigroup a => a -> a -> a
<> Bool -> Mod OptionFields WantedCompiler
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{} -> String -> ReadM WantedCompiler
forall a. String -> ReadM a
readerError (String -> ReadM WantedCompiler) -> String -> ReadM WantedCompiler
forall a b. (a -> b) -> a -> b
$ String
"Failed to parse compiler: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s
    Right WantedCompiler
x -> WantedCompiler -> ReadM WantedCompiler
forall a. a -> ReadM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure WantedCompiler
x