forked from kazu-yamamoto/cab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CmdDB.hs
381 lines (356 loc) · 12 KB
/
CmdDB.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
module CmdDB where
import Commands
import Control.Monad
import Data.List
import Program
import System.Console.GetOpt
import System.Exit
import System.IO
import Types
import Utils
----------------------------------------------------------------
commandDB :: CommandDB
commandDB = [
CommandSpec {
command = Sync
, commandNames = ["sync", "update"]
, document = "Fetch the latest package index"
, routing = RouteCabal ["update"]
, switches = []
, manual = Nothing
}
, CommandSpec {
command = Install
, commandNames = ["install"]
, document = "Install packages"
, routing = RouteCabal ["install"]
, switches = [(SwNoharm, Just "--dry-run -v")
-- FIXME cabal-dev not support --dry-run
,(SwSandbox, Just "--sandbox")
,(SwFlag, Just "--flags")
]
, manual = Just "[<package> [<ver>]]"
}
, CommandSpec {
command = Uninstall
, commandNames = ["uninstall", "delete", "remove", "unregister"]
, document = "Uninstall packages"
, routing = RouteFunc uninstall
, switches = [(SwNoharm, Nothing)
,(SwRecursive, Nothing)
,(SwSandbox, Just "--sandbox")
] -- don't allow SwAll
, manual = Just "<package> [<ver>]"
}
, CommandSpec {
command = Installed
, commandNames = ["installed", "list"]
, document = "List installed packages"
, routing = RouteFunc installed
, switches = [(SwAll, Nothing)
,(SwRecursive, Nothing)
,(SwInfo, Nothing)
,(SwSandbox, Just "--sandbox")
]
, manual = Nothing
}
, CommandSpec {
command = Configure
, commandNames = ["configure", "conf"]
, document = "Configure a cabal package"
, routing = RouteCabal ["configure"]
, switches = [(SwSandbox, Just "--sandbox")
,(SwFlag, Just "--flags")
,(SwTest, Just "--enable-tests")]
, manual = Nothing
}
, CommandSpec {
command = Build
, commandNames = ["build"]
, document = "Build a cabal package"
, routing = RouteCabal ["build"]
, switches = []
, manual = Nothing
}
, CommandSpec {
command = Clean
, commandNames = ["clean"]
, document = "Clean up a build directory"
, routing = RouteCabal ["clean"]
, switches = []
, manual = Nothing
}
, CommandSpec {
command = Outdated
, commandNames = ["outdated"]
, document = "Display outdated packages"
, routing = RouteFunc outdated
, switches = [(SwAll, Nothing)
,(SwSandbox, Just "--sandbox")]
, manual = Nothing
}
, CommandSpec {
command = Info
, commandNames = ["info"]
, document = "Display information of a package"
, routing = RouteCabal ["info"]
, switches = [(SwSandbox, Just "--sandbox")]
, manual = Just "<package> [<ver>]"
}
, CommandSpec {
command = Sdist
, commandNames = ["sdist", "pack"]
, document = "Make tar.gz for source distribution"
, routing = RouteCabal ["sdist"]
, switches = []
, manual = Nothing
}
, CommandSpec {
command = Upload
, commandNames = ["upload", "up"]
, document = "Uploading tar.gz to HackageDB"
, routing = RouteCabal ["upload"]
, switches = [(SwNoharm, Just "-c")]
, manual = Nothing
}
, CommandSpec {
command = Unpack
, commandNames = ["unpack"]
, document = "Untar a package in the current directory"
, routing = RouteCabal ["unpack"]
, switches = []
, manual = Just "<package> [<ver>]"
}
, CommandSpec {
command = Deps
, commandNames = ["deps"]
, document = "Show dependencies of this package"
, routing = RouteFunc deps
, switches = [(SwRecursive, Nothing)
,(SwAll, Nothing)
,(SwInfo, Nothing)
,(SwSandbox, Just "--sandbox")
]
, manual = Just "<package> [<ver>]"
}
, CommandSpec {
command = RevDeps
, commandNames = ["revdeps", "dependents"]
, document = "Show reverse dependencies of this package"
, routing = RouteFunc revdeps
, switches = [(SwRecursive, Nothing)
,(SwAll, Nothing)
,(SwInfo, Nothing)
,(SwSandbox, Just "--sandbox")
]
, manual = Just "<package> [<ver>]"
}
, CommandSpec {
command = Check
, commandNames = ["check"]
, document = "Check consistency of packages"
, routing = RouteFunc check
, switches = [(SwSandbox, Just "--sandbox")]
, manual = Nothing
}
, CommandSpec {
command = GenPaths
, commandNames = ["genpaths", "genpath"]
, document = "Generate Paths_<pkg>.hs"
, routing = RouteFunc genpaths
, switches = []
, manual = Nothing
}
, CommandSpec {
command = Search
, commandNames = ["search"]
, document = "Search available packages by package name"
, routing = RouteFunc search
, switches = []
, manual = Just "<key>"
}
, CommandSpec {
command = Env
, commandNames = ["env"]
, document = "Show environment variables"
, routing = RouteFunc env
, switches = [(SwSandbox, Just "--sandbox")]
, manual = Nothing
}
, CommandSpec {
command = Add
, commandNames = ["add", "add-source"]
, document = "Add a source directory"
, routing = RouteFunc add
, switches = [(SwSandbox, Just "--sandbox")]
, manual = Just "<source>"
}
, CommandSpec {
command = Test
, commandNames = ["test"]
, document = "Run tests"
, routing = RouteCabal ["test"]
, switches = [(SwSandbox, Just "--sandbox")]
, manual = Nothing
}
, CommandSpec {
command = Doc
, commandNames = ["doc", "haddock", "man"]
, document = "Generate manuals"
, routing = RouteCabal ["haddock", "--hyperlink-source"]
, switches = []
, manual = Nothing
}
, CommandSpec {
command = Ghci
, commandNames = ["ghci"]
, document = "Run GHCi within a sandbox"
, routing = RouteFunc ghci
, switches = [(SwSandbox, Just "--sandbox")]
, manual = Nothing
}
, CommandSpec {
command = Help
, commandNames = ["help"]
, document = "Display the help message of the command"
, routing = RouteFunc helpCommandAndExit
, switches = []
, manual = Just "[<command>]"
}
]
----------------------------------------------------------------
commandSpecByName :: String -> CommandDB -> Maybe CommandSpec
commandSpecByName _ [] = Nothing
commandSpecByName x (ent:ents)
| x `elem` commandNames ent = Just ent
| otherwise = commandSpecByName x ents
----------------------------------------------------------------
getOptDB :: GetOptDB
getOptDB = [
Option ['n'] ["dry-run"]
(NoArg OptNoharm)
"Run without destructive operations"
, Option ['r'] ["recursive"]
(NoArg OptRecursive)
"Follow dependencies recursively"
, Option ['a'] ["all"]
(NoArg OptAll)
"Show global packages in addition to user packages"
, Option ['i'] ["info"]
(NoArg OptInfo)
"Show license and author information"
, Option ['s'] ["sandbox"]
(ReqArg OptSandbox "<sandbox>")
"Specify a sandbox directory"
, Option ['f'] ["flags"]
(ReqArg OptFlag "<flags>")
"Specify flags"
, Option ['t'] ["test"]
(NoArg OptTest)
"Enable test"
, Option ['h'] ["help"]
(NoArg OptHelp)
"Show help message"
]
optionDB :: OptionDB
optionDB = zip [SwNoharm,SwRecursive,SwAll,SwInfo,SwSandbox,SwFlag,SwTest] getOptDB
----------------------------------------------------------------
optionName :: OptionSpec -> String
optionName (_,Option (c:_) _ (ReqArg _ arg) _) = '-':c:' ':arg
optionName (_,Option (c:_) _ _ _) = '-':[c]
optionName _ = ""
optionNames :: OptionSpec -> [String]
optionNames (_,Option (c:_) (s:_) _ _) = ['-':[c],'-':'-':s]
optionNames _ = []
optionDesc :: OptionSpec -> String
optionDesc (_,Option _ _ _ desc) = desc
getOptNames :: GetOptSpec -> (String,String)
getOptNames (Option (c:_) (s:_) _ _) = ('-':[c],'-':'-':s)
getOptNames _ = error "getOptNames"
resolveOptionString :: [Arg] -> Switch -> [UnknownOpt]
resolveOptionString oargs sw = case lookup sw optionDB of
Nothing -> error "resolveOptionString"
Just gspec -> let (s,l) = getOptNames gspec
in checkShort s ++ checkLong l
where
checkShort s = filter (==s) oargs
checkLong l = filter (l `isPrefixOf`) oargs
optionsToString :: [Option] -> SwitchDB -> [String]
optionsToString opts swdb = concatMap suboption opts
where
suboption opt = case lookup (toSwitch opt) swdb of
Nothing -> []
Just Nothing -> []
Just (Just x) -> case opt of
OptSandbox dir -> [x++"="++dir]
OptFlag flags -> [x++"="++flags]
_ -> [x]
----------------------------------------------------------------
helpCommandAndExit :: FunctionCommand
helpCommandAndExit _ [] _ = helpAndExit
helpCommandAndExit _ (cmd:_) _ = do
case mcmdspec of
Nothing -> helpAndExit
Just cmdspec -> do
putStrLn $ "Usage: " ++ cmd ++ " " ++ showOptions cmdspec ++ showArgs cmdspec
putStr "\n"
putStrLn $ document cmdspec
putStr "\n"
putStrLn $ "Aliases: " ++ showAliases cmdspec
putStr "\n"
printOptions cmdspec
exitSuccess
where
mcmdspec = commandSpecByName cmd commandDB
showOptions cmdspec = "[" ++ joinBy "] [" (concatMap (masterOption optionDB) (opts cmdspec)) ++ "]"
showArgs cmdspec = maybe "" (" " ++) $ manual cmdspec
opts = map fst . switches
masterOption [] _ = []
masterOption (spec:specs) o
| fst spec == o = optionName spec : masterOption specs o
| otherwise = masterOption specs o
showAliases = joinBy ", " . commandNames
printOptions :: CommandSpec -> IO ()
printOptions cmdspec =
forM_ opts (printOption optionDB)
where
opts = map fst $ switches cmdspec
printOption [] _ = return ()
printOption (spec:specs) o
| fst spec == o =
putStrLn $ (joinBy ", " . reverse . optionNames $ spec)
++ "\t" ++ optionDesc spec
| otherwise = printOption specs o
----------------------------------------------------------------
helpAndExit :: IO ()
helpAndExit = do
putStrLn $ programName ++ " " ++ " -- " ++ description
putStrLn ""
putStrLn $ "Version: " ++ showVersion version
putStrLn "Usage:"
putStrLn $ "\t" ++ programName
putStrLn $ "\t" ++ programName ++ " <command> [args...]"
putStrLn "\t where"
printCommands (getCommands commandDB)
exitSuccess
where
getCommands = map concat
. split helpCommandNumber
. intersperse ", "
. map (head . commandNames)
printCommands [] = return ()
printCommands (x:xs) = do
putStrLn $ "\t <command> = " ++ x
mapM_ (\cmds -> putStrLn $ "\t " ++ cmds) xs
helpCommandNumber :: Int
helpCommandNumber = 10
----------------------------------------------------------------
illegalCommandAndExit :: String -> IO ()
illegalCommandAndExit x = do
hPutStrLn stderr $ "Illegal command: " ++ x
exitFailure
----------------------------------------------------------------
illegalOptionsAndExit :: [UnknownOpt] -> IO ()
illegalOptionsAndExit xs = do -- FixME
hPutStrLn stderr $ "Illegal options: " ++ joinBy " " xs
exitFailure