Skip to content

Commit b58c878

Browse files
committed
Initial diff command.
1 parent f70959e commit b58c878

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

src/HaskellCI.hs

+32
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import HaskellCI.Bash
5656
import HaskellCI.Cli
5757
import HaskellCI.Compiler
5858
import HaskellCI.Config
59+
import HaskellCI.Config.Diff
5960
import HaskellCI.Config.Dump
6061
import HaskellCI.Diagnostics
6162
import HaskellCI.GitConfig
@@ -88,6 +89,17 @@ main = do
8889
CommandDumpConfig -> do
8990
putStr $ unlines $ runDG configGrammar
9091

92+
CommandDiffConfig cfg fp Nothing -> do
93+
newConfig <- configFromRegenOrConfigFile fp
94+
95+
let oldConfig = optConfigMorphism opts emptyConfig
96+
putStr . unlines $ diffConfigs cfg configGrammar oldConfig newConfig
97+
98+
CommandDiffConfig cfg oldConfigFp (Just newConfigFp) -> do
99+
oldConfig <- configFromRegenOrConfigFile oldConfigFp
100+
newConfig <- configFromRegenOrConfigFile newConfigFp
101+
putStr . unlines $ diffConfigs cfg configGrammar oldConfig newConfig
102+
91103
CommandRegenerate -> do
92104
regenerateBash opts
93105
regenerateGitHub opts
@@ -114,6 +126,26 @@ main = do
114126
ifor_ :: Map.Map k v -> (k -> v -> IO a) -> IO ()
115127
ifor_ xs f = Map.foldlWithKey' (\m k a -> m >> void (f k a)) (return ()) xs
116128

129+
-------------------------------------------------------------------------------
130+
-- Diffing
131+
-------------------------------------------------------------------------------
132+
configFromRegenOrConfigFile :: FilePath -> IO Config
133+
configFromRegenOrConfigFile fp = do
134+
withContents fp noFile $ \contents -> case findRegendataArgv contents of
135+
Nothing -> readConfigFile fp
136+
Just (mversion, argv) -> do
137+
-- warn if we regenerate using older haskell-ci
138+
for_ mversion $ \version -> for_ (simpleParsec haskellCIVerStr) $ \haskellCIVer ->
139+
when (haskellCIVer < version) $ do
140+
putStrLnWarn $ "Regenerating using older haskell-ci-" ++ haskellCIVerStr
141+
putStrLnWarn $ "File generated using haskell-ci-" ++ prettyShow version
142+
143+
opts <- snd <$> parseOptions argv
144+
optConfigMorphism opts <$> findConfigFile (optConfig opts)
145+
where
146+
noFile :: IO Config
147+
noFile = putStrLnErr $ "No file named \"" ++ fp ++ "\" exists."
148+
117149
-------------------------------------------------------------------------------
118150
-- Travis
119151
-------------------------------------------------------------------------------

src/HaskellCI/Cli.hs

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ 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)
1516
import HaskellCI.OptparseGrammar
1617
import HaskellCI.VersionInfo
1718

@@ -26,6 +27,7 @@ data Command
2627
| CommandRegenerate
2728
| CommandListGHC
2829
| CommandDumpConfig
30+
| CommandDiffConfig DiffConfig FilePath (Maybe FilePath)
2931
| CommandVersionInfo
3032
deriving Show
3133

@@ -135,6 +137,7 @@ cliParserInfo = O.info ((,) <$> cmdP <*> optionsP O.<**> versionP O.<**> O.helpe
135137
, O.command "github" $ O.info githubP $ O.progDesc "Generate GitHub Actions config"
136138
, O.command "list-ghc" $ O.info (pure CommandListGHC) $ O.progDesc "List known GHC versions"
137139
, 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 ""
138141
, O.command "version-info" $ O.info (pure CommandVersionInfo) $ O.progDesc "Print versions info haskell-ci was compiled with"
139142
]) <|> travisP
140143

@@ -147,6 +150,11 @@ cliParserInfo = O.info ((,) <$> cmdP <*> optionsP O.<**> versionP O.<**> O.helpe
147150
githubP = CommandGitHub
148151
<$> O.strArgument (O.metavar "CABAL.FILE" <> O.action "file" <> O.help "Either <pkg.cabal> or cabal.project")
149152

153+
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.")
156+
<*> O.optional (O.strArgument (O.metavar "FILE" <> O.action "file" <> O.help "Either a generated CI file or Haskell-CI config file."))
157+
150158
-------------------------------------------------------------------------------
151159
-- Parsing helpers
152160
-------------------------------------------------------------------------------

src/HaskellCI/Config/Diff.hs

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import qualified Distribution.Pretty as C
1919
import qualified Text.PrettyPrint as PP
2020

2121
import HaskellCI.OptionsGrammar
22+
import HaskellCI.Config.Empty (runEG)
2223

2324
data ShowDiffOptions = ShowAllOptions | ShowChangedOptions
2425
deriving (Eq, Show, Generic, Binary)
@@ -47,6 +48,11 @@ diffConfigGrammar = DiffConfig
4748
<*> C.booleanFieldDef "diff-show-old" (field @"diffShowOld") False
4849
^^^ help "Show the old values for every field"
4950

51+
defaultDiffConfig :: DiffConfig
52+
defaultDiffConfig = case runEG diffConfigGrammar of
53+
Left xs -> error $ "Required fields: " ++ show xs
54+
Right x -> x
55+
5056
newtype DiffOptions s a =
5157
DiffOptions { runDiffOptions :: (s, s) -> DiffConfig -> [String] }
5258
deriving Functor
@@ -98,7 +104,7 @@ instance C.FieldGrammar C.Pretty DiffOptions where
98104
optionalFieldAla fn pack valueLens = DiffOptions $
99105
diffUnique toPretty toPretty fn valueLens
100106
where
101-
toPretty = maybe "" C.prettyShow . fmap pack
107+
toPretty = maybe "" (C.prettyShow . pack)
102108

103109
optionalFieldDefAla fn pack valueLens _ = DiffOptions $
104110
diffUnique id (C.prettyShow . pack) fn valueLens

0 commit comments

Comments
 (0)