-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.hs
97 lines (78 loc) · 2.77 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
{-# LANGUAGE OverloadedStrings, CPP #-}
import CabalMeta
import OmniConfig
import Shelly
import Paths_cabal_meta
import qualified Data.Text as T
import Control.Monad (forM_)
import Data.Maybe (isNothing, isJust)
import Data.Text (Text)
import Data.Version (showVersion)
import Prelude hiding (FilePath)
headDef :: a -> [a] -> a
headDef d [] = d
headDef _ (x:_) = x
help :: Text
help = T.intercalate "\n" [
"cabal-meta is a cabal wrapper for installing multiple packages at once that may not be on hackage"
,"run with:"
,""
," cabal-meta [--[no-]dev] install [cabal install arguments]"
,""
," --dev means use cabal-dev instead of cabal"
,""
,"You can also set options through the CABAL_META_OPTS environment variable or the ~/.cabal-meta/opts file"
]
cabal_install_ :: CabalExe -> [Text] -> Sh ()
cabal_install_ cabal = command_ (progName cabal) ["install"]
data CabalExe = Cabal | CabalDev deriving Show
progName :: CabalExe -> FilePath
progName Cabal = "cabal"
progName CabalDev = "cabal-dev"
assertCabalDependencies :: CabalExe -> IO Bool
assertCabalDependencies Cabal = shelly $ do
whenM (test_e "cabal-dev") $ do
echo help
echo "\n\ncabal-dev/ folder found. use the --dev option"
quietExit 1
mPath <- which "cabal-src-install"
if isNothing mPath
then warn >> return False
else return True
where
warn = echo "\nWARNING: cabal-src not installed. run:\n cabal install cabal-src\n"
assertCabalDependencies CabalDev = do
mcd <- shelly $ which "cabal-dev"
case mcd of
Just _ -> return False
Nothing -> error "--dev requires cabal-dev to be installed"
main :: IO ()
main = do
allArgs <- fmap (filter $ not . T.null) $
allProgramOpts [commandLine, environment "cabal-meta",
homeOptFile "cabal-meta"]
when ("--version" `elem` allArgs) $ do
putStrLn $ "cabal-meta " ++ showVersion version
shelly $ exit 0
let (mDev, noDevArgs) = checkNegatedOpt "dev" allArgs
let isDev = isJust mDev
let cabal = if isDev then CabalDev else Cabal
unless (headDef "" noDevArgs == "install") $ do
putStrLn $ T.unpack help
putStrLn $ "using cabal: " ++ show cabal
shelly $
if (headDef "" noDevArgs == "--help") then exit 0 else quietExit 1
installSrc <- assertCabalDependencies cabal
let (_:args) = noDevArgs
shelly $ verbosely $ do
packageSources <- readPackages True "."
let installs = packageList packageSources
echo "Installing packages:"
mapM_ echo $ map (T.intercalate " ") installs
cabal_install_ cabal $ args ++ concat installs
case (cabal, installSrc) of
(Cabal, True) ->
forM_ (unstablePackages packageSources) $ \pkg ->
chdir (diskPath pkg) $ run "cabal-src-install" ["--src-only"]
_ -> return ()
return ()