Browse files

init version

  • Loading branch information...
0 parents commit 9c8ad50c2249bce51adc26e0eb8e2319dcf50464 @tanakh committed Dec 2, 2011
Showing with 184 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +30 −0 LICENSE
  3. +47 −0 README.md
  4. +2 −0 Setup.hs
  5. +57 −0 System/Process/QQ.hs
  6. +9 −0 Test.hs
  7. +33 −0 process-qq.cabal
6 .gitignore
@@ -0,0 +1,6 @@
+cabal-dev/
+dist/
+*.o
+*.hi
+*~
+*#
30 LICENSE
@@ -0,0 +1,30 @@
+Copyright (c)2011, Hideyuki Tanaka
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ * Neither the name of Hideyuki Tanaka nor the names of other
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 README.md
@@ -0,0 +1,47 @@
+process-qq: A Quasi-Quoter to execute processes
+===============================================
+
+# About
+
+This is a simple package for executing external process using quasi-quoters.
+
+# Install
+
+~~~ {.bash}
+$ cabal update
+$ cabal install process-qq
+~~~
+
+# API
+
+process-qq has two quasi-quoters, `cmd` and `enumCmd`.
+
+The result type of `cmd` is Lazy ByteString,
+`enumCmd`'s is `MonadIO m => Enumerator ByteString m a`.
+
+Command is failed, an Exception is thrown.
+
+Command is executed in ***run-time***, not compile-time.
+
+# Example
+
+* Invoke a process simply
+
+~~~ {.haskell}
+main = print =<< [cmd|ls|]
+~~~
+
+* Enumerate a process
+
+~~~ {.haskell}
+main =
+ run_ $ [enumCmd|curl http://www.google.com/|] $$ iterHandle stdout
+~~~
+
+* Unquoting (syntax is same as [shakespeare-text](http://hackage.haskell.org/package/shakespeare-text))
+
+~~~ {.haskell}
+main = do
+ [url] <- getArgs
+ print =<< [cmd|curl #{url}|]
+~~~
2 Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
57 System/Process/QQ.hs
@@ -0,0 +1,57 @@
+{-# LANGUAGE TemplateHaskell #-}
+
+module System.Process.QQ (
+ cmd,
+ enumCmd,
+ ) where
+
+import Control.Concurrent
+import Control.Exception
+import Control.Monad
+import Control.Monad.Trans
+import qualified Data.ByteString as B
+import Data.Enumerator as E
+import Data.Enumerator.Binary as EB
+import qualified Data.Text.Lazy as LT
+import Language.Haskell.TH
+import Language.Haskell.TH.Quote
+import System.Exit
+import System.IO
+import System.Process
+import Text.Shakespeare.Text
+
+def :: QuasiQuoter
+def = QuasiQuoter {
+ quoteExp = undefined,
+ quotePat = undefined,
+ quoteType = undefined,
+ quoteDec = undefined
+ }
+
+cmd :: QuasiQuoter
+cmd = def { quoteExp = genCmd }
+
+enumCmd :: QuasiQuoter
+enumCmd = def { quoteExp = genEnumCmd }
+
+genCmd :: String -> ExpQ
+genCmd str =
+ [| E.run_ $ enumProcess $(quoteExp lt str) $$ EB.consume |]
+
+genEnumCmd :: String -> ExpQ
+genEnumCmd str =
+ [| enumProcess $(quoteExp lt str) |]
+
+enumProcess :: MonadIO m => LT.Text -> E.Enumerator B.ByteString m a
+enumProcess s step = do
+ h <- liftIO $ openProcess s
+ EB.enumHandle 65536 h step
+
+openProcess :: LT.Text -> IO Handle
+openProcess s = do
+ (_, Just h, _, ph) <- createProcess (shell $ LT.unpack s) { std_out = CreatePipe }
+ _ <- forkIO $ do
+ ec <- waitForProcess ph
+ when (ec /= ExitSuccess) $ do
+ throwIO ec
+ return h
9 Test.hs
@@ -0,0 +1,9 @@
+{-# LANGUAGE QuasiQuotes #-}
+
+import System.Process.QQ
+import System.Environment
+
+main :: IO ()
+main = do
+ [url] <- getArgs
+ print =<< [cmd|curl #{url}|]
33 process-qq.cabal
@@ -0,0 +1,33 @@
+Name: process-qq
+Version: 0.1.0
+Synopsis: Quasi-Quoters for exec process
+Description: Quasi-Quoters for exec process
+Homepage: http://github.com/tanakh/process-qq
+License: BSD3
+License-file: LICENSE
+Author: Hideyuki Tanaka
+Maintainer: Hideyuki Tanaka <tanaka.hideyuki@gmail.com>
+Copyright: (c) 2011, Hideyuki Tanaka
+Category: System
+Build-type: Simple
+
+Extra-source-files: README.md
+ Test.hs
+
+Cabal-version: >=1.8
+
+Library
+ Exposed-modules: System.Process.QQ
+
+ Build-depends: base >= 4 && < 5
+ , template-haskell >= 2.4 && < 2.7
+ , mtl >= 2.0 && < 2.1
+ , bytestring >= 0.9 && < 0.10
+ , text >= 0.11 && < 0.12
+ , process >= 1.0 && < 1.1
+ , enumerator >= 0.4.16 && < 0.5
+ , shakespeare-text >= 0.10 && < 0.11
+
+Source-repository head
+ Type: git
+ Location: git://github.com/tanakh/process-qq.git

0 comments on commit 9c8ad50

Please sign in to comment.