Skip to content

Commit 93577e7

Browse files
committed
simplify diff
1 parent 6480428 commit 93577e7

File tree

3 files changed

+31
-62
lines changed

3 files changed

+31
-62
lines changed

src/HaskellCI.hs

+12
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ main = do
8888
CommandDumpConfig -> do
8989
putStr $ unlines $ runDG configGrammar
9090

91+
CommandDiffConfig _Nothing __Nothing -> do
92+
let oldConfig = emptyConfig -- default
93+
newConfig' <- findConfigFile (optConfig opts)
94+
let newConfig = optConfigMorphism opts newConfig'
95+
putStr $ unlines $ diffConfigs configGrammar oldConfig newConfig
96+
97+
98+
{-
9199
CommandDiffConfig cfg fp Nothing -> do
92100
newConfig <- configFromRegenOrConfigFile fp
93101
@@ -98,6 +106,7 @@ main = do
98106
oldConfig <- configFromRegenOrConfigFile oldConfigFp
99107
newConfig <- configFromRegenOrConfigFile newConfigFp
100108
putStr . unlines $ diffConfigs cfg configGrammar oldConfig newConfig
109+
-}
101110

102111
CommandRegenerate -> do
103112
regenerateBash opts
@@ -128,6 +137,8 @@ main = do
128137
-------------------------------------------------------------------------------
129138
-- Diffing
130139
-------------------------------------------------------------------------------
140+
141+
{-
131142
configFromRegenOrConfigFile :: FilePath -> IO Config
132143
configFromRegenOrConfigFile fp = do
133144
withContents fp noFile $ \contents -> case findRegendataArgv contents of
@@ -144,6 +155,7 @@ configFromRegenOrConfigFile fp = do
144155
where
145156
noFile :: IO Config
146157
noFile = putStrLnErr $ "No file named \"" ++ fp ++ "\" exists."
158+
-}
147159

148160
-------------------------------------------------------------------------------
149161
-- Travis

src/HaskellCI/Cli.hs

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import System.IO (hPutStrLn, stderr)
1212
import qualified Options.Applicative as O
1313

1414
import HaskellCI.Config
15-
import HaskellCI.Config.Diff (DiffConfig, defaultDiffConfig, diffConfigGrammar)
1615
import HaskellCI.OptparseGrammar
1716
import HaskellCI.VersionInfo
1817

@@ -27,7 +26,7 @@ data Command
2726
| CommandRegenerate
2827
| CommandListGHC
2928
| CommandDumpConfig
30-
| CommandDiffConfig DiffConfig FilePath (Maybe FilePath)
29+
| CommandDiffConfig (Maybe FilePath) (Maybe FilePath)
3130
| CommandVersionInfo
3231
deriving Show
3332

@@ -137,7 +136,7 @@ cliParserInfo = O.info ((,) <$> cmdP <*> optionsP O.<**> versionP O.<**> O.helpe
137136
, O.command "github" $ O.info githubP $ O.progDesc "Generate GitHub Actions config"
138137
, O.command "list-ghc" $ O.info (pure CommandListGHC) $ O.progDesc "List known GHC versions"
139138
, O.command "dump-config" $ O.info (pure CommandDumpConfig) $ O.progDesc "Dump cabal.haskell-ci config with default values"
140-
, O.command "diff-config" $ O.info diffP $ O.progDesc ""
139+
, O.command "diff-config" $ O.info diffP $ O.progDesc "Diff between configuration files"
141140
, O.command "version-info" $ O.info (pure CommandVersionInfo) $ O.progDesc "Print versions info haskell-ci was compiled with"
142141
]) <|> travisP
143142

