Skip to content

Commit

Permalink
[#78] Replace 'Has' with 'HasRIO' where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
vaclavsvejcar committed Sep 18, 2021
1 parent 5e74b52 commit 2981afe
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
14 changes: 5 additions & 9 deletions src/Headroom/Command/Init.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ import Headroom.Configuration.Enrich ( Enrich(..)
import Headroom.Configuration.Types ( Configuration(..)
, LicenseType(..)
)
import Headroom.Data.Has ( Has(..) )
import Headroom.Data.Has ( Has(..)
, HasRIO
)
import Headroom.Data.Lens ( suffixLenses )
import Headroom.Embedded ( configFileStub
, defaultConfig
Expand Down Expand Up @@ -169,10 +171,7 @@ findSupportedFileTypes = do


-- | Checks whether application config file already exists.
doesAppConfigExist :: ( HasLogFunc env
, Has (FileSystem (RIO env)) env
, Has Paths env
)
doesAppConfigExist :: (HasLogFunc env, HasRIO FileSystem env, Has Paths env)
=> RIO env Bool
doesAppConfigExist = do
FileSystem {..} <- viewL
Expand Down Expand Up @@ -223,10 +222,7 @@ createConfigFile = do
]


makeTemplatesDir :: ( HasLogFunc env
, Has (FileSystem (RIO env)) env
, Has Paths env
)
makeTemplatesDir :: (HasLogFunc env, HasRIO FileSystem env, Has Paths env)
=> RIO env ()
makeTemplatesDir = do
FileSystem {..} <- viewL
Expand Down
16 changes: 9 additions & 7 deletions src/Headroom/Command/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ import Headroom.Configuration.Types ( Configuration(..)
, RunMode(..)
)
import Headroom.Data.EnumExtra ( EnumExtra(..) )
import Headroom.Data.Has ( Has(..) )
import Headroom.Data.Has ( Has(..)
, HasRIO
)
import Headroom.Data.Lens ( suffixLenses
, suffixLensesFor
)
Expand Down Expand Up @@ -239,7 +241,7 @@ warnOnDryRun = do


findSourceFiles :: ( Has CtConfiguration env
, Has (FileSystem (RIO env)) env
, HasRIO FileSystem env
, HasLogFunc env
)
=> [FileType]
Expand All @@ -259,7 +261,7 @@ findSourceFiles fileTypes = do


excludeIgnored :: ( Has CtConfiguration env
, Has (FileSystem (RIO env)) env
, HasRIO FileSystem env
, HasLogFunc env
)
=> [FilePath]
Expand Down Expand Up @@ -386,8 +388,8 @@ chooseAction info header = do
-- of 'TemplateRef' is selected).
loadTemplateRefs :: forall a env
. ( Template a
, Has (Network (RIO env)) env
, Has (FileSystem (RIO env)) env
, HasRIO Network env
, HasRIO FileSystem env
, HasLogFunc env
)
=> [TemplateRef] -- ^ template references
Expand Down Expand Up @@ -420,8 +422,8 @@ loadTemplateRefs refs = do


loadTemplates :: ( Has CtConfiguration env
, Has (FileSystem (RIO env)) env
, Has (Network (RIO env)) env
, HasRIO Network env
, HasRIO FileSystem env
, HasLogFunc env
)
=> RIO env (Map FileType HeaderTemplate)
Expand Down
8 changes: 5 additions & 3 deletions src/Headroom/Configuration/GlobalConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import Data.Aeson ( FromJSON(..)
, genericParseJSON
)
import qualified Data.Yaml as Y
import Headroom.Data.Has ( Has(..) )
import Headroom.Data.Has ( Has(..)
, HasRIO
)
import Headroom.Data.Serialization ( aesonOptions )
import Headroom.Embedded ( defaultGlobalConfig )
import Headroom.IO.FileSystem ( FileSystem(..) )
Expand Down Expand Up @@ -56,7 +58,7 @@ instance FromJSON GlobalConfig where

-- | Checks if global configuration /YAML/ file is already present and if not,
-- it creates one with default values.
initGlobalConfigIfNeeded :: (Has (FileSystem (RIO env)) env) => RIO env ()
initGlobalConfigIfNeeded :: (HasRIO FileSystem env) => RIO env ()
initGlobalConfigIfNeeded = do
FileSystem {..} <- viewL
userDir <- fsGetUserDirectory
Expand All @@ -66,7 +68,7 @@ initGlobalConfigIfNeeded = do


-- | Loads global configuration from /YAML/ file.
loadGlobalConfig :: (Has (FileSystem (RIO env)) env) => RIO env GlobalConfig
loadGlobalConfig :: (HasRIO FileSystem env) => RIO env GlobalConfig
loadGlobalConfig = do
FileSystem {..} <- viewL
userDir <- fsGetUserDirectory
Expand Down
18 changes: 18 additions & 0 deletions src/Headroom/Data/Has.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NoImplicitPrelude #-}

Expand All @@ -16,6 +17,7 @@ application.

module Headroom.Data.Has
( Has(..)
, HasRIO
)
where

Expand All @@ -42,3 +44,19 @@ class Has a t where

viewL :: MonadReader t m => m a
viewL = view hasLens


-- | Handy type alias that allows to avoid ugly type singatures. Allows to
-- transform this:
--
-- @
-- foo :: (Has (Network (RIO env)) env) => RIO env ()
-- @
--
-- into
--
-- @
-- foo :: (HasRIO Network env) => RIO env ()
-- @
--
type HasRIO a env = Has (a (RIO env)) env
6 changes: 4 additions & 2 deletions src/Headroom/Updater.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ where
import Data.Aeson ( Value(String) )
import qualified Data.Aeson as A
import Data.String.Interpolate ( iii )
import Headroom.Data.Has ( Has(..) )
import Headroom.Data.Has ( Has(..)
, HasRIO
)
import Headroom.IO.Network ( Network(..) )
import Headroom.Meta.Version ( Version
, parseVersion
Expand All @@ -49,7 +51,7 @@ import qualified Text.URI as URI


-- | Fetches and parses latest version from update server.
fetchLatestVersion :: (Has (Network (RIO env)) env) => RIO env Version
fetchLatestVersion :: (HasRIO Network env) => RIO env Version
fetchLatestVersion = do
Network {..} <- viewL
apiURI <- latestVersionApiURI
Expand Down

0 comments on commit 2981afe

Please sign in to comment.