forked from kazu-yamamoto/cab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Types.hs
118 lines (95 loc) · 2.86 KB
/
Types.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
module Types where
import Data.List
import System.Console.GetOpt
type Arg = String
type UnknownOpt = String
type ParsedArgs = Either [UnknownOpt] ([Arg],[Option])
----------------------------------------------------------------
data Switch = SwNoharm
| SwRecursive
| SwAll
| SwInfo
| SwSandbox
| SwFlag
| SwTest
deriving (Eq,Show)
data Option = OptNoharm
| OptRecursive
| OptAll
| OptInfo
| OptSandbox String
| OptFlag String
| OptTest
| OptHelp
deriving (Eq,Show)
toSwitch :: Option -> Switch
toSwitch OptNoharm = SwNoharm
toSwitch OptRecursive = SwRecursive
toSwitch OptAll = SwAll
toSwitch OptInfo = SwInfo
toSwitch (OptSandbox _) = SwSandbox
toSwitch (OptFlag _) = SwFlag
toSwitch OptTest = SwTest
toSwitch _ = error "toSwitch"
getSandbox :: [Option] -> Maybe FilePath
getSandbox = getValue (\x -> toSwitch x == SwSandbox)
getFlag :: [Option] -> Maybe FilePath
getFlag = getValue (\x -> toSwitch x == SwFlag)
getValue :: (Option -> Bool) -> [Option] -> Maybe FilePath
getValue p opts = case find p opts of
Nothing -> Nothing
Just (OptSandbox path) -> Just path
_ -> error "getSandbox"
type SwitchSpec = (Switch, Maybe String)
type SwitchDB = [SwitchSpec]
type GetOptSpec = OptDescr Option
type GetOptDB = [GetOptSpec]
type OptionSpec = (Switch,GetOptSpec)
type OptionDB = [OptionSpec]
----------------------------------------------------------------
data Command = Sync
| Install
| Uninstall
| Installed
| Configure
| Build
| Clean
| Outdated
| Sdist
| Upload
| Unpack
| Info
| Deps
| RevDeps
| Check
| GenPaths
| Search
| Env
| Add
| Ghci
| Test
| Doc
| Help
deriving (Eq,Show)
data CommandSpec = CommandSpec {
command :: Command
, commandNames :: [String]
, document :: String
, routing :: Route
, switches :: SwitchDB
, manual :: Maybe String
}
type CommandDB = [CommandSpec]
----------------------------------------------------------------
type FunctionCommand = CommandSpec -> [String] -> [Option] -> IO ()
data Route = RouteFunc FunctionCommand
| RouteCabal [String]
cabalCommand :: [Option] -> String
cabalCommand opts
| SwSandbox `elem` map toSwitch opts = "cabal-dev"
| otherwise = "cabal"
----------------------------------------------------------------
cabEnvVar :: String
cabEnvVar = "CAB_SANDBOX_PATH"
ghcEnvVar :: String
ghcEnvVar = "GHC_PACKAGE_PATH"