forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.hs
More file actions
58 lines (49 loc) · 2.24 KB
/
Graph.hs
File metadata and controls
58 lines (49 loc) · 2.24 KB
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
56
57
58
module Language.PureScript.Graph (graph) where
import Prelude
import qualified Data.Aeson as Json
import qualified Data.Aeson.Key as Json.Key
import qualified Data.Aeson.KeyMap as Json.Map
import qualified Data.Map as Map
import Control.Monad (forM)
import Data.Aeson ((.=))
import Data.Foldable (foldl')
import Data.Map (Map)
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import System.IO.UTF8 (readUTF8FileT)
import qualified Language.PureScript.Crash as Crash
import qualified Language.PureScript.CST as CST
import qualified Language.PureScript.Make as Make
import qualified Language.PureScript.ModuleDependencies as Dependencies
import qualified Language.PureScript.Options as Options
import Language.PureScript.Errors (MultipleErrors)
import Language.PureScript.Names (ModuleName, runModuleName)
-- | Given a set of filepaths, try to build the dependency graph and return
-- that as its JSON representation (or a bunch of errors, if any)
graph :: [FilePath] -> IO (Either MultipleErrors Json.Value, MultipleErrors)
graph input = do
moduleFiles <- readInput input
Make.runMake Options.defaultOptions $ do
ms <- CST.parseModulesFromFiles id moduleFiles
let parsedModuleSig = Dependencies.moduleSignature . CST.resPartial
(_sorted, moduleGraph) <- Dependencies.sortModules Dependencies.Direct (parsedModuleSig . snd) ms
let pathMap = Map.fromList $
map (\(p, m) -> (Dependencies.sigModuleName (parsedModuleSig m), p)) ms
pure (moduleGraphToJSON pathMap moduleGraph)
moduleGraphToJSON
:: Map ModuleName FilePath
-> Dependencies.ModuleGraph
-> Json.Value
moduleGraphToJSON paths = Json.Object . foldl' insert mempty
where
insert :: Json.Object -> (ModuleName, [ModuleName]) -> Json.Object
insert obj (mn, depends) = Json.Map.insert (Json.Key.fromText (runModuleName mn)) value obj
where
path = fromMaybe (Crash.internalError "missing module name in graph") $ Map.lookup mn paths
value = Json.object
[ "path" .= path
, "depends" .= fmap runModuleName depends
]
readInput :: [FilePath] -> IO [(FilePath, Text)]
readInput inputFiles =
forM inputFiles $ \inFile -> (inFile, ) <$> readUTF8FileT inFile