19

Say I have a console program that has an option to display its version. Currently whenever I update version in .cabal file I need to go to source code and update constant — string representation of current version number as well. This feels against DRY principle and now I'm wondering, is it possible to get version of my project as defined in .cabal file from source code? Maybe Cabal defines some CPP macro or something else?

Mark Karpov
  • 7,499
  • 2
  • 27
  • 62

1 Answers1

18

Indeed Cabal allows to access information from .cabal file in your program. According to Cabal documentation, you can import special module that exists during building of your package, like this:

import Paths_packagename (version)
import Data.Version (showVersion)

myVersion :: String
myVersion = showVersion version

The module Paths_packagename provides version of type Version.

Mark Karpov
  • 7,499
  • 2
  • 27
  • 62
  • 3
    Is this still the current best approach? – orome Nov 02 '18 at 19:09
  • What else is the path_packagename exporting? I cannot find any documentation as it is an automatically generated package. I would appreciate a link! – user855443 Feb 09 '19 at 16:46
  • 3
    One small notice: change `` to your real package name. E.g. if your package name is `'my-awesome-package'` you should `import Paths_my_awesome_package` – Geradlus_RU Sep 11 '19 at 10:56