Skip to content

Instantly share code, notes, and snippets.

@cdepillabout
Last active Aug 29, 2015
Embed
What would you like to do?
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