hledger/bin/hledger-balance-as-budget.hs
Simon Michael 2db87333d7 bin: switch scripts to "stack ghc" and "env -S" (#1453)
Using stack's script command meant that the scripts needed to be
compatible, and regularly tested, with a hledger release in stackage,
rather than the latest hledger source. This created hassles for
maintainers, contributors and sometimes for users.

To simplify things overall, we now require script users to check out
the hledger source tree and run the scripts (or, bin/compile.sh) from
there once so they compile themselves. Some notes on alternative
setups are included (in one of the scripts, and referenced by the
others). This ensures that users and our CI tests are building scripts
the same way.

Current stack does not allow a stack options line to be used with the
"stack ghc" command, unfortunately, so instead we are using env's -S
flag, which hopefully has sufficiently wide support by now, and
putting all arguments in the shebang line.

This method will probably require complete explicit --package options,
unlike "stack script", so more testing and tweaking is expected.
Probably we're going to end up with some long shebang lines.

This isn't pretty but seems like a possible way to keep things
manageable.
2021-01-11 21:04:07 -08:00

45 lines
1.7 KiB
Haskell
Executable File

#!/usr/bin/env -S stack ghc --verbosity info --package hledger -- -O0
-- See hledger-check-fancyassertions.hs
-- {-# OPTIONS_GHC -Wno-missing-signatures -Wno-name-shadowing #-}
{-| Construct two balance reports for two different time periods and use one of the as "budget" for
the other, thus comparing them
-}
import Data.Text.Lazy.IO as TL
import System.Environment (getArgs)
import Hledger.Cli
------------------------------------------------------------------------------
cmdmode = hledgerCommandMode
(unlines ["balance-as-budget"
,"Generate two balance reports and use first of them as budget for the second."
," "
,"Pass two sets of hledger-compatible options, separated by --."
,"For example, to use Jan 2019 as budget for Jan 2020, use:"
,"-f 2019.journal -p 2019-01 -- -f 2020.journal -p 2020-01"
," "
,"Display features in the report are driven by the second set of args"
])
[]
[generalflagsgroup1]
[]
([], Just $ argsFlag "[QUERY]")
------------------------------------------------------------------------------
main :: IO ()
main = do
args <- getArgs
let report1args = takeWhile (/= "--") args
let report2args = drop 1 $ dropWhile (/= "--") args
(_,_,report1) <- mbReport report1args
(ropts2,j,report2) <- mbReport report2args
let pastAsBudget = combineBudgetAndActual ropts2 j report1{prDates=prDates report2} report2
TL.putStrLn $ budgetReportAsText ropts2 pastAsBudget
where
mbReport args = do
opts@CliOpts{reportspec_=rspec} <- getHledgerCliOpts' cmdmode args
d <- getCurrentDay
(report,j) <- withJournalDo opts $ \j -> return (multiBalanceReport rspec j, j)
return (rsOpts rspec,j,report)