forked from fosskers/aura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaura.hs
213 lines (185 loc) · 7.38 KB
/
aura.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
{-# LANGUAGE TupleSections #-}
{-
Copyright 2012, 2013 Colin Woodbury <[email protected]>
This file is part of Aura.
Aura is free s
oftwar
e:youcanredist
ributeitand/ormodify
itunderthetermsoftheGN
UGeneralPublicLicenseasp
ublishedbytheFreeSoftw
areFoundation,either ver sio n3o fth
eLicense,or(atyou ropti on)an ylate rvers
ion.Auraisdistr ibutedi nthehop ethatit willbeu
seful,butWITHOUTA NYWAR RANTY ;with outev
entheimpliedwarranty ofM ERC HAN TAB
ILITYorFITNESSFORAPART
ICULARPURPOSE.SeetheGNUG
eneralPublicLicensefor
moredetails.Youshoul
dhavereceiveda
copyof
the GNU General Public License
along with Aura. If not, see <http://www.gnu.org/licenses/>.
-}
import System.Environment (getArgs)
import Control.Monad (when)
import System.Exit (exitSuccess, exitFailure)
import Data.List (nub, sort, intercalate)
import Aura.Colour.Text (yellow)
import Aura.Shell (shellCmd)
import Aura.Settings.Enable
import Aura.Settings.Base
import Aura.Monad.Aura
import Aura.Languages
import Aura.Pacman
import Aura.Flags
import Aura.Utils
import Aura.Core
import Aura.Logo
import Utilities (replaceByPatt)
import Shell (showCursor, hideCursor)
import Aura.Commands.A as A
import Aura.Commands.B as B
import Aura.Commands.C as C
import Aura.Commands.L as L
import Aura.Commands.M as M
import Aura.Commands.O as O
---
type UserInput = ([Flag],[String],[String])
auraVersion :: String
auraVersion = "1.2.1.3"
main :: IO a
main = getArgs >>= prepSettings . processFlags >>= execute >>= exit
processFlags :: [String] -> (UserInput,Maybe Language)
processFlags args = ((flags,nub input,pacOpts'),language)
where (language,rest) = parseLanguageFlag args
(flags,input,pacOpts) = parseFlags language rest
pacOpts' = nub $ pacOpts ++ reconvertFlags flags dualFlagMap
-- | Set the local environment.
prepSettings :: (UserInput,Maybe Language) -> IO (UserInput,Settings)
prepSettings (ui,lang) = (ui,) `fmap` getSettings lang ui
-- | Hand user input to the Aura Monad and run it.
execute :: (UserInput,Settings) -> IO (Either AuraError ())
execute ((flags,input,pacOpts),ss) = do
let flags' = filter notSettingsFlag flags
when (Debug `elem` flags) $ debugOutput ss
runAura (executeOpts (flags',input,pacOpts)) ss
exit :: Either AuraError () -> IO a
exit (Left e) = putStrLn (getErrorMsg e) >> exitFailure
exit (Right _) = exitSuccess
-- | After determining what Flag was given, dispatches a function.
-- The `flags` must be sorted to guarantee the pattern matching
-- below will work properly.
-- If no matches on Aura flags are found, the flags are assumed to be
-- pacman's.
executeOpts :: UserInput -> Aura ()
executeOpts ([],_,[]) = executeOpts ([Help],[],[])
executeOpts (flags,input,pacOpts) =
case sort flags of
(AURInstall:fs) ->
case fs of
[] -> trueRoot (sudo $ A.install pacOpts input)
[Upgrade] -> trueRoot (sudo $ A.upgradeAURPkgs pacOpts input)
[Info] -> A.aurPkgInfo input
[Search] -> A.aurSearch input
[ViewDeps] -> A.displayPkgDeps input
[Download] -> A.downloadTarballs input
[GetPkgbuild] -> A.displayPkgbuild input
(Refresh:fs') -> sudo $ syncAndContinue (fs',input,pacOpts)
badFlags -> scoldAndFail executeOpts_1
(ABSInstall:fs) ->
case fs of
[] -> trueRoot (sudo $ M.install pacOpts input)
[TreeSync] -> sudo $ M.addToTree input
[Search] -> M.absSearch input
[Info] -> M.absInfo input
[Clean] -> sudo M.cleanABSTree
[ViewDeps] -> M.displayPkgDeps input
[GetPkgbuild] -> M.displayPkgbuild input
(Refresh:fs') -> sudo $ syncABSAndContinue (fs',input,pacOpts)
badFlags -> scoldAndFail executeOpts_1
(SaveState:fs) ->
case fs of
[] -> sudo B.saveState
[Clean] -> sudo $ B.cleanStates input
[RestoreState] -> sudo B.restoreState
badFlags -> scoldAndFail executeOpts_1
(Cache:fs) ->
case fs of
[] -> sudo $ C.downgradePackages input
[Clean] -> sudo $ C.cleanCache input
[Clean,Clean] -> sudo C.cleanNotSaved
[Search] -> C.searchCache input
[CacheBackup] -> sudo $ C.backupCache input
badFlags -> scoldAndFail executeOpts_1
(LogFile:fs) ->
case fs of
[] -> ask >>= L.viewLogFile . logFilePathOf
[Search] -> L.searchLogFile input
[Info] -> L.logInfoOnPkg input
badFlags -> scoldAndFail executeOpts_1
(Orphans:fs) ->
case fs of
[] -> O.displayOrphans input
[Abandon] -> sudo $ orphans >>= flip removePkgs pacOpts
badFlags -> scoldAndFail executeOpts_1
[ViewConf] -> viewConfFile
[Languages] -> displayOutputLanguages
[Help] -> printHelpMsg pacOpts
[Version] -> getVersionInfo >>= animateVersionMsg
pacmanFlags -> catch (pacman $ pacOpts ++ hijackedFlags ++ input)
pacmanFailure
where hijackedFlags = reconvertFlags flags hijackedFlagMap
-- | `-y` was included with `-A`. Sync database before continuing.
syncAndContinue :: UserInput -> Aura ()
syncAndContinue (flags,input,pacOpts) =
syncDatabase pacOpts >> executeOpts (AURInstall:flags,input,pacOpts)
-- | `-y` was included with `-M`. Sync local ABS tree before continuing.
syncABSAndContinue :: UserInput -> Aura ()
syncABSAndContinue (flags,input,pacOpts) =
M.absSync >> executeOpts (ABSInstall:flags,input,pacOpts)
----------
-- GENERAL
----------
viewConfFile :: Aura ()
viewConfFile = shellCmd "less" [pacmanConfFile]
displayOutputLanguages :: Aura ()
displayOutputLanguages = do
notify displayOutputLanguages_1
liftIO $ mapM_ print allLanguages
printHelpMsg :: [String] -> Aura ()
printHelpMsg [] = ask >>= \ss -> do
pacmanHelp <- getPacmanHelpMsg
liftIO . putStrLn . getHelpMsg ss $ pacmanHelp
printHelpMsg pacOpts = pacman $ pacOpts ++ ["-h"]
getHelpMsg :: Settings -> [String] -> String
getHelpMsg settings pacmanHelpMsg = intercalate "\n" allMessages
where lang = langOf settings
allMessages = [replacedLines, auraOperMsg lang, manpageMsg lang]
replacedLines = unlines $ map (replaceByPatt patterns) pacmanHelpMsg
colouredMsg = yellow $ inheritedOperTitle lang
patterns = [("pacman","aura"), ("operations",colouredMsg)]
-- | Animated version message.
animateVersionMsg :: [String] -> Aura ()
animateVersionMsg verMsg = ask >>= \ss -> liftIO $ do
hideCursor
mapM_ (putStrLn . padString verMsgPad) verMsg -- Version message
raiseCursorBy 7 -- Initial reraising of the cursor.
drawPills 3
mapM_ putStrLn $ renderPacmanHead 0 Open -- Initial rendering of head.
raiseCursorBy 4
takeABite 0 -- Initial bite animation.
mapM_ pillEating pillsAndWidths
clearGrid
version ss
showCursor
where pillEating (p,w) = clearGrid >> drawPills p >> takeABite w
pillsAndWidths = [(2,5),(1,10),(0,15)]
version :: Settings -> IO ()
version ss = do
putStrLn auraLogo
putStrLn $ "AURA Version " ++ auraVersion
putStrLn " by Colin Woodbury\n"
mapM_ putStrLn . translatorMsg . langOf $ ss