Created
May 17, 2013 03:11
-
-
Save mmhelloworld/5596670 to your computer and use it in GitHub Desktop.
Run a process (drip launching clojure here for example) using Frege
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module hellofrege.ProcessTest where | |
data ProcessBuilder = mutable native java.lang.ProcessBuilder where | |
native new :: MutableIO StringArr -> IO ProcessBuilder | |
native redirectErrorStream :: ProcessBuilder -> Bool -> IO () | |
native start :: ProcessBuilder -> IO Process throws IOException | |
native directory :: ProcessBuilder -> MutableIO File -> IO ProcessBuilder | |
data Process = mutable native java.lang.Process where | |
native getOutputStream :: Process -> IO OutputStream | |
native getInputStream :: Process -> IO InputStream | |
fromListST :: [String] -> STMutable u StringArr | |
fromListST strings = (StringArr.new strings.length >>= loop 0 strings) where | |
loop j (x:xs) arr = do StringArr.setAt arr j x; loop (j+1) xs arr | |
loop j [] arr = return arr | |
fromList strings = ST.run (fromListST strings >>= readonly id) | |
getOut :: InputStream -> IO [String] | |
getOut ins = do | |
isr <- InputStreamReader.new ins "utf-8" | |
br <- BufferedReader.new isr | |
br.getLines | |
main _ = do | |
let cmd = ["drip", "-jar", "clojure-1.5.1.jar", "-e", "(+ 1 2 3)"] | |
pb <- fromListST cmd >>= ProcessBuilder.new | |
dir <- File.new "bin/clojure-1.5.1" | |
pb.directory dir | |
pb.redirectErrorStream true | |
process <- pb.start | |
pout <- process.getInputStream | |
output <- getOut pout | |
mapM_ println output | |
`catch` ioe where | |
ioe :: IOException -> IO () | |
ioe e = println e.getMessage | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment