2020-01-23 00:55:55 +03:00
|
|
|
|
{-# LANGUAGE ImplicitParams #-}
|
|
|
|
|
|
2018-07-03 18:34:25 +03:00
|
|
|
|
module Hasura.Server.Version
|
2021-09-24 01:56:37 +03:00
|
|
|
|
( Version (..),
|
|
|
|
|
HasVersion,
|
|
|
|
|
currentVersion,
|
|
|
|
|
consoleAssetsVersion,
|
|
|
|
|
withVersion,
|
2018-07-03 18:34:25 +03:00
|
|
|
|
)
|
|
|
|
|
where
|
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
import Control.Lens ((^.), (^?))
|
|
|
|
|
import Data.Aeson (FromJSON (..), ToJSON (..))
|
|
|
|
|
import Data.SemVer qualified as V
|
|
|
|
|
import Data.Text qualified as T
|
|
|
|
|
import Data.Text.Conversions (FromText (..), ToText (..))
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
import Text.Regex.TDFA ((=~~))
|
2021-06-15 18:05:41 +03:00
|
|
|
|
|
2020-01-23 00:55:55 +03:00
|
|
|
|
data Version
|
|
|
|
|
= VersionDev !Text
|
|
|
|
|
| VersionRelease !V.Version
|
|
|
|
|
deriving (Show, Eq)
|
2018-07-03 18:34:25 +03:00
|
|
|
|
|
2020-01-23 00:55:55 +03:00
|
|
|
|
instance ToText Version where
|
|
|
|
|
toText = \case
|
2021-09-24 01:56:37 +03:00
|
|
|
|
VersionDev txt -> txt
|
2020-01-23 00:55:55 +03:00
|
|
|
|
VersionRelease version -> "v" <> V.toText version
|
2019-01-28 16:55:28 +03:00
|
|
|
|
|
2020-01-23 00:55:55 +03:00
|
|
|
|
instance FromText Version where
|
|
|
|
|
fromText txt = case V.fromText $ T.dropWhile (== 'v') txt of
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Left _ -> VersionDev txt
|
2020-01-23 00:55:55 +03:00
|
|
|
|
Right version -> VersionRelease version
|
2019-05-16 10:45:29 +03:00
|
|
|
|
|
2020-01-23 00:55:55 +03:00
|
|
|
|
instance ToJSON Version where
|
|
|
|
|
toJSON = toJSON . toText
|
2019-05-16 10:45:29 +03:00
|
|
|
|
|
2020-01-23 00:55:55 +03:00
|
|
|
|
instance FromJSON Version where
|
|
|
|
|
parseJSON = fmap fromText . parseJSON
|
2019-05-16 10:45:29 +03:00
|
|
|
|
|
2020-01-23 00:55:55 +03:00
|
|
|
|
-- | Lots of random things need access to the current version. It would be very convenient to define
|
|
|
|
|
-- @version :: 'Version'@ in this module and export it, and indeed, that’s what we used to do! But
|
|
|
|
|
-- that turns out to cause problems: the version is compiled into the executable via Template
|
|
|
|
|
-- Haskell, so the Pro codebase runs into awkward problems. Since the Pro codebase depends on this
|
|
|
|
|
-- code as a library, it has to do gymnastics to ensure that this library always gets recompiled in
|
|
|
|
|
-- order to use the updated version, and that’s really hacky.
|
|
|
|
|
--
|
|
|
|
|
-- A better solution is to explicitly plumb the version through to everything that needs it, but
|
|
|
|
|
-- that would be noisy, so as a compromise we use an implicit parameter. Since implicit parameters
|
|
|
|
|
-- are a little cumbersome, we hide the parameter itself behind this 'HasVersion' constraint,
|
|
|
|
|
-- 'currentVersion' can be used to access it, and 'withVersion' can be used to bring a version into
|
|
|
|
|
-- scope.
|
|
|
|
|
type HasVersion = ?version :: Version
|
|
|
|
|
|
|
|
|
|
currentVersion :: HasVersion => Version
|
|
|
|
|
currentVersion = ?version
|
|
|
|
|
|
|
|
|
|
withVersion :: Version -> (HasVersion => r) -> r
|
|
|
|
|
withVersion version x = let ?version = version in x
|
|
|
|
|
|
|
|
|
|
-- | A version-based string used to form the CDN URL for fetching console assets.
|
|
|
|
|
consoleAssetsVersion :: HasVersion => Text
|
|
|
|
|
consoleAssetsVersion = case currentVersion of
|
|
|
|
|
VersionDev txt -> "versioned/" <> txt
|
|
|
|
|
VersionRelease v -> case getReleaseChannel v of
|
|
|
|
|
Nothing -> "versioned/" <> vMajMin
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Just c -> "channel/" <> c <> "/" <> vMajMin
|
2020-01-23 00:55:55 +03:00
|
|
|
|
where
|
|
|
|
|
vMajMin = T.pack ("v" <> show (v ^. V.major) <> "." <> show (v ^. V.minor))
|
2019-05-16 10:45:29 +03:00
|
|
|
|
where
|
2020-01-23 00:55:55 +03:00
|
|
|
|
getReleaseChannel :: V.Version -> Maybe Text
|
|
|
|
|
getReleaseChannel sv = case sv ^. V.release of
|
2021-09-24 01:56:37 +03:00
|
|
|
|
[] -> Just "stable"
|
|
|
|
|
(mr : _) -> case getTextFromId mr of
|
2020-01-23 00:55:55 +03:00
|
|
|
|
Nothing -> Nothing
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Just r ->
|
|
|
|
|
if
|
|
|
|
|
| T.null r -> Nothing
|
|
|
|
|
| otherwise -> T.pack <$> getChannelFromPreRelease (T.unpack r)
|
2020-03-04 17:40:47 +03:00
|
|
|
|
|
|
|
|
|
getChannelFromPreRelease :: String -> Maybe String
|
2021-09-24 01:56:37 +03:00
|
|
|
|
getChannelFromPreRelease sv = sv =~~ ("^([a-z]+)" :: String)
|
2020-03-04 17:40:47 +03:00
|
|
|
|
|
2020-01-23 00:55:55 +03:00
|
|
|
|
getTextFromId :: V.Identifier -> Maybe Text
|
|
|
|
|
getTextFromId i = Just i ^? (toTextualM . V._Textual)
|
|
|
|
|
where
|
2021-09-24 01:56:37 +03:00
|
|
|
|
toTextualM _ Nothing = pure Nothing
|
2020-01-23 00:55:55 +03:00
|
|
|
|
toTextualM f (Just a) = f a
|