{-# LANGUAGE DerivingStrategies #-}{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE GeneralizedNewtypeDeriving #-}{-# LANGUAGE LambdaCase #-}{-# LANGUAGE MultiParamTypeClasses #-}{-# LANGUAGE NamedFieldPuns #-}{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE RecordWildCards #-}{-# LANGUAGE TypeFamilies #-}{-# LANGUAGE NoImplicitPrelude #-}module HWM.Domain.ConfigT ( ConfigT (..), runConfigT, VersionMap, updateConfig, updateConfigM, Env (..), unpackConfigT, askCache, askMatrix, askVersion, saveConfig, resolveResultUI, getArchiveConfigs, )whereimport Control.Monad.Error.Classimport HWM.Core.Common (Check (..), Name)import HWM.Core.Formatting (Format (..), Status (..), StatusM)import HWM.Core.Has (Has (..))import HWM.Core.Options (Options (..))import HWM.Core.Result (Issue (..), MonadIssue (..), Result (..), ResultT, runResultT)import HWM.Core.Version (Version, askVersion)import HWM.Domain.Config (Config (..))import HWM.Domain.Environments (Environments (..), environmentHash)import HWM.Domain.Registry (Registry)import HWM.Domain.Release (ArtifactConfig, Release (..))import HWM.Domain.Workspace (PkgRegistry, Workspace, pkgRegistry)import HWM.Runtime.Cache (Cache, VersionMap, loadCache, saveCache)import HWM.Runtime.Files (Signature, addHash, getFileSignature, readYaml, rewrite_)import HWM.Runtime.UI (MonadUI (..), UIT, printSummary, runUI, sectionConfig)import Reludedata Env (m :: Type -> Type) = Env { options :: Options, config :: Config, cache :: Cache, pkgs :: PkgRegistry, fileSignature :: Signature }type ConfigEnv = Env IOnewtype ConfigT (a :: Type) = ConfigT { _runConfigT :: ReaderT ConfigEnv (ResultT (UIT IO)) a } deriving ( Functor, Applicative, Monad, MonadReader ConfigEnv, MonadError Issue, MonadIO )instance Has (Env m) Options where obtain = optionsinstance Has (Env m) Cache where obtain = cacheinstance Has (Env m) Config where obtain = configinstance Has (Env m) Workspace where obtain Env {config} = cfgWorkspace configinstance Has (Env m) Environments where obtain Env {config} = cfgEnvironments configinstance Has (Env m) Version where obtain Env {config} = cfgVersion configinstance Has (Env m) PkgRegistry where obtain = pkgsinstance Has (Env m) Registry where obtain Env {config} = fromMaybe mempty (cfgRegistry config)instance Has (Env m) Signature where obtain = fileSignatureinstance MonadUI ConfigT where uiWrite txt = do Options {optionsQuiet} <- asks options unless optionsQuiet $ liftIO $ putStr (toString txt) uiIndentLevel = ConfigT $ lift uiIndentLevel uiWithIndent f (ConfigT (ReaderT action)) = ConfigT $ ReaderT (uiWithIndent f . action)debug :: Text -> ConfigT ()debug _ = pure ()instance MonadIssue ConfigT where injectIssue = ConfigT . lift . injectIssue catchIssues (ConfigT action) = ConfigT $ ReaderT (catchIssues . runReaderT action) mapIssue f (ConfigT action) = ConfigT $ ReaderT $ \env -> mapIssue f (runReaderT action env)checkConfig :: ConfigT ()checkConfig = do cfg <- asks config ops <- asks options check cfg debug $ "save " <> format (optionsHwm ops) saveConfig cfg opssaveConfig :: (MonadError Issue m, MonadIO m) => Config -> Options -> m ()saveConfig config ops = do let file = optionsHwm ops status <- rewrite_ file (const $ pure config) case status of Updated -> addHash file (environmentHash (cfgEnvironments config)) _ -> pure ()updateConfig :: (Config -> ConfigT Config) -> ConfigT b -> ConfigT bupdateConfig f = updateConfigM f []updateConfigM :: (Config -> ConfigT Config) -> StatusM ConfigT -> ConfigT b -> ConfigT bupdateConfigM f xs m = do config' <- asks config >>= f local (\e -> e {config = config'}) (checkConfig >> sectionConfig (("hwm.yaml", pure Updated) : xs) >> m)runConfigT :: ConfigT () -> Options -> IO ()runConfigT m opts@Options {..} = do config <- resolveResultTSilent (readYaml optionsHwm) cache <- loadCache (envsDefault (cfgEnvironments config)) fileSignature <- getFileSignature optionsHwm let currentSignature = environmentHash (cfgEnvironments config) pkgs <- resolveResultTSilent (pkgRegistry (cfgWorkspace config)) let env = Env {options = opts, config, cache, pkgs, fileSignature} resultT = unpackConfigT (if fileSignature /= currentSignature then checkConfig >> m else m) env resolveResultT resultT cacheresolveResultT :: ResultT (UIT IO) a -> Cache -> IO ()resolveResultT resT cache = runUI ( runResultT resT >>= \case Success {..} -> do liftIO $ saveCache cache printSummary issues Failure {..} -> do printSummary (toList failure) exitFailure )resolveResultTSilent :: ResultT IO a -> IO aresolveResultTSilent m = do r <- runResultT m case r of Success {..} -> pure result Failure {..} -> runUI (printSummary (toList failure)) >> exitFailureresolveResultUI :: ResultT (UIT IO) a -> UIT IO aresolveResultUI m = do r <- runResultT m case r of Success {..} -> pure result Failure {..} -> printSummary (toList failure) >> exitFailureunpackConfigT :: ConfigT a -> ConfigEnv -> ResultT (UIT IO) aunpackConfigT (ConfigT action) = runReaderT actionaskCache :: ConfigT CacheaskCache = asks cacheaskMatrix :: ConfigT EnvironmentsaskMatrix = asks (cfgEnvironments . config)getArchiveConfigs :: ConfigT (Map Name ArtifactConfig)getArchiveConfigs = do release <- asks (cfgRelease . config) pure $ fromMaybe mempty (release >>= rlsArtifacts)