-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathDiff.hs
95 lines (74 loc) · 2.83 KB
/
Diff.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
module HaskellCI.Config.Diff where
import HaskellCI.Prelude
import Distribution.Fields.Field (FieldName)
import Distribution.Utils.ShortText (fromShortText)
import qualified Distribution.Compat.Lens as L
import qualified Distribution.FieldGrammar as C
import qualified Distribution.Pretty as C
import HaskellCI.OptionsGrammar
newtype DiffOptions s a =
DiffOptions { runDiffOptions :: (s, s) -> [String] }
deriving Functor
instance Applicative (DiffOptions s) where
pure _ = DiffOptions $ \_ -> []
DiffOptions f <*> DiffOptions x = DiffOptions (f <> x)
diffConfigs :: DiffOptions a a -> a -> a -> [String]
diffConfigs grammar oldVal newVal =
runDiffOptions grammar (oldVal, newVal)
diffUnique
:: Eq b
=> (a -> b)
-> (a -> String)
-> FieldName
-> L.ALens' s a
-> (s, s)
-> [String]
diffUnique project render fn lens (diffOld, diffNew)
| notEqual =
[ "-" ++ fromUTF8BS fn ++ ": " ++ render oldValue
, "+" ++ fromUTF8BS fn ++ ": " ++ render newValue
, ""
]
| otherwise = []
where
notEqual = project oldValue /= project newValue
oldValue = L.aview lens $ diffOld
newValue = L.aview lens $ diffNew
instance C.FieldGrammar C.Pretty DiffOptions where
blurFieldGrammar lens (DiffOptions diff) =
DiffOptions $ diff . bimap (L.aview lens) (L.aview lens)
uniqueFieldAla fn pack valueLens = DiffOptions $
diffUnique (C.prettyShow . pack) (C.prettyShow . pack) fn valueLens
booleanFieldDef fn valueLens _ = DiffOptions $
diffUnique id C.prettyShow fn valueLens
optionalFieldAla fn pack valueLens = DiffOptions $
diffUnique toPretty toPretty fn valueLens
where
toPretty = maybe "" (C.prettyShow . pack)
optionalFieldDefAla fn pack valueLens _ = DiffOptions $
diffUnique id (C.prettyShow . pack) fn valueLens
monoidalFieldAla fn pack valueLens = DiffOptions $
diffUnique (C.prettyShow . pack) (C.prettyShow . pack) fn valueLens
freeTextField fn valueLens = DiffOptions $
diffUnique id (fromMaybe "") fn valueLens
freeTextFieldDef fn valueLens = DiffOptions $
diffUnique id id fn valueLens
freeTextFieldDefST fn valueLens = DiffOptions $
diffUnique id fromShortText fn valueLens
prefixedFields _ _ = pure []
knownField _ = pure ()
deprecatedSince _ _ = id
availableSince _ _ = id
removedIn _ _ = id
hiddenField = id
instance OptionsGrammar C.Pretty DiffOptions where
metahelp _ = help
help h (DiffOptions xs) = DiffOptions $ \vals ->
case xs vals of
[] -> []
diffString -> ("-- " ++ h) : diffString