2020-07-12 20:29:50 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
2020-07-06 20:39:16 +00:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2020-10-11 19:07:13 +00:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2020-07-06 20:39:16 +00:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2020-10-11 19:07:13 +00:00
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
{-# LANGUAGE ViewPatterns #-}
|
2020-07-06 20:39:16 +00:00
|
|
|
|
|
|
|
module BrickMain where
|
|
|
|
|
|
|
|
import GHCup
|
|
|
|
import GHCup.Download
|
|
|
|
import GHCup.Errors
|
|
|
|
import GHCup.Types
|
|
|
|
import GHCup.Utils
|
|
|
|
import GHCup.Utils.File
|
|
|
|
import GHCup.Utils.Logger
|
|
|
|
|
|
|
|
import Brick
|
|
|
|
import Brick.Widgets.Border
|
|
|
|
import Brick.Widgets.Border.Style
|
|
|
|
import Brick.Widgets.Center
|
2020-10-11 19:07:13 +00:00
|
|
|
import Brick.Widgets.List ( listSelectedFocusedAttr
|
|
|
|
, listSelectedAttr
|
|
|
|
, listAttr
|
|
|
|
)
|
2020-07-12 20:29:50 +00:00
|
|
|
#if !defined(TAR)
|
2020-07-06 20:39:16 +00:00
|
|
|
import Codec.Archive
|
2020-07-12 20:29:50 +00:00
|
|
|
#endif
|
2020-07-06 20:39:16 +00:00
|
|
|
import Control.Exception.Safe
|
|
|
|
import Control.Monad.Logger
|
|
|
|
import Control.Monad.Reader
|
|
|
|
import Control.Monad.Trans.Resource
|
|
|
|
import Data.Bool
|
|
|
|
import Data.Functor
|
|
|
|
import Data.List
|
|
|
|
import Data.Maybe
|
|
|
|
import Data.IORef
|
|
|
|
import Data.String.Interpolate
|
2020-10-11 19:07:13 +00:00
|
|
|
import Data.Vector ( Vector
|
|
|
|
, (!?)
|
|
|
|
)
|
2020-07-06 20:39:16 +00:00
|
|
|
import Data.Versions hiding ( str )
|
|
|
|
import Haskus.Utils.Variant.Excepts
|
|
|
|
import Prelude hiding ( appendFile )
|
|
|
|
import System.Exit
|
|
|
|
import System.IO.Unsafe
|
|
|
|
import URI.ByteString
|
|
|
|
|
2020-11-20 23:32:26 +00:00
|
|
|
import qualified GHCup.Types as GT
|
|
|
|
|
2020-07-06 20:39:16 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Graphics.Vty as Vty
|
|
|
|
import qualified Data.Vector as V
|
|
|
|
|
2020-10-11 19:07:13 +00:00
|
|
|
|
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
data BrickData = BrickData
|
2020-10-11 19:07:13 +00:00
|
|
|
{ lr :: [ListResult]
|
|
|
|
, dls :: GHCupDownloads
|
2020-07-13 16:27:21 +00:00
|
|
|
, pfreq :: PlatformRequest
|
2020-10-11 19:07:13 +00:00
|
|
|
}
|
|
|
|
deriving Show
|
2020-10-09 20:55:33 +00:00
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
data BrickSettings = BrickSettings
|
2020-10-11 19:07:13 +00:00
|
|
|
{ showAll :: Bool
|
|
|
|
}
|
|
|
|
deriving Show
|
2020-10-09 20:55:33 +00:00
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
data BrickInternalState = BrickInternalState
|
2020-10-11 19:07:13 +00:00
|
|
|
{ clr :: Vector ListResult
|
|
|
|
, ix :: Int
|
|
|
|
}
|
|
|
|
deriving Show
|
2020-07-06 20:39:16 +00:00
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
data BrickState = BrickState
|
|
|
|
{ appData :: BrickData
|
|
|
|
, appSettings :: BrickSettings
|
|
|
|
, appState :: BrickInternalState
|
2020-10-24 20:03:00 +00:00
|
|
|
, appKeys :: KeyBindings
|
2020-10-11 19:07:13 +00:00
|
|
|
}
|
|
|
|
deriving Show
|
2020-07-06 20:39:16 +00:00
|
|
|
|
|
|
|
|
2020-10-24 20:03:00 +00:00
|
|
|
keyHandlers :: KeyBindings
|
|
|
|
-> [ ( Vty.Key
|
2020-10-23 23:06:53 +00:00
|
|
|
, BrickSettings -> String
|
|
|
|
, BrickState -> EventM n (Next BrickState)
|
2020-10-09 20:55:33 +00:00
|
|
|
)
|
|
|
|
]
|
2020-10-24 20:03:00 +00:00
|
|
|
keyHandlers KeyBindings {..} =
|
|
|
|
[ (bQuit, const "Quit" , halt)
|
|
|
|
, (bInstall, const "Install" , withIOAction install')
|
|
|
|
, (bUninstall, const "Uninstall", withIOAction del')
|
|
|
|
, (bSet, const "Set" , withIOAction set')
|
|
|
|
, (bChangelog, const "ChangeLog", withIOAction changelog')
|
|
|
|
, ( bShowAll
|
2020-10-23 23:06:53 +00:00
|
|
|
, (\BrickSettings {..} ->
|
2020-10-09 20:55:33 +00:00
|
|
|
if showAll then "Hide old versions" else "Show all versions"
|
|
|
|
)
|
2020-10-11 19:07:13 +00:00
|
|
|
, hideShowHandler
|
2020-10-09 20:55:33 +00:00
|
|
|
)
|
2020-10-24 20:03:00 +00:00
|
|
|
, (bUp, const "Up", \BrickState {..} -> continue (BrickState { appState = (moveCursor 1 appState Up), .. }))
|
|
|
|
, (bDown, const "Down", \BrickState {..} -> continue (BrickState { appState = (moveCursor 1 appState Down), .. }))
|
2020-07-06 20:39:16 +00:00
|
|
|
]
|
2020-10-11 19:07:13 +00:00
|
|
|
where
|
2020-10-23 23:06:53 +00:00
|
|
|
hideShowHandler (BrickState {..}) =
|
2020-10-11 19:07:13 +00:00
|
|
|
let newAppSettings = appSettings { showAll = not . showAll $ appSettings }
|
|
|
|
newInternalState = constructList appData newAppSettings (Just appState)
|
2020-10-24 20:03:00 +00:00
|
|
|
in continue (BrickState appData newAppSettings newInternalState appKeys)
|
|
|
|
|
|
|
|
|
|
|
|
showKey :: Vty.Key -> String
|
|
|
|
showKey (Vty.KChar c) = [c]
|
|
|
|
showKey (Vty.KUp) = "↑"
|
|
|
|
showKey (Vty.KDown) = "↓"
|
|
|
|
showKey key = tail (show key)
|
2020-07-06 20:39:16 +00:00
|
|
|
|
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
ui :: BrickState -> Widget String
|
|
|
|
ui BrickState { appSettings = as@(BrickSettings {}), ..}
|
2020-10-11 19:07:13 +00:00
|
|
|
= ( padBottom Max
|
2020-07-06 20:39:16 +00:00
|
|
|
$ ( withBorderStyle unicode
|
|
|
|
$ borderWithLabel (str "GHCup")
|
2020-10-11 19:07:13 +00:00
|
|
|
$ (center $ (header <=> hBorder <=> renderList' appState))
|
2020-07-06 20:39:16 +00:00
|
|
|
)
|
|
|
|
)
|
2020-09-20 21:06:35 +00:00
|
|
|
<=> footer
|
2020-07-06 20:39:16 +00:00
|
|
|
|
|
|
|
where
|
2020-09-20 21:06:35 +00:00
|
|
|
footer =
|
|
|
|
withAttr "help"
|
|
|
|
. txtWrap
|
|
|
|
. T.pack
|
|
|
|
. foldr1 (\x y -> x <> " " <> y)
|
2020-10-24 20:03:00 +00:00
|
|
|
$ (fmap (\(key, s, _) -> (showKey key <> ":" <> s as)) $ keyHandlers appKeys)
|
2020-09-20 21:06:35 +00:00
|
|
|
header =
|
|
|
|
(minHSize 2 $ emptyWidget)
|
|
|
|
<+> (padLeft (Pad 2) $ minHSize 6 $ str "Tool")
|
|
|
|
<+> (minHSize 15 $ str "Version")
|
|
|
|
<+> (padLeft (Pad 1) $ minHSize 25 $ str "Tags")
|
|
|
|
<+> (padLeft (Pad 5) $ str "Notes")
|
2020-10-11 19:07:13 +00:00
|
|
|
renderList' = withDefAttr listAttr . drawListElements renderItem True
|
|
|
|
renderItem _ b listResult@(ListResult {..}) =
|
2020-07-06 20:39:16 +00:00
|
|
|
let marks = if
|
|
|
|
| lSet -> (withAttr "set" $ str "✔✔")
|
|
|
|
| lInstalled -> (withAttr "installed" $ str "✓ ")
|
|
|
|
| otherwise -> (withAttr "not-installed" $ str "✗ ")
|
|
|
|
ver = case lCross of
|
|
|
|
Nothing -> T.unpack . prettyVer $ lVer
|
|
|
|
Just c -> T.unpack (c <> "-" <> prettyVer lVer)
|
2020-07-11 16:53:11 +00:00
|
|
|
dim = if lNoBindist
|
|
|
|
then updateAttrMap (const dimAttributes) . withAttr "no-bindist"
|
|
|
|
else id
|
2020-10-11 19:44:11 +00:00
|
|
|
hooray
|
|
|
|
| elem Latest lTag && not lInstalled =
|
|
|
|
withAttr "hooray"
|
|
|
|
| otherwise = id
|
2020-10-11 19:07:13 +00:00
|
|
|
active = if b then forceAttr "active" else id
|
2020-10-11 19:44:11 +00:00
|
|
|
in hooray $ active $ dim
|
2020-07-11 16:53:11 +00:00
|
|
|
( marks
|
2020-09-20 21:06:35 +00:00
|
|
|
<+> (( padLeft (Pad 2)
|
|
|
|
$ minHSize 6
|
2020-10-11 19:07:13 +00:00
|
|
|
$ (printTool lTool)
|
2020-09-20 21:06:35 +00:00
|
|
|
)
|
2020-07-06 20:39:16 +00:00
|
|
|
)
|
2020-10-11 19:07:13 +00:00
|
|
|
<+> (minHSize 15 $ (str ver))
|
2020-10-09 20:55:33 +00:00
|
|
|
<+> (let l = catMaybes . fmap printTag $ sort lTag
|
2020-10-11 19:07:13 +00:00
|
|
|
in padLeft (Pad 1) $ minHSize 25 $ if null l
|
|
|
|
then emptyWidget
|
|
|
|
else foldr1 (\x y -> x <+> str "," <+> y) l
|
2020-07-11 16:53:11 +00:00
|
|
|
)
|
2020-09-20 15:57:16 +00:00
|
|
|
<+> ( padLeft (Pad 5)
|
|
|
|
$ let notes = printNotes listResult
|
|
|
|
in if null notes
|
|
|
|
then emptyWidget
|
|
|
|
else foldr1 (\x y -> x <+> str "," <+> y) $ notes
|
|
|
|
)
|
2020-10-11 19:16:48 +00:00
|
|
|
<+> (vLimit 1 $ fill ' ')
|
2020-07-11 16:53:11 +00:00
|
|
|
)
|
2020-07-06 20:39:16 +00:00
|
|
|
|
2020-10-11 19:07:13 +00:00
|
|
|
printTag Recommended = Just $ withAttr "recommended" $ str "recommended"
|
|
|
|
printTag Latest = Just $ withAttr "latest" $ str "latest"
|
|
|
|
printTag Prerelease = Just $ withAttr "prerelease" $ str "prerelease"
|
|
|
|
printTag (Base pvp'') = Just $ str ("base-" ++ T.unpack (prettyPVP pvp''))
|
|
|
|
printTag Old = Nothing
|
|
|
|
printTag (UnknownTag t) = Just $ str t
|
|
|
|
|
|
|
|
printTool Cabal = str "cabal"
|
|
|
|
printTool GHC = str "GHC"
|
|
|
|
printTool GHCup = str "GHCup"
|
|
|
|
printTool HLS = str "HLS"
|
2020-07-06 20:39:16 +00:00
|
|
|
|
2020-09-20 21:06:35 +00:00
|
|
|
printNotes ListResult {..} =
|
|
|
|
(if hlsPowered then [withAttr "hls-powered" $ str "hls-powered"] else mempty
|
|
|
|
)
|
|
|
|
++ (if fromSrc then [withAttr "compiled" $ str "compiled"] else mempty)
|
|
|
|
++ (if lStray then [withAttr "stray" $ str "stray"] else mempty)
|
|
|
|
|
2020-10-11 19:07:13 +00:00
|
|
|
-- | Draws the list elements.
|
|
|
|
--
|
|
|
|
-- Evaluates the underlying container up to, and a bit beyond, the
|
|
|
|
-- selected element. The exact amount depends on available height
|
|
|
|
-- for drawing and 'listItemHeight'. At most, it will evaluate up to
|
|
|
|
-- element @(i + h + 1)@ where @i@ is the selected index and @h@ is the
|
|
|
|
-- available height.
|
|
|
|
drawListElements :: (Int -> Bool -> ListResult -> Widget String)
|
|
|
|
-> Bool
|
2020-10-23 23:06:53 +00:00
|
|
|
-> BrickInternalState
|
2020-10-11 19:07:13 +00:00
|
|
|
-> Widget String
|
2020-10-23 23:06:53 +00:00
|
|
|
drawListElements drawElem foc is@(BrickInternalState clr _) =
|
2020-10-11 21:37:27 +00:00
|
|
|
Widget Greedy Greedy $
|
2020-10-11 19:07:13 +00:00
|
|
|
let
|
2020-10-11 21:37:27 +00:00
|
|
|
es = clr
|
2020-10-11 19:07:13 +00:00
|
|
|
listSelected = fmap fst $ listSelectedElement' is
|
|
|
|
|
|
|
|
drawnElements = flip V.imap es $ \i' e ->
|
2020-10-11 21:37:27 +00:00
|
|
|
let addSeparator w = case es !? (i' - 1) of
|
2020-10-11 19:07:13 +00:00
|
|
|
Just e' | lTool e' /= lTool e ->
|
|
|
|
hBorder <=> w
|
|
|
|
_ -> w
|
|
|
|
|
2020-10-11 21:37:27 +00:00
|
|
|
isSelected = Just i' == listSelected
|
|
|
|
elemWidget = drawElem i' isSelected e
|
2020-10-11 19:07:13 +00:00
|
|
|
selItemAttr = if foc
|
|
|
|
then withDefAttr listSelectedFocusedAttr
|
|
|
|
else withDefAttr listSelectedAttr
|
|
|
|
makeVisible = if isSelected then visible . selItemAttr else id
|
|
|
|
in addSeparator $ makeVisible elemWidget
|
|
|
|
|
2020-10-11 21:37:27 +00:00
|
|
|
in render
|
2020-10-11 19:07:13 +00:00
|
|
|
$ viewport "GHCup" Vertical
|
|
|
|
$ vBox
|
|
|
|
$ V.toList drawnElements
|
|
|
|
|
2020-07-06 20:39:16 +00:00
|
|
|
|
|
|
|
minHSize :: Int -> Widget n -> Widget n
|
|
|
|
minHSize s' = hLimit s' . vLimit 1 . (<+> fill ' ')
|
|
|
|
|
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
app :: App BrickState e String
|
2020-07-06 20:39:16 +00:00
|
|
|
app = App { appDraw = \st -> [ui st]
|
|
|
|
, appHandleEvent = eventHandler
|
|
|
|
, appStartEvent = return
|
2020-07-11 16:53:11 +00:00
|
|
|
, appAttrMap = const defaultAttributes
|
2020-07-06 20:39:16 +00:00
|
|
|
, appChooseCursor = neverShowCursor
|
|
|
|
}
|
2020-07-11 16:53:11 +00:00
|
|
|
|
|
|
|
defaultAttributes :: AttrMap
|
|
|
|
defaultAttributes = attrMap
|
|
|
|
Vty.defAttr
|
|
|
|
[ ("active" , Vty.defAttr `Vty.withBackColor` Vty.blue)
|
|
|
|
, ("not-installed", Vty.defAttr `Vty.withForeColor` Vty.red)
|
|
|
|
, ("set" , Vty.defAttr `Vty.withForeColor` Vty.green)
|
|
|
|
, ("installed" , Vty.defAttr `Vty.withForeColor` Vty.green)
|
|
|
|
, ("recommended" , Vty.defAttr `Vty.withForeColor` Vty.green)
|
2020-09-20 15:57:16 +00:00
|
|
|
, ("hls-powered" , Vty.defAttr `Vty.withForeColor` Vty.green)
|
2020-07-11 16:53:11 +00:00
|
|
|
, ("latest" , Vty.defAttr `Vty.withForeColor` Vty.yellow)
|
2020-07-28 18:55:00 +00:00
|
|
|
, ("prerelease" , Vty.defAttr `Vty.withForeColor` Vty.red)
|
2020-09-20 21:06:35 +00:00
|
|
|
, ("compiled" , Vty.defAttr `Vty.withForeColor` Vty.blue)
|
|
|
|
, ("stray" , Vty.defAttr `Vty.withForeColor` Vty.blue)
|
2020-07-11 16:53:11 +00:00
|
|
|
, ("help" , Vty.defAttr `Vty.withStyle` Vty.italic)
|
2020-10-11 19:44:11 +00:00
|
|
|
, ("hooray" , Vty.defAttr `Vty.withForeColor` Vty.brightWhite)
|
2020-07-11 16:53:11 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
dimAttributes :: AttrMap
|
|
|
|
dimAttributes = attrMap
|
|
|
|
(Vty.defAttr `Vty.withStyle` Vty.dim)
|
|
|
|
[ ("active" , Vty.defAttr `Vty.withBackColor` Vty.blue)
|
|
|
|
, ("no-bindist", Vty.defAttr `Vty.withStyle` Vty.dim)
|
|
|
|
]
|
|
|
|
|
2020-10-24 20:03:00 +00:00
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
eventHandler :: BrickState -> BrickEvent n e -> EventM n (Next BrickState)
|
2020-10-24 20:03:00 +00:00
|
|
|
eventHandler st@(BrickState {..}) ev = do
|
|
|
|
AppState { keyBindings = kb } <- liftIO $ readIORef settings'
|
|
|
|
case ev of
|
|
|
|
(MouseDown _ Vty.BScrollUp _ _) ->
|
|
|
|
continue (BrickState { appState = moveCursor 1 appState Up, .. })
|
|
|
|
(MouseDown _ Vty.BScrollDown _ _) ->
|
|
|
|
continue (BrickState { appState = moveCursor 1 appState Down, .. })
|
|
|
|
(VtyEvent (Vty.EvResize _ _)) -> continue st
|
|
|
|
(VtyEvent (Vty.EvKey Vty.KUp _)) ->
|
|
|
|
continue (BrickState { appState = (moveCursor 1 appState Up), .. })
|
|
|
|
(VtyEvent (Vty.EvKey Vty.KDown _)) ->
|
|
|
|
continue (BrickState { appState = (moveCursor 1 appState Down), .. })
|
|
|
|
(VtyEvent (Vty.EvKey key _)) ->
|
|
|
|
case find (\(key', _, _) -> key' == key) (keyHandlers kb) of
|
|
|
|
Nothing -> continue st
|
|
|
|
Just (_, _, handler) -> handler st
|
|
|
|
_ -> continue st
|
|
|
|
|
|
|
|
|
|
|
|
moveCursor :: Int -> BrickInternalState -> Direction -> BrickInternalState
|
|
|
|
moveCursor steps ais@(BrickInternalState {..}) direction =
|
|
|
|
let newIx = if direction == Down then ix + steps else ix - steps
|
2020-10-11 19:07:13 +00:00
|
|
|
in case clr !? newIx of
|
2020-10-23 23:06:53 +00:00
|
|
|
Just _ -> BrickInternalState { ix = newIx, .. }
|
2020-10-11 19:07:13 +00:00
|
|
|
Nothing -> ais
|
2020-10-09 20:55:33 +00:00
|
|
|
|
|
|
|
|
2020-07-06 20:39:16 +00:00
|
|
|
-- | Suspend the current UI and run an IO action in terminal. If the
|
|
|
|
-- IO action returns a Left value, then it's thrown as userError.
|
2020-10-23 23:06:53 +00:00
|
|
|
withIOAction :: (BrickState -> (Int, ListResult) -> IO (Either String a))
|
|
|
|
-> BrickState
|
|
|
|
-> EventM n (Next BrickState)
|
2020-10-11 19:07:13 +00:00
|
|
|
withIOAction action as = case listSelectedElement' (appState as) of
|
2020-07-06 20:39:16 +00:00
|
|
|
Nothing -> continue as
|
|
|
|
Just (ix, e) -> suspendAndResume $ do
|
2020-07-28 19:53:54 +00:00
|
|
|
action as (ix, e) >>= \case
|
|
|
|
Left err -> putStrLn $ ("Error: " <> err)
|
|
|
|
Right _ -> putStrLn "Success"
|
2020-10-11 19:07:13 +00:00
|
|
|
getAppData Nothing (pfreq . appData $ as) >>= \case
|
|
|
|
Right data' -> do
|
2020-07-28 19:53:54 +00:00
|
|
|
putStrLn "Press enter to continue"
|
|
|
|
_ <- getLine
|
2020-10-11 19:07:13 +00:00
|
|
|
pure (updateList data' as)
|
2020-07-28 19:53:54 +00:00
|
|
|
Left err -> throwIO $ userError err
|
2020-07-06 20:39:16 +00:00
|
|
|
|
|
|
|
|
2020-10-11 19:07:13 +00:00
|
|
|
-- | Update app data and list internal state based on new evidence.
|
2020-10-23 23:06:53 +00:00
|
|
|
-- This synchronises @BrickInternalState@ with @BrickData@
|
|
|
|
-- and @BrickSettings@.
|
|
|
|
updateList :: BrickData -> BrickState -> BrickState
|
|
|
|
updateList appD (BrickState {..}) =
|
2020-10-11 19:07:13 +00:00
|
|
|
let newInternalState = constructList appD appSettings (Just appState)
|
2020-10-23 23:06:53 +00:00
|
|
|
in BrickState { appState = newInternalState
|
2020-10-24 20:03:00 +00:00
|
|
|
, appData = appD
|
|
|
|
, appSettings = appSettings
|
|
|
|
, appKeys = appKeys
|
|
|
|
}
|
2020-10-11 19:07:13 +00:00
|
|
|
|
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
constructList :: BrickData
|
|
|
|
-> BrickSettings
|
|
|
|
-> Maybe BrickInternalState
|
|
|
|
-> BrickInternalState
|
2020-10-11 19:07:13 +00:00
|
|
|
constructList appD appSettings mapp =
|
|
|
|
replaceLR (filterVisible (showAll appSettings)) (lr appD) mapp
|
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
listSelectedElement' :: BrickInternalState -> Maybe (Int, ListResult)
|
|
|
|
listSelectedElement' (BrickInternalState {..}) = fmap (ix, ) $ clr !? ix
|
2020-10-11 19:07:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
selectLatest :: Vector ListResult -> Int
|
|
|
|
selectLatest v =
|
|
|
|
case V.findIndex (\ListResult {..} -> lTool == GHC && Latest `elem` lTag) v of
|
|
|
|
Just ix -> ix
|
|
|
|
Nothing -> 0
|
|
|
|
|
|
|
|
|
|
|
|
-- | Replace the @appState@ or construct it based on a filter function
|
|
|
|
-- and a new @[ListResult]@ evidence.
|
|
|
|
-- When passed an existing @appState@, tries to keep the selected element.
|
|
|
|
replaceLR :: (ListResult -> Bool)
|
|
|
|
-> [ListResult]
|
2020-10-23 23:06:53 +00:00
|
|
|
-> Maybe BrickInternalState
|
|
|
|
-> BrickInternalState
|
2020-10-11 19:07:13 +00:00
|
|
|
replaceLR filterF lr s =
|
|
|
|
let oldElem = s >>= listSelectedElement'
|
|
|
|
newVec = V.fromList . filter filterF $ lr
|
|
|
|
newSelected =
|
|
|
|
case oldElem >>= \(_, oldE) -> V.findIndex (toolEqual oldE) newVec of
|
|
|
|
Just ix -> ix
|
|
|
|
Nothing -> selectLatest newVec
|
2020-10-23 23:06:53 +00:00
|
|
|
in BrickInternalState newVec newSelected
|
2020-10-11 19:07:13 +00:00
|
|
|
where
|
|
|
|
toolEqual e1 e2 =
|
|
|
|
lTool e1 == lTool e2 && lVer e1 == lVer e2 && lCross e1 == lCross e2
|
|
|
|
|
|
|
|
|
|
|
|
filterVisible :: Bool -> ListResult -> Bool
|
|
|
|
filterVisible showAll e | lInstalled e = True
|
|
|
|
| showAll = True
|
|
|
|
| otherwise = not (elem Old (lTag e))
|
|
|
|
|
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
install' :: BrickState -> (Int, ListResult) -> IO (Either String ())
|
|
|
|
install' BrickState { appData = BrickData {..} } (_, ListResult {..}) = do
|
2020-07-06 20:39:16 +00:00
|
|
|
settings <- readIORef settings'
|
|
|
|
l <- readIORef logger'
|
|
|
|
let runLogger = myLoggerT l
|
|
|
|
|
2020-10-11 19:07:13 +00:00
|
|
|
let run =
|
|
|
|
runLogger
|
|
|
|
. flip runReaderT settings
|
|
|
|
. runResourceT
|
|
|
|
. runE
|
|
|
|
@'[ AlreadyInstalled
|
2020-07-12 20:29:50 +00:00
|
|
|
#if !defined(TAR)
|
2020-10-11 19:07:13 +00:00
|
|
|
, ArchiveResult
|
2020-07-12 20:29:50 +00:00
|
|
|
#endif
|
2020-10-11 19:07:13 +00:00
|
|
|
, UnknownArchive
|
|
|
|
, FileDoesNotExistError
|
|
|
|
, CopyError
|
|
|
|
, NoDownload
|
|
|
|
, NotInstalled
|
|
|
|
, BuildFailed
|
|
|
|
, TagNotFound
|
|
|
|
, DigestError
|
|
|
|
, DownloadFailed
|
|
|
|
, NoUpdate
|
|
|
|
, TarDirDoesNotExist
|
|
|
|
]
|
2020-07-06 20:39:16 +00:00
|
|
|
|
|
|
|
(run $ do
|
|
|
|
case lTool of
|
2020-07-13 16:27:21 +00:00
|
|
|
GHC -> liftE $ installGHCBin dls lVer pfreq
|
|
|
|
Cabal -> liftE $ installCabalBin dls lVer pfreq
|
|
|
|
GHCup -> liftE $ upgradeGHCup dls Nothing False pfreq $> ()
|
2020-09-20 15:57:16 +00:00
|
|
|
HLS -> liftE $ installHLSBin dls lVer pfreq $> ()
|
2020-07-06 20:39:16 +00:00
|
|
|
)
|
|
|
|
>>= \case
|
|
|
|
VRight _ -> pure $ Right ()
|
|
|
|
VLeft (V (AlreadyInstalled _ _)) -> pure $ Right ()
|
|
|
|
VLeft (V (BuildFailed _ e)) ->
|
|
|
|
pure $ Left [i|Build failed with #{e}|]
|
|
|
|
VLeft (V NoDownload) ->
|
|
|
|
pure $ Left [i|No available version for #{prettyVer lVer}|]
|
|
|
|
VLeft (V NoUpdate) -> pure $ Right ()
|
|
|
|
VLeft e -> pure $ Left [i|#{e}
|
|
|
|
Also check the logs in ~/.ghcup/logs|]
|
|
|
|
|
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
set' :: BrickState -> (Int, ListResult) -> IO (Either String ())
|
2020-07-06 20:39:16 +00:00
|
|
|
set' _ (_, ListResult {..}) = do
|
|
|
|
settings <- readIORef settings'
|
|
|
|
l <- readIORef logger'
|
|
|
|
let runLogger = myLoggerT l
|
|
|
|
|
|
|
|
let run =
|
|
|
|
runLogger
|
|
|
|
. flip runReaderT settings
|
2020-10-11 19:07:13 +00:00
|
|
|
. runE @'[FileDoesNotExistError , NotInstalled , TagNotFound]
|
2020-07-06 20:39:16 +00:00
|
|
|
|
|
|
|
(run $ do
|
|
|
|
case lTool of
|
|
|
|
GHC -> liftE $ setGHC (GHCTargetVersion lCross lVer) SetGHCOnly $> ()
|
|
|
|
Cabal -> liftE $ setCabal lVer $> ()
|
2020-09-20 15:57:16 +00:00
|
|
|
HLS -> liftE $ setHLS lVer $> ()
|
2020-07-06 20:39:16 +00:00
|
|
|
GHCup -> pure ()
|
|
|
|
)
|
|
|
|
>>= \case
|
|
|
|
VRight _ -> pure $ Right ()
|
|
|
|
VLeft e -> pure $ Left [i|#{e}|]
|
|
|
|
|
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
del' :: BrickState -> (Int, ListResult) -> IO (Either String ())
|
2020-07-06 20:39:16 +00:00
|
|
|
del' _ (_, ListResult {..}) = do
|
|
|
|
settings <- readIORef settings'
|
|
|
|
l <- readIORef logger'
|
|
|
|
let runLogger = myLoggerT l
|
|
|
|
|
|
|
|
let run = runLogger . flip runReaderT settings . runE @'[NotInstalled]
|
|
|
|
|
|
|
|
(run $ do
|
|
|
|
case lTool of
|
|
|
|
GHC -> liftE $ rmGHCVer (GHCTargetVersion lCross lVer) $> ()
|
|
|
|
Cabal -> liftE $ rmCabalVer lVer $> ()
|
2020-09-20 15:57:16 +00:00
|
|
|
HLS -> liftE $ rmHLSVer lVer $> ()
|
2020-07-06 20:39:16 +00:00
|
|
|
GHCup -> pure ()
|
|
|
|
)
|
|
|
|
>>= \case
|
|
|
|
VRight _ -> pure $ Right ()
|
|
|
|
VLeft e -> pure $ Left [i|#{e}|]
|
|
|
|
|
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
changelog' :: BrickState -> (Int, ListResult) -> IO (Either String ())
|
|
|
|
changelog' BrickState { appData = BrickData {..} } (_, ListResult {..}) = do
|
2020-07-06 20:39:16 +00:00
|
|
|
case getChangeLog dls lTool (Left lVer) of
|
|
|
|
Nothing -> pure $ Left
|
|
|
|
[i|Could not find ChangeLog for #{lTool}, version #{prettyVer lVer}|]
|
|
|
|
Just uri -> do
|
2020-07-13 21:10:17 +00:00
|
|
|
let cmd = case _rPlatform pfreq of
|
2020-10-11 19:07:13 +00:00
|
|
|
Darwin -> "open"
|
|
|
|
Linux _ -> "xdg-open"
|
|
|
|
FreeBSD -> "xdg-open"
|
2020-07-13 21:10:17 +00:00
|
|
|
exec cmd True [serializeURIRef' uri] Nothing Nothing >>= \case
|
2020-07-06 20:39:16 +00:00
|
|
|
Right _ -> pure $ Right ()
|
|
|
|
Left e -> pure $ Left [i|#{e}|]
|
|
|
|
|
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
settings' :: IORef AppState
|
2020-07-06 20:39:16 +00:00
|
|
|
{-# NOINLINE settings' #-}
|
2020-08-05 19:50:39 +00:00
|
|
|
settings' = unsafePerformIO $ do
|
|
|
|
dirs <- getDirs
|
2020-10-23 23:06:53 +00:00
|
|
|
newIORef $ AppState (Settings { cache = True
|
|
|
|
, noVerify = False
|
|
|
|
, keepDirs = Never
|
|
|
|
, downloader = Curl
|
|
|
|
, verbose = False
|
2020-10-25 13:17:17 +00:00
|
|
|
, urlSource = GHCupURL
|
2020-10-23 23:06:53 +00:00
|
|
|
, ..
|
|
|
|
})
|
|
|
|
dirs
|
2020-10-24 20:03:00 +00:00
|
|
|
defaultKeyBindings
|
2020-10-23 23:06:53 +00:00
|
|
|
|
2020-07-06 20:39:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger' :: IORef LoggerConfig
|
|
|
|
{-# NOINLINE logger' #-}
|
|
|
|
logger' = unsafePerformIO
|
|
|
|
(newIORef $ LoggerConfig { lcPrintDebug = False
|
|
|
|
, colorOutter = \_ -> pure ()
|
|
|
|
, rawOutter = \_ -> pure ()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
brickMain :: AppState
|
2020-10-11 19:07:13 +00:00
|
|
|
-> LoggerConfig
|
|
|
|
-> GHCupDownloads
|
|
|
|
-> PlatformRequest
|
|
|
|
-> IO ()
|
2020-11-20 23:32:26 +00:00
|
|
|
brickMain s l av pfreq' = do
|
2020-07-06 20:39:16 +00:00
|
|
|
writeIORef settings' s
|
|
|
|
-- logger interpreter
|
|
|
|
writeIORef logger' l
|
|
|
|
let runLogger = myLoggerT l
|
|
|
|
|
2020-10-09 20:55:33 +00:00
|
|
|
eAppData <- getAppData (Just av) pfreq'
|
|
|
|
case eAppData of
|
2020-10-11 19:07:13 +00:00
|
|
|
Right ad ->
|
|
|
|
defaultMain
|
|
|
|
app
|
2020-10-23 23:06:53 +00:00
|
|
|
(BrickState ad
|
2020-10-11 19:07:13 +00:00
|
|
|
defaultAppSettings
|
|
|
|
(constructList ad defaultAppSettings Nothing)
|
2020-10-24 20:03:00 +00:00
|
|
|
(keyBindings s)
|
|
|
|
|
2020-10-11 19:07:13 +00:00
|
|
|
)
|
|
|
|
$> ()
|
|
|
|
Left e -> do
|
2020-07-06 20:39:16 +00:00
|
|
|
runLogger ($(logError) [i|Error building app state: #{show e}|])
|
|
|
|
exitWith $ ExitFailure 2
|
|
|
|
|
|
|
|
|
2020-10-23 23:06:53 +00:00
|
|
|
defaultAppSettings :: BrickSettings
|
|
|
|
defaultAppSettings = BrickSettings { showAll = False }
|
2020-10-09 20:55:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
getDownloads' :: IO (Either String GHCupDownloads)
|
|
|
|
getDownloads' = do
|
2020-07-06 20:39:16 +00:00
|
|
|
settings <- readIORef settings'
|
|
|
|
l <- readIORef logger'
|
|
|
|
let runLogger = myLoggerT l
|
|
|
|
|
|
|
|
r <-
|
|
|
|
runLogger
|
|
|
|
. flip runReaderT settings
|
2020-10-11 19:07:13 +00:00
|
|
|
. runE @'[JSONError , DownloadFailed , FileDoesNotExistError]
|
|
|
|
$ fmap _ghcupDownloads
|
|
|
|
$ liftE
|
2020-11-20 23:32:26 +00:00
|
|
|
$ getDownloadsF (urlSource . GT.settings $ settings)
|
2020-07-06 20:39:16 +00:00
|
|
|
|
|
|
|
case r of
|
|
|
|
VRight a -> pure $ Right a
|
|
|
|
VLeft e -> pure $ Left [i|#{e}|]
|
2020-10-09 20:55:33 +00:00
|
|
|
|
|
|
|
|
2020-10-11 19:07:13 +00:00
|
|
|
getAppData :: Maybe GHCupDownloads
|
|
|
|
-> PlatformRequest
|
2020-10-23 23:06:53 +00:00
|
|
|
-> IO (Either String BrickData)
|
2020-10-09 20:55:33 +00:00
|
|
|
getAppData mg pfreq' = do
|
|
|
|
settings <- readIORef settings'
|
|
|
|
l <- readIORef logger'
|
|
|
|
let runLogger = myLoggerT l
|
|
|
|
|
|
|
|
r <- maybe getDownloads' (pure . Right) mg
|
|
|
|
|
2020-10-11 19:07:13 +00:00
|
|
|
runLogger . flip runReaderT settings $ do
|
|
|
|
case r of
|
|
|
|
Right dls -> do
|
|
|
|
lV <- listVersions dls Nothing Nothing pfreq'
|
2020-10-23 23:06:53 +00:00
|
|
|
pure $ Right $ (BrickData (reverse lV) dls pfreq')
|
2020-10-11 19:07:13 +00:00
|
|
|
Left e -> pure $ Left [i|#{e}|]
|
|
|
|
|