Add CLI option for dot path
This commit is contained in:
parent
5ae9b28d10
commit
81b058f033
|
@ -19,24 +19,23 @@ import qualified Rubyhs.References
|
|||
|
||||
main :: IO ()
|
||||
main = do
|
||||
Command{targets, printAST} <- getCommand
|
||||
Command{targets, printAST, maybeDotPath} <- getCommand
|
||||
blocks <- parseInput @Block targets
|
||||
if printAST
|
||||
then traverse_ @[] putEncoded blocks
|
||||
else traverse_ act blocks
|
||||
where
|
||||
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
|
||||
drawDot x
|
||||
case maybeDotPath of
|
||||
Nothing -> pure ()
|
||||
Just dotPath -> drawDot dotPath x
|
||||
if printAST
|
||||
then traverse_ @[] putEncoded blocks
|
||||
else traverse_ act blocks
|
||||
|
||||
graphOut :: FilePath
|
||||
graphOut = "out.dot"
|
||||
|
||||
drawDot :: Map Rubyhs.References.Node (Set Rubyhs.References.Node) -> IO ()
|
||||
drawDot
|
||||
= Text.writeFile graphOut
|
||||
drawDot :: FilePath -> Map Rubyhs.References.Node (Set Rubyhs.References.Node) -> IO ()
|
||||
drawDot p
|
||||
= Text.writeFile p
|
||||
. Graphviz.digraph
|
||||
. fmap (fmap (qoutes . Rubyhs.References.prettyContext) . \(x, xs) -> x :| toList xs)
|
||||
. toList
|
||||
|
@ -63,15 +62,22 @@ putEncoded = ByteString.putStrLn . Aeson.encode
|
|||
data Command = Command
|
||||
{ targets :: [FilePath]
|
||||
, printAST :: Bool
|
||||
, maybeDotPath :: Maybe FilePath
|
||||
}
|
||||
|
||||
command :: Parser Command
|
||||
command = Command <$> targets <*> printAST
|
||||
command = Command <$> targets <*> printAST <*> dotPath
|
||||
where
|
||||
targets = many (Options.argument Options.str (Options.metavar "TARGET"))
|
||||
printAST = Options.switch
|
||||
$ Options.long "print-ast"
|
||||
<> 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 = Options.execParser opts
|
||||
|
|
Loading…
Reference in a new issue