-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.hs
200 lines (176 loc) · 5.95 KB
/
main.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
module Main where
import Cabin.Package
import Cabin.Internal
import Control.Monad (filterM, liftM)
import Data.Maybe (fromMaybe)
import Data.Monoid (mempty, (<>))
import Options.Applicative
import Options.Applicative.Types (Parser(NilP))
import System.Directory
import System.Environment (lookupEnv)
import System.Exit (exitWith, ExitCode(..))
import System.FilePath
import System.Posix.Files (createSymbolicLink)
import System.Process
--------------------------------------------------------------------------------
-- Parsers
--------------------------------------------------------------------------------
data InstallOpts = InstallOpts {
ioName :: Maybe String
, ioPackages :: [String]
}
installOpts :: Parser InstallOpts
installOpts = InstallOpts
<$> optional (strOption (
long "name"
<> short 'n'
<> metavar "NAME"
<> help ("Name for the new cabin. If not provided, the first package "
++ "name is used.")
))
<*> some (argument str (metavar "PACKAGES..."))
data LoadOpts = LoadOpts {
loNames :: [String]
}
loadOpts :: Parser LoadOpts
loadOpts = LoadOpts
<$> some (argument str (metavar "CABINS..."))
data ListOpts = ListOpts {
loOnlyActive :: Bool
}
listOpts :: Parser ListOpts
listOpts = ListOpts
<$> switch ( long "active"
<> short 'a'
<> help "Show only active cabins."
)
emptyOpts :: Parser ()
emptyOpts = NilP $ Just ()
data Command = Install InstallOpts
| List ListOpts
| Load LoadOpts
| Unload LoadOpts
| Reindex ()
commandParser :: Parser Command
commandParser = subparser (
command "install" (info (fmap Install installOpts)
(progDesc "Install a program into a new cabin."))
<> command "list" (info (fmap List listOpts) (
progDesc "List available cabins."))
<> command "load" (info (fmap Load loadOpts) (
progDesc "Load an installed cabin."))
<> command "unload" (info (fmap Unload loadOpts) (
progDesc "Unload a loaded cabin."))
<> command "reindex" (info (fmap Reindex emptyOpts) (
progDesc "Rebuild the cabin DB."))
)
--------------------------------------------------------------------------------
-- Main
--------------------------------------------------------------------------------
main :: IO ()
main = execParser opts >>= run where
opts = info (helper <*> commandParser) (
fullDesc
<> progDesc "Cabin - cabal binary tool."
<> header "Cabin."
)
run :: Command -> IO ()
run (Install opts) = install opts
run (List opts) = list opts
run (Reindex _) = reindex
run (Load opts) = load opts
run (Unload opts) = unload opts
--------------------------------------------------------------------------------
-- Commands
--------------------------------------------------------------------------------
install :: InstallOpts -> IO ()
install opts = do
prepareEnvironment
let pkgs = ioPackages opts
name = fromMaybe (head pkgs) $ ioName opts
cabal <- getCabalPath
cabinPath <- fmap (</> name) getCabinPath
cabinDB <- readCabinDB
-- Create directory, sandbox init, cabal init
createDirectoryIfMissing True cabinPath
let sboxInitProc = (proc cabal ["sandbox","init"]) {
cwd = Just cabinPath
}
cabalInstallProc = (proc cabal ("install" : pkgs)) {
cwd = Just cabinPath
}
ec <- cmdSeq [sboxInitProc, cabalInstallProc]
case ec of
ExitSuccess -> do
cabin <- infoCabin cabinPath
let cabinDB' = cabinDB <> (CabinDB [cabin])
writeCabinDB cabinDB'
ExitFailure _ -> removeDirectoryRecursive cabinPath
exitWith ec
list :: ListOpts -> IO ()
list opts = do
dataDir <- getCabinPath
createDirectoryIfMissing True dataDir
cabs <- readCabinDB
cabStats <- mapM (\a -> cabinStatus a >>= \b -> return (a,b)) $ listCabins cabs
mapM_ (putStrLn . showCab) cabStats
where showCab ((Cabin name _, status)) = name ++ " (" ++ show status ++ ")"
load :: LoadOpts -> IO ()
load opts = do
prepareEnvironment
cdb <- readCabinDB
binPath <- getBinaryPath
mapM_ (go cdb binPath) $ loNames opts
where
go cdb binPath name =
let cabin = findCabinByName name cdb
linkPath a = binPath </> (takeFileName a)
in case cabin of
Nothing -> putStrLn $ "No cabin "++name++" found!"
Just c@(Cabin _ (CabinInfo _ bins)) -> do
stat <- cabinStatus c
case stat of
Loaded -> return ()
Unloaded -> do
mapM_ (\a -> createSymbolicLink a (linkPath a)) bins
writeCabinStatus c Loaded
unload :: LoadOpts -> IO ()
unload opts = do
prepareEnvironment
cdb <- readCabinDB
binPath <- getBinaryPath
mapM_ (go cdb binPath) $ loNames opts
where
go cdb binPath name =
let cabin = findCabinByName name cdb
linkPath a = binPath </> (takeFileName a)
in case cabin of
Nothing -> putStrLn $ "No cabin "++name++" found!"
Just c@(Cabin _ (CabinInfo _ bins)) -> do
stat <- cabinStatus c
case stat of
Unloaded -> return ()
Loaded -> do
mapM_ (\a -> removeFile $ linkPath a) bins
writeCabinStatus c Unloaded
reindex :: IO ()
reindex = do
prepareEnvironment
cp <- getCabinPath
putativeCabins <- liftM (filter (
\a -> notElem (takeFileName a) [".", ".."]
)) $
getDirectoryContents cp
|> liftM (cp </>)
>>= filterM doesDirectoryExist
putStrLn . show $ putativeCabins
cabins <- mapM infoCabin putativeCabins
writeCabinDB $ CabinDB cabins
--------------------------------------------------------------------------------
-- Helpers
--------------------------------------------------------------------------------
prepareEnvironment :: IO ()
prepareEnvironment = do
getCabinPath >>= createDirectoryIfMissing True
getBinaryPath >>= createDirectoryIfMissing True
getLoadedPath >>= createDirectoryIfMissing True