process-qq (empty) → 0.2.0
raw patch · 6 files changed
+201/−0 lines, 6 filesdep +basedep +bytestringdep +enumeratorsetup-changed
Dependencies added: base, bytestring, enumerator, mtl, process, shakespeare-text, template-haskell, text
Files
- LICENSE +30/−0
- README.md +51/−0
- Setup.hs +2/−0
- System/Process/QQ.hs +76/−0
- Test.hs +9/−0
- process-qq.cabal +33/−0
+ LICENSE view
@@ -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.
+ README.md view
@@ -0,0 +1,51 @@+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 three quasi-quoters, `cmd`, `lcmd` and `enumCmd`.++The result type of `cmd` is `(Strict) ByteString`,+`lcmd` 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}+{-# LANGUAGE QuasiQuotes #-}+import System.Process.QQ++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}|]+~~~
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ System/Process/QQ.hs view
@@ -0,0 +1,76 @@+{-# LANGUAGE TemplateHaskell #-}++module System.Process.QQ (+ cmd,+ lcmd,+ enumCmd,+ ) where++import Control.Applicative+import Control.Exception+import Control.Monad+import Control.Monad.Trans+import qualified Data.ByteString as B+import qualified Data.ByteString.Lazy as BL+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 }++lcmd :: QuasiQuoter+lcmd = def { quoteExp = genLCmd }++enumCmd :: QuasiQuoter+enumCmd = def { quoteExp = genEnumCmd }++genCmd :: String -> ExpQ+genCmd str =+ [| E.run_ $ enumProcess $(quoteExp lt str) $$ do+ (B.concat . BL.toChunks <$> EB.consume)+ |]++genLCmd :: String -> ExpQ+genLCmd 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, ph) <- liftIO $ openProcess s+ r <- EB.enumHandle 65536 h step+ r `seq` checkRet ph+ return r++openProcess :: LT.Text -> IO (Handle, ProcessHandle)+openProcess s = do+ (Just g, Just h, _, ph) <- createProcess (shell $ LT.unpack s)+ { std_in = CreatePipe+ , std_out = CreatePipe+ , std_err = Inherit }+ hClose g+ return (h, ph)++checkRet :: MonadIO m => ProcessHandle -> E.Iteratee a m ()+checkRet ph = liftIO $ do+ ec <- waitForProcess ph+ when (ec /= ExitSuccess) $ do+ throwIO ec
+ Test.hs view
@@ -0,0 +1,9 @@+{-# LANGUAGE QuasiQuotes #-}++import System.Process.QQ+import System.Environment++main :: IO ()+main = do+ [url] <- getArgs+ print =<< [cmd|curl #{url}|]
+ process-qq.cabal view
@@ -0,0 +1,33 @@+Name: process-qq+Version: 0.2.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