From e64c26e603973531647583f88365f14804f82a58 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 11 Sep 2025 10:02:27 +0100 Subject: [PATCH] lib!: rename withJournal -> withJournalDo, with alias and deprecation warning --- bin/hledger-balance-as-budget.hs | 2 +- bin/hledger-check-postable.hs | 2 +- bin/hledger-check-tagfiles.cabal.hs | 2 +- bin/hledger-check-tagfiles.hs | 2 +- bin/hledger-combine-balances.hs | 2 +- bin/hledger-move.hs | 2 +- bin/hledger-register-max.hs | 2 +- bin/hledger-register-max2.hs | 2 +- bin/hledger-report1.hs | 2 +- bin/hledger-script-example-short.hs | 2 +- bin/hledger-script-example.hs | 2 +- bin/hledger-smooth.hs | 2 +- bin/hledger-swap-dates.hs | 2 +- doc/CODE.md | 2 +- hledger-ui/Hledger/UI/Main.hs | 2 +- hledger-web/Hledger/Web/Main.hs | 4 ++-- hledger-web/Hledger/Web/Test.hs | 2 +- hledger/Hledger/Cli.hs | 4 ++-- hledger/Hledger/Cli/Utils.hs | 12 ++++++++---- 19 files changed, 28 insertions(+), 24 deletions(-) diff --git a/bin/hledger-balance-as-budget.hs b/bin/hledger-balance-as-budget.hs index 666f2a251..2e24de360 100755 --- a/bin/hledger-balance-as-budget.hs +++ b/bin/hledger-balance-as-budget.hs @@ -44,5 +44,5 @@ main = do mbReport args = do opts@CliOpts{reportspec_=rspec} <- getHledgerCliOpts' balancemode args d <- getCurrentDay - (report,j) <- withJournalDo opts $ \j -> return (multiBalanceReport rspec j, j) + (report,j) <- withJournal opts $ \j -> return (multiBalanceReport rspec j, j) return (opts, _rsReportOpts rspec,j,report) diff --git a/bin/hledger-check-postable.hs b/bin/hledger-check-postable.hs index 037c7720e..c153641c6 100755 --- a/bin/hledger-check-postable.hs +++ b/bin/hledger-check-postable.hs @@ -36,7 +36,7 @@ Check that no postings are made to accounts with a postable:(n|no) tag. main :: IO () main = do opts@CliOpts{reportspec_=_rspec} <- getHledgerCliOpts cmdmode - withJournalDo opts $ \j -> do + withJournal opts $ \j -> do let postedaccts = journalAccountNamesUsed j checkAcctPostable :: Journal -> AccountName -> Either AccountName () diff --git a/bin/hledger-check-tagfiles.cabal.hs b/bin/hledger-check-tagfiles.cabal.hs index a17f52052..37b1ceed6 100755 --- a/bin/hledger-check-tagfiles.cabal.hs +++ b/bin/hledger-check-tagfiles.cabal.hs @@ -21,7 +21,7 @@ import Hledger.Cli.Script import System.Directory import System.Exit -main = withJournalDo defcliopts $ \j -> do +main = withJournal defcliopts $ \j -> do let filetags = [ (t,v) | (t',v') <- concatMap transactionAllTags $ jtxns j , let t = T.unpack t' diff --git a/bin/hledger-check-tagfiles.hs b/bin/hledger-check-tagfiles.hs index c9994f2e8..d82b30e55 100755 --- a/bin/hledger-check-tagfiles.hs +++ b/bin/hledger-check-tagfiles.hs @@ -22,7 +22,7 @@ import Hledger.Cli.Script import System.Directory import System.Exit -main = withJournalDo defcliopts $ \j -> do +main = withJournal defcliopts $ \j -> do let filetags = [ (t,v) | (t',v') <- concatMap transactionAllTags $ jtxns j , let t = T.unpack t' diff --git a/bin/hledger-combine-balances.hs b/bin/hledger-combine-balances.hs index 23c679f8d..d7d330712 100755 --- a/bin/hledger-combine-balances.hs +++ b/bin/hledger-combine-balances.hs @@ -69,5 +69,5 @@ main = do where mbReport args = do opts@CliOpts{reportspec_=rspec} <- getHledgerCliOpts' cmdmode args - report <- withJournalDo opts (return . multiBalanceReport rspec) + report <- withJournal opts (return . multiBalanceReport rspec) return (rspec,report) diff --git a/bin/hledger-move.hs b/bin/hledger-move.hs index d5af5598a..d0bea9ded 100755 --- a/bin/hledger-move.hs +++ b/bin/hledger-move.hs @@ -128,7 +128,7 @@ $ hledger-move all assets:broker1:FOO assets:broker2:FOO # move all FOO lots to main :: IO () main = do copts@CliOpts{rawopts_=rawopts, reportspec_=rspec0} <- getHledgerCliOpts cmdmode - withJournalDo copts $ \j -> do + withJournal copts $ \j -> do -- d <- getCurrentDay let -- arg errors diff --git a/bin/hledger-register-max.hs b/bin/hledger-register-max.hs index 40292ad98..1c53019d4 100755 --- a/bin/hledger-register-max.hs +++ b/bin/hledger-register-max.hs @@ -34,7 +34,7 @@ import Hledger.Cli.Script main = do args <- getArgs opts <- argsToCliOpts ("register" : args) [] - withJournalDo opts $ \j -> do + withJournal opts $ \j -> do let r = postingsReport (reportspec_ opts) j getamt = pamount.fourth5 diff --git a/bin/hledger-register-max2.hs b/bin/hledger-register-max2.hs index f9d395cbf..dee120426 100755 --- a/bin/hledger-register-max2.hs +++ b/bin/hledger-register-max2.hs @@ -30,7 +30,7 @@ cmdmode = hledgerCommandMode (unlines main = do opts@CliOpts{reportspec_=rspec} <- getHledgerCliOpts cmdmode - withJournalDo opts $ \j -> do + withJournal opts $ \j -> do let r = postingsReport rspec j maxbal = fifth5 $ maximumBy (comparing fifth5) r diff --git a/bin/hledger-report1.hs b/bin/hledger-report1.hs index 1e451eb78..4ca6dbc18 100755 --- a/bin/hledger-report1.hs +++ b/bin/hledger-report1.hs @@ -23,7 +23,7 @@ cmdmode = hledgerCommandMode (unlines main = do opts@CliOpts{reportspec_=rspec} <- getHledgerCliOpts cmdmode - withJournalDo opts $ flip compoundBalanceCommand opts $ + withJournal opts $ flip compoundBalanceCommand opts $ -- see https://hackage.haskell.org/package/hledger/docs/Hledger-Cli-CompoundBalanceCommand.html -- and https://hackage.haskell.org/package/hledger-lib-1.31/docs/Hledger-Query.html diff --git a/bin/hledger-script-example-short.hs b/bin/hledger-script-example-short.hs index 09ee18500..d2017b497 100644 --- a/bin/hledger-script-example-short.hs +++ b/bin/hledger-script-example-short.hs @@ -25,7 +25,7 @@ cmdmode = hledgerCommandMode (unlines main = do opts@CliOpts{reportspec_=rspec} <- getHledgerCliOpts cmdmode - withJournalDo opts $ \j -> do + withJournal opts $ \j -> do putStrLn "hello" diff --git a/bin/hledger-script-example.hs b/bin/hledger-script-example.hs index 938870dcf..07d64017d 100755 --- a/bin/hledger-script-example.hs +++ b/bin/hledger-script-example.hs @@ -68,7 +68,7 @@ main = do opts@CliOpts{reportspec_=rspec} <- getHledgerCliOpts cmdmode -- 2. read the journal file - withJournalDo opts $ \j -> do + withJournal opts $ \j -> do -- 3. do something with it. putStrLn $ (show $ length $ jtxns j) <> " transactions in " <> (show $ journalFilePath j) diff --git a/bin/hledger-smooth.hs b/bin/hledger-smooth.hs index c4f5c346f..1deb91ca3 100755 --- a/bin/hledger-smooth.hs +++ b/bin/hledger-smooth.hs @@ -63,7 +63,7 @@ main = do -- Don't let our ACCT argument be interpreted as a query by print ,reportspec_ = rspec{_rsReportOpts=ropts{querystring_=[]}} } - withJournalDo copts' $ \j -> do + withJournal copts' $ \j -> do today <- getCurrentDay let menddate = reportPeriodLastDay rspec diff --git a/bin/hledger-swap-dates.hs b/bin/hledger-swap-dates.hs index d86da58cf..e4d7dd77a 100755 --- a/bin/hledger-swap-dates.hs +++ b/bin/hledger-swap-dates.hs @@ -28,7 +28,7 @@ Swap date and date2, on transactions which have date2 defined. main :: IO () main = do opts@CliOpts{reportspec_=rspec} <- getHledgerCliOpts cmdmode - withJournalDo opts $ + withJournal opts $ \j -> do d <- getCurrentDay let diff --git a/doc/CODE.md b/doc/CODE.md index dd9bc18cf..52e561c79 100644 --- a/doc/CODE.md +++ b/doc/CODE.md @@ -100,7 +100,7 @@ Try tracing the execution of a hledger command: 1. [Hledger.Cli.Main:main](https://github.com/simonmichael/hledger/blob/master/hledger/Hledger/Cli/Main.hs#L302) parses the command line to select a command, then 2. gives it to -[Hledger.Cli.Utils:withJournalDo](https://github.com/simonmichael/hledger/blob/master/hledger/Hledger/Cli/Utils.hs#L73), +[Hledger.Cli.Utils:withJournal](https://github.com/simonmichael/hledger/blob/master/hledger/Hledger/Cli/Utils.hs#L73), which runs it after doing all the initial parsing. 3. Parsing code is under [hledger-lib:Hledger.Read](https://github.com/simonmichael/hledger/tree/master/hledger-lib/Hledger/Read.hs), diff --git a/hledger-ui/Hledger/UI/Main.hs b/hledger-ui/Hledger/UI/Main.hs index 036e4a1c2..2823b216f 100644 --- a/hledger-ui/Hledger/UI/Main.hs +++ b/hledger-ui/Hledger/UI/Main.hs @@ -118,7 +118,7 @@ hledgerUiMain = handleExit $ withGhcDebug' $ withProgName "hledger-ui.log" $ do _ | boolopt "man" rawopts -> runManForTopic "hledger-ui" Nothing _ | boolopt "version" rawopts -> putStrLn prognameandversion -- _ | boolopt "binary-filename" rawopts -> putStrLn (binaryfilename progname) - _ -> withJournalDo copts' (runBrickUi opts) + _ -> withJournal copts' (runBrickUi opts) when (ghcDebugMode == GDPauseAtEnd) $ ghcDebugPause' diff --git a/hledger-web/Hledger/Web/Main.hs b/hledger-web/Hledger/Web/Main.hs index ae40dd54c..26e3ad7c4 100644 --- a/hledger-web/Hledger/Web/Main.hs +++ b/hledger-web/Hledger/Web/Main.hs @@ -53,7 +53,7 @@ import Hledger.Web.WebOptions -- Run in fast reloading mode for yesod devel. hledgerWebDev :: IO (Int, Application) hledgerWebDev = - withJournalDo (cliopts_ defwebopts) (defaultDevelApp loader . makeApplication defwebopts) + withJournal (cliopts_ defwebopts) (defaultDevelApp loader . makeApplication defwebopts) where loader = Yesod.Default.Config.loadConfig @@ -93,7 +93,7 @@ hledgerWebMain = handleExit $ withGhcDebug' $ do | boolopt "test" rawopts_ -> do -- remove --test and --, leaving other args for hspec (`withArgs` hledgerWebTest) . filter (`notElem` ["--test","--"]) =<< getArgs - | otherwise -> withJournalDo copts (web wopts) + | otherwise -> withJournal copts (web wopts) when (ghcDebugMode == GDPauseAtEnd) $ ghcDebugPause' diff --git a/hledger-web/Hledger/Web/Test.hs b/hledger-web/Hledger/Web/Test.hs index a29912f8a..678e89cc9 100644 --- a/hledger-web/Hledger/Web/Test.hs +++ b/hledger-web/Hledger/Web/Test.hs @@ -14,7 +14,7 @@ stack new projectname yesodweb/sqlite These tests don't exactly match the production code path, eg these bits are missing: - withJournalDo copts (web wopts) -- extra withJournalDo logic (journalTransform..) + withJournal copts (web wopts) -- extra withJournal logic (journalTransform..) ... -- query logic, more options logic let depthlessinitialq = filterQuery (not . queryIsDepth) . _rsQuery . reportspec_ $ cliopts_ wopts diff --git a/hledger/Hledger/Cli.hs b/hledger/Hledger/Cli.hs index d70798242..820880f25 100644 --- a/hledger/Hledger/Cli.hs +++ b/hledger/Hledger/Cli.hs @@ -429,14 +429,14 @@ main = handleExit $ withGhcDebug' $ do -- 6.4.3. builtin command which should create the journal if missing - do that and run it | cmdname `elem` ["add","import"] -> do ensureJournalFileExists . NE.head =<< journalFilePathFromOpts opts - withJournalDo opts (cmdaction opts) + withJournal opts (cmdaction opts) -- 6.4.4. "run" and "repl" need findBuiltinCommands passed to it to avoid circular dependency in the code | cmdname == "run" -> Hledger.Cli.Commands.Run.run Nothing findBuiltinCommand addons opts | cmdname == "repl" -> Hledger.Cli.Commands.Run.repl findBuiltinCommand addons opts -- 6.4.5. all other builtin commands - read the journal and if successful run the command with it - | otherwise -> withJournalDo opts $ cmdaction opts + | otherwise -> withJournal opts $ cmdaction opts -- 6.5. external addon command found - run it, -- passing any cli arguments written after the command name diff --git a/hledger/Hledger/Cli/Utils.hs b/hledger/Hledger/Cli/Utils.hs index a9b96746c..b85d454f3 100644 --- a/hledger/Hledger/Cli/Utils.hs +++ b/hledger/Hledger/Cli/Utils.hs @@ -10,6 +10,7 @@ Hledger.Utils. module Hledger.Cli.Utils ( unsupportedOutputFormatError, + withJournal, withJournalDo, writeOutput, writeOutputLazyText, @@ -67,8 +68,8 @@ unsupportedOutputFormatError fmt = "Sorry, output format \""++fmt++"\" is unreco -- | Parse the user's specified journal file(s) as a Journal, maybe apply some -- transformations according to options, and run a hledger command with it. -- Or, throw an error. -withJournalDo :: CliOpts -> (Journal -> IO a) -> IO a -withJournalDo opts cmd = do +withJournal :: CliOpts -> (Journal -> IO a) -> IO a +withJournal opts cmd = do -- We kludgily read the file before parsing to grab the full text, unless -- it's stdin, or it doesn't exist and we are adding. We read it strictly -- to let the add command work. @@ -76,6 +77,9 @@ withJournalDo opts cmd = do j <- runExceptT $ journalTransform opts <$> readJournalFiles (inputopts_ opts) (NE.toList journalpaths) either error' cmd j -- PARTIAL: +{-# DEPRECATED withJournalDo "renamed, please use withJournal instead" #-} +withJournalDo = withJournal + -- | Apply some extra post-parse transformations to the journal, if enabled by options. -- These happen after parsing and finalising the journal, but before report calculation. -- They are, in processing order: @@ -89,7 +93,7 @@ journalTransform opts = pivotByOpts opts <&> anonymiseByOpts opts <&> maybeObfuscate opts --- XXX Called by withJournalDo, journalReload, uiReloadJournal, withJournalCached. +-- XXX Called by withJournal, journalReload, uiReloadJournal, withJournalCached. -- Could it be moved down into journalFinalise ? These steps only depend on InputOpts. -- | Apply the pivot transformation on a journal (replacing account names by a different field's value), if option is present. @@ -142,7 +146,7 @@ writeOutputLazyText opts s = do -- them has changed since last read. (If the file is standard input, -- this will either do nothing or give an error, not tested yet). -- Returns a journal or error message, and a flag indicating whether --- it was re-read or not. Like withJournalDo and journalReload, reads +-- it was re-read or not. Like withJournal and journalReload, reads -- the full journal, without filtering. journalReloadIfChanged :: CliOpts -> Day -> Journal -> ExceptT String IO (Journal, Bool) journalReloadIfChanged opts _d j = do