diff --git a/src/Rubyhs.hs b/src/Rubyhs.hs index 46a38a5..698ed63 100644 --- a/src/Rubyhs.hs +++ b/src/Rubyhs.hs @@ -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 + 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 then traverse_ @[] putEncoded 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 -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 @@ -61,17 +60,24 @@ putEncoded :: ToJSON a => a -> IO () putEncoded = ByteString.putStrLn . Aeson.encode data Command = Command - { targets :: [FilePath] - , printAST :: Bool + { 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