可能重复:
Haskell:在源代码中指定版本的正确做法?
有没有办法将阴谋集团元数据导出到我的代码中?例如,使用自动工具,我定义了程序版本configure.ac
并将其作为我的代码的定义。目前,在 cabal 中,我必须在两个地方维护程序版本 - 在 .cabal 和--version
. 任何优雅的解决方案?
可能重复:
Haskell:在源代码中指定版本的正确做法?
有没有办法将阴谋集团元数据导出到我的代码中?例如,使用自动工具,我定义了程序版本configure.ac
并将其作为我的代码的定义。目前,在 cabal 中,我必须在两个地方维护程序版本 - 在 .cabal 和--version
. 任何优雅的解决方案?
让我用一个 shell 会话来回答这个问题:
~ $ cd /tmp/
/tmp $ mkdir test
/tmp $ cd test/
/tmp/test $ cabal init
Package name? [default: test]
Package version? [default: 0.1.0.0]
Please choose a license:
* 1) (none)
2) GPL-2
3) GPL-3
4) LGPL-2.1
5) LGPL-3
6) BSD3
7) MIT
8) PublicDomain
9) AllRightsReserved
10) Other (specify)
Your choice? [default: (none)]
Author name? [default: Joachim Breitner]
Maintainer email? [default: mail@joachim-breitner.de]
Project homepage URL?
Project synopsis?
Project category:
* 1) (none)
2) Codec
3) Concurrency
4) Control
5) Data
6) Database
7) Development
8) Distribution
9) Game
10) Graphics
11) Language
12) Math
13) Network
14) Sound
15) System
16) Testing
17) Text
18) Web
19) Other (specify)
Your choice? [default: (none)]
What does the package build:
1) Library
2) Executable
Your choice? 2
Include documentation on what each field means (y/n)? [default: n]
Guessing dependencies...
Generating LICENSE...
Warning: unknown license type, you must put a copy in LICENSE yourself.
Generating Setup.hs...
Generating test.cabal...
Warning: no synopsis given. You should edit the .cabal file and add one.
You may want to edit the .cabal file and add a Description field.
/tmp/test $ vim *.cabal
/tmp/test $ cat *.cabal
name: test
version: 0.1.0.0
license-file: LICENSE
author: Joachim Breitner
maintainer: mail@joachim-breitner.de
build-type: Simple
cabal-version: >=1.8
executable test
main-is: Main.hs
build-depends: base ==4.5.*
/tmp/test $ echo 'main = return ()' > Main.hs
/tmp/test $ cabal configure && cabal build
Resolving dependencies...
Configuring test-0.1.0.0...
Warning: The 'license-file' field refers to the file 'LICENSE' which does not
exist.
Building test-0.1.0.0...
Preprocessing executable 'test' for test-0.1.0.0...
[1 of 1] Compiling Main ( Main.hs, dist/build/test/test-tmp/Main.o )
Linking dist/build/test/test ...
/tmp/test $ find dist/
dist/
dist/package.conf.inplace
dist/setup-config
dist/build
dist/build/autogen
dist/build/autogen/Paths_test.hs
dist/build/autogen/cabal_macros.h
dist/build/test
dist/build/test/test-tmp
dist/build/test/test-tmp/Main.o
dist/build/test/test-tmp/Main.hi
dist/build/test/test
/tmp/test $ grep -i version dist/build/autogen/Paths_test.hs
version,
import Data.Version (Version(..))
version :: Version
version = Version {versionBranch = [0,1,0,0], versionTags = []}
/tmp/test $ echo 'import Paths_test' > Main.hs
/tmp/test $ echo 'main = print version' >> Main.hs
/tmp/test $ cabal build
Building test-0.1.0.0...
Preprocessing executable 'test' for test-0.1.0.0...
[1 of 2] Compiling Paths_test ( dist/build/autogen/Paths_test.hs, dist/build/test/test-tmp/Paths_test.o )
[2 of 2] Compiling Main ( Main.hs, dist/build/test/test-tmp/Main.o )
Linking dist/build/test/test ...
/tmp/test $ ./dist/build/test/test
Version {versionBranch = [0,1,0,0], versionTags = []}
(TL;DR:cabal 生成一个称为Paths_<pkgname>
定义常量的模块version :: Version
。)