rubyhs/src/Rubyhs.hs

47 lines
1.5 KiB
Haskell
Raw Normal View History

2019-09-26 21:45:14 +00:00
{-# LANGUAGE DuplicateRecordFields, OverloadedLists #-}
{-# OPTIONS_GHC -Wall #-}
module Rubyhs (main) where
2019-09-27 16:32:01 +00:00
import Control.Monad.Fail (MonadFail)
import Data.Aeson (eitherDecode, encode)
2019-09-26 21:45:14 +00:00
import Data.ByteString.Lazy (ByteString)
2019-09-27 16:32:01 +00:00
import Data.Foldable (traverse_)
import Data.Language.Ruby (Block)
import Frelude
import System.Environment (getArgs)
import System.Process (readProcess)
2019-09-26 21:45:14 +00:00
import qualified Data.ByteString.Lazy.Char8 as ByteString
2019-10-11 09:08:36 +00:00
import Rubyhs.References (references)
import qualified Rubyhs.References as References
import Data.Text.Prettyprint.Doc.Render.Text (renderStrict)
import Data.Text.Prettyprint.Doc (layoutCompact)
import Data.HashMap.Strict (HashMap)
import GHC.Exts (fromList, toList)
2019-09-26 21:45:14 +00:00
main :: IO ()
main = getArgs >>= traverse_ run
run :: FilePath -> IO ()
run p = do
json <- runParser p
block <- decodeFail @_ @Block $ ByteString.pack json
ByteString.putStrLn $ encode block
2019-10-11 09:08:36 +00:00
let References.Env{declarations, applications} = references block
printEm declarations
printEm applications
where
go (x, y) = (renderStrict $ layoutCompact $ pretty x, y)
printEm x = ByteString.putStrLn $ encode $ fromList @(HashMap _ _) $ go <$> toList x
2019-09-26 21:45:14 +00:00
decodeFail :: MonadFail m => FromJSON a => ByteString -> m a
decodeFail s = case eitherDecode s of
2019-10-11 09:08:36 +00:00
Left err -> Frelude.fail err
2019-09-26 21:45:14 +00:00
Right a -> pure a
runParser :: FilePath -> IO String
runParser p = sh "ruby-parse" ["--emit-json", "--25", p]
sh :: String -> [String] -> IO String
sh cmd args = readProcess cmd args mempty