@@ -6,6 +6,7 @@ module Main where
66import qualified Data.Text as T
77import qualified Data.Text.Lazy as LT
88
9+ import Control.Monad (void )
910import Data.Text (Text )
1011import Data.Text.Lazy.Builder (toLazyText )
1112import Haskell.Template.Task (grade )
@@ -29,9 +30,12 @@ import Rainbow (
2930import System.Directory (getTemporaryDirectory )
3031import System.Exit (die , exitFailure , exitSuccess )
3132import System.Environment (getArgs )
32- import System.FilePath ((-<.>) )
33+ import System.FilePath ((</>) , ( -<.>) )
3334import System.IO (readFile' )
35+ import Text.ParserCombinators.ReadP ((+++) , char , choice , string )
36+ import Text.ParserCombinators.ReadPrec (lift )
3437import Text.PrettyPrint.Leijen.Text (Doc , SimpleDoc (.. ), renderPretty )
38+ import Text.Read (Read (.. ), readMaybe )
3539
3640
3741
@@ -53,16 +57,32 @@ suggestionStyle :: Text -> Chunk
5357suggestionStyle = bold . fore brightYellow . chunk
5458
5559
60+ data Mode = Submission | Solution
61+
62+
63+ instance Read Mode where
64+ readPrec = lift $ do
65+ void $ char ' s' +++ char ' S'
66+ choice [
67+ string " ubmission" >> pure Submission ,
68+ string " olution" >> pure Solution
69+ ]
70+
71+
5672main :: IO ()
5773main = do
5874 args <- getArgs
5975 case args of
60- [task,submission] -> do
61- taskContents <- openDotHs task
62- submissionContents <- openDotHs submission
63- runTemplateTask taskContents submissionContents
64- exitSuccess
65- _ -> die usage
76+ [task,subMode] -> do
77+ let openFileInDir dir = openDotHs $ " examples" </> dir </> task
78+ taskContents <- openFileInDir " configs"
79+ submissionContents <- maybe
80+ modeHelp
81+ (openFileInDir . modeToDir)
82+ $ readMaybe subMode
83+ runTemplateTask taskContents submissionContents
84+ exitSuccess
85+ _ -> usage
6686
6787
6888runTemplateTask :: String -> String -> IO ()
@@ -80,12 +100,21 @@ runTemplateTask task submission = do
80100 putChunkLn $ statusLabel green " SUCCESS"
81101
82102
103+ modeToDir :: Mode -> FilePath
104+ modeToDir Submission = " tasks"
105+ modeToDir Solution = " solutions"
106+
107+
83108openDotHs :: FilePath -> IO String
84109openDotHs path = readFile' $ path -<.> " hs"
85110
86111
87- usage :: String
88- usage = " usage: test-task <config path> <solution path>"
112+ modeHelp :: IO a
113+ modeHelp = die " Parse error on submission mode. Try 'submission' or 'solution'"
114+
115+
116+ usage :: IO a
117+ usage = die " usage: test-task <task name> <[submission|solution]>"
89118
90119
91120styleRejections :: Text -> Chunk
0 commit comments