@@ -151,8 +150,7 @@ cliParserInfo = O.info ((,) <$> cmdP <*> optionsP O.<**> versionP O.<**> O.helpe
151150
<$> O.strArgument (O.metavar "CABAL.FILE" <> O.action "file" <> O.help "Either <pkg.cabal> or cabal.project")
152151

153152
diffP = CommandDiffConfig
154-
<$> (runOptparseGrammar diffConfigGrammar <*> pure defaultDiffConfig)
155-
<*> O.strArgument (O.metavar "FILE" <> O.action "file" <> O.help "Either a generated CI file or Haskell-CI config file.")
153+
<$> O.optional (O.strArgument (O.metavar "FILE" <> O.action "file" <> O.help "Either a generated CI file or Haskell-CI config file."))
156154
<*> O.optional (O.strArgument (O.metavar "FILE" <> O.action "file" <> O.help "Either a generated CI file or Haskell-CI config file."))
157155

158156
-------------------------------------------------------------------------------

src/HaskellCI/Config/Diff.hs

+16-57
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,22 @@ import Distribution.Fields.Field (FieldName)
1111
import Distribution.Utils.ShortText (fromShortText)
1212

1313
import qualified Distribution.Compat.Lens as L
14-
import qualified Distribution.Compat.CharParsing as C
1514
import qualified Distribution.FieldGrammar as C
16-
import qualified Distribution.Parsec as C
1715
import qualified Distribution.Pretty as C
18-
import qualified Text.PrettyPrint as PP
1916

2017
import HaskellCI.OptionsGrammar
21-
import HaskellCI.Config.Empty (runEG)
22-
23-
data ShowDiffOptions = ShowAllOptions | ShowChangedOptions
24-
deriving (Eq, Show, Generic, Binary)
25-
26-
instance C.Parsec ShowDiffOptions where
27-
parsec = ShowAllOptions <$ C.string "all"
28-
<|> ShowChangedOptions <$ C.string "changed"
29-
30-
instance C.Pretty ShowDiffOptions where
31-
pretty ShowAllOptions = PP.text "all"
32-
pretty ShowChangedOptions = PP.text "changed"
33-
34-
data DiffConfig = DiffConfig
35-
{ diffShowOptions :: ShowDiffOptions
36-
, diffShowOld :: Bool
37-
} deriving (Show, Generic, Binary)
38-
39-
diffConfigGrammar
40-
:: ( OptionsGrammar c g
41-
, Applicative (g DiffConfig)
42-
, c (Identity ShowDiffOptions))
43-
=> g DiffConfig DiffConfig
44-
diffConfigGrammar = DiffConfig
45-
<$> C.optionalFieldDef "diff-show-options" (field @"diffShowOptions") ShowChangedOptions
46-
^^^ help "Which fields to show"
47-
<*> C.booleanFieldDef "diff-show-old" (field @"diffShowOld") False
48-
^^^ help "Show the old values for every field"
49-
50-
defaultDiffConfig :: DiffConfig
51-
defaultDiffConfig = case runEG diffConfigGrammar of
52-
Left xs -> error $ "Required fields: " ++ show xs
53-
Right x -> x
5418

5519
newtype DiffOptions s a =
56-
DiffOptions { runDiffOptions :: (s, s) -> DiffConfig -> [String] }
20+
DiffOptions { runDiffOptions :: (s, s) -> [String] }
5721
deriving Functor
5822

5923
instance Applicative (DiffOptions s) where
60-
pure _ = DiffOptions $ \_ _ -> []
24+
pure _ = DiffOptions $ \_ -> []
6125
DiffOptions f <*> DiffOptions x = DiffOptions (f <> x)
6226

63-
diffConfigs :: DiffConfig -> DiffOptions a a -> a -> a -> [String]
64-
diffConfigs config grammar oldVal newVal =
65-
runDiffOptions grammar (oldVal, newVal) config
27+
diffConfigs :: DiffOptions a a -> a -> a -> [String]
28+
diffConfigs grammar oldVal newVal =
29+
runDiffOptions grammar (oldVal, newVal)
6630

6731
diffUnique
6832
:: Eq b
@@ -71,25 +35,20 @@ diffUnique
7135
-> FieldName
7236
-> L.ALens' s a
7337
-> (s, s)
74-
-> DiffConfig
7538
-> [String]
76-
diffUnique project render fn lens (diffOld, diffNew) opts =
77-
case diffShowOptions opts of
78-
ShowChangedOptions | notEqual -> []
79-
ShowAllOptions | notEqual -> newLine
80-
_ -> oldLine ++ newLine
39+
diffUnique project render fn lens (diffOld, diffNew)
40+
| notEqual =
41+
[ "-" ++ fromUTF8BS fn ++ ": " ++ render oldValue
42+
, "+" ++ fromUTF8BS fn ++ ": " ++ render newValue
43+
, ""
44+
]
45+
46+
| otherwise = []
8147
where
82-
notEqual = project oldValue == project newValue
48+
notEqual = project oldValue /= project newValue
8349
oldValue = L.aview lens $ diffOld
8450
newValue = L.aview lens $ diffNew
8551

86-
oldLine
87-
| diffShowOld opts = ["-- " ++ fromUTF8BS fn ++ ": " ++ render oldValue]
88-
| otherwise = []
89-
90-
newLine = [ fromUTF8BS fn ++ ": " ++ render newValue, ""]
91-
92-
9352
instance C.FieldGrammar C.Pretty DiffOptions where
9453
blurFieldGrammar lens (DiffOptions diff) =
9554
DiffOptions $ diff . bimap (L.aview lens) (L.aview lens)
@@ -130,7 +89,7 @@ instance C.FieldGrammar C.Pretty DiffOptions where
13089
instance OptionsGrammar C.Pretty DiffOptions where
13190
metahelp _ = help
13291

133-
help h (DiffOptions xs) = DiffOptions $ \vals config ->
134-
case xs vals config of
92+
help h (DiffOptions xs) = DiffOptions $ \vals ->
93+
case xs vals of
13594
[] -> []
13695
diffString -> ("-- " ++ h) : diffString

0 commit comments

Comments
 (0)