mirror of
https://github.com/github/semantic.git
synced 2024-11-24 08:54:07 +03:00
commit
271c4af962
26
app/Main.hs
26
app/Main.hs
@ -14,18 +14,21 @@ import Control.Comonad.Cofree
|
||||
import qualified Data.ByteString.Char8 as B1
|
||||
import qualified Data.ByteString.Lazy as B2
|
||||
import Options.Applicative
|
||||
import System.Directory
|
||||
import System.FilePath
|
||||
import qualified System.IO as IO
|
||||
|
||||
data Output = Unified | Split
|
||||
data Renderer = Unified | Split
|
||||
|
||||
data Argument = Argument { output :: Output, sourceA :: FilePath, sourceB :: FilePath }
|
||||
data Argument = Argument { renderer :: Renderer, output :: Maybe FilePath, sourceA :: FilePath, sourceB :: FilePath }
|
||||
|
||||
arguments :: Parser Argument
|
||||
arguments = Argument
|
||||
<$> (flag Split Unified (long "unified" <> help "output a unified diff")
|
||||
<|> flag' Split (long "split" <> help "output a split diff"))
|
||||
<*> argument str (metavar "FILE a")
|
||||
<*> argument str (metavar "FILE b")
|
||||
<*> (optional $ strOption (long "output" <> short 'o' <> help "output directory for split diffs, defaulting to stdout if unspecified"))
|
||||
<*> strArgument (metavar "FILE a")
|
||||
<*> strArgument (metavar "FILE b")
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
@ -38,16 +41,21 @@ main = do
|
||||
bTerm <- parse bContents
|
||||
return (replaceLeavesWithWordBranches aContents aTerm, replaceLeavesWithWordBranches bContents bTerm)
|
||||
let diff = interpret comparable aTerm bTerm in
|
||||
case output arguments of
|
||||
case renderer arguments of
|
||||
Unified -> do
|
||||
output <- unified diff aContents bContents
|
||||
B1.putStr output
|
||||
rendered <- unified diff aContents bContents
|
||||
B1.putStr rendered
|
||||
Split -> do
|
||||
output <- split diff aContents bContents
|
||||
B2.putStr output
|
||||
rendered <- split diff aContents bContents
|
||||
case output arguments of
|
||||
Just path -> do
|
||||
isDir <- doesDirectoryExist path
|
||||
IO.withFile (if isDir then path </> (takeFileName sourceBPath -<.> ".html") else path) IO.WriteMode (write rendered)
|
||||
Nothing -> B2.putStr rendered
|
||||
where
|
||||
opts = info (helper <*> arguments)
|
||||
(fullDesc <> progDesc "Diff some things" <> header "semantic-diff - diff semantically")
|
||||
write rendered h = B2.hPut h rendered
|
||||
|
||||
parserForType :: String -> P.Parser
|
||||
parserForType mediaType = maybe P.lineByLineParser parseTreeSitterFile $ case mediaType of
|
||||
|
@ -50,6 +50,7 @@ executable semantic-diff-exe
|
||||
, bytestring
|
||||
, optparse-applicative
|
||||
, filepath
|
||||
, directory
|
||||
default-language: Haskell2010
|
||||
extra-libraries: bridge
|
||||
extra-lib-dirs: .
|
||||
@ -69,6 +70,7 @@ executable semantic-diff-profile
|
||||
, bytestring
|
||||
, optparse-applicative
|
||||
, filepath
|
||||
, directory
|
||||
default-language: Haskell2010
|
||||
extra-libraries: bridge
|
||||
extra-lib-dirs: .
|
||||
|
Loading…
Reference in New Issue
Block a user