packages feed

ghcup-0.2.7.0 / lib / GHCup / Command / Set.hs

{-# LANGUAGE CPP #-}{-# LANGUAGE DataKinds #-}{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE MultiParamTypeClasses #-}{-# LANGUAGE OverloadedStrings #-}module GHCup.Command.Set whereimport GHCup.Errorsimport GHCup.Legacy.Cabalimport GHCup.Legacy.HLSimport GHCup.Legacy.Stackimport GHCup.Legacy.Utils (rmPlainGHC, binarySymLinkDestination, ghcInternalBinDir, ghcToolFiles)import GHCup.Preludeimport GHCup.Query.DBimport GHCup.Query.GHCupDirsimport GHCup.Query.Symlinkimport GHCup.System.Directoryimport GHCup.Typesimport GHCup.Types.Opticsimport GHCup.Warningsimport Control.Applicativeimport Control.Monad#if !MIN_VERSION_base(4,13,0)import Control.Monad.Fail ( MonadFail )#endifimport Control.Monad.Readerimport Data.Maybeimport Data.Data (Proxy(..))import Data.Variant.Exceptsimport Data.Versions        hiding ( patch )import Prelude              hiding ( abs )import System.FilePathimport System.IO.Errorimport qualified Data.Text    as Timport qualified Data.Text.IO as Timport GHCup.Query.DB.HLSsetToolVersion ::  ( MonadReader env m  , HasDirs env  , HasLog env  , HasPlatformReq env  , MonadIOish m  )  => Tool  -> TargetVersion  -> Excepts '[ParseError, NotInstalled] m TargetVersionsetToolVersion tool ver = setToolVersion' tool ver NothingsetToolVersion' ::  ( MonadReader env m  , HasDirs env  , HasLog env  , HasPlatformReq env  , MonadIOish m  )  => Tool  -> TargetVersion  -> Maybe FilePath        -- ^ if @Nothing@, then we operate in ~/.ghcup/bin,                           --   otherwise in a tmp directory  -> Excepts '[ParseError, NotInstalled] m TargetVersion-- TODO: warn about hls compatibilitysetToolVersion' tool tver mTmpDir = do  hideExcept' @NoToolVersionSet Proxy $ when (isNothing mTmpDir) $ unsetTool tool (_tvTarget tver)  let ver' = _tvVersion tver  vspec <- lift $ runE $ getSymlinkSpec tool tver  case vspec of    -- legacy    VLeft _      | tool == ghc   -> liftE legacySetGHC      | tool == cabal -> liftE $ setCabal ver' mTmpDir      | tool == stack -> liftE $ setStack ver' mTmpDir      | tool == hls   -> liftE $ setHLS ver' SetHLSOnly mTmpDir      | otherwise -> throwE $ NotInstalled tool tver    VRight spec -> do      lift $ logDebug2 $ T.pack (show spec)      toolDir <- lift $ fmap fromGHCupPath $ toolInstallDestination tool tver      symls <- maybe        (lift $ getUnqualifiedSymlinks spec toolDir)        (\tmpDir -> lift $ getUnqualifiedSymlinks' tmpDir spec toolDir)        mTmpDir      shadows <- fmap catMaybes <$> forM symls $ \(target, bin) -> do        lift $ createLink target bin        liftIO (isShadowed bin) >>= \case          Nothing -> pure Nothing          Just pa -> pure $ Just (pa, bin)      unless (null shadows) $ logWarn $ T.pack $ prettyHFError (ToolShadowed tool (_tvVersion tver) shadows)  -- record in 'set' file  when (isNothing mTmpDir) $ do    setFile <- lift $ recordedSetVersionFile tool (_tvTarget tver)    liftIO $ createDirRecursive' (takeDirectory setFile)    liftIO $ T.writeFile setFile (prettyVer . _tvVersion $ tver)    currentHLS <- liftE $ getSetVersion' hls Nothing    currentGHC <- liftE $ getSetVersion' ghc Nothing    supportedGHC <- lift $ maybe (pure []) (getHLSGHCs . _vrVersion . fst) currentHLS    lift $ warnAboutHlsCompatibility (_vrVersion . fst <$> currentHLS) (_vrVersion . fst <$> currentGHC) supportedGHC  pure tver where  legacySetGHC = do    binDir' <- maybe (binDir <$> lift getDirs) pure mTmpDir    ghcdir <- lift $ ghcupGHCDir tver    -- for ghc tools (ghc, ghci, haddock, ...)    verfiles <- ghcToolFiles tver    forM_ verfiles $ \file -> do      -- create symlink      internalBinDir <- ghcInternalBinDir tver      let fullF = binDir' </> file  <> exeExt      destL <- binarySymLinkDestination binDir' (internalBinDir </> file <> exeExt)      lift $ createLink destL fullF    when ((isNothing . _tvTarget $ tver) && isNothing mTmpDir)      $ lift $ symlinkShareDir (fromGHCupPath ghcdir) (T.unpack $ prettyVer (_tvVersion tver))  symlinkShareDir :: ( MonadReader env m                     , HasDirs env                     , HasLog env                     , MonadIOish m                     )                  => FilePath                  -> String                  -> m ()  symlinkShareDir ghcdir ver' = do    Dirs {..} <- getDirs    let destdir = fromGHCupPath baseDir    let sharedir     = "share"    let fullsharedir = ghcdir </> sharedir    logDebug $ "Checking for sharedir existence: " <> T.pack fullsharedir    whenM (liftIO $ doesDirectoryExist fullsharedir) $ do      let fullF   = destdir </> sharedir      let targetF = "." </> "ghc" </> ver' </> sharedir      hideError doesNotExistErrorType $ rmDirectoryLink fullF      if isWindows      then liftIO             -- On windows we need to be more permissive             -- in case symlinks can't be created, be just             -- give up here. This symlink isn't strictly necessary.             $ hideError permissionErrorType             $ hideError illegalOperationErrorType             $ createDirectoryLink targetF fullF      else liftIO             $ createDirectoryLink targetF fullFunsetTool ::  ( MonadReader env m  , HasDirs env  , HasLog env  , HasPlatformReq env  , MonadIOish m  )  => Tool  -> Maybe T.Text  -> Excepts '[ParseError, NotInstalled, NoToolVersionSet] m ()unsetTool tool target = do  unlessM (fmap isJust $ liftE $ getSetVersion' tool target) $ throwE (NoToolVersionSet tool target)  mset <- liftE $ getSetVersion' tool target  setFile <- lift $ recordedSetVersionFile tool target  case mset of    Just (VersionRev{..}, mSetFile) -> do      let tver = TargetVersion target _vrVersion      vspec <- lift $ runE $ getSymlinkSpec tool tver      case vspec of        VLeft _          | tool == ghc -> liftE $ rmPlainGHC target          | tool == cabal -> lift unsetCabal          | tool == stack -> lift unsetStack          | tool == hls -> lift unsetHLS          | otherwise -> pure ()        VRight spec -> do          toolDir <- lift $ fmap fromGHCupPath $ toolInstallDestination tool tver          unqualSyml <- lift $ getUnqualifiedSymlinks spec toolDir          forM_ unqualSyml (rmLink . snd)          forM_ mSetFile recycleFile    Nothing -> pure ()  when (tool == ghc) $ do    Dirs {..} <- getDirs    hideError doesNotExistErrorType $ rmDirectoryLink (fromGHCupPath baseDir </> "share")  logDebug2 $ "rm -f " <> T.pack setFile  hideError doesNotExistErrorType $ recycleFile setFile