From e6b3c8d8be64f03083af67fc5d7c5aa1da0ddeb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frederik=20Hangh=C3=B8j=20Iversen?= Date: Fri, 18 Oct 2019 20:30:57 +0200 Subject: [PATCH] Change CLI --- src/Data/Language/Ruby/AST.hs | 4 ++-- src/Rubyhs.hs | 42 +++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/Data/Language/Ruby/AST.hs b/src/Data/Language/Ruby/AST.hs index cf9e59d..975d1ce 100644 --- a/src/Data/Language/Ruby/AST.hs +++ b/src/Data/Language/Ruby/AST.hs @@ -20,8 +20,8 @@ module Data.Language.Ruby.AST , Ivar(..) , Atom(..) , Defs(..) - , Self(..) - , Nil(..) + , Self + , Nil , Cbase ) where diff --git a/src/Rubyhs.hs b/src/Rubyhs.hs index 6696186..db02ad5 100644 --- a/src/Rubyhs.hs +++ b/src/Rubyhs.hs @@ -18,19 +18,18 @@ import qualified Rubyhs.References as Ruby main :: IO () main = do - Command{targets, printAST, maybeDotPath} <- getCommand - blocks <- parseInput @Ruby.Begin targets + Command{targets, printAST, printRefs, printGraph, maybeDotPath} <- getCommand + begins <- parseInput @Ruby.Begin targets let - act block = do - let res@(Ruby.Result x) = Ruby.references $ block - putEncoded res - ByteString.putStrLn . Aeson.encode . toJSONForest . Ruby.graph $ block + act begin = do + let res@(Ruby.Result x) = Ruby.references $ begin + when printAST $ putEncoded begin + when printRefs $ putEncoded res + when printGraph $ drawGraph $ Ruby.graph begin case maybeDotPath of Nothing -> pure () Just dotPath -> drawDot dotPath x - if printAST - then traverse_ @[] putEncoded blocks - else traverse_ act blocks + traverse_ act begins drawDot :: FilePath -> Map Ruby.Node (Set Ruby.Node) -> IO () drawDot p @@ -39,13 +38,20 @@ drawDot p . fmap (fmap (qoutes . Ruby.prettyContext) . \(x, xs) -> x :| toList xs) . toList +drawGraph :: Forest Ruby.Node -> IO () +drawGraph + = ByteString.putStrLn + . Aeson.encode + . toJSONForest + . fmap (fmap Ruby.prettyContext) + qoutes :: Text -> Text qoutes x = "\"" <> x <> "\"" -toJSONForest :: Forest Ruby.Node -> Aeson.Value +toJSONForest :: Forest Text -> Aeson.Value toJSONForest = Aeson.Object . fromList . fmap go where - go (Node x xs) = (Ruby.prettyContext x, toJSONForest xs) + go (Node a as) = (a, toJSONForest as) -- | If arguments is non-empty treat all arguments as filepaths and -- parse the modules at those locations. If there are no arguments, @@ -61,16 +67,24 @@ putEncoded = ByteString.putStrLn . Aeson.encode data Command = Command { targets :: [FilePath] , printAST :: Bool + , printRefs :: Bool + , printGraph :: Bool , maybeDotPath :: Maybe FilePath } command :: Parser Command -command = Command <$> targets <*> printAST <*> dotPath +command = Command <$> targets <*> printAST <*> printRefs <*> printGraph <*> dotPath where targets = many (Options.argument Options.str (Options.metavar "TARGET")) printAST = Options.switch - $ Options.long "print-ast" - <> Options.help "Print AST and exit" + $ Options.long "ast" + <> Options.help "Print AST" + printRefs = Options.switch + $ Options.long "refs" + <> Options.help "Print references" + printGraph = Options.switch + $ Options.long "graph" + <> Options.help "Print call graph" dotPath = Options.optional $ Options.strOption