Skip to content

Instantly share code, notes, and snippets.

@EugeneN
Last active March 7, 2016 04:59
Show Gist options
  • Save EugeneN/c1dada5c9c93a8fd2088 to your computer and use it in GitHub Desktop.
Save EugeneN/c1dada5c9c93a8fd2088 to your computer and use it in GitHub Desktop.
Unix shell scripting in Haskell example
#!/usr/bin/env runhaskell
{-# LANGUAGE OverloadedStrings #-}
import Turtle
import Prelude hiding (FilePath)
import qualified Data.Text as T
import System.Environment (getArgs)
usage = "Please specify module name you would like to build as first argument."
pscOut = "output"
cafeOut = "./cafe-out"
src = "src/"
pureBasePath = "pureBase/src"
bowerCmd = "bower install"
pulpCmd = "pulp build"
rmTreeFiles root = sh $ do
file <- lstree root
True <- liftIO $ testfile file
liftIO $ rm file
rmTree root = sh $ do
liftIO $ rmTreeFiles root
liftIO $ rmtree root
srcFileToModule fn =
let fnTxt = either id id (toText fn)
modName = (T.replace "/" "." . T.replace src "" . T.replace ".purs" "") fnTxt
in fromText modName
main = do
args <- getArgs
case args of
[modNameArg] -> do
let modDir = fromText $ T.pack modNameArg
curPath <- pwd
cd modDir
ExitSuccess <- shell bowerCmd empty
ExitSuccess <- shell pulpCmd empty
rmTree cafeOut
mktree cafeOut
sh $ do
pursFile <- find (plus dot <> ".purs") $ fromText src
let modPath = srcFileToModule pursFile
liftIO $ mv (pscOut <> modPath) (cafeOut <> modPath)
mktree $ curPath <> pureBasePath
sh $ do
extraMod <- ls pscOut
let dest = curPath <> pureBasePath <> filename extraMod
exist <- liftIO $ testdir dest
liftIO $ if exist
then putStrLn $ "Already exist: " <> show dest <> ", skipping."
else mv extraMod dest
echo "done"
_ -> echo usage
#!/usr/bin/env runhaskell
{-# LANGUAGE OverloadedStrings #-}
import Turtle
import Prelude hiding (FilePath)
import Data.Traversable
import qualified Data.Text as T
import System.Environment (getArgs)
usage = "Please specify module name you would like to create as first argument."
pulpInit = "pulp init"
bowerrc = ".bowerrc"
bowerrcData = "{ \"directory\": \"../bower_components\" }"
slug = "slug.json"
slugData = "{ \"paths\": [ \"./cafe-out/\"] }"
main = do
args <- getArgs
case args of
[modNameArg] -> do
let modDir = fromText $ T.pack modNameArg
exists <- testdir modDir
if exists
then die ("Directory " <> either id id (toText modDir) <> " already exists.")
else do
mkdir modDir
cd modDir
output bowerrc bowerrcData
output slug slugData
rc <- shell pulpInit empty
case rc of
ExitFailure n -> do
rm bowerrc
rm slug
cd ".."
rmdir modDir
die (pulpInit <> " failed with exit code: " <> repr n <> ", cleaning up.")
ExitSuccess -> echo "done"
_ -> echo usage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment