
module Language.R.Package.Meta (
    rVersion
  , packageVersion
  ) where

import System.Exit
import Text.Printf
import qualified Data.Text as T
import Data.Attoparsec.Text as A

import Language.R.Cmd
import Language.R.Package.Description

rVersion :: IO (Either String Version)
rVersion = do
  (e, out, err) <- rscriptRead "writeLines(format(getRversion()), sep = '')"
  if e == ExitSuccess
    then return $ A.parseOnly parseVersion (T.pack out)
    else return $ Left err

packageVersion :: String -> IO (Either String Version)
packageVersion pkg = do
  (e, out, err) <- rscriptRead $ printf "writeLines(format(utils::packageVersion('%v')), sep = '')" pkg
  if e == ExitSuccess
    then return $ A.parseOnly parseVersion (T.pack out)
    else return $ Left err
