{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeApplications #-}
module Headroom.PostProcess.UpdateCopyright
(
SelectedAuthors(..)
, UpdateCopyrightMode(..)
, updateCopyright
, updateYears
)
where
import Data.String.Interpolate ( i )
import Headroom.Data.Has ( Has(..) )
import Headroom.Data.Regex ( re
, replace
)
import Headroom.Data.Text ( mapLines
, read
)
import Headroom.PostProcess.Types ( PostProcess(..) )
import Headroom.Types ( CurrentYear(..) )
import RIO
import qualified RIO.NonEmpty as NE
import qualified RIO.Text as T
newtype SelectedAuthors = SelectedAuthors (NonEmpty Text) deriving (SelectedAuthors -> SelectedAuthors -> Bool
(SelectedAuthors -> SelectedAuthors -> Bool)
-> (SelectedAuthors -> SelectedAuthors -> Bool)
-> Eq SelectedAuthors
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SelectedAuthors -> SelectedAuthors -> Bool
$c/= :: SelectedAuthors -> SelectedAuthors -> Bool
== :: SelectedAuthors -> SelectedAuthors -> Bool
$c== :: SelectedAuthors -> SelectedAuthors -> Bool
Eq, Int -> SelectedAuthors -> ShowS
[SelectedAuthors] -> ShowS
SelectedAuthors -> String
(Int -> SelectedAuthors -> ShowS)
-> (SelectedAuthors -> String)
-> ([SelectedAuthors] -> ShowS)
-> Show SelectedAuthors
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SelectedAuthors] -> ShowS
$cshowList :: [SelectedAuthors] -> ShowS
show :: SelectedAuthors -> String
$cshow :: SelectedAuthors -> String
showsPrec :: Int -> SelectedAuthors -> ShowS
$cshowsPrec :: Int -> SelectedAuthors -> ShowS
Show)
data UpdateCopyrightMode
= UpdateAllAuthors
| UpdateSelectedAuthors SelectedAuthors
deriving (UpdateCopyrightMode -> UpdateCopyrightMode -> Bool
(UpdateCopyrightMode -> UpdateCopyrightMode -> Bool)
-> (UpdateCopyrightMode -> UpdateCopyrightMode -> Bool)
-> Eq UpdateCopyrightMode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UpdateCopyrightMode -> UpdateCopyrightMode -> Bool
$c/= :: UpdateCopyrightMode -> UpdateCopyrightMode -> Bool
== :: UpdateCopyrightMode -> UpdateCopyrightMode -> Bool
$c== :: UpdateCopyrightMode -> UpdateCopyrightMode -> Bool
Eq, Int -> UpdateCopyrightMode -> ShowS
[UpdateCopyrightMode] -> ShowS
UpdateCopyrightMode -> String
(Int -> UpdateCopyrightMode -> ShowS)
-> (UpdateCopyrightMode -> String)
-> ([UpdateCopyrightMode] -> ShowS)
-> Show UpdateCopyrightMode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UpdateCopyrightMode] -> ShowS
$cshowList :: [UpdateCopyrightMode] -> ShowS
show :: UpdateCopyrightMode -> String
$cshow :: UpdateCopyrightMode -> String
showsPrec :: Int -> UpdateCopyrightMode -> ShowS
$cshowsPrec :: Int -> UpdateCopyrightMode -> ShowS
Show)
updateCopyright :: (Has CurrentYear env, Has UpdateCopyrightMode env)
=> PostProcess env
updateCopyright :: PostProcess env
updateCopyright = (Text -> Reader env Text) -> PostProcess env
forall env. (Text -> Reader env Text) -> PostProcess env
PostProcess ((Text -> Reader env Text) -> PostProcess env)
-> (Text -> Reader env Text) -> PostProcess env
forall a b. (a -> b) -> a -> b
$ \Text
input -> do
CurrentYear
currentYear <- ReaderT env Identity CurrentYear
forall a t (m :: * -> *). (Has a t, MonadReader t m) => m a
viewL
UpdateCopyrightMode
mode <- ReaderT env Identity UpdateCopyrightMode
forall a t (m :: * -> *). (Has a t, MonadReader t m) => m a
viewL
Text -> Reader env Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Reader env Text) -> Text -> Reader env Text
forall a b. (a -> b) -> a -> b
$ (Text -> Text) -> Text -> Text
mapLines (UpdateCopyrightMode -> CurrentYear -> Text -> Text
update UpdateCopyrightMode
mode CurrentYear
currentYear) Text
input
where
update :: UpdateCopyrightMode -> CurrentYear -> Text -> Text
update UpdateCopyrightMode
mode CurrentYear
year Text
line | UpdateCopyrightMode -> Text -> Bool
shouldUpdate UpdateCopyrightMode
mode Text
line = CurrentYear -> Text -> Text
updateYears CurrentYear
year Text
line
| Bool
otherwise = Text
line
shouldUpdate :: UpdateCopyrightMode -> Text -> Bool
shouldUpdate UpdateCopyrightMode
UpdateAllAuthors Text
_ = Bool
True
shouldUpdate (UpdateSelectedAuthors (SelectedAuthors NonEmpty Text
authors)) Text
input =
(Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Text -> Text -> Bool
`T.isInfixOf` Text
input) (NonEmpty Text -> [Text]
forall a. NonEmpty a -> [a]
NE.toList NonEmpty Text
authors)
updateYears :: CurrentYear
-> Text
-> Text
updateYears :: CurrentYear -> Text -> Text
updateYears CurrentYear
cy = Regex -> (Text -> [Text] -> Text) -> Text -> Text
replace [re|(\d{4})(?:-)?(\d{4})?|] Text -> [Text] -> Text
go
where
go :: Text -> [Text] -> Text
go Text
_ [Text
r1] | (Just Integer
y1) <- Text -> Maybe Integer
forall a. Read a => Text -> Maybe a
read Text
r1 = CurrentYear -> Integer -> Text
bumpYear CurrentYear
cy Integer
y1
go Text
_ rs :: [Text]
rs@[Text
_, Text
_] | [Just Integer
y1, Just Integer
y2] <- Text -> Maybe Integer
forall a. Read a => Text -> Maybe a
read (Text -> Maybe Integer) -> [Text] -> [Maybe Integer]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text]
rs = CurrentYear -> Integer -> Integer -> Text
bumpRange CurrentYear
cy Integer
y1 Integer
y2
go Text
other [Text]
_ = Text
other
bumpYear :: CurrentYear -> Integer -> Text
bumpYear :: CurrentYear -> Integer -> Text
bumpYear (CurrentYear Integer
cy) Integer
y | Integer
y Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
cy = Integer -> Text
forall a. Show a => a -> Text
tshow Integer
y
| Bool
otherwise = [i|#{y}-#{cy}|]
bumpRange :: CurrentYear -> Integer -> Integer -> Text
bumpRange :: CurrentYear -> Integer -> Integer -> Text
bumpRange (CurrentYear Integer
cy) Integer
y1 Integer
y2 | Integer
y2 Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
cy = [i|#{y1}-#{y2}|]
| Bool
otherwise = [i|#{y1}-#{cy}|]