2020-04-09 19:53:22 +02:00
|
|
|
{-# LANGUAGE CPP #-}
|
2020-01-11 21:15:05 +01:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
|
{-# LANGUAGE TypeApplications #-}
|
2020-08-05 21:50:39 +02:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2020-01-11 21:15:05 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module Main where
|
|
|
|
|
|
2020-07-06 22:39:16 +02:00
|
|
|
#if defined(BRICK)
|
|
|
|
|
import BrickMain ( brickMain )
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-10-15 22:24:23 +02:00
|
|
|
import GHCup.OptParse
|
|
|
|
|
|
2020-01-11 21:15:05 +01:00
|
|
|
import GHCup.Download
|
|
|
|
|
import GHCup.Errors
|
2020-04-10 17:36:27 +02:00
|
|
|
import GHCup.Platform
|
2020-01-11 21:15:05 +01:00
|
|
|
import GHCup.Types
|
|
|
|
|
import GHCup.Utils
|
2021-11-05 22:57:15 +01:00
|
|
|
import GHCup.Logger
|
|
|
|
|
import GHCup.Prelude
|
|
|
|
|
import GHCup.QQ.String
|
2020-01-11 21:15:05 +01:00
|
|
|
import GHCup.Version
|
|
|
|
|
|
2021-08-27 14:37:44 +02:00
|
|
|
import Cabal.Plan ( findPlanJson, SearchPlanJson(..) )
|
2021-02-22 21:55:05 +01:00
|
|
|
import Control.Concurrent
|
2021-07-21 15:43:45 +02:00
|
|
|
import Control.Concurrent.Async
|
2020-05-15 21:53:45 +02:00
|
|
|
import Control.Exception.Safe
|
2020-04-09 19:53:22 +02:00
|
|
|
#if !MIN_VERSION_base(4,13,0)
|
|
|
|
|
import Control.Monad.Fail ( MonadFail )
|
|
|
|
|
#endif
|
2020-01-11 21:15:05 +01:00
|
|
|
import Control.Monad.Reader
|
2021-08-27 14:37:44 +02:00
|
|
|
import Data.Aeson ( decodeStrict', Value )
|
|
|
|
|
import Data.Aeson.Encode.Pretty ( encodePretty )
|
2020-03-09 22:21:22 +01:00
|
|
|
import Data.Either
|
2020-03-17 01:58:59 +01:00
|
|
|
import Data.Functor
|
2020-04-18 15:05:05 +02:00
|
|
|
import Data.Maybe
|
2020-03-17 22:58:52 +01:00
|
|
|
import GHC.IO.Encoding
|
2020-01-11 21:15:05 +01:00
|
|
|
import Haskus.Utils.Variant.Excepts
|
2020-04-17 16:56:56 +02:00
|
|
|
import Language.Haskell.TH
|
2021-09-04 15:10:07 +02:00
|
|
|
import Language.Haskell.TH.Syntax ( Quasi(qAddDependentFile) )
|
2020-01-11 21:15:05 +01:00
|
|
|
import Options.Applicative hiding ( style )
|
2020-04-17 22:11:41 +02:00
|
|
|
import Options.Applicative.Help.Pretty ( text )
|
2020-01-11 21:15:05 +01:00
|
|
|
import Prelude hiding ( appendFile )
|
|
|
|
|
import System.Environment
|
|
|
|
|
import System.Exit
|
|
|
|
|
import System.IO hiding ( appendFile )
|
2021-03-02 00:15:03 +01:00
|
|
|
import Text.PrettyPrint.HughesPJClass ( prettyShow )
|
2020-01-11 21:15:05 +01:00
|
|
|
|
|
|
|
|
import qualified Data.ByteString as B
|
|
|
|
|
import qualified Data.Text as T
|
|
|
|
|
import qualified Data.Text.IO as T
|
|
|
|
|
import qualified Data.Text.Encoding as E
|
2021-10-30 13:23:02 +02:00
|
|
|
import qualified GHCup.Types as Types
|
2020-04-25 12:06:41 +02:00
|
|
|
|
2020-03-17 01:58:59 +01:00
|
|
|
|
2021-09-20 22:24:20 +02:00
|
|
|
|
2021-05-14 23:09:45 +02:00
|
|
|
toSettings :: Options -> IO (Settings, KeyBindings)
|
2020-10-24 22:03:00 +02:00
|
|
|
toSettings options = do
|
2021-09-24 23:11:51 +02:00
|
|
|
noColor <- isJust <$> lookupEnv "NO_COLOR"
|
2020-10-24 22:03:00 +02:00
|
|
|
userConf <- runE @'[ JSONError ] ghcupConfigFile >>= \case
|
|
|
|
|
VRight r -> pure r
|
|
|
|
|
VLeft (V (JSONDecodeError e)) -> do
|
|
|
|
|
B.hPut stderr ("Error decoding config file: " <> (E.encodeUtf8 . T.pack . show $ e))
|
|
|
|
|
pure defaultUserSettings
|
|
|
|
|
_ -> do
|
|
|
|
|
die "Unexpected error!"
|
2021-09-24 23:11:51 +02:00
|
|
|
pure $ mergeConf options userConf noColor
|
2020-10-24 22:03:00 +02:00
|
|
|
where
|
2021-09-24 23:11:51 +02:00
|
|
|
mergeConf :: Options -> UserSettings -> Bool -> (Settings, KeyBindings)
|
|
|
|
|
mergeConf Options{..} UserSettings{..} noColor =
|
2021-10-30 13:23:02 +02:00
|
|
|
let cache = fromMaybe (fromMaybe (Types.cache defaultSettings) uCache) optCache
|
|
|
|
|
metaCache = fromMaybe (fromMaybe (Types.metaCache defaultSettings) uMetaCache) optMetaCache
|
|
|
|
|
noVerify = fromMaybe (fromMaybe (Types.noVerify defaultSettings) uNoVerify) optNoVerify
|
|
|
|
|
verbose = fromMaybe (fromMaybe (Types.verbose defaultSettings) uVerbose) optVerbose
|
|
|
|
|
keepDirs = fromMaybe (fromMaybe (Types.keepDirs defaultSettings) uKeepDirs) optKeepDirs
|
2020-10-25 14:17:17 +01:00
|
|
|
downloader = fromMaybe (fromMaybe defaultDownloader uDownloader) optsDownloader
|
2020-10-24 22:03:00 +02:00
|
|
|
keyBindings = maybe defaultKeyBindings mergeKeys uKeyBindings
|
2021-10-30 13:23:02 +02:00
|
|
|
urlSource = maybe (fromMaybe (Types.urlSource defaultSettings) uUrlSource) OwnSource optUrlSource
|
|
|
|
|
noNetwork = fromMaybe (fromMaybe (Types.noNetwork defaultSettings) uNoNetwork) optNoNetwork
|
|
|
|
|
gpgSetting = fromMaybe (fromMaybe (Types.gpgSetting defaultSettings) uGPGSetting) optGpg
|
2021-05-14 23:09:45 +02:00
|
|
|
in (Settings {..}, keyBindings)
|
2020-10-24 22:03:00 +02:00
|
|
|
#if defined(INTERNAL_DOWNLOADER)
|
|
|
|
|
defaultDownloader = Internal
|
|
|
|
|
#else
|
|
|
|
|
defaultDownloader = Curl
|
|
|
|
|
#endif
|
|
|
|
|
mergeKeys :: UserKeyBindings -> KeyBindings
|
|
|
|
|
mergeKeys UserKeyBindings {..} =
|
|
|
|
|
let KeyBindings {..} = defaultKeyBindings
|
|
|
|
|
in KeyBindings {
|
|
|
|
|
bUp = fromMaybe bUp kUp
|
|
|
|
|
, bDown = fromMaybe bDown kDown
|
|
|
|
|
, bQuit = fromMaybe bQuit kQuit
|
|
|
|
|
, bInstall = fromMaybe bInstall kInstall
|
|
|
|
|
, bUninstall = fromMaybe bUninstall kUninstall
|
|
|
|
|
, bSet = fromMaybe bSet kSet
|
|
|
|
|
, bChangelog = fromMaybe bChangelog kChangelog
|
2021-05-15 00:31:36 +02:00
|
|
|
, bShowAllVersions = fromMaybe bShowAllVersions kShowAll
|
|
|
|
|
, bShowAllTools = fromMaybe bShowAllTools kShowAllTools
|
2020-10-24 22:03:00 +02:00
|
|
|
}
|
2020-01-11 21:15:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-08-27 14:37:44 +02:00
|
|
|
plan_json :: String
|
2021-09-04 15:27:57 +02:00
|
|
|
plan_json = $( do
|
|
|
|
|
(fp, c) <- runIO (handleIO (\_ -> pure ("", "")) $ do
|
2021-08-27 14:37:44 +02:00
|
|
|
fp <- findPlanJson (ProjectRelativeToDir ".")
|
|
|
|
|
c <- B.readFile fp
|
|
|
|
|
(Just res) <- pure $ decodeStrict' @Value c
|
2021-09-04 15:27:57 +02:00
|
|
|
pure (fp, T.unpack $ decUTF8Safe' $ encodePretty res))
|
2021-10-15 22:24:23 +02:00
|
|
|
unless (null fp) $ qAddDependentFile fp
|
2021-09-04 15:27:57 +02:00
|
|
|
pure . LitE . StringL $ c)
|
2021-08-27 14:37:44 +02:00
|
|
|
|
2020-01-11 21:15:05 +01:00
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
|
main = do
|
2021-05-14 23:09:45 +02:00
|
|
|
-- https://gitlab.haskell.org/ghc/ghc/issues/8118
|
|
|
|
|
setLocaleEncoding utf8
|
|
|
|
|
|
|
|
|
|
void enableAnsiSupport
|
|
|
|
|
|
2020-04-17 18:54:21 +02:00
|
|
|
let versionHelp = infoOption
|
2021-10-15 22:24:23 +02:00
|
|
|
( "The GHCup Haskell installer, version " <> (head . lines $ describe_result)
|
2020-04-17 18:54:21 +02:00
|
|
|
)
|
2020-04-17 22:11:41 +02:00
|
|
|
(long "version" <> help "Show version" <> hidden)
|
2021-08-27 14:37:44 +02:00
|
|
|
let planJson = infoOption
|
|
|
|
|
plan_json
|
|
|
|
|
(long "plan-json" <> help "Show the build-time configuration" <> internal)
|
2020-04-17 16:56:56 +02:00
|
|
|
let numericVersionHelp = infoOption
|
|
|
|
|
numericVer
|
|
|
|
|
( long "numeric-version"
|
|
|
|
|
<> help "Show the numeric version (for use in scripts)"
|
2020-04-17 22:11:41 +02:00
|
|
|
<> hidden
|
2020-04-17 16:56:56 +02:00
|
|
|
)
|
2020-04-22 20:12:57 +02:00
|
|
|
let listCommands = infoOption
|
|
|
|
|
"install set rm install-cabal list upgrade compile debug-info tool-requirements changelog"
|
|
|
|
|
( long "list-commands"
|
|
|
|
|
<> help "List available commands for shell completion"
|
|
|
|
|
<> internal
|
|
|
|
|
)
|
2020-01-11 21:15:05 +01:00
|
|
|
|
2020-07-04 21:49:59 +02:00
|
|
|
let main_footer = [s|Discussion:
|
2020-04-17 22:11:41 +02:00
|
|
|
ghcup installs the Glasgow Haskell Compiler from the official
|
|
|
|
|
release channels, enabling you to easily switch between different
|
2020-04-22 16:14:10 +02:00
|
|
|
versions. It maintains a self-contained ~/.ghcup directory.
|
|
|
|
|
|
|
|
|
|
ENV variables:
|
|
|
|
|
* TMPDIR: where ghcup does the work (unpacking, building, ...)
|
|
|
|
|
* GHCUP_INSTALL_BASE_PREFIX: the base of ghcup (default: $HOME)
|
2020-10-25 10:54:57 +01:00
|
|
|
* GHCUP_USE_XDG_DIRS: set to anything to use XDG style directories
|
2020-04-17 22:11:41 +02:00
|
|
|
|
|
|
|
|
Report bugs at <https://gitlab.haskell.org/haskell/ghcup-hs/issues>|]
|
|
|
|
|
|
2020-04-17 16:56:56 +02:00
|
|
|
customExecParser
|
|
|
|
|
(prefs showHelpOnError)
|
2021-08-27 14:37:44 +02:00
|
|
|
(info (opts <**> helper <**> versionHelp <**> numericVersionHelp <**> planJson <**> listCommands)
|
2020-04-18 15:05:05 +02:00
|
|
|
(footerDoc (Just $ text main_footer))
|
|
|
|
|
)
|
2020-01-11 21:15:05 +01:00
|
|
|
>>= \opt@Options {..} -> do
|
2021-07-18 14:39:49 +02:00
|
|
|
dirs@Dirs{..} <- getAllDirs
|
2021-05-14 23:09:45 +02:00
|
|
|
|
2020-03-17 19:16:21 +01:00
|
|
|
-- create ~/.ghcup dir
|
2021-06-13 13:41:06 +02:00
|
|
|
ensureDirectories dirs
|
|
|
|
|
|
|
|
|
|
(settings, keybindings) <- toSettings opt
|
2020-03-17 19:16:21 +01:00
|
|
|
|
2020-01-11 21:15:05 +01:00
|
|
|
-- logger interpreter
|
2021-10-15 22:24:23 +02:00
|
|
|
logfile <- runReaderT initGHCupFileLogging dirs
|
2021-09-23 12:16:49 +02:00
|
|
|
no_color <- isJust <$> lookupEnv "NO_COLOR"
|
2020-07-06 22:39:16 +02:00
|
|
|
let loggerConfig = LoggerConfig
|
2020-10-24 22:03:00 +02:00
|
|
|
{ lcPrintDebug = verbose settings
|
2021-09-23 12:16:49 +02:00
|
|
|
, consoleOutter = T.hPutStr stderr
|
|
|
|
|
, fileOutter =
|
2021-07-02 23:26:07 +02:00
|
|
|
case optCommand of
|
|
|
|
|
Nuke -> \_ -> pure ()
|
2021-08-30 22:41:58 +02:00
|
|
|
_ -> T.appendFile logfile
|
2021-09-23 12:16:49 +02:00
|
|
|
, fancyColors = not no_color
|
2020-01-11 21:15:05 +01:00
|
|
|
}
|
2021-08-30 22:41:58 +02:00
|
|
|
let leanAppstate = LeanAppState settings dirs keybindings loggerConfig
|
|
|
|
|
let runLogger = flip runReaderT leanAppstate
|
2021-09-23 12:16:49 +02:00
|
|
|
let siletRunLogger = flip runReaderT (leanAppstate { loggerConfig = loggerConfig { consoleOutter = \_ -> pure () } } :: LeanAppState)
|
2021-05-14 23:09:45 +02:00
|
|
|
|
2021-07-15 13:32:48 +02:00
|
|
|
|
|
|
|
|
-------------------------
|
|
|
|
|
-- Setting up appstate --
|
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
|
|
|
2021-07-18 14:39:49 +02:00
|
|
|
appState = do
|
|
|
|
|
pfreq <- (
|
|
|
|
|
runLogger . runE @'[NoCompatiblePlatform, NoCompatibleArch, DistroNotFound] . liftE $ platformRequest
|
|
|
|
|
) >>= \case
|
|
|
|
|
VRight r -> pure r
|
|
|
|
|
VLeft e -> do
|
|
|
|
|
runLogger
|
2021-08-30 22:41:58 +02:00
|
|
|
(logError $ T.pack $ prettyShow e)
|
2021-07-18 14:39:49 +02:00
|
|
|
exitWith (ExitFailure 2)
|
|
|
|
|
|
|
|
|
|
ghcupInfo <-
|
2021-08-30 22:41:58 +02:00
|
|
|
( flip runReaderT leanAppstate
|
2021-09-18 19:45:32 +02:00
|
|
|
. runE @'[DigestError, GPGError, JSONError , DownloadFailed, FileDoesNotExistError]
|
2021-10-15 22:24:23 +02:00
|
|
|
$ liftE getDownloadsF
|
2021-07-18 14:39:49 +02:00
|
|
|
)
|
|
|
|
|
>>= \case
|
|
|
|
|
VRight r -> pure r
|
|
|
|
|
VLeft e -> do
|
|
|
|
|
runLogger
|
2021-08-30 22:41:58 +02:00
|
|
|
(logError $ T.pack $ prettyShow e)
|
2021-07-18 14:39:49 +02:00
|
|
|
exitWith (ExitFailure 2)
|
2021-08-30 22:41:58 +02:00
|
|
|
let s' = AppState settings dirs keybindings ghcupInfo pfreq loggerConfig
|
2021-05-14 23:09:45 +02:00
|
|
|
|
2021-10-15 22:24:23 +02:00
|
|
|
race_ (liftIO $ runReaderT cleanupTrash s')
|
2021-08-30 22:41:58 +02:00
|
|
|
(threadDelay 5000000 >> runLogger (logWarn $ "Killing cleanup thread (exceeded 5s timeout)... please remove leftover files in " <> T.pack recycleDir <> " manually"))
|
2021-07-21 15:43:45 +02:00
|
|
|
|
2021-07-26 18:13:20 +02:00
|
|
|
case optCommand of
|
|
|
|
|
Nuke -> pure ()
|
|
|
|
|
Whereis _ _ -> pure ()
|
|
|
|
|
DInfo -> pure ()
|
|
|
|
|
ToolRequirements -> pure ()
|
|
|
|
|
ChangeLog _ -> pure ()
|
2021-09-27 12:52:45 +02:00
|
|
|
UnSet _ -> pure ()
|
2021-07-26 18:13:20 +02:00
|
|
|
#if defined(BRICK)
|
|
|
|
|
Interactive -> pure ()
|
|
|
|
|
#endif
|
|
|
|
|
_ -> lookupEnv "GHCUP_SKIP_UPDATE_CHECK" >>= \case
|
2021-10-15 22:24:23 +02:00
|
|
|
Nothing -> runReaderT checkForUpdates s'
|
2021-07-26 18:13:20 +02:00
|
|
|
Just _ -> pure ()
|
2021-07-15 13:32:48 +02:00
|
|
|
|
2021-07-18 14:39:49 +02:00
|
|
|
-- TODO: always run for windows
|
2021-10-15 22:24:23 +02:00
|
|
|
siletRunLogger (flip runReaderT s' $ runE ensureGlobalTools) >>= \case
|
2021-07-18 14:39:49 +02:00
|
|
|
VRight _ -> pure ()
|
|
|
|
|
VLeft e -> do
|
|
|
|
|
runLogger
|
2021-08-30 22:41:58 +02:00
|
|
|
(logError $ T.pack $ prettyShow e)
|
2021-07-18 14:39:49 +02:00
|
|
|
exitWith (ExitFailure 30)
|
|
|
|
|
pure s'
|
2021-07-15 13:32:48 +02:00
|
|
|
|
2021-05-14 23:09:45 +02:00
|
|
|
|
2021-07-18 23:29:09 +02:00
|
|
|
#if defined(IS_WINDOWS)
|
|
|
|
|
-- FIXME: windows needs 'ensureGlobalTools', which requires
|
|
|
|
|
-- full appstate
|
|
|
|
|
runLeanAppState = runAppState
|
|
|
|
|
#else
|
2021-07-18 14:39:49 +02:00
|
|
|
runLeanAppState = flip runReaderT leanAppstate
|
2021-07-18 23:29:09 +02:00
|
|
|
#endif
|
2021-07-18 14:39:49 +02:00
|
|
|
runAppState action' = do
|
|
|
|
|
s' <- liftIO appState
|
2021-10-15 22:24:23 +02:00
|
|
|
runReaderT action' s'
|
2021-07-18 14:39:49 +02:00
|
|
|
|
2021-05-14 23:09:45 +02:00
|
|
|
|
2021-10-15 22:24:23 +02:00
|
|
|
-----------------
|
|
|
|
|
-- Run command --
|
|
|
|
|
-----------------
|
2020-05-11 00:18:53 +02:00
|
|
|
|
|
|
|
|
res <- case optCommand of
|
2020-07-06 22:39:16 +02:00
|
|
|
#if defined(BRICK)
|
2021-05-14 23:09:45 +02:00
|
|
|
Interactive -> do
|
2021-07-18 23:29:09 +02:00
|
|
|
s' <- appState
|
2021-08-30 22:41:58 +02:00
|
|
|
liftIO $ brickMain s' >> pure ExitSuccess
|
2020-07-06 22:39:16 +02:00
|
|
|
#endif
|
2021-10-15 22:24:23 +02:00
|
|
|
Install installCommand -> install installCommand settings appState runLogger
|
|
|
|
|
InstallCabalLegacy iopts -> install (Left (InstallCabal iopts)) settings appState runLogger
|
|
|
|
|
Set setCommand -> set setCommand runAppState runLeanAppState runLogger
|
|
|
|
|
UnSet unsetCommand -> unset unsetCommand runLeanAppState runLogger
|
|
|
|
|
List lo -> list lo no_color runAppState
|
|
|
|
|
Rm rmCommand -> rm rmCommand runAppState runLogger
|
|
|
|
|
DInfo -> dinfo runAppState runLogger
|
|
|
|
|
Compile compileCommand -> compile compileCommand settings runAppState runLogger
|
|
|
|
|
Config configCommand -> config configCommand settings keybindings runLogger
|
|
|
|
|
Whereis whereisOptions
|
|
|
|
|
whereisCommand -> whereis whereisCommand whereisOptions runAppState leanAppstate runLogger
|
|
|
|
|
Upgrade uOpts force' -> upgrade uOpts force' runAppState runLogger
|
|
|
|
|
ToolRequirements -> toolRequirements runAppState runLogger
|
|
|
|
|
ChangeLog changelogOpts -> changelog changelogOpts runAppState runLogger
|
|
|
|
|
Nuke -> nuke appState runLogger
|
|
|
|
|
Prefetch pfCom -> prefetch pfCom runAppState runLogger
|
|
|
|
|
GC gcOpts -> gc gcOpts runAppState runLogger
|
2021-06-18 15:09:01 +05:30
|
|
|
|
2020-04-17 18:26:55 +02:00
|
|
|
case res of
|
|
|
|
|
ExitSuccess -> pure ()
|
2020-04-17 20:50:23 +02:00
|
|
|
ef@(ExitFailure _) -> exitWith ef
|
2021-06-18 15:01:32 +05:30
|
|
|
|
2020-01-11 21:15:05 +01:00
|
|
|
pure ()
|
|
|
|
|
|
2020-04-17 18:26:55 +02:00
|
|
|
|