{-# 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, Env (..), unpackConfigT, askCache, askMatrix, askVersion, saveConfig, resolveResultUI, getArchiveConfigs, )whereimport Control.Monad.Error.Classimport qualified Crypto.Hash.SHA256 as SHA256import qualified Data.ByteString.Base16 as Base16import qualified Data.Text as Timport qualified Data.Text.Encoding as Timport HWM.Core.Common (Check (..), Name)import HWM.Core.Formatting (Format (..))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 (..))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 (addHash, readYaml, rewrite_)import HWM.Runtime.UI (MonadUI (..), UIT, printSummary, runUI)import Reludedata Env (m :: Type -> Type) = Env { options :: Options, config :: Config, cache :: Cache, pkgs :: PkgRegistry }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 MonadUI ConfigT where uiWrite txt = do Options {quiet} <- asks options unless quiet $ liftIO $ putStr (toString txt) uiIndentLevel = ConfigT $ lift uiIndentLevel uiWithIndent f (ConfigT (ReaderT action)) = ConfigT $ ReaderT (uiWithIndent f . action)getFileHash :: FilePath -> IO (Maybe Text)getFileHash filePath = do content <- T.decodeUtf8 <$> readFileBS filePath case T.lines content of (firstLine : _) -> case T.stripPrefix "# hash: " firstLine of Just hash -> pure (Just hash) Nothing -> pure Nothing [] -> pure Nothingdebug :: 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)computeHash :: Config -> TextcomputeHash cfg = let hashInput = T.encodeUtf8 (T.pack (show (envTargets $ cfgEnvironments cfg))) hashBytes = SHA256.hash hashInput in T.decodeUtf8 (Base16.encode hashBytes)hasHashChanged :: Config -> Maybe Text -> BoolhasHashChanged _ Nothing = TruehasHashChanged cfg (Just storedHash) = storedHash /= computeHash cfgcheckConfig :: ConfigT ()checkConfig = do cfg <- asks config ops <- asks options check cfg debug $ "save " <> format (hwm ops) saveConfig cfg opssaveConfig :: (MonadError Issue m, MonadIO m) => Config -> Options -> m ()saveConfig config ops = do let file = hwm ops rewrite_ file (const $ pure config) addHash file (computeHash config)updateConfig :: (Config -> ConfigT Config) -> ConfigT b -> ConfigT bupdateConfig f m = do config' <- asks config >>= f local (\e -> e {config = config'}) (checkConfig >> m)runConfigT :: ConfigT () -> Options -> IO ()runConfigT m opts@Options {..} = do config <- resolveResultTSilent (readYaml hwm) cache <- loadCache (envDefault (cfgEnvironments config)) changed <- hasHashChanged config <$> getFileHash hwm pkgs <- resolveResultTSilent (pkgRegistry (cfgWorkspace config)) let env = Env {options = opts, config, cache, pkgs} resultT = unpackConfigT (if changed 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)