2010-06-14 06:38:56 +00:00
|
|
|
{-# LANGUAGE DeriveDataTypeable #-}
|
|
|
|
|
2010-01-06 05:38:06 +00:00
|
|
|
module Main where
|
|
|
|
|
2010-03-11 10:03:17 +00:00
|
|
|
import Browse
|
2013-03-04 09:11:09 +00:00
|
|
|
import CabalApi
|
2010-03-11 10:03:17 +00:00
|
|
|
import Check
|
2010-04-30 07:27:10 +00:00
|
|
|
import Control.Applicative
|
2010-06-14 06:38:56 +00:00
|
|
|
import Control.Exception
|
2013-03-02 03:18:55 +00:00
|
|
|
import Cradle
|
2010-06-14 06:38:56 +00:00
|
|
|
import Data.Typeable
|
2011-12-26 08:08:00 +00:00
|
|
|
import Data.Version
|
2013-03-04 02:21:41 +00:00
|
|
|
import Debug
|
2013-03-02 03:18:55 +00:00
|
|
|
import Flag
|
2010-11-12 07:27:50 +00:00
|
|
|
import Info
|
2010-04-30 07:27:10 +00:00
|
|
|
import Lang
|
2010-05-06 06:29:55 +00:00
|
|
|
import Lint
|
2010-06-14 05:42:17 +00:00
|
|
|
import List
|
2011-12-26 08:08:00 +00:00
|
|
|
import Paths_ghc_mod
|
2010-06-14 06:38:56 +00:00
|
|
|
import Prelude
|
2010-01-06 05:38:06 +00:00
|
|
|
import System.Console.GetOpt
|
2010-05-06 04:14:17 +00:00
|
|
|
import System.Directory
|
2010-03-11 10:03:17 +00:00
|
|
|
import System.Environment (getArgs)
|
2013-03-29 12:58:55 +00:00
|
|
|
import System.IO (hPutStr, hPutStrLn, stdout, stderr, hSetEncoding, utf8)
|
2010-04-30 09:36:31 +00:00
|
|
|
import Types
|
2010-01-06 05:38:06 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2011-11-14 09:12:18 +00:00
|
|
|
ghcOptHelp :: String
|
|
|
|
ghcOptHelp = " [-g GHC_opt1 -g GHC_opt2 ...] "
|
|
|
|
|
2010-01-06 05:38:06 +00:00
|
|
|
usage :: String
|
2011-12-26 08:08:00 +00:00
|
|
|
usage = "ghc-mod version " ++ showVersion version ++ "\n"
|
2010-01-06 05:38:06 +00:00
|
|
|
++ "Usage:\n"
|
2011-11-14 09:12:18 +00:00
|
|
|
++ "\t ghc-mod list" ++ ghcOptHelp ++ "[-l]\n"
|
2011-01-27 05:29:39 +00:00
|
|
|
++ "\t ghc-mod lang [-l]\n"
|
2012-01-23 04:18:40 +00:00
|
|
|
++ "\t ghc-mod flag [-l]\n"
|
2013-02-23 08:51:55 +00:00
|
|
|
++ "\t ghc-mod browse" ++ ghcOptHelp ++ "[-l] [-o] [-d] <module> [<module> ...]\n"
|
2011-11-14 09:12:18 +00:00
|
|
|
++ "\t ghc-mod check" ++ ghcOptHelp ++ "<HaskellFile>\n"
|
2012-02-27 02:23:56 +00:00
|
|
|
++ "\t ghc-mod expand" ++ ghcOptHelp ++ "<HaskellFile>\n"
|
2013-03-05 07:25:37 +00:00
|
|
|
++ "\t ghc-mod debug" ++ ghcOptHelp ++ "<HaskellFile>\n"
|
2011-11-14 09:12:18 +00:00
|
|
|
++ "\t ghc-mod info" ++ ghcOptHelp ++ "<HaskellFile> <module> <expression>\n"
|
2012-02-13 04:23:04 +00:00
|
|
|
++ "\t ghc-mod type" ++ ghcOptHelp ++ "<HaskellFile> <module> <line-no> <column-no>\n"
|
2011-01-27 05:29:39 +00:00
|
|
|
++ "\t ghc-mod lint [-h opt] <HaskellFile>\n"
|
2010-05-04 03:47:55 +00:00
|
|
|
++ "\t ghc-mod boot\n"
|
2010-01-06 05:38:06 +00:00
|
|
|
++ "\t ghc-mod help\n"
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
argspec :: [OptDescr (Options -> Options)]
|
2010-04-23 09:09:38 +00:00
|
|
|
argspec = [ Option "l" ["tolisp"]
|
2012-02-13 11:31:36 +00:00
|
|
|
(NoArg (\opts -> opts { outputStyle = LispStyle }))
|
2010-01-06 05:38:06 +00:00
|
|
|
"print as a list of Lisp"
|
2010-06-14 02:56:35 +00:00
|
|
|
, Option "h" ["hlintOpt"]
|
|
|
|
(ReqArg (\h opts -> opts { hlintOpts = h : hlintOpts opts }) "hlintOpt")
|
2011-10-20 02:24:25 +00:00
|
|
|
"hlint options"
|
2011-10-19 10:09:36 +00:00
|
|
|
, Option "g" ["ghcOpt"]
|
|
|
|
(ReqArg (\g opts -> opts { ghcOpts = g : ghcOpts opts }) "ghcOpt")
|
2011-10-20 02:24:25 +00:00
|
|
|
"GHC options"
|
2011-01-27 05:29:39 +00:00
|
|
|
, Option "o" ["operators"]
|
|
|
|
(NoArg (\opts -> opts { operators = True }))
|
|
|
|
"print operators, too"
|
2013-02-23 08:51:55 +00:00
|
|
|
, Option "d" ["detailed"]
|
|
|
|
(NoArg (\opts -> opts { detailed = True }))
|
|
|
|
"print detailed info"
|
2012-04-09 19:39:58 +00:00
|
|
|
, Option "s" ["sandbox"]
|
|
|
|
(ReqArg (\s opts -> opts { sandbox = Just s }) "path")
|
|
|
|
"specify cabal-dev sandbox (default 'cabal-dev`)"
|
2010-01-06 05:38:06 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
parseArgs :: [OptDescr (Options -> Options)] -> [String] -> (Options, [String])
|
|
|
|
parseArgs spec argv
|
|
|
|
= case getOpt Permute spec argv of
|
2012-03-11 07:40:52 +00:00
|
|
|
(o,n,[] ) -> (foldr id defaultOptions o, n)
|
2010-06-14 06:38:56 +00:00
|
|
|
(_,_,errs) -> throw (CmdArg errs)
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
data GHCModError = SafeList
|
|
|
|
| NoSuchCommand String
|
|
|
|
| CmdArg [String]
|
|
|
|
| FileNotExist String deriving (Show, Typeable)
|
|
|
|
|
|
|
|
instance Exception GHCModError
|
2010-01-06 05:38:06 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
main :: IO ()
|
2010-06-14 05:42:17 +00:00
|
|
|
main = flip catches handlers $ do
|
2013-03-30 13:01:24 +00:00
|
|
|
-- #if __GLASGOW_HASKELL__ >= 611
|
2013-03-29 12:58:55 +00:00
|
|
|
hSetEncoding stdout utf8
|
2013-03-30 13:01:24 +00:00
|
|
|
-- #endif
|
2010-01-06 05:38:06 +00:00
|
|
|
args <- getArgs
|
2011-11-14 10:20:02 +00:00
|
|
|
let (opt',cmdArg) = parseArgs argspec args
|
2013-03-04 09:11:09 +00:00
|
|
|
(strVer,ver) <- getGHCVersion
|
|
|
|
cradle <- findCradle (sandbox opt') strVer
|
|
|
|
let opt = adjustOpts opt' cradle ver
|
2013-03-04 00:09:13 +00:00
|
|
|
cmdArg0 = cmdArg !. 0
|
|
|
|
cmdArg1 = cmdArg !. 1
|
|
|
|
cmdArg2 = cmdArg !. 2
|
|
|
|
cmdArg3 = cmdArg !. 3
|
|
|
|
cmdArg4 = cmdArg !. 4
|
|
|
|
res <- case cmdArg0 of
|
2010-04-30 07:27:10 +00:00
|
|
|
"browse" -> concat <$> mapM (browseModule opt) (tail cmdArg)
|
2010-03-11 13:39:07 +00:00
|
|
|
"list" -> listModules opt
|
2013-03-04 00:09:13 +00:00
|
|
|
"check" -> withFile (checkSyntax opt cradle) cmdArg1
|
|
|
|
"expand" -> withFile (checkSyntax opt { expandSplice = True } cradle) cmdArg1
|
2013-03-04 09:11:09 +00:00
|
|
|
"debug" -> withFile (debugInfo opt cradle strVer) cmdArg1
|
2013-03-04 00:09:13 +00:00
|
|
|
"type" -> withFile (typeExpr opt cradle cmdArg2 (read cmdArg3) (read cmdArg4)) cmdArg1
|
|
|
|
"info" -> withFile (infoExpr opt cradle cmdArg2 cmdArg3) cmdArg1
|
|
|
|
"lint" -> withFile (lintSyntax opt) cmdArg1
|
2010-04-02 05:40:06 +00:00
|
|
|
"lang" -> listLanguages opt
|
2012-01-23 04:18:40 +00:00
|
|
|
"flag" -> listFlags opt
|
2010-05-04 03:47:55 +00:00
|
|
|
"boot" -> do
|
|
|
|
mods <- listModules opt
|
|
|
|
langs <- listLanguages opt
|
2012-01-23 04:18:40 +00:00
|
|
|
flags <- listFlags opt
|
2012-01-23 09:30:07 +00:00
|
|
|
pre <- concat <$> mapM (browseModule opt) preBrowsedModules
|
2012-01-23 04:18:40 +00:00
|
|
|
return $ mods ++ langs ++ flags ++ pre
|
2010-06-14 06:38:56 +00:00
|
|
|
cmd -> throw (NoSuchCommand cmd)
|
2010-03-11 13:39:07 +00:00
|
|
|
putStr res
|
2010-01-06 05:38:06 +00:00
|
|
|
where
|
2010-06-14 06:38:56 +00:00
|
|
|
handlers = [Handler handler1, Handler handler2]
|
|
|
|
handler1 :: ErrorCall -> IO ()
|
2011-08-24 06:58:12 +00:00
|
|
|
handler1 = print -- for debug
|
2010-06-14 06:38:56 +00:00
|
|
|
handler2 :: GHCModError -> IO ()
|
|
|
|
handler2 SafeList = printUsage
|
|
|
|
handler2 (NoSuchCommand cmd) = do
|
|
|
|
hPutStrLn stderr $ "\"" ++ cmd ++ "\" not supported"
|
|
|
|
printUsage
|
|
|
|
handler2 (CmdArg errs) = do
|
|
|
|
mapM_ (hPutStr stderr) errs
|
|
|
|
printUsage
|
|
|
|
handler2 (FileNotExist file) = do
|
|
|
|
hPutStrLn stderr $ "\"" ++ file ++ "\" not found"
|
|
|
|
printUsage
|
2011-08-24 06:58:12 +00:00
|
|
|
printUsage = hPutStrLn stderr $ '\n' : usageInfo usage argspec
|
2010-05-06 06:29:55 +00:00
|
|
|
withFile cmd file = do
|
|
|
|
exist <- doesFileExist file
|
|
|
|
if exist
|
|
|
|
then cmd file
|
2010-06-14 06:38:56 +00:00
|
|
|
else throw (FileNotExist file)
|
2013-03-04 00:09:13 +00:00
|
|
|
xs !. idx
|
2010-06-14 06:38:56 +00:00
|
|
|
| length xs <= idx = throw SafeList
|
|
|
|
| otherwise = xs !! idx
|
2013-03-04 09:11:09 +00:00
|
|
|
adjustOpts opt cradle ver = case mPkgConf of
|
2013-03-02 07:14:55 +00:00
|
|
|
Nothing -> opt
|
2013-03-04 09:11:09 +00:00
|
|
|
Just pkgConf -> opt {
|
|
|
|
ghcOpts = ghcPackageConfOptions ver pkgConf ++ ghcOpts opt
|
|
|
|
}
|
2013-03-02 07:14:55 +00:00
|
|
|
where
|
2013-03-04 09:11:09 +00:00
|
|
|
mPkgConf = cradlePackageConf cradle
|
2010-01-06 05:38:06 +00:00
|
|
|
|
2012-01-23 09:30:07 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
preBrowsedModules :: [String]
|
|
|
|
preBrowsedModules = [
|
|
|
|
"Prelude"
|
|
|
|
, "Control.Applicative"
|
|
|
|
, "Control.Monad"
|
|
|
|
, "Control.Exception"
|
|
|
|
, "Data.Char"
|
|
|
|
, "Data.List"
|
|
|
|
, "Data.Maybe"
|
|
|
|
, "System.IO"
|
|
|
|
]
|
2013-03-04 09:11:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
ghcPackageConfOptions :: Int -> String -> [String]
|
|
|
|
ghcPackageConfOptions ver file
|
|
|
|
| ver >= 706 = ["-package-db", file, "-no-user-package-conf"]
|
|
|
|
| otherwise = ["-package-conf", file, "-no-user-package-conf"]
|