Change CLI
This commit is contained in:
parent
017a9402fe
commit
e6b3c8d8be
|
@ -20,8 +20,8 @@ module Data.Language.Ruby.AST
|
||||||
, Ivar(..)
|
, Ivar(..)
|
||||||
, Atom(..)
|
, Atom(..)
|
||||||
, Defs(..)
|
, Defs(..)
|
||||||
, Self(..)
|
, Self
|
||||||
, Nil(..)
|
, Nil
|
||||||
, Cbase
|
, Cbase
|
||||||
) where
|
) where
|
||||||
|
|
||||||
|
|
|
@ -18,19 +18,18 @@ import qualified Rubyhs.References as Ruby
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
Command{targets, printAST, maybeDotPath} <- getCommand
|
Command{targets, printAST, printRefs, printGraph, maybeDotPath} <- getCommand
|
||||||
blocks <- parseInput @Ruby.Begin targets
|
begins <- parseInput @Ruby.Begin targets
|
||||||
let
|
let
|
||||||
act block = do
|
act begin = do
|
||||||
let res@(Ruby.Result x) = Ruby.references $ block
|
let res@(Ruby.Result x) = Ruby.references $ begin
|
||||||
putEncoded res
|
when printAST $ putEncoded begin
|
||||||
ByteString.putStrLn . Aeson.encode . toJSONForest . Ruby.graph $ block
|
when printRefs $ putEncoded res
|
||||||
|
when printGraph $ drawGraph $ Ruby.graph begin
|
||||||
case maybeDotPath of
|
case maybeDotPath of
|
||||||
Nothing -> pure ()
|
Nothing -> pure ()
|
||||||
Just dotPath -> drawDot dotPath x
|
Just dotPath -> drawDot dotPath x
|
||||||
if printAST
|
traverse_ act begins
|
||||||
then traverse_ @[] putEncoded blocks
|
|
||||||
else traverse_ act blocks
|
|
||||||
|
|
||||||
drawDot :: FilePath -> Map Ruby.Node (Set Ruby.Node) -> IO ()
|
drawDot :: FilePath -> Map Ruby.Node (Set Ruby.Node) -> IO ()
|
||||||
drawDot p
|
drawDot p
|
||||||
|
@ -39,13 +38,20 @@ drawDot p
|
||||||
. fmap (fmap (qoutes . Ruby.prettyContext) . \(x, xs) -> x :| toList xs)
|
. fmap (fmap (qoutes . Ruby.prettyContext) . \(x, xs) -> x :| toList xs)
|
||||||
. toList
|
. toList
|
||||||
|
|
||||||
|
drawGraph :: Forest Ruby.Node -> IO ()
|
||||||
|
drawGraph
|
||||||
|
= ByteString.putStrLn
|
||||||
|
. Aeson.encode
|
||||||
|
. toJSONForest
|
||||||
|
. fmap (fmap Ruby.prettyContext)
|
||||||
|
|
||||||
qoutes :: Text -> Text
|
qoutes :: Text -> Text
|
||||||
qoutes x = "\"" <> x <> "\""
|
qoutes x = "\"" <> x <> "\""
|
||||||
|
|
||||||
toJSONForest :: Forest Ruby.Node -> Aeson.Value
|
toJSONForest :: Forest Text -> Aeson.Value
|
||||||
toJSONForest = Aeson.Object . fromList . fmap go
|
toJSONForest = Aeson.Object . fromList . fmap go
|
||||||
where
|
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
|
-- | If arguments is non-empty treat all arguments as filepaths and
|
||||||
-- parse the modules at those locations. If there are no arguments,
|
-- parse the modules at those locations. If there are no arguments,
|
||||||
|
@ -61,16 +67,24 @@ putEncoded = ByteString.putStrLn . Aeson.encode
|
||||||
data Command = Command
|
data Command = Command
|
||||||
{ targets :: [FilePath]
|
{ targets :: [FilePath]
|
||||||
, printAST :: Bool
|
, printAST :: Bool
|
||||||
|
, printRefs :: Bool
|
||||||
|
, printGraph :: Bool
|
||||||
, maybeDotPath :: Maybe FilePath
|
, maybeDotPath :: Maybe FilePath
|
||||||
}
|
}
|
||||||
|
|
||||||
command :: Parser Command
|
command :: Parser Command
|
||||||
command = Command <$> targets <*> printAST <*> dotPath
|
command = Command <$> targets <*> printAST <*> printRefs <*> printGraph <*> dotPath
|
||||||
where
|
where
|
||||||
targets = many (Options.argument Options.str (Options.metavar "TARGET"))
|
targets = many (Options.argument Options.str (Options.metavar "TARGET"))
|
||||||
printAST = Options.switch
|
printAST = Options.switch
|
||||||
$ Options.long "print-ast"
|
$ Options.long "ast"
|
||||||
<> Options.help "Print AST and exit"
|
<> 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
|
dotPath
|
||||||
= Options.optional
|
= Options.optional
|
||||||
$ Options.strOption
|
$ Options.strOption
|
||||||
|
|
Loading…
Reference in a new issue