{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

-- | Copyright: (c) 2021-2022 berberman
-- SPDX-License-Identifier: MIT
-- Maintainer: berberman <[email protected]>
-- Stability: experimental
-- Portability: portable
--
-- [nvchecker](https://github.com/lilydjwg/nvchecker) is a program checking new versions of packages.
-- We encode the checking process into shake build system, generating configuration of nvchecker and calling it externally.
-- Now we call nvchecker for each 'VersionSource', which seems not to be efficient, but it's tolerable when running in parallel.
--
-- Meanwhile, we lose the capabilities of tracking version updates, i.e. normally nvchecker will help us maintain a list of old versions,
-- so that we are able to know which package's version is updated in this run. Fortunately, we can reimplement this in shake,
-- see 'nvcheckerRule' for details.
module NvFetcher.Nvchecker
  ( -- * Types
    VersionSortMethod (..),
    ListOptions (..),
    CheckVersion (..),
    NvcheckerOptions (..),
    VersionSource (..),
    NvcheckerResult (..),

    -- * Rules
    nvcheckerRule,
    checkVersion,
    checkVersion',
  )
where

import Control.Monad (void)
import Control.Monad.Extra (fromMaybeM)
import qualified Data.Aeson as A
import Data.Coerce (coerce)
import Data.Maybe (fromJust, mapMaybe)
import Data.String (fromString)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Development.Shake
import Development.Shake.Rule
import NvFetcher.Types
import NvFetcher.Types.ShakeExtras
import NvFetcher.Utils
import Prettyprinter (pretty, (<+>))
import Toml (Value (Bool, Text))
import qualified Toml
import Toml.Type.Edsl

-- | Rules of nvchecker
nvcheckerRule :: Rules ()
nvcheckerRule :: Rules ()
nvcheckerRule = do
  Rules ()
persistedRule
  Rules ()
oneShotRule

-- | Nvchecker rule for packages, which is aware of version changes and supports using stale version.
-- nvchecker will be called at most one time given a package key. Follow-up using of this rule will return cached result.
-- 'PackageKey' is required for caching.
-- Run this rule by calling 'checkVersion'
persistedRule :: Rules ()
persistedRule :: Rules ()
persistedRule = BuiltinLint (WithPackageKey CheckVersion) NvcheckerResult
-> BuiltinIdentity (WithPackageKey CheckVersion) NvcheckerResult
-> BuiltinRun (WithPackageKey CheckVersion) NvcheckerResult
-> Rules ()
forall key value.
(RuleResult key ~ value, ShakeValue key, Typeable value,
 NFData value, Show value, Partial) =>
BuiltinLint key value
-> BuiltinIdentity key value -> BuiltinRun key value -> Rules ()
addBuiltinRule BuiltinLint (WithPackageKey CheckVersion) NvcheckerResult
forall key value. BuiltinLint key value
noLint BuiltinIdentity (WithPackageKey CheckVersion) NvcheckerResult
forall key value. BuiltinIdentity key value
noIdentity (BuiltinRun (WithPackageKey CheckVersion) NvcheckerResult
 -> Rules ())
-> BuiltinRun (WithPackageKey CheckVersion) NvcheckerResult
-> Rules ()
forall a b. (a -> b) -> a -> b
$ \(WithPackageKey (key :: CheckVersion
key@(CheckVersion VersionSource
versionSource NvcheckerOptions
options), PackageKey
pkg)) Maybe ByteString
_old RunMode
_mode -> do
  String -> Action ()
putInfo (String -> Action ())
-> (Doc Any -> String) -> Doc Any -> Action ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc Any -> String
forall a. Show a => a -> String
show (Doc Any -> Action ()) -> Doc Any -> Action ()
forall a b. (a -> b) -> a -> b
$ Doc Any
"#" Doc Any -> Doc Any -> Doc Any
forall ann. Doc ann -> Doc ann -> Doc ann
<+> CheckVersion -> Doc Any
forall a ann. Pretty a => a -> Doc ann
pretty CheckVersion
key
  Maybe Version
oldVer <- PackageKey -> Action (Maybe Version)
getRecentLastVersion PackageKey
pkg
  UseStaleVersion
useStaleVersion <- Package -> UseStaleVersion
_ppinned (Package -> UseStaleVersion)
-> (Maybe Package -> Package) -> Maybe Package -> UseStaleVersion
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Package -> Package
forall a. Partial => Maybe a -> a
fromJust (Maybe Package -> UseStaleVersion)
-> Action (Maybe Package) -> Action UseStaleVersion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PackageKey -> Action (Maybe Package)
lookupPackage PackageKey
pkg
  let useStale :: Bool
useStale = case UseStaleVersion
useStaleVersion of
        UseStaleVersion
PermanentStale -> Bool
True
        UseStaleVersion
TemporaryStale -> Bool
True
        UseStaleVersion
_ -> Bool
False
  case Bool
useStale of
    Bool
True
      | Just Version
oldVer' <- Maybe Version
oldVer -> do
        -- use the stale version if we have
        String -> Action ()
putInfo (String -> Action ()) -> String -> Action ()
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ Text
"Skip running nvchecker, use stale version " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Version -> Text
coerce Version
oldVer' Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" for " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> PackageKey -> Text
coerce PackageKey
pkg
        let result :: NvcheckerResult
result = NvcheckerResult :: Version -> Maybe Version -> Bool -> NvcheckerResult
NvcheckerResult {nvNow :: Version
nvNow = Version
oldVer', nvOld :: Maybe Version
nvOld = Maybe Version
oldVer, nvStale :: Bool
nvStale = Bool
True}
        RunResult NvcheckerResult -> Action (RunResult NvcheckerResult)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RunResult NvcheckerResult -> Action (RunResult NvcheckerResult))
-> RunResult NvcheckerResult -> Action (RunResult NvcheckerResult)
forall a b. (a -> b) -> a -> b
$ RunChanged
-> ByteString -> NvcheckerResult -> RunResult NvcheckerResult
forall value. RunChanged -> ByteString -> value -> RunResult value
RunResult RunChanged
ChangedRecomputeSame (NvcheckerResult -> ByteString
forall a. Binary a => a -> ByteString
encode' NvcheckerResult
result) NvcheckerResult
result

    -- run nvchecker
    Bool
_ -> do
      -- if we already run this rule for a package, we can recover the last result from getLastVersionUpdated
      -- (when cacheNvchecker is enabled)
      Bool
useCache <- Action Bool
nvcheckerCacheEnabled
      Version
now <- Action Version -> Action (Maybe Version) -> Action Version
forall (m :: * -> *) a. Monad m => m a -> m (Maybe a) -> m a
fromMaybeM (NvcheckerRaw -> Version
coerce (NvcheckerRaw -> Version) -> Action NvcheckerRaw -> Action Version
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PackageKey
-> NvcheckerOptions -> VersionSource -> Action NvcheckerRaw
runNvchecker PackageKey
pkg NvcheckerOptions
options VersionSource
versionSource) (if Bool
useCache then PackageKey -> Action (Maybe Version)
getLastVersionUpdated PackageKey
pkg else Maybe Version -> Action (Maybe Version)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Version
forall a. Maybe a
Nothing)
      let runChanged :: RunChanged
runChanged = case Maybe Version
oldVer of
            Just Version
oldVer'
              | Version
oldVer' Version -> Version -> Bool
forall a. Eq a => a -> a -> Bool
== Version
now -> RunChanged
ChangedRecomputeSame
            Maybe Version
_ -> RunChanged
ChangedRecomputeDiff
          result :: NvcheckerResult
result = NvcheckerResult :: Version -> Maybe Version -> Bool -> NvcheckerResult
NvcheckerResult {nvNow :: Version
nvNow = Version
now, nvOld :: Maybe Version
nvOld = Maybe Version
oldVer, nvStale :: Bool
nvStale = Bool
False}
      -- always update
      PackageKey -> Version -> Action ()
updateLastVersion PackageKey
pkg Version
now
      RunResult NvcheckerResult -> Action (RunResult NvcheckerResult)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RunResult NvcheckerResult -> Action (RunResult NvcheckerResult))
-> RunResult NvcheckerResult -> Action (RunResult NvcheckerResult)
forall a b. (a -> b) -> a -> b
$ RunChanged
-> ByteString -> NvcheckerResult -> RunResult NvcheckerResult
forall value. RunChanged -> ByteString -> value -> RunResult value
RunResult RunChanged
runChanged ByteString
forall a. Monoid a => a
mempty NvcheckerResult
result

-- | Nvchecker rule without cache
-- Rule this rule by calling 'checkVersion''
oneShotRule :: Rules ()
oneShotRule :: Rules ()
oneShotRule = Rules (CheckVersion -> Action NvcheckerResult) -> Rules ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Rules (CheckVersion -> Action NvcheckerResult) -> Rules ())
-> Rules (CheckVersion -> Action NvcheckerResult) -> Rules ()
forall a b. (a -> b) -> a -> b
$
  (CheckVersion -> Action NvcheckerResult)
-> Rules (CheckVersion -> Action NvcheckerResult)
forall q a.
(RuleResult q ~ a, ShakeValue q, ShakeValue a, Partial) =>
(q -> Action a) -> Rules (q -> Action a)
addOracle ((CheckVersion -> Action NvcheckerResult)
 -> Rules (CheckVersion -> Action NvcheckerResult))
-> (CheckVersion -> Action NvcheckerResult)
-> Rules (CheckVersion -> Action NvcheckerResult)
forall a b. (a -> b) -> a -> b
$ \key :: CheckVersion
key@(CheckVersion VersionSource
versionSource NvcheckerOptions
options) -> do
    String -> Action ()
putInfo (String -> Action ())
-> (Doc Any -> String) -> Doc Any -> Action ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc Any -> String
forall a. Show a => a -> String
show (Doc Any -> Action ()) -> Doc Any -> Action ()
forall a b. (a -> b) -> a -> b
$ CheckVersion -> Doc Any
forall a ann. Pretty a => a -> Doc ann
pretty CheckVersion
key
    NvcheckerRaw Version
now <- PackageKey
-> NvcheckerOptions -> VersionSource -> Action NvcheckerRaw
runNvchecker (Text -> PackageKey
PackageKey Text
"pkg") NvcheckerOptions
options VersionSource
versionSource
    NvcheckerResult -> Action NvcheckerResult
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NvcheckerResult -> Action NvcheckerResult)
-> NvcheckerResult -> Action NvcheckerResult
forall a b. (a -> b) -> a -> b
$ Version -> Maybe Version -> Bool -> NvcheckerResult
NvcheckerResult Version
now Maybe Version
forall a. Maybe a
Nothing Bool
False

runNvchecker :: PackageKey -> NvcheckerOptions -> VersionSource -> Action NvcheckerRaw
runNvchecker :: PackageKey
-> NvcheckerOptions -> VersionSource -> Action NvcheckerRaw
runNvchecker PackageKey
pkg NvcheckerOptions
options VersionSource
versionSource = (String -> Action NvcheckerRaw) -> Action NvcheckerRaw
forall a. (String -> Action a) -> Action a
withTempFile ((String -> Action NvcheckerRaw) -> Action NvcheckerRaw)
-> (String -> Action NvcheckerRaw) -> Action NvcheckerRaw
forall a b. (a -> b) -> a -> b
$ \String
config -> Action NvcheckerRaw -> Action NvcheckerRaw
forall a. Action a -> Action a
withRetry (Action NvcheckerRaw -> Action NvcheckerRaw)
-> Action NvcheckerRaw -> Action NvcheckerRaw
forall a b. (a -> b) -> a -> b
$ do
  let nvcheckerConfig :: String
nvcheckerConfig = Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ TOML -> Text
Toml.pretty (TOML -> Text) -> TOML -> Text
forall a b. (a -> b) -> a -> b
$ TDSL -> TOML
mkToml (TDSL -> TOML) -> TDSL -> TOML
forall a b. (a -> b) -> a -> b
$ PackageKey -> NvcheckerOptions -> VersionSource -> TDSL
genNvConfig PackageKey
pkg NvcheckerOptions
options VersionSource
versionSource
  String -> Action ()
putVerbose (String -> Action ()) -> String -> Action ()
forall a b. (a -> b) -> a -> b
$ String
"Generated nvchecker config for " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> PackageKey -> String
forall a. Show a => a -> String
show PackageKey
pkg String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
":" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
nvcheckerConfig
  String -> String -> Action ()
forall (m :: * -> *).
(MonadIO m, Partial) =>
String -> String -> m ()
writeFile' String
config String
nvcheckerConfig
  (CmdTime Double
t, Stdout ByteString
out, CmdLine String
c) <- Action (CmdTime, Stdout ByteString, CmdLine)
-> Action (CmdTime, Stdout ByteString, CmdLine)
forall a. Action a -> Action a
quietly (Action (CmdTime, Stdout ByteString, CmdLine)
 -> Action (CmdTime, Stdout ByteString, CmdLine))
-> (String -> Action (CmdTime, Stdout ByteString, CmdLine))
-> String
-> Action (CmdTime, Stdout ByteString, CmdLine)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Action (CmdTime, Stdout ByteString, CmdLine)
forall args r. (Partial, CmdArguments args) => args
cmd (String -> Action (CmdTime, Stdout ByteString, CmdLine))
-> String -> Action (CmdTime, Stdout ByteString, CmdLine)
forall a b. (a -> b) -> a -> b
$ String
"nvchecker --logger json -c " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
config
  String -> Action ()
putVerbose (String -> Action ()) -> String -> Action ()
forall a b. (a -> b) -> a -> b
$ String
"Finishing running " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
c String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
", took " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Double -> String
forall a. Show a => a -> String
show Double
t String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"s"
  let out' :: Text
out' = ByteString -> Text
T.decodeUtf8 ByteString
out
      result :: [NvcheckerRaw]
result = (Text -> Maybe NvcheckerRaw) -> [Text] -> [NvcheckerRaw]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (ByteString -> Maybe NvcheckerRaw
forall a. FromJSON a => ByteString -> Maybe a
A.decodeStrict (ByteString -> Maybe NvcheckerRaw)
-> (Text -> ByteString) -> Text -> Maybe NvcheckerRaw
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8) (Text -> [Text]
T.lines Text
out')
  case [NvcheckerRaw]
result of
    [NvcheckerRaw
x] -> NvcheckerRaw -> Action NvcheckerRaw
forall (f :: * -> *) a. Applicative f => a -> f a
pure NvcheckerRaw
x
    [NvcheckerRaw]
_ -> String -> Action NvcheckerRaw
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Action NvcheckerRaw) -> String -> Action NvcheckerRaw
forall a b. (a -> b) -> a -> b
$ String
"Failed to parse output from nvchecker: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
out'

genNvConfig :: PackageKey -> NvcheckerOptions -> VersionSource -> TDSL
genNvConfig :: PackageKey -> NvcheckerOptions -> VersionSource -> TDSL
genNvConfig PackageKey
pkg NvcheckerOptions
options VersionSource
versionSource = Key -> TDSL -> TDSL
table (String -> Key
forall a. IsString a => String -> a
fromString (String -> Key) -> String -> Key
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ PackageKey -> Text
coerce PackageKey
pkg) (TDSL -> TDSL) -> TDSL -> TDSL
forall a b. (a -> b) -> a -> b
$ do
  VersionSource -> TDSL
genVersionSource VersionSource
versionSource
  NvcheckerOptions -> TDSL
genOptions NvcheckerOptions
options
  where
    Key
key =:? :: Key -> Maybe Text -> TDSL
=:? (Just Text
x) = Key
key Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text Text
x
    Key
_ =:? Maybe Text
_ = () -> TDSL
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    genVersionSource :: VersionSource -> TDSL
genVersionSource = \case
      GitHubRelease {Text
_repo :: VersionSource -> Text
_owner :: VersionSource -> Text
_repo :: Text
_owner :: Text
..} -> do
        Key
"source" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Value 'TText
"github"
        Key
"github" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text (Text
_owner Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
_repo)
        Key
"use_latest_release" Key -> Value 'TBool -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Bool -> Value 'TBool
Bool Bool
True
      GitHubTag {Text
ListOptions
_listOptions :: VersionSource -> ListOptions
_listOptions :: ListOptions
_repo :: Text
_owner :: Text
_repo :: VersionSource -> Text
_owner :: VersionSource -> Text
..} -> do
        Key
"source" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Value 'TText
"github"
        Key
"github" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text (Text
_owner Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
_repo)
        Key
"use_max_tag" Key -> Value 'TBool -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Bool -> Value 'TBool
Bool Bool
True
        ListOptions -> TDSL
genListOptions ListOptions
_listOptions
      Git {Text
Branch
_vbranch :: VersionSource -> Branch
_vurl :: VersionSource -> Text
_vbranch :: Branch
_vurl :: Text
..} -> do
        Key
"source" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Value 'TText
"git"
        Key
"git" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text Text
_vurl
        Key
"branch" Key -> Maybe Text -> TDSL
=:? Branch -> Maybe Text
coerce Branch
_vbranch
        Key
"use_commit" Key -> Value 'TBool -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Bool -> Value 'TBool
Bool Bool
True
      Aur {Text
_aur :: VersionSource -> Text
_aur :: Text
..} -> do
        Key
"source" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Value 'TText
"aur"
        Key
"aur" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text Text
_aur
        Key
"strip_release" Key -> Value 'TBool -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Bool -> Value 'TBool
Bool Bool
True
      ArchLinux {Text
_archpkg :: VersionSource -> Text
_archpkg :: Text
..} -> do
        Key
"source" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Value 'TText
"archpkg"
        Key
"archpkg" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text Text
_archpkg
        Key
"strip_release" Key -> Value 'TBool -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Bool -> Value 'TBool
Bool Bool
True
      Pypi {Text
_pypi :: VersionSource -> Text
_pypi :: Text
..} -> do
        Key
"source" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Value 'TText
"pypi"
        Key
"pypi" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text Text
_pypi
      Manual {Text
_manual :: VersionSource -> Text
_manual :: Text
..} -> do
        Key
"source" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Value 'TText
"manual"
        Key
"manual" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text Text
_manual
      Repology {Text
_repology :: VersionSource -> Text
_repo :: Text
_repology :: Text
_repo :: VersionSource -> Text
..} -> do
        Key
"source" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Value 'TText
"repology"
        Key
"repology" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text Text
_repology
        Key
"repo" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text Text
_repo
      Webpage {Text
ListOptions
_regex :: VersionSource -> Text
_listOptions :: ListOptions
_regex :: Text
_vurl :: Text
_vurl :: VersionSource -> Text
_listOptions :: VersionSource -> ListOptions
..} -> do
        Key
"source" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Value 'TText
"regex"
        Key
"url" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text Text
_vurl
        Key
"regex" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text Text
_regex
        ListOptions -> TDSL
genListOptions ListOptions
_listOptions
      HttpHeader {Text
ListOptions
_listOptions :: ListOptions
_regex :: Text
_vurl :: Text
_regex :: VersionSource -> Text
_vurl :: VersionSource -> Text
_listOptions :: VersionSource -> ListOptions
..} -> do
        Key
"source" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Value 'TText
"httpheader"
        Key
"url" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text Text
_vurl
        Key
"regex" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text Text
_regex
        ListOptions -> TDSL
genListOptions ListOptions
_listOptions
      OpenVsx {Text
_ovExtName :: VersionSource -> Text
_ovPublisher :: VersionSource -> Text
_ovExtName :: Text
_ovPublisher :: Text
..} -> do
        Key
"source" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Value 'TText
"openvsx"
        Key
"openvsx" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text (Text
_ovPublisher Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
_ovExtName)
      VscodeMarketplace {Text
_vsmExtName :: VersionSource -> Text
_vsmPublisher :: VersionSource -> Text
_vsmExtName :: Text
_vsmPublisher :: Text
..} -> do
        Key
"source" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Value 'TText
"vsmarketplace"
        Key
"vsmarketplace" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text (Text
_vsmPublisher Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
_vsmExtName)
      Cmd {Text
_vcmd :: VersionSource -> Text
_vcmd :: Text
..} -> do
        Key
"source" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Value 'TText
"cmd"
        Key
"cmd" Key -> Value 'TText -> TDSL
forall (a :: TValue). Key -> Value a -> TDSL
=: Text -> Value 'TText
Text Text
_vcmd
    genListOptions :: ListOptions -> TDSL
genListOptions ListOptions {Maybe Text
Maybe VersionSortMethod
_ignored :: ListOptions -> Maybe Text
_sortVersionKey :: ListOptions -> Maybe VersionSortMethod
_excludeRegex :: ListOptions -> Maybe Text
_includeRegex :: ListOptions -> Maybe Text
_ignored :: Maybe Text
_sortVersionKey :: Maybe VersionSortMethod
_excludeRegex :: Maybe Text
_includeRegex :: Maybe Text
..} = do
      Key
"include_regex" Key -> Maybe Text -> TDSL
=:? Maybe Text
_includeRegex
      Key
"exclude_regex" Key -> Maybe Text -> TDSL
=:? Maybe Text
_excludeRegex
      Key
"sort_version_key" Key -> Maybe Text -> TDSL
=:? (VersionSortMethod -> Text)
-> Maybe VersionSortMethod -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String -> Text
T.pack (String -> Text)
-> (VersionSortMethod -> String) -> VersionSortMethod -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VersionSortMethod -> String
forall a. Show a => a -> String
show) Maybe VersionSortMethod
_sortVersionKey
      Key
"ignored" Key -> Maybe Text -> TDSL
=:? Maybe Text
_ignored
    genOptions :: NvcheckerOptions -> TDSL
genOptions NvcheckerOptions {Maybe Text
_toPattern :: NvcheckerOptions -> Maybe Text
_fromPattern :: NvcheckerOptions -> Maybe Text
_stripPrefix :: NvcheckerOptions -> Maybe Text
_toPattern :: Maybe Text
_fromPattern :: Maybe Text
_stripPrefix :: Maybe Text
..} = do
      Key
"prefix" Key -> Maybe Text -> TDSL
=:? Maybe Text
_stripPrefix
      Key
"from_pattern" Key -> Maybe Text -> TDSL
=:? Maybe Text
_fromPattern
      Key
"to_pattern" Key -> Maybe Text -> TDSL
=:? Maybe Text
_toPattern

-- | Run nvchecker given 'PackageKey'
-- Recording version changes and using stale version are available.
checkVersion :: VersionSource -> NvcheckerOptions -> PackageKey -> Action NvcheckerResult
checkVersion :: VersionSource
-> NvcheckerOptions -> PackageKey -> Action NvcheckerResult
checkVersion VersionSource
v NvcheckerOptions
o PackageKey
k = WithPackageKey CheckVersion -> Action NvcheckerResult
forall key value.
(Partial, RuleResult key ~ value, ShakeValue key,
 Typeable value) =>
key -> Action value
apply1 (WithPackageKey CheckVersion -> Action NvcheckerResult)
-> WithPackageKey CheckVersion -> Action NvcheckerResult
forall a b. (a -> b) -> a -> b
$ (CheckVersion, PackageKey) -> WithPackageKey CheckVersion
forall k. (k, PackageKey) -> WithPackageKey k
WithPackageKey (VersionSource -> NvcheckerOptions -> CheckVersion
CheckVersion VersionSource
v NvcheckerOptions
o, PackageKey
k)

-- | Run nvchecker without cache
checkVersion' :: VersionSource -> NvcheckerOptions -> Action NvcheckerResult
checkVersion' :: VersionSource -> NvcheckerOptions -> Action NvcheckerResult
checkVersion' VersionSource
v NvcheckerOptions
o = CheckVersion -> Action NvcheckerResult
forall q a.
(RuleResult q ~ a, ShakeValue q, ShakeValue a) =>
q -> Action a
askOracle (CheckVersion -> Action NvcheckerResult)
-> CheckVersion -> Action NvcheckerResult
forall a b. (a -> b) -> a -> b
$ VersionSource -> NvcheckerOptions -> CheckVersion
CheckVersion VersionSource
v NvcheckerOptions
o