on master help sign in
Merge pull request #3411 from steshaw/refactor-main

Refactor `main/Main.hs`

commit2f7a893ea7f02bea53115c3bdf74f97aa7fa1142 authorJan de Muijnck-Hughes <jfdm@users.noreply.github.com> dateThu 8 Sep 2016, 16:47 (10 years ago) parenteab81ec4431513
1 file changed+50−54
main/Main.hs+50−54
main/Main.hs+50−54
@@ -1,8 +1,7 @@
11module Main where
22
3−import System.Exit ( exitSuccess )
4−
53import Control.Monad ( when )
4+import System.Exit ( exitSuccess )
65
76import Idris.AbsSyntax
87import Idris.Error
@@ -14,63 +13,60 @@ import Idris.Main
1413
1514import Util.System ( setupBundledCC )
1615
16+processShowOptions :: [Opt] -> Idris ()
17+processShowOptions opts = runIO $ do
18+ when (ShowAll `elem` opts) $ showExitIdrisInfo
19+ when (ShowLoggingCats `elem` opts) $ showExitIdrisLoggingCategories
20+ when (ShowIncs `elem` opts) $ showExitIdrisFlagsInc
21+ when (ShowLibs `elem` opts) $ showExitIdrisFlagsLibs
22+ when (ShowLibdir `elem` opts) $ showExitIdrisLibDir
23+ when (ShowPkgs `elem` opts) $ showExitIdrisInstalledPackages
1724
25+check :: [Opt] -> (Opt -> Maybe a) -> ([a] -> Idris ()) -> Idris ()
26+check opts extractOpts action = do
27+ case opt extractOpts opts of
28+ [] -> return ()
29+ fs -> do action fs
30+ runIO exitSuccess
1831
19−-- Main program reads command line options, parses the main program, and gets
20−-- on with the REPL.
32+processClientOptions :: [Opt] -> Idris ()
33+processClientOptions opts = check opts getClient $ \fs -> case fs of
34+ (c:_) -> do
35+ setVerbose False
36+ setQuiet True
37+ case getPort opts of
38+ Just DontListen -> ifail "\"--client\" and \"--port none\" are incompatible"
39+ Just (ListenPort port) -> runIO $ runClient (Just port) c
40+ Nothing -> runIO $ runClient Nothing c
2141
22−main :: IO ()
23−main = do opts <- runArgParser
24− runMain (runIdris opts)
42+processPackageOptions :: [Opt] -> Idris ()
43+processPackageOptions opts = do
44+ check opts getPkgCheck $ \fs -> runIO $ do
45+ mapM_ (checkPkg opts (WarnOnly `elem` opts) True) fs
46+ check opts getPkgClean $ \fs -> runIO $ do
47+ mapM_ (cleanPkg opts) fs
48+ check opts getPkgMkDoc $ \fs -> runIO $ do
49+ mapM_ (documentPkg opts) fs
50+ check opts getPkgTest $ \fs -> runIO $ do
51+ mapM_ (testPkg opts) fs
52+ check opts getPkg $ \fs -> runIO $ do
53+ mapM_ (buildPkg opts (WarnOnly `elem` opts)) fs
54+ check opts getPkgREPL $ \fs -> case fs of
55+ [f] -> replPkg opts f
56+ _ -> ifail "Too many packages"
2557
2658-- | The main function for the Idris executable.
2759runIdris :: [Opt] -> Idris ()
2860runIdris opts = do
29− runIO setupBundledCC
30− -- Show information then quit.
31− when (ShowAll `elem` opts) $ runIO showExitIdrisInfo
32− when (ShowLoggingCats `elem` opts) $ runIO showExitIdrisLoggingCategories
33− when (ShowIncs `elem` opts) $ runIO showExitIdrisFlagsInc
34− when (ShowLibs `elem` opts) $ runIO showExitIdrisFlagsLibs
35− when (ShowLibdir `elem` opts) $ runIO showExitIdrisLibDir
36− when (ShowPkgs `elem` opts) $ runIO showExitIdrisInstalledPackages
37−
38− -- Be a client to an IDE Mode server.
39− case opt getClient opts of
40− [] -> return ()
41− (c:_) -> do setVerbose False
42− setQuiet True
43− case getPort opts of
44− Just DontListen ->
45− ifail "\"--client\" and \"--port none\" are incompatible"
46− Just (ListenPort port) -> runIO $ runClient (Just port) c
47− Nothing -> runIO $ runClient Nothing c
48− runIO exitSuccess
49−
50− -- Work with Idris packages.
51− case opt getPkgCheck opts of
52− [] -> return ()
53− fs -> do runIO $ mapM_ (checkPkg opts (WarnOnly `elem` opts) True) fs
54− runIO exitSuccess
55− case opt getPkgClean opts of
56− [] -> return ()
57− fs -> do runIO $ mapM_ (cleanPkg opts) fs
58− runIO exitSuccess
59− case opt getPkgMkDoc opts of -- IdrisDoc
60− [] -> return ()
61− fs -> do runIO $ mapM_ (documentPkg opts) fs
62− runIO exitSuccess
63− case opt getPkgTest opts of
64− [] -> return ()
65− fs -> do runIO $ mapM_ (testPkg opts) fs
66− runIO exitSuccess
67−
68− -- Either launch one of the interaction modes, or install a
69− -- package.
70− case opt getPkg opts of
71− [] -> case opt getPkgREPL opts of
72− [] -> idrisMain opts
73− [f] -> replPkg opts f
74− _ -> ifail "Too many packages"
75− fs -> runIO $ mapM_ (buildPkg opts (WarnOnly `elem` opts)) fs
61+ runIO setupBundledCC
62+ processShowOptions opts -- Show information then quit.
63+ processClientOptions opts -- Be a client to an IDE Mode server.
64+ processPackageOptions opts -- Work with Idris packages.
65+ idrisMain opts -- Launch REPL or compile mode.
7666
67+-- Main program reads command line options, parses the main program, and gets
68+-- on with the REPL.
69+main :: IO ()
70+main = do
71+ opts <- runArgParser
72+ runMain (runIdris opts)