shell-monad 0.0.2 → 0.0.3
raw patch · 3 files changed
+39/−21 lines, 3 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Control.Monad.Shell: add :: Expr -> Script ()
- Control.Monad.Shell: data Expr
- Control.Monad.Shell: data Q
- Control.Monad.Shell: indent :: Expr -> Expr
- Control.Monad.Shell: instance CmdArg Q
- Control.Monad.Shell: instance Eq Q
- Control.Monad.Shell: instance Monoid Q
- Control.Monad.Shell: instance Ord Q
- Control.Monad.Shell: instance Show Q
+ Control.Monad.Shell: Output :: (Script ()) -> Output
+ Control.Monad.Shell: class CmdArg a
+ Control.Monad.Shell: data Quoted a
+ Control.Monad.Shell: instance CmdArg (Quoted Text)
+ Control.Monad.Shell: instance CmdArg Output
+ Control.Monad.Shell: instance Eq a => Eq (Quoted a)
+ Control.Monad.Shell: instance Monoid a => Monoid (Quoted a)
+ Control.Monad.Shell: instance Ord a => Ord (Quoted a)
+ Control.Monad.Shell: instance Show a => Show (Quoted a)
+ Control.Monad.Shell: newtype Output
- Control.Monad.Shell: quote :: Text -> Q
+ Control.Monad.Shell: quote :: Text -> Quoted Text
- Control.Monad.Shell: val :: Var -> Q
+ Control.Monad.Shell: val :: Var -> Quoted Text
Files
- CHANGELOG +8/−0
- Control/Monad/Shell.hs +30/−20
- shell-monad.cabal +1/−1
CHANGELOG view
@@ -1,3 +1,11 @@+shell-monad (0.0.3) unstable; urgency=medium++ * Added Output data type, which alows cmd to accept (Output (Script ()))+ as a parameter.+ * Better Quoted data type.++ -- Joey Hess <id@joeyh.name> Thu, 25 Dec 2014 12:07:04 -0400+ shell-monad (0.0.2) unstable; urgency=medium * Better constructing of pipes.
Control/Monad/Shell.hs view
@@ -4,6 +4,7 @@ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE DeriveFunctor #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-} module Control.Monad.Shell ( Script,@@ -11,13 +12,12 @@ linearScript, Var, val,- Q,+ Quoted, quote,- Expr,- indent, run, cmd,- add,+ CmdArg,+ Output(..), comment, newVar, newVarContaining,@@ -45,23 +45,19 @@ deriving (Eq, Ord, Show) -- | Expand a shell variable to its value.-val :: Var -> Q+val :: Var -> Quoted L.Text val (Var v) = Q ("\"$" <> v <> "\"") --- | A piece of text that is safely quoted.-newtype Q = Q { getQ :: L.Text }- deriving (Eq, Ord, Show)--instance Monoid Q where- mempty = Q L.empty- mappend (Q a) (Q b) = Q (a <> b)+-- | A value that is safely quoted.+newtype Quoted a = Q { getQ :: a }+ deriving (Eq, Ord, Show, Monoid) -- | Quotes the value to allow it to be safely exposed to the shell. -- -- The method used is to replace ' with '"'"' and wrap the value inside -- single quotes. This works for POSIX shells, as well as other shells -- like csh.-quote :: L.Text -> Q+quote :: L.Text -> Quoted L.Text quote t | L.all (isAlphaNum) t = Q t | otherwise = Q $ q <> L.intercalate "'\"'\"'" (L.splitOn q t) <> q@@ -167,9 +163,7 @@ -- | Variadic argument version of 'run'. ----- The command can be passed any number of arguments.--- As well as passing Text and Q arguments, it also accepts Var arguments,--- which passes the value of a shell variable to the command.+-- The command can be passed any number of CmdArgs. -- -- Convenient usage of 'cmd' requires the following: --@@ -192,15 +186,22 @@ class CmdArg a where toTextArg :: a -> L.Text +-- | Text arguments are automatically quoted. instance CmdArg L.Text where toTextArg = getQ . quote +-- | Var arguments cause the (quoted) value of a shell variable to be+-- passed to the command. instance CmdArg Var where toTextArg v = toTextArg (val v) -instance CmdArg Q where+-- | Quoted Text arguments are passed as-is.+instance CmdArg (Quoted L.Text) where toTextArg (Q v) = v +instance CmdArg Output where+ toTextArg (Output s) = "\"$(" <> linearScript s <> ")\""+ class ShellCmd t where cmdAll :: L.Text -> [L.Text] -> t @@ -210,6 +211,15 @@ instance (f ~ ()) => ShellCmd (Script f) where cmdAll c acc = add $ Cmd $ L.intercalate " " (c:reverse acc) +-- | The output of a command, or even a more complicated Script+-- can be passed as a parameter to 'cmd'+--+-- Examples:+--+-- > cmd "echo" "hello there," (Output (cmd "whoami"))+-- > cmd "echo" "root's pwent" (Output (cmd "cat" "/etc/passwd" -|- cmd "grep" "root"))+newtype Output = Output (Script ())+ -- | Adds an Expr to the script. add :: Expr -> Script () add expr = Script $ \env -> ([expr], env, ())@@ -221,8 +231,8 @@ -- | Defines a new shell variable. -- -- The name of the variable that appears in the shell script will be based--- on provided name, but each call to newVar will generate a new, unique--- variable name.+-- on provided name (which can be mempty), but each call to newVar will+-- generate a new, unique variable name. newVar :: L.Text -- ^ base of variable name -> Script Var@@ -347,7 +357,7 @@ add $ Cmd $ word <> " :" mapM_ (add . indent) =<< runM s --- | Generates shell code to read a variable from stdin.+-- | Generates shell code to fill a variable with a line read from stdin. readVar :: Var -> Script () readVar (Var vname) = add $ Cmd $ "read " <> getQ (quote vname)
shell-monad.cabal view
@@ -1,5 +1,5 @@ Name: shell-monad-Version: 0.0.2+Version: 0.0.3 Cabal-Version: >= 1.8 License: BSD3 Maintainer: Joey Hess <id@joeyh.name>