1
1
mirror of https://github.com/github/semantic.git synced 2025-01-05 14:11:33 +03:00

Add imports and calls flags to graph

This commit is contained in:
joshvera 2018-05-02 11:39:15 -04:00
parent 7b34ffd384
commit acdf31430e
3 changed files with 25 additions and 33 deletions

View File

@ -24,14 +24,14 @@ module Rendering.Renderer
, defaultSymbolFields , defaultSymbolFields
) where ) where
import Prologue
import Data.Aeson (Value) import Data.Aeson (Value)
import Data.Output import Data.Output
import Prologue
import Rendering.DOT as R import Rendering.DOT as R
import Rendering.Imports as R
import Rendering.JSON as R import Rendering.JSON as R
import Rendering.SExpression as R import Rendering.SExpression as R
import Rendering.Symbol as R import Rendering.Symbol as R
import Rendering.Imports as R
import Rendering.TOC as R import Rendering.TOC as R
-- | Specification of renderers for diffs, producing output in the parameter type. -- | Specification of renderers for diffs, producing output in the parameter type.
@ -68,10 +68,8 @@ deriving instance Show (TermRenderer output)
-- | Specification of renderers for graph analysis, producing output in the parameter type. -- | Specification of renderers for graph analysis, producing output in the parameter type.
data GraphRenderer output where data GraphRenderer output where
JSONImportGraphRenderer :: GraphRenderer ByteString JSONGraphRenderer :: GraphRenderer ByteString
DOTImportGraphRenderer :: GraphRenderer ByteString DOTGraphRenderer :: GraphRenderer ByteString
JSONCallGraphRenderer :: GraphRenderer ByteString
DOTCallGraphRenderer :: GraphRenderer ByteString
deriving instance Eq (GraphRenderer output) deriving instance Eq (GraphRenderer output)
deriving instance Show (GraphRenderer output) deriving instance Show (GraphRenderer output)

View File

@ -17,7 +17,7 @@ import qualified Paths_semantic as Library (version)
import Prologue import Prologue
import Rendering.Renderer import Rendering.Renderer
import qualified Semantic.Diff as Semantic (diffBlobPairs) import qualified Semantic.Diff as Semantic (diffBlobPairs)
import qualified Semantic.Graph as Semantic (graph) import Semantic.Graph as Semantic (graph, GraphType(..))
import Semantic.IO (languageForFilePath) import Semantic.IO (languageForFilePath)
import qualified Semantic.Log as Log import qualified Semantic.Log as Log
import qualified Semantic.Parse as Semantic (parseBlobs) import qualified Semantic.Parse as Semantic (parseBlobs)
@ -34,8 +34,8 @@ runDiff (SomeRenderer diffRenderer) = Semantic.diffBlobPairs diffRenderer <=< Ta
runParse :: SomeRenderer TermRenderer -> Either Handle [File] -> Task.TaskEff ByteString runParse :: SomeRenderer TermRenderer -> Either Handle [File] -> Task.TaskEff ByteString
runParse (SomeRenderer parseTreeRenderer) = Semantic.parseBlobs parseTreeRenderer <=< Task.readBlobs runParse (SomeRenderer parseTreeRenderer) = Semantic.parseBlobs parseTreeRenderer <=< Task.readBlobs
runGraph :: SomeRenderer GraphRenderer -> Maybe FilePath -> FilePath -> Language -> [FilePath] -> Task.TaskEff ByteString runGraph :: Semantic.GraphType -> SomeRenderer GraphRenderer -> Maybe FilePath -> FilePath -> Language -> [FilePath] -> Task.TaskEff ByteString
runGraph (SomeRenderer r) rootDir dir excludeDirs = Semantic.graph r <=< Task.readProject rootDir dir excludeDirs runGraph graphType (SomeRenderer r) rootDir dir excludeDirs = Semantic.graph graphType r <=< Task.readProject rootDir dir excludeDirs
-- | A parser for the application's command-line arguments. -- | A parser for the application's command-line arguments.
-- --
@ -56,7 +56,7 @@ arguments = info (version <*> helper <*> ((,) <$> optionsParser <*> argumentsPar
pure $ Log.Options disableColour logLevel requestId False False Log.logfmtFormatter 0 failOnWarning pure $ Log.Options disableColour logLevel requestId False False Log.logfmtFormatter 0 failOnWarning
argumentsParser = do argumentsParser = do
subparser <- hsubparser (diffCommand <> parseCommand <> importGraphCommand <> callGraphCommand) subparser <- hsubparser (diffCommand <> parseCommand <> graphCommand)
output <- Right <$> strOption (long "output" <> short 'o' <> help "Output path, defaults to stdout") <|> pure (Left stdout) output <- Right <$> strOption (long "output" <> short 'o' <> help "Output path, defaults to stdout") <|> pure (Left stdout)
pure $ subparser >>= Task.writeToOutput output pure $ subparser >>= Task.writeToOutput output
@ -84,23 +84,16 @@ arguments = info (version <*> helper <*> ((,) <$> optionsParser <*> argumentsPar
filesOrStdin <- Right <$> some (argument filePathReader (metavar "FILES...")) <|> pure (Left stdin) filesOrStdin <- Right <$> some (argument filePathReader (metavar "FILES...")) <|> pure (Left stdin)
pure $ runParse renderer filesOrStdin pure $ runParse renderer filesOrStdin
importGraphCommand = command "import-graph" (info importGraphArgumentsParser (progDesc "Compute an import graph for a directory or entry point")) graphCommand = command "graph" (info graphArgumentsParser (progDesc "Compute a graph for a directory or entry point"))
importGraphArgumentsParser = do graphArgumentsParser = do
renderer <- flag (SomeRenderer DOTImportGraphRenderer) (SomeRenderer DOTImportGraphRenderer) (long "dot" <> help "Output in DOT graph format (default)") graphType <- flag ImportGraph ImportGraph (long "imports" <> help "Compute an import graph")
<|> flag' (SomeRenderer JSONImportGraphRenderer) (long "json" <> help "Output JSON graph") <|> flag' CallGraph (long "calls" <> help "Compute a call graph")
renderer <- flag (SomeRenderer DOTGraphRenderer) (SomeRenderer DOTGraphRenderer) (long "dot" <> help "Output in DOT graph format (default)")
<|> flag' (SomeRenderer JSONGraphRenderer) (long "json" <> help "Output JSON graph")
rootDir <- rootDirectoryOption rootDir <- rootDirectoryOption
excludeDirs <- excludeDirsOption excludeDirs <- excludeDirsOption
File{..} <- argument filePathReader (metavar "DIRECTORY:LANGUAGE") File{..} <- argument filePathReader (metavar "DIRECTORY:LANGUAGE")
pure $ runGraph renderer rootDir filePath (fromJust fileLanguage) excludeDirs pure $ runGraph graphType renderer rootDir filePath (fromJust fileLanguage) excludeDirs
callGraphCommand = command "call-graph" (info callGraphArgumentsParser (progDesc "Compute a call graph for a directory or entry point"))
callGraphArgumentsParser = do
renderer <- flag (SomeRenderer DOTCallGraphRenderer) (SomeRenderer DOTCallGraphRenderer) (long "dot" <> help "Output in DOT graph format (default)")
<|> flag' (SomeRenderer JSONCallGraphRenderer) (long "json" <> help "Output JSON graph")
rootDir <- rootDirectoryOption
excludeDirs <- many (strOption (long "exclude-dir" <> help "Exclude a directory (e.g. vendor)"))
File{..} <- argument filePathReader (metavar "DIRECTORY:LANGUAGE")
pure $ runGraph renderer rootDir filePath (fromJust fileLanguage) excludeDirs
rootDirectoryOption = optional (strOption (long "root" <> help "Root directory of project. Optional, defaults to entry file/directory." <> metavar "DIRECTORY")) rootDirectoryOption = optional (strOption (long "root" <> help "Root directory of project. Optional, defaults to entry file/directory." <> metavar "DIRECTORY"))
excludeDirsOption = many (strOption (long "exclude-dir" <> help "Exclude a directory (e.g. vendor)")) excludeDirsOption = many (strOption (long "exclude-dir" <> help "Exclude a directory (e.g. vendor)"))

View File

@ -29,22 +29,23 @@ import Rendering.Renderer
import Semantic.IO (Files) import Semantic.IO (Files)
import Semantic.Task import Semantic.Task
data GraphType = ImportGraph | CallGraph
graph :: Members '[Distribute WrappedTask, Files, Task, Exc SomeException, Telemetry] effs graph :: Members '[Distribute WrappedTask, Files, Task, Exc SomeException, Telemetry] effs
=> GraphRenderer output => GraphType
-> GraphRenderer output
-> Project -> Project
-> Eff effs ByteString -> Eff effs ByteString
graph renderer project graph graphType renderer project
| SomeAnalysisParser parser prelude <- someAnalysisParser | SomeAnalysisParser parser prelude <- someAnalysisParser
(Proxy :: Proxy '[ Analysis.Evaluatable, Analysis.Declarations1, FreeVariables1, Functor, Eq1, Ord1, Show1 ]) (projectLanguage project) = do (Proxy :: Proxy '[ Analysis.Evaluatable, Analysis.Declarations1, FreeVariables1, Functor, Eq1, Ord1, Show1 ]) (projectLanguage project) = do
package <- parsePackage parser prelude project package <- parsePackage parser prelude project
let graph = case renderer of let graph = case graphType of
JSONCallGraphRenderer -> graphCalls ImportGraph -> graphImports
DOTCallGraphRenderer -> graphCalls CallGraph -> graphCalls
_ -> graphImports
graph package >>= case renderer of graph package >>= case renderer of
JSONCallGraphRenderer -> pure . toOutput JSONGraphRenderer -> pure . toOutput
JSONImportGraphRenderer -> pure . toOutput DOTGraphRenderer -> pure . renderGraph
_ -> pure . renderGraph
-- | Parse a list of files into a 'Package'. -- | Parse a list of files into a 'Package'.
parsePackage :: Members '[Distribute WrappedTask, Files, Task] effs parsePackage :: Members '[Distribute WrappedTask, Files, Task] effs