Change CLI

This commit is contained in:
Frederik Hanghøj Iversen 2019-10-18 20:30:57 +02:00
parent 017a9402fe
commit e6b3c8d8be
2 changed files with 30 additions and 16 deletions

View file

@ -20,8 +20,8 @@ module Data.Language.Ruby.AST
, Ivar(..)
, Atom(..)
, Defs(..)
, Self(..)
, Nil(..)
, Self
, Nil
, Cbase
) where

View file

@ -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