packages feed

ghcup-0.2.7.0 / lib-opt / GHCup / OptParse / Common.hs

{-# LANGUAGE CPP #-}{-# LANGUAGE DataKinds #-}{-# LANGUAGE DuplicateRecordFields #-}{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE NumericUnderscores #-}{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE TypeApplications #-}module GHCup.OptParse.Common whereimport GHCup.CabalConfigimport GHCup.Command.Listimport GHCup.Downloadimport GHCup.Hardcoded.Versionimport GHCup.Input.Parsersimport GHCup.Preludeimport GHCup.Prelude.Processimport GHCup.Query.GHCupDirsimport GHCup.Query.Metadataimport GHCup.Query.Systemimport GHCup.System.Directoryimport GHCup.Typesimport GHCup.Types.Opticsimport qualified GHCup.Input.Parsers as Parsersimport Control.Concurrentimport Control.Concurrent.Asyncimport Control.DeepSeqimport Control.Exception        ( evaluate )import Control.Exception.Safeimport Control.Monad            ( forM, join )#if !MIN_VERSION_base(4,13,0)import Control.Monad.Fail ( MonadFail )#endifimport Control.Monad.Readerimport Data.Aeson#if MIN_VERSION_aeson(2,0,0)import qualified Data.Aeson.Key    as KMimport qualified Data.Aeson.KeyMap as KM#elseimport qualified Data.HashMap.Strict as KM#endifimport           Data.Bifunctorimport           Data.ByteString.Lazy ( ByteString )import           Data.Charimport           Data.Eitherimport           Data.Functorimport           Data.List            ( isPrefixOf, nub, stripPrefix )import           Data.Maybeimport           Data.Variant.Exceptsimport qualified Data.Vector          as Vimport           Data.Versionsimport           GHC.IO.Exceptionimport           Options.Applicative  hiding ( style )import           Prelude              hiding ( appendFile )import           Safe                 ( lastMay )import           System.FilePathimport           System.Process       ( readProcess )import           Text.HTML.TagSoup    hiding ( Tag )import qualified Data.Map.Strict       as Mimport qualified Data.Text             as Timport qualified System.FilePath.Posix as FP    --------------    --[ Parser ]--    --------------parseUrlSourceP :: Parser [NewURLSource]parseUrlSourceP =    option      (eitherReader parseUrlSource)      (  short 's'      <> long "url-source"      <> metavar "<URL_SOURCE|cross|prereleases|vanilla|default>"      <> help "Alternative ghcup download info"      <> completer urlSourceCompleter      )toolVersionTagArgument :: [ListCriteria] -> Maybe Tool -> Parser ToolVersiontoolVersionTagArgument criteria tool =  argument (eitherReader (parser tool))    (metavar (mv tool)    <> completer (tagCompleter (fromMaybe ghc tool) [])    <> foldMap (completer . versionCompleter criteria) tool) where  mv (Just (Tool "ghc")) = "GHC_VERSION|TAG|RELEASE_DATE"  mv (Just (Tool "hls")) = "HLS_VERSION|TAG|RELEASE_DATE"  mv _                   = "VERSION|TAG|RELEASE_DATE"  parser (Just (Tool "ghc")) = Parsers.ghcVersionTagEither  parser Nothing             = Parsers.ghcVersionTagEither  parser _                   = Parsers.toolVersionTagEitherversionParser' :: [ListCriteria] -> Maybe Tool -> Parser VersionversionParser' criteria tool = argument  (eitherReader (first show . version . T.pack))  (metavar "VERSION"  <> foldMap (completer . versionCompleter criteria) tool)ghcVersionArgument :: [ListCriteria] -> Maybe Tool -> Parser TargetVersionghcVersionArgument criteria tool = argument (eitherReader Parsers.ghcVersionEither)                                            (metavar "VERSION" <> help "Which version to install" <> foldMap (completer . versionCompleter criteria) tool)-- https://github.com/pcapriotti/optparse-applicative/issues/148-- | A switch that can be enabled using --foo and disabled using --no-foo.---- The option modifier is applied to only the option that is *not* enabled-- by default. For example:---- > invertableSwitch "recursive" True (help "do not recurse into directories")---- This example makes --recursive enabled by default, so-- the help is shown only for --no-recursive.invertableSwitch    :: String              -- ^ long option    -> Maybe Char          -- ^ short option for the non-default option    -> Bool                -- ^ is switch enabled by default?    -> Mod FlagFields Bool -- ^ option modifier    -> Parser (Maybe Bool)invertableSwitch longopt shortopt defv optmod = invertableSwitch' longopt shortopt defv    (if defv then mempty else optmod)    (if defv then optmod else mempty)-- | Allows providing option modifiers for both --foo and --no-foo.invertableSwitch'    :: String              -- ^ long option (eg "foo")    -> Maybe Char          -- ^ short option for the non-default option    -> Bool                -- ^ is switch enabled by default?    -> Mod FlagFields Bool -- ^ option modifier for --foo    -> Mod FlagFields Bool -- ^ option modifier for --no-foo    -> Parser (Maybe Bool)invertableSwitch' longopt shortopt defv enmod dismod = optional    ( flag' True ( enmod <> long longopt <> if defv then mempty else maybe mempty short shortopt)    <|> flag' False (dismod <> long nolongopt <> if defv then maybe mempty short shortopt else mempty)    )  where    nolongopt = "no-" ++ longopt    ------------------    --[ Completers ]--    ------------------revisionCompleter :: CompleterrevisionCompleter = listCompleter ["updates", "all", "none"]nightlyCompleter :: CompleternightlyCompleter = listCompleter ["latest", "all", "none"]docsCompleter :: CompleterdocsCompleter = listCompleter ["none", "no-haddocks", "no-sphinx", "no-sphinx-html", "no-sphinx-pdfs", "no-sphinx-man"]toolCompleter :: CompletertoolCompleter = listCompleter ["ghc", "cabal", "hls", "stack"]gitFileUri :: [String] -> CompletergitFileUri add = mkCompleter $ fileUri' (["git://"] <> add)urlSourceCompleter :: CompleterurlSourceCompleter = mkCompleter $ urlSourceCompleter' []urlSourceCompleter' :: [String] -> String -> IO [String]urlSourceCompleter' add str' = do  let static = ["GHCupURL", "StackSetupURL", "cross", "prereleases", "vanilla"]  file <- fileUri' add str'  pure $ static ++ filefileUri :: CompleterfileUri = mkCompleter $ fileUri' []fileUri' :: [String] -> String -> IO [String]fileUri' add = \case  "" -> do    pwd <- getCurrentDirectory    pure $ ["https://", "http://", "file:///", "file://" <> pwd <> "/"] <> add  xs   | "file:///" `isPrefixOf` xs -> fmap ("file://" <>) <$>      case stripPrefix "file://" xs of        Nothing -> pure []        Just r ->  do          pwd <- getCurrentDirectory          dirs  <- compgen "directory" r ["-S", "/"]          files <- filter (\f -> (f <> "/") `notElem` dirs) <$> compgen "file" r []          pure (dirs <> files <> if r `isPrefixOf` pwd then [pwd <> "/"] else [])   | xs `isPrefixOf` "file:///" -> pure ["file:///"]   | xs `isPrefixOf` "https://" -> pure ["https://"]   | xs `isPrefixOf` "http://"  -> pure ["http://"]   | otherwise -> pure [] where  compgen :: String -> String -> [String] -> IO [String]  compgen action' r opts = do    let cmd = unwords $ ["compgen", "-A", action'] <> opts <> ["--", requote r]    result <- tryIO $ readProcess "bash" ["-c", cmd] ""    return . lines . fromRight [] $ result  -- | Strongly quote the string we pass to compgen.  --  -- We need to do this so bash doesn't expand out any ~ or other  -- chars we want to complete on, or emit an end of line error  -- when seeking the close to the quote.  --  -- NOTE: copied from https://hackage.haskell.org/package/optparse-applicative-0.17.0.0/docs/src/Options.Applicative.Builder.Completer.html#requote  requote :: String -> String  requote s =    let      -- Bash doesn't appear to allow "mixed" escaping      -- in bash completions. So we don't have to really      -- worry about people swapping between strong and      -- weak quotes.      unescaped =        case s of          -- It's already strongly quoted, so we          -- can use it mostly as is, but we must          -- ensure it's closed off at the end and          -- there's no single quotes in the          -- middle which might confuse bash.          ('\'': rs) -> unescapeN rs          -- We're weakly quoted.          ('"': rs)  -> unescapeD rs          -- We're not quoted at all.          -- We need to unescape some characters like          -- spaces and quotation marks.          elsewise   -> unescapeU elsewise    in      strong unescaped    where      strong ss = '\'' : foldr go "'" ss        where          -- If there's a single quote inside the          -- command: exit from the strong quote and          -- emit it the quote escaped, then resume.          go '\'' t = "'\\''" ++ t          go h t    = h : t      -- Unescape a strongly quoted string      -- We have two recursive functions, as we      -- can enter and exit the strong escaping.      unescapeN = goX        where          goX ('\'' : xs) = goN xs          goX (x : xs)    = x : goX xs          goX []          = []          goN ('\\' : '\'' : xs) = '\'' : goN xs          goN ('\'' : xs)        = goX xs          goN (x : xs)           = x : goN xs          goN []                 = []      -- Unescape an unquoted string      unescapeU = goX        where          goX []              = []          goX ('\\' : x : xs) = x : goX xs          goX (x : xs)        = x : goX xs      -- Unescape a weakly quoted string      unescapeD = goX        where          -- Reached an escape character          goX ('\\' : x : xs)            -- If it's true escapable, strip the            -- slashes, as we're going to strong            -- escape instead.            | x `elem` ("$`\"\\\n" :: String) = x : goX xs            | otherwise = '\\' : x : goX xs          -- We've ended quoted section, so we          -- don't recurse on goX, it's done.          goX ('"' : xs)            = xs          -- Not done, but not a special character          -- just continue the fold.          goX (x : xs)            = x : goX xs          goX []            = []tagCompleter :: Tool -> [String] -> CompletertagCompleter tool add = listIOCompleter $ do  dirs' <- liftIO getAllDirs  let loggerConfig = LoggerConfig        { lcPrintDebugLvl = Nothing        , consoleOutter  = mempty        , fileOutter     = mempty        , fancyColors    = False        , consoleAnsi    = False        }  mpFreq <- flip runReaderT loggerConfig . runE $ platformRequest  forFold mpFreq $ \pfreq -> do    let appState = LeanAppState          (defaultSettings { noNetwork = True })          dirs'          defaultKeyBindings          pfreq          loggerConfig    mGhcUpInfo <- flip runReaderT appState . runE $ getDownloadsF pfreq    case mGhcUpInfo of      VRight ghcupInfo -> do        let allTags = filter (/= Old)              $ _vmTags =<< M.elems (availableToolVersions (_ghcupDownloads ghcupInfo) tool)        pure $ nub $ (add ++) $ fmap tagToString allTags      VLeft _ -> pure  (nub $ ["recommended", "latest", "latest-prerelease"] ++ add)versionCompleter :: [ListCriteria] -> Tool -> CompleterversionCompleter criteria tool = versionCompleter' criteria tool (const True)versionCompleter' :: [ListCriteria] -> Tool -> (Version -> Bool) -> CompleterversionCompleter' criteria tool filter' = listIOCompleter $ do  dirs' <- liftIO getAllDirs  let loggerConfig = LoggerConfig        { lcPrintDebugLvl = Nothing        , consoleOutter  = mempty        , fileOutter     = mempty        , fancyColors    = False        , consoleAnsi    = False        }  mpFreq <- flip runReaderT loggerConfig . runE $ platformRequest  let settings = defaultSettings { noNetwork = True }  forFold mpFreq $ \pfreq -> do    let leanAppState = LeanAppState                     settings                     dirs'                     defaultKeyBindings                     pfreq                     loggerConfig    mGhcUpInfo <- flip runReaderT leanAppState . runE $ getDownloadsF pfreq    forFold mGhcUpInfo $ \ghcupInfo -> do      let appState = AppState            settings            dirs'            defaultKeyBindings            ghcupInfo            pfreq            loggerConfig          runEnv = flip runReaderT appState . runE      (VRight installedVersions) <- runEnv $ listVersions (Just [tool]) criteria ShowUpdates False NShowNone (Nothing, Nothing)      return $ fmap (T.unpack . prettyVer) . filter filter' . maybe [] (fmap lVer . snd) $ M.lookup tool installedVersionstoolDlCompleter :: Tool -> CompletertoolDlCompleter tool = mkCompleter $ \case  "" -> pure (initUrl tool <> ["https://", "http://", "file:///"])  word    | "file://" `isPrefixOf` word -> fileUri' [] word    -- downloads.haskell.org    | "https://downloads.haskell.org/" `isPrefixOf` word ->        fmap (completePrefix word) . prefixMatch (FP.takeFileName word) <$> fromHRef word    -- github releases    | "https://github.com/haskell/haskell-language-server/releases/download/" `isPrefixOf` word    , let xs = splitPath word    , (length xs == 6 && last word == '/') || (length xs == 7 && last word /= '/') ->        fmap (\x -> completePrefix word x <> "/") . prefixMatch (FP.takeFileName word) <$> getGithubReleases "haskell" "haskell-language-server"    | "https://github.com/commercialhaskell/stack/releases/download/" == word    , let xs = splitPath word    , (length xs == 6 && last word == '/') || (length xs == 7 && last word /= '/') ->        fmap (\x -> completePrefix word x <> "/") . prefixMatch (FP.takeFileName word) <$> getGithubReleases "commercialhaskell" "stack"    -- github release assets    | "https://github.com/haskell/haskell-language-server/releases/download/" `isPrefixOf` word    , let xs = splitPath word    , (length xs == 7 && last word == '/') || length xs == 8    , let rel = xs !! 6    , length rel > 1 -> do        fmap (completePrefix word) . prefixMatch (FP.takeFileName word) <$> getGithubAssets "haskell" "haskell-language-server" (init rel)    | "https://github.com/commercialhaskell/stack/releases/download/" `isPrefixOf` word    , let xs = splitPath word    , (length xs == 7 && last word == '/') || length xs == 8    , let rel = xs !! 6    , length rel > 1 -> do        fmap (completePrefix word) . prefixMatch (FP.takeFileName word) <$> getGithubAssets "commercialhaskell" "stack" (init rel)    -- github    | "https://github.com/c" `isPrefixOf` word -> pure ["https://github.com/commercialhaskell/stack/releases/download/"]    | "https://github.com/h" `isPrefixOf` word -> pure ["https://github.com/haskell/haskell-language-server/releases/download/"]    | "https://g" `isPrefixOf` word    , tool == stack -> pure ["https://github.com/commercialhaskell/stack/releases/download/"]    | "https://g" `isPrefixOf` word    , tool == hls -> pure ["https://github.com/haskell/haskell-language-server/releases/download/"]    | "https://d" `isPrefixOf` word -> pure $ filter ("https://downloads.haskell.org/" `isPrefixOf`) $ initUrl tool    | "h" `isPrefixOf` word -> pure $ initUrl tool    | word `isPrefixOf` "file:///" -> pure ["file:///"]    | word `isPrefixOf` "https://" -> pure ["https://"]    | word `isPrefixOf` "http://"  -> pure ["http://"]    | otherwise -> pure [] where  initUrl :: Tool -> [String]  initUrl (Tool "ghc")   = [ "https://downloads.haskell.org/~ghc/"                           , "https://downloads.haskell.org/~ghcup/unofficial-bindists/ghc/"                           ]  initUrl (Tool "cabal") = [ "https://downloads.haskell.org/~cabal/"                           , "https://downloads.haskell.org/~ghcup/unofficial-bindists/cabal/"                           ]  initUrl (Tool "ghcup") = [ "https://downloads.haskell.org/~ghcup/" ]  initUrl (Tool "hls")   = [ "https://github.com/haskell/haskell-language-server/releases/download/"                           , "https://downloads.haskell.org/~ghcup/unofficial-bindists/haskell-language-server/"                           ]  initUrl (Tool "stack") = [ "https://github.com/commercialhaskell/stack/releases/download/"                           , "https://downloads.haskell.org/~ghcup/unofficial-bindists/stack/"                           ]  initUrl _              = []  completePrefix :: String -- ^ url, e.g.    'https://github.com/haskell/haskell-languag'                 -> String -- ^ match, e.g.  'haskell-language-server'                 -> String -- ^ result, e.g. 'https://github.com/haskell/haskell-language-server'  completePrefix url match =    let base = FP.takeDirectory url        fn   = FP.takeFileName url    in if fn `isPrefixOf` match then base <> "/" <> match else url  prefixMatch :: String -> [String] -> [String]  prefixMatch pref = filter (pref `isPrefixOf`)  fromHRef :: String -> IO [String]  fromHRef url = withCurl (FP.takeDirectory url) 2_000_000 $ \stdout ->      pure        . fmap (T.unpack . decUTF8Safe' . fromAttrib "href")        . filter isTagOpen        . filter (~== ("<a href>" :: String))        . parseTags        $ stdout  withCurl :: String                      -- ^ url           -> Int                         -- ^ delay           -> (ByteString -> IO [String]) -- ^ callback           -> IO [String]  withCurl url delay cb = do    let limit = threadDelay delay    race limit (executeOut "curl" ["-fL", url] Nothing) >>= \case      Right (CapturedProcess {_exitCode, _stdOut}) -> do        case _exitCode of          ExitSuccess ->            (try @_ @SomeException . cb $ _stdOut) >>= \case              Left _ ->  pure []              Right r' -> do                r <- try @_ @SomeException                  . evaluate                  . force                  $ r'                either (\_ -> pure []) pure r          ExitFailure _ -> pure []      Left _ -> pure []  getGithubReleases :: String                    -> String                    -> IO [String]  getGithubReleases owner repo = withCurl url 3_000_000 $ \stdout -> do    Just xs <- pure $ decode' @Array stdout    fmap V.toList $ forM xs $ \x -> do      (Object r) <- pure x      Just (String name) <- pure $ KM.lookup (mkval "tag_name") r      pure $ T.unpack name   where    url = "https://api.github.com/repos/" <> owner <> "/" <> repo <> "/releases"  getGithubAssets :: String                  -> String                  -> String                  -> IO [String]  getGithubAssets owner repo tag = withCurl url 3_000_000 $ \stdout -> do    Just xs <- pure $ decode' @Object stdout    Just (Array assets) <- pure $ KM.lookup (mkval "assets") xs    fmap V.toList $ forM assets $ \val -> do      (Object asset) <- pure val      Just (String name) <- pure $ KM.lookup (mkval "name") asset      pure $ T.unpack name   where    url = "https://api.github.com/repos/" <> owner <> "/" <> repo <> "/releases/tags/" <> tag#if MIN_VERSION_aeson(2,0,0)  mkval = KM.fromString#else  mkval = id#endifcheckForUpdates :: ( MonadReader env m                   , HasGHCupInfo env                   , HasDirs env                   , HasPlatformReq env                   , HasLog env                   , MonadIOish m                   )                => m [(Tool, TargetVersionRev)]checkForUpdates = do  dl@GHCupInfo { _ghcupDownloads = dls } <- getGHCupInfo  pfreq <- getPlatformReq  (VRight lInstalled') <- runE $ listVersions Nothing [ListInstalled True] ShowUpdates False NShowNone (Nothing, Nothing)  let latestInstalled tool = do        (_, xs) <- M.lookup tool lInstalled'        ListResult{..} <- lastMay xs        pure $ TargetVersion lCross lVer  ghcup' <- forMM (getLatest dls ghcup) $ \(TargetVersion _ l) -> do    (Right ghcup_ver) <- pure $ version $ prettyPVP ghcUpVer    let tver = mkTVer l    let rev = maybe 0 (\(_, i, _) -> i) $ getRev dls ghcup tver Nothing    if l > ghcup_ver then pure $ Just (ghcup, TargetVersionRev tver rev) else pure Nothing  otherTools <- forM (fst <$> allAvailableTools dls) $ \t ->    forMM (getLatest dls t) $ \l -> do      let mver = latestInstalled t      let rev = maybe 0 (\(_, i, _) -> i) $ getRev dls t l Nothing      forMM mver $ \ver ->        if l > ver then pure $ Just (t, TargetVersionRev l rev) else pure Nothing  let revUpdates = mconcat $ M.toList lInstalled' <&> \(tool, (_, lrs)) -> catMaybes $        lrs <&> \ListResult{..} -> do          let tver = TargetVersion lCross lVer          (rev, _) <- either (const Nothing) pure $ getDownloadInfo tool (TargetVersionReq tver Nothing) dl pfreq          if rev > fst lRev          then Just (tool, TargetVersionRev tver rev)          else Nothing  pure $ nub (catMaybes (ghcup':otherTools) <> revUpdates) where  forMM a f = fmap join $ forM a flogGHCPostRm :: (MonadReader env m, HasLog env, MonadIO m) => TargetVersion -> m ()logGHCPostRm ghcVer = do  cabalStore <- liftIO $ handleIO (\_ -> if isWindows then pure "C:\\cabal\\store" else pure "~/.cabal/store or ~/.local/state/cabal/store")    getStoreDir  let storeGhcDir = cabalStore </> ("ghc-" <> T.unpack (prettyVer $ _tvVersion ghcVer))  logInfo $ T.pack $ "After removing GHC you might also want to clean up your cabal store at: " <> storeGhcDir