Add CLI option for dot path

This commit is contained in:
Frederik Hanghøj Iversen 2019-10-16 21:57:12 +02:00
parent 5ae9b28d10
commit 81b058f033

View file

@ -19,24 +19,23 @@ import qualified Rubyhs.References
main :: IO () main :: IO ()
main = do main = do
Command{targets, printAST} <- getCommand Command{targets, printAST, maybeDotPath} <- getCommand
blocks <- parseInput @Block targets blocks <- parseInput @Block targets
let
act block = do
let res@(Rubyhs.References.Result x) = Rubyhs.References.references $ block
putEncoded res
ByteString.putStrLn . Aeson.encode . toJSONForest . Rubyhs.References.graph $ block
case maybeDotPath of
Nothing -> pure ()
Just dotPath -> drawDot dotPath x
if printAST if printAST
then traverse_ @[] putEncoded blocks then traverse_ @[] putEncoded blocks
else traverse_ act blocks else traverse_ act blocks
where
act block = do
let res@(Rubyhs.References.Result x) = Rubyhs.References.references $ block
putEncoded res
ByteString.putStrLn . Aeson.encode . toJSONForest . Rubyhs.References.graph $ block
drawDot x
graphOut :: FilePath drawDot :: FilePath -> Map Rubyhs.References.Node (Set Rubyhs.References.Node) -> IO ()
graphOut = "out.dot" drawDot p
= Text.writeFile p
drawDot :: Map Rubyhs.References.Node (Set Rubyhs.References.Node) -> IO ()
drawDot
= Text.writeFile graphOut
. Graphviz.digraph . Graphviz.digraph
. fmap (fmap (qoutes . Rubyhs.References.prettyContext) . \(x, xs) -> x :| toList xs) . fmap (fmap (qoutes . Rubyhs.References.prettyContext) . \(x, xs) -> x :| toList xs)
. toList . toList
@ -61,17 +60,24 @@ putEncoded :: ToJSON a => a -> IO ()
putEncoded = ByteString.putStrLn . Aeson.encode putEncoded = ByteString.putStrLn . Aeson.encode
data Command = Command data Command = Command
{ targets :: [FilePath] { targets :: [FilePath]
, printAST :: Bool , printAST :: Bool
, maybeDotPath :: Maybe FilePath
} }
command :: Parser Command command :: Parser Command
command = Command <$> targets <*> printAST command = Command <$> targets <*> printAST <*> 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 "print-ast"
<> Options.help "Print AST and exit" <> Options.help "Print AST and exit"
dotPath
= Options.optional
$ Options.strOption
$ Options.long "dot"
<> Options.metavar "PATH"
<> Options.help "Write dot graph"
getCommand :: IO Command getCommand :: IO Command
getCommand = Options.execParser opts getCommand = Options.execParser opts