Skip to content

Instantly share code, notes, and snippets.

@cdepillabout
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdepillabout/f38795b9107bc30d1b7d to your computer and use it in GitHub Desktop.
Save cdepillabout/f38795b9107bc30d1b7d to your computer and use it in GitHub Desktop.
sample for using stack to make a .hs executable and run it from the command line
#!/usr/bin/env stack
-- stack --resolver=nightly-2015-07-08 runghc --package=shelly
-- The ExtendedDefaultRules extension gives us an experience similar to
-- @ghci@. Raw values (1 and 10 below) that implement the 'Num' type
-- class will be defaulted to 'Int' (specified with the @default@
-- command below). Raw values (all strings) that implement the
-- 'IsString' typeclass will be defaulted to Text (also specified
-- with the @default@ command below).
--
-- Without this extension turned on, ghc will produce errors like this:
-- @
-- code.hs:27:25:
-- No instance for (Data.String.IsString a0)
-- arising from the literal ‘"-alF"’
-- @
{-# LANGUAGE ExtendedDefaultRules #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wall #-}
-- Don't warn that things (like strings and numbers) are being defaulted to
-- certain types. It's okay because this is just shell programming.
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
import Control.Monad (forM_, void)
import qualified Data.Text as T
import Shelly ((</>), (<.>), (-|-), cmd, inspect, shelly, touchfile, withTmpDir)
-- Define the the types that should be defaulted to. We can define one
-- type for string-like things, and one type for integer-like things. It
-- doesn't matter what order they are in.
default (T.Text, Int)
main :: IO ()
main = shelly $
withTmpDir $ \temp -> do
forM_ [1..10] $ \i ->
touchfile $ temp </> show i <.> "txt"
inspect temp
void $ cmd "ls" "-alF" temp
void $ cmd "find" temp
void $ cmd "echo" "here is my try at grepping:"
void $ cmd "ls" "-alF" temp -|- cmd "grep" "10\\.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment