Skip to content

Commit 5e2e32b

Browse files
committed
Initial diff command.
1 parent 39d9630 commit 5e2e32b

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
@@ -57,6 +57,7 @@ import HaskellCI.Bash
5757
import HaskellCI.Cli
5858
import HaskellCI.Compiler
5959
import HaskellCI.Config
60+
import HaskellCI.Config.Diff
6061
import HaskellCI.Config.Dump
6162
import HaskellCI.Diagnostics
6263
import HaskellCI.GitConfig
@@ -89,6 +90,17 @@ main = do
8990
CommandDumpConfig -> do
9091
putStr $ unlines $ runDG configGrammar
9192

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

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

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)