Skip to content

Commit

Permalink
Merge pull request #80 from vaclavsvejcar/feature/78
Browse files Browse the repository at this point in the history
[#78] Check for Headroom updates
  • Loading branch information
vaclavsvejcar authored Oct 21, 2021
2 parents 76732bc + 1c2635a commit 0bca0ae
Show file tree
Hide file tree
Showing 29 changed files with 1,308 additions and 130 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @vaclavsvejcar
1 change: 1 addition & 0 deletions .hlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
# - ignore: {name: Use const, within: SpecialModule} # Only within certain modules
- ignore: { name: "Use newtype instead of data" }
- ignore: { name: Redundant do, within: spec }
- ignore: {name: "Redundant lambda"}


# Define some custom infix operators
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
All notable changes to this project will be documented in this file.

## 0.4.3.0 (in development)
- Bump _LTS Haskell_ to `18.8`
- [#78] Check automatically for Headroom updates
- Bump _LTS Haskell_ to `18.13`

## 0.4.2.0 (released 2021-07-01)
- [#42] Add option to exclude source paths ignored by VCS
Expand Down
11 changes: 11 additions & 0 deletions embedded/default-global-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## This is the global configuration file for Headroom.
## See https://github.com/vaclavsvejcar/headroom for more details.

## Configuration for Headroom Updater.
updates:

## Whether Headroom should automatically check for available updates.
check-for-updates: true

## How often (in days) should Headroom check for updates.
update-interval-days: 1
16 changes: 15 additions & 1 deletion headroom.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cabal-version: 2.2
--
-- see: https://github.com/sol/hpack
--
-- hash: 3845aee60e6509e43b9ea9ad092405846976b023c0e54af2c6587198799158ba
-- hash: 7d5147259bf3565fd10f38a3aa865b29df91aa3c463366a623d71bb99f0042c5

name: headroom
version: 0.4.3.0
Expand All @@ -25,6 +25,7 @@ extra-source-files:
README.md
embedded/config-file.yaml
embedded/default-config.yaml
embedded/default-global-config.yaml
embedded/license/apache2/c.mustache
embedded/license/apache2/cpp.mustache
embedded/license/apache2/css.mustache
Expand Down Expand Up @@ -127,6 +128,7 @@ extra-source-files:
test-data/test-traverse/foo/b.html
test-data/test-traverse/foo/test.xml
test-data/test-traverse/foo/bar/c.html
test-data/updater/github-resp.json

source-repository head
type: git
Expand All @@ -135,6 +137,7 @@ source-repository head
library
exposed-modules:
Headroom.Command
Headroom.Command.Bootstrap
Headroom.Command.Gen
Headroom.Command.Init
Headroom.Command.Readers
Expand All @@ -144,6 +147,7 @@ library
Headroom.Configuration
Headroom.Configuration.Compat
Headroom.Configuration.Enrich
Headroom.Configuration.GlobalConfig
Headroom.Configuration.Types
Headroom.Data.Coerce
Headroom.Data.EnumExtra
Expand Down Expand Up @@ -176,6 +180,7 @@ library
Headroom.Header.Sanitize
Headroom.Header.Types
Headroom.IO.FileSystem
Headroom.IO.KVStore
Headroom.IO.Network
Headroom.Meta
Headroom.Meta.Version
Expand All @@ -188,8 +193,10 @@ library
Headroom.Template.TemplateRef
Headroom.Types
Headroom.UI
Headroom.UI.Message
Headroom.UI.Progress
Headroom.UI.Table
Headroom.Updater
Headroom.Variables
Headroom.Variables.Types
other-modules:
Expand All @@ -209,13 +216,16 @@ library
, http-client
, http-types
, microlens
, microlens-aeson
, microlens-th
, modern-uri
, mtl
, mustache
, optparse-applicative
, pcre-heavy
, pcre-light
, persistent
, persistent-sqlite
, req
, rio
, string-interpolate
Expand Down Expand Up @@ -263,6 +273,7 @@ test-suite spec
Headroom.Command.RunSpec
Headroom.Configuration.CompatSpec
Headroom.Configuration.EnrichSpec
Headroom.Configuration.GlobalConfigSpec
Headroom.ConfigurationSpec
Headroom.Data.CoerceSpec
Headroom.Data.EnumExtraSpec
Expand All @@ -287,6 +298,7 @@ test-suite spec
Headroom.Header.SanitizeSpec
Headroom.HeaderSpec
Headroom.IO.FileSystemSpec
Headroom.IO.KVStoreSpec
Headroom.Meta.VersionSpec
Headroom.PostProcess.TypesSpec
Headroom.PostProcess.UpdateCopyrightSpec
Expand All @@ -295,8 +307,10 @@ test-suite spec
Headroom.Template.MustacheSpec
Headroom.Template.TemplateRefSpec
Headroom.TypesSpec
Headroom.UI.MessageSpec
Headroom.UI.ProgressSpec
Headroom.UI.TableSpec
Headroom.UpdaterSpec
Headroom.VariablesSpec
Paths_headroom
hs-source-dirs:
Expand Down
4 changes: 4 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extra-source-files:
- test-data/test-traverse/*
- test-data/test-traverse/foo/*
- test-data/test-traverse/foo/bar/*
- test-data/updater/*

dependencies:
- base >= 4.7 && < 5
Expand Down Expand Up @@ -70,12 +71,15 @@ library:
- http-client
- http-types
- microlens
- microlens-aeson
- microlens-th
- modern-uri
- mtl
- mustache
- pcre-light
- pcre-heavy
- persistent
- persistent-sqlite
- req
- string-interpolate
- template-haskell
Expand Down
136 changes: 136 additions & 0 deletions src/Headroom/Command/Bootstrap.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}

{-|
Module : Headroom.Command.Bootstrap
Description : Logic for bootstrapping Headroom
Copyright : (c) 2019-2021 Vaclav Svejcar
License : BSD-3-Clause
Maintainer : [email protected]
Stability : experimental
Portability : POSIX
Logic for running shared code and bootstrapping all /Headroom/ command /RIO/
applications.
-}

module Headroom.Command.Bootstrap
( BootstrapEnv(..)
, bootstrap
, runRIO'
, globalKVStore
)
where

import Data.String.Interpolate ( iii )
import Headroom.Configuration.GlobalConfig ( GlobalConfig(..)
, globalConfigPath
, initGlobalConfigIfNeeded
, loadGlobalConfig
)
import Headroom.Data.Has ( Has(..)
, HasRIO
)
import Headroom.IO.FileSystem ( FileSystem(..) )
import Headroom.IO.KVStore ( KVStore
, StorePath(..)
, sqliteKVStore
)
import Headroom.IO.Network ( Network )
import Headroom.Meta ( cacheFileName
, globalConfigDirName
, productInfo
, webRepo
)
import Headroom.Meta.Version ( Version
, printVersionP
)
import Headroom.UI.Message ( messageInfo )
import Headroom.Updater ( checkUpdates )
import RIO
import RIO.FilePath ( (</>) )
import qualified RIO.Text as T


-- | Bootstrap environment, containing pieces shared between all commands.
data BootstrapEnv = BootstrapEnv
{ beGlobalConfig :: GlobalConfig -- ^ loaded global configuration
}


-- | Runs /RIO/ application using provided environment data and flag
-- whether to run in debug mode.
runRIO' :: (LogFunc -> IO env)
-- ^ function returning environment data
-> Bool
-- ^ whether to run in debug mode
-> RIO env a
-- ^ /RIO/ application to execute
-> IO a
-- ^ execution result
runRIO' enfFn isDebug logic = do
defLogOptions <- logOptionsHandle stderr isDebug
withLogFunc (setLogUseLoc False defLogOptions) $ \logFunc -> do
env <- liftIO $ enfFn logFunc
runRIO env logic


-- | Executes the initialization logic that should be performed before any other
-- code is executed. During this bootstrap, for example /global configuration/
-- is initialized and loaded, welcome message is printed to console and updates
-- are checked.
bootstrap :: ( HasRIO FileSystem env
, HasRIO KVStore env
, HasRIO Network env
, HasLogFunc env
)
=> RIO env BootstrapEnv
bootstrap = do
welcomeMessage
initGlobalConfigIfNeeded
globalConfig@GlobalConfig {..} <- loadGlobalConfig
checkUpdates gcUpdates >>= \case
Nothing -> pure ()
Just newVersion -> displayUpdate newVersion
pure BootstrapEnv { beGlobalConfig = globalConfig }


-- | Shared /SQLite/-based 'KVStore'.
globalKVStore :: (HasRIO FileSystem env) => RIO env (KVStore (RIO env))
globalKVStore = do
FileSystem {..} <- viewL
userDir <- fsGetUserDirectory
pure
. sqliteKVStore
. StorePath
. T.pack
$ userDir
</> globalConfigDirName
</> cacheFileName


------------------------------ PRIVATE FUNCTIONS -----------------------------

welcomeMessage :: HasLogFunc env => RIO env ()
welcomeMessage = logInfo . display $ productInfo


displayUpdate :: (HasRIO FileSystem env, HasLogFunc env)
=> Version
-> RIO env ()
displayUpdate version = do
configPath <- globalConfigPath
logInfo . display . messageInfo $ message configPath
where
message configPath = [iii|
New version #{printVersionP version} is available for download, you can
get it from #{webRepo}.\n\t
Tired of seeing this message? You can change the behaviour in global
config file here:\n\t
#{configPath}
|]
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
Loading

0 comments on commit 0bca0ae

Please sign in to comment.