Skip to content

Instantly share code, notes, and snippets.

@carymrobbins
Last active December 19, 2015 02:19
Show Gist options
  • Save carymrobbins/5882494 to your computer and use it in GitHub Desktop.
Save carymrobbins/5882494 to your computer and use it in GitHub Desktop.
Wrapper for the ghci interpreter to automatically set extensions based on your cabal file.
#!/usr/local/bin/runhaskell
{-
DECPRECATED - Currently cabal repl provides a much better solution to this problem.
Use this script as a "replacement" for the ghci interpreter to
automatically include extensions from your cabal file.
Be sure to set the cabalFile and (optionally) ghciInit values in this script.
You may also need to update the bang line to the path of runhaskell.
Prerequisites:
$ # cd to the directory of your cabal file and copy this script there.
$ chmod 755 ghci.hs # Make this script executable.
$ touch .ghci # Create the .ghci config so we can set perms.
$ chmod 644 .ghci # Ensure it is only writable by you.
$ chmod 744 . # Ensure this directory is only writable by you.
$ cabal install executable-path # Needed to get the relative path.
Now you can run it with:
$ ./ghci.hs
$ ./ghci.hs MyModule.hs # Arguments work as expected
-}
import Control.Monad
import qualified Data.Text as T
import System.Environment
import System.Environment.Executable
import System.IO
import System.Process
cabalFile = "yourproject.cabal"
ghciInit = "\
\-- Insert your initial cabal file here\
\ "
getDir :: String -> String
getDir path = dropWhile (/='/') $ reverse $
dropWhile (/='/') $ reverse path
getScriptDir :: IO String
getScriptDir = liftM (getDir . show) getScriptPath
notContains :: String -> String -> Bool
notContains substr = not . (T.isInfixOf (T.pack substr)) . T.pack
stringify :: (T.Text -> T.Text) -> (String -> String)
stringify f = T.unpack . f . T.pack
replace :: String -> String -> String -> String
replace find replaceWith = stringify $ T.replace (T.pack find) (T.pack replaceWith)
remove :: String -> String -> String
remove substr = replace substr ""
strip :: String -> String
strip = stringify $ T.strip
main = do
dir <- getScriptDir
cabal <- readFile $ dir ++ "/" ++ cabalFile
let conf = toConf . getExts $ cabal
writeFile (dir ++ "/.ghci") $ unlines [ghciInit, conf]
args <- getArgs
rawSystem "ghci" args
getExts :: String -> [String]
getExts = filter ((>0) . length)
. takeWhile (notContains ":")
. map (strip . remove "extensions:")
. dropWhile (notContains "extensions:")
. lines
toConf :: [String] -> String
toConf = unlines . map (":set -X"++)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment