版本

本节介绍如何创建给定软件包的不同版本,首先从手动更改 conanfile.py recipe 中的 version 属性开始,然后介绍 set_version() 方法作为自动化软件包版本定义的机制。

注意

本节使用非常简单、空的 recipe,不构建任何代码,因此没有 build()package() 等,以最简单的 recipe 说明版本控制,并允许示例轻松运行并且非常快速和简单。在实际应用中,recipe 将是完整的 recipe,如本教程的前几节所示,构建实际的库和软件包。

让我们从一个非常简单的 recipe 开始

conanfile.py
from conan import ConanFile

class pkgRecipe(ConanFile):
    name = "pkg"
    version = "1.0"

    # The recipe would export files and package them, but not really
    # necessary for the purpose of this part of the tutorial
    # exports_sources = "include/*"
    # def package(self):
    #    ...

我们可以使用以下命令创建 pkg/1.0

$ conan create .
...
pkg/1.0 .
...

$ conan list "pkg/*"
Local Cache
  pkg
    pkg/1.0

如果现在我们对该库的源文件进行了一些更改,这将是一个新版本,我们可以将 conanfile.py 的版本更改为 version = "1.1" 并创建新的 pkg/1.1 版本

# Make sure you modified conanfile.py to version=1.1
$ conan create .
...
pkg/1.1 .
...

$ conan list "pkg/*"
Local Cache
  pkg
    pkg/1.0
    pkg/1.1

正如我们所看到的,现在我们在缓存中看到了 pkg/1.0pkg/1.1。 Conan 缓存可以为相同的 pkg 包存储任意数量的不同版本和配置。

自动化版本

除了手动更改 conanfile.py 中的版本之外,还可以使用两种不同的方法自动执行此操作。

首先,可以直接在命令行中提供 version。 在上面的示例中,我们可以从 recipe 中删除 version 属性并执行以下操作

# Make sure you removed the version attribute in conanfile.py
$ conan create . --version=1.2
...
pkg/1.2 .
...

$ conan list "pkg/*"
Local Cache
  pkg
    pkg/1.0
    pkg/1.1
    pkg/1.2

另一种可能性是使用 set_version() 方法动态定义版本,例如,如果版本已经存在于源代码或文本文件中,或者应该从 git 版本中推断出来。

假设我们在 repo 中有一个 version.txt 文件,其中仅包含版本字符串 1.3。 那么,这可以这样完成

conanfile.py
from conan import ConanFile
from conan.tools.files import  load


class pkgRecipe(ConanFile):
    name = "pkg"

    def set_version(self):
        self.version = load(self, "version.txt")
# No need to specify the version in CLI arg or in recipe attribute
$ conan create .
...
pkg/1.3 .
...

$ conan list "pkg/*"
Local Cache
  pkg
    pkg/1.0
    pkg/1.1
    pkg/1.2
    pkg/1.3

也可以组合命令行版本定义,如果命令行参数未提供,则回退到从文件中读取,使用以下语法

conanfile.py
def set_version(self):
    # if self.version is already defined from CLI --version arg, it will
    # not load version.txt
    self.version = self.version or load(self, "version.txt")
# This will create the "1.4" version even if the version.txt file contains "1.3"
$ conan create . --version=1.4
...
pkg/1.4 .
...

$ conan list "pkg/*"
Local Cache
  pkg
    pkg/1.0
    pkg/1.1
    pkg/1.2
    pkg/1.3
    pkg/1.4

同样,也可以从 Git 标签获取版本

conanfile.py
from conan import ConanFile
from conan.tools.scm import Git

class pkgRecipe(ConanFile):
    name = "pkg"

    def set_version(self):
        git = Git(self)
        tag = git.run("describe --tags")
        self.version = tag
# assuming this is a git repo, and it was tagged to 1.5
$ git init .
$ git add .
$ git commit -m "initial commit"
$ git tag 1.5
$ conan create .
    ...
    pkg/1.5 .
    ...

    $ conan list "pkg/*"
    Local Cache
      pkg
        pkg/1.0
        pkg/1.1
        pkg/1.2
        pkg/1.3
        pkg/1.4
        pkg/1.5

注意

最佳实践

  • 我们可以尝试使用诸如分支名称或 commit 之类的东西作为版本号。 但是,这可能有一些缺点,例如,当需要此包时,它需要在每个需要此包的其他包 recipe 中显式地 requires = "pkg/commit",并且可能难以一致地更新使用者,并且难以知道是否正在使用较新或较旧的依赖项。

需要新版本

创建新的软件包版本时,如果需要此版本的其他软件包 recipe 包含显式的 requires,则固定确切的版本,例如

app/conanfile.py
from conan import ConanFile

class AppRecipe(ConanFile):
    name = "app"
    version = "1.0"
    requires = "pkg/1.0"

然后,安装或创建 app recipe 将保持需要和使用 pkg/1.0 版本,而不是更新的版本。 要开始使用新的 pkg 版本,必须显式更新 requires,例如

app/conanfile.py
from conan import ConanFile

class AppRecipe(ConanFile):
    name = "app"
    version = "1.0"
    requires = "pkg/1.5"

这个过程虽然实现了非常好的可重复性和可追溯性,但如果我们在管理一个大型依赖关系图,并且希望更快地使用最新的依赖版本并减少手动干预,那么这个过程可能会有点乏味。为了自动化这个过程,可以使用下一节中解释的 *version-ranges*。