{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
module NvFetcher.GetGitCommitDate
(
DateFormat (..),
GetGitCommitDate (..),
getGitCommitDateRule,
getGitCommitDate,
)
where
import Control.Monad (void)
import Data.Coerce (coerce)
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import qualified Data.Text as T
import Development.Shake
import NvFetcher.Types
import Prettyprinter (pretty, (<+>))
getGitCommitDateRule :: Rules ()
getGitCommitDateRule :: Rules ()
getGitCommitDateRule = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ do
forall q a.
(RuleResult q ~ a, ShakeValue q, ShakeValue a, Partial) =>
(q -> Action a) -> Rules (q -> Action a)
addOracleCache forall a b. (a -> b) -> a -> b
$ \q :: GetGitCommitDate
q@(GetGitCommitDate (Text -> String
T.unpack -> String
url) (Text -> String
T.unpack -> String
rev) DateFormat
format) -> forall a. (String -> Action a) -> Action a
withTempDir forall a b. (a -> b) -> a -> b
$ \String
repo -> do
String -> Action ()
putInfo forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ Doc Any
"#" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty GetGitCommitDate
q
(StdoutTrim String
out) <- forall a. Action a -> Action a
quietly forall a b. (a -> b) -> a -> b
$ do
forall args. (Partial, CmdArguments args, Unit args) => args
cmd_ [String -> CmdOption
Cwd String
repo, Bool -> CmdOption
EchoStderr Bool
False, Bool -> CmdOption
EchoStdout Bool
False] (String
"git init" :: String)
forall args. (Partial, CmdArguments args, Unit args) => args
cmd_ [String -> CmdOption
Cwd String
repo, Bool -> CmdOption
EchoStderr Bool
False] forall a b. (a -> b) -> a -> b
$ String
"git remote add origin " forall a. Semigroup a => a -> a -> a
<> String
url
forall args. (Partial, CmdArguments args, Unit args) => args
cmd_ [String -> CmdOption
Cwd String
repo, Bool -> CmdOption
EchoStderr Bool
False] forall a b. (a -> b) -> a -> b
$ String
"git fetch --depth 1 origin " forall a. Semigroup a => a -> a -> a
<> String
rev
forall args. (Partial, CmdArguments args, Unit args) => args
cmd_ [String -> CmdOption
Cwd String
repo, Bool -> CmdOption
EchoStderr Bool
False] (String
"git checkout FETCH_HEAD" :: String)
forall args r. (Partial, CmdArguments args) => args
cmd [String -> CmdOption
Cwd String
repo, CmdOption
Shell] forall a b. (a -> b) -> a -> b
$ String
"git --no-pager log -1 --format=%cd --date=format:\"" forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack (forall a. a -> Maybe a -> a
fromMaybe Text
"%Y-%m-%d" forall a b. (a -> b) -> a -> b
$ coerce :: forall a b. Coercible a b => a -> b
coerce DateFormat
format) forall a. Semigroup a => a -> a -> a
<> String
"\""
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
out
getGitCommitDate :: Text -> Text -> DateFormat -> Action Text
getGitCommitDate :: Text -> Text -> DateFormat -> Action Text
getGitCommitDate Text
url Text
rev DateFormat
format = forall q a.
(RuleResult q ~ a, ShakeValue q, ShakeValue a) =>
q -> Action a
askOracle forall a b. (a -> b) -> a -> b
$ Text -> Text -> DateFormat -> GetGitCommitDate
GetGitCommitDate Text
url Text
rev DateFormat
format