3
import Control.Applicative
4
import qualified Data.ByteString.Char8
6
import System.Console.GetOpt
7
import System.Environment
9
import qualified Agda.Packaging.Config
11
import qualified Agda.Packaging.Database
13
import qualified Agda.Packaging.Monad
16
import Interface.Command
18
import Interface.Options
19
import Interface.Usage
20
import Interface.Version
22
-------------------------------------------------------------------------------
26
progName <- getProgName
28
case getOpt Permute allOpts givenArgs of
29
-- if --help was given
30
(givenOpts, _givenCmds, []) | OptHelp `elem` givenOpts ->
31
bye $ usageInfo (BS.unpack (usageHeader (BS.pack progName))) allOpts
33
-- if --version was given
34
(givenOpts, _givenCmds, []) | OptVersion `elem` givenOpts ->
37
-- if a command was given
38
(givenOpts, givenCmds, []) ->
39
processCmds progName givenOpts givenCmds
42
(_givenOpts, _givenCmds, errors) ->
44
++ "See --help for usage info."
47
-- Process the commands (not the args prefixed with '--'),
48
-- determine the package database stack, and initialize the
49
-- environment for the AgdaPkg monad
50
processCmds :: String -> [Opt] -> [Cmd] -> IO ()
51
processCmds progName givenOpts givenCmds = do
53
-- if only --global was given
54
if (OptDBGlobal `elem` givenOpts
55
&& not (OptDBUser `elem` givenOpts))
58
<*> Agda.getPkgDBPathGlobal
61
-- if only --user was given
62
if (OptDBGlobal `elem` givenOpts
63
&& not (OptDBUser `elem` givenOpts))
66
<*> Agda.getPkgDBPathUser
69
-- if neither or both of --global and --user were given
70
pure (\ db1 db2 -> db1 : db2 : [])
71
<*> Agda.getPkgDBPathGlobal
72
<*> Agda.getPkgDBPathUser
73
-- load the package databases from the path stack
74
pkgDBStack <- Agda.getPkgDBs pkgDBPathStack
75
let initConfig = Agda.AgdaPkgConfig
76
{ Agda.configOpts = givenOpts
77
, Agda.configOrigBroken = [] -- FIXME
78
, Agda.configPkgDBStack = pkgDBStack
79
, Agda.configProgName = progName }
80
-- process the commands
81
-- FIXME: this runReaderT should be hidden by the API
82
Agda.runReaderT (Agda.runAgdaPkg dispatch) initConfig
85
-- Take some action according to the commands (not the args
86
-- prefixed with '--'). The interface follows the
87
-- specification outlined in the 'Haskell Cabal' document,
88
-- with deviations to conform to essential parts of the
89
-- current GHC interface.
90
dispatch :: Agda.AgdaPkg Opt ()
94
-- FIXME: this liftIO should be hidden by API
95
Agda.liftIO $ die $ "no command specified\n"
96
++ "See --help for usage info."
98
["describe" , _pkgId] ->
101
["expose" , pkgId] ->
107
["field" , _pkgId, _fields] ->
116
["register" , fileName] ->
119
["unregister", _pkgId] ->
122
["update" , fileName] ->
127
-- FIXME: This liftIO should be hidden by API
128
Agda.liftIO $ die $ "unrecognized command "
129
++ "`" ++ cmd ++ "'\n"
130
++ "See --help for usage info."