-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHelper.hs
55 lines (44 loc) · 1.43 KB
/
Helper.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{-# LANGUAGE ScopedTypeVariables #-}
module Helper where
import Control.Exception
import Control.Monad
import Data.String.Interpolate.Util
import GHC
import Name
import Outputable
import System.Exit
import System.FilePath
import Test.Mockery.Directory
import Ast
import Graph
withFoo :: String -> IO () -> IO ()
withFoo code =
withModules [("Foo", unindent code)]
withFooHeader :: String -> IO () -> IO ()
withFooHeader code =
withFoo ("module Foo where\n" ++ unindent code)
withModules :: [(String, String)] -> IO () -> IO ()
withModules modules action = do
inTempDirectory $ do
forM_ modules $ \ (name, code) -> do
writeFile (name <.> "hs") (unindent code)
action
parseStringGraph :: [FilePath] -> IO (Graph String)
parseStringGraph files = do
result <- parse files
case result of
Left e -> die e
Right r -> return $ fmap showName $ usedTopLevelNames r
showName :: Name -> String
showName name = mod ++ "." ++ id
where
mod = maybe "<unknown module>" (showSDocUnsafe . ppr) $
nameModule_maybe name
id = showSDocUnsafe $ ppr name
swallowExceptions :: IO () -> IO ()
swallowExceptions = handle (\ (_ :: SomeException) -> return ())
usageGraph :: Graph a -> [(a, [a])]
usageGraph = _usageGraph
eitherToError :: IO (Either String a) -> IO a
eitherToError action = do
either die return =<< action