Repository: conan-io/conan
Pull request #13377 (2023-03-08 11:05:54)

Feature/jinja templates conan version
Changelog: Feature: Allow ``conan_version`` in profile.jinja and global.conf jinja templating
Docs: https://github.com/conan-io/conan/pull/13377

Close https://github.com/conan-io/conan/issues/10818


Patch:
diff --git a/conans/client/cache/cache.py b/conans/client/cache/cache.py
index b827edb16..156580103 100644
--- a/conans/client/cache/cache.py
+++ b/conans/client/cache/cache.py
@@ -5,6 +5,7 @@ from collections import OrderedDict
 
 from jinja2 import Environment, select_autoescape, FileSystemLoader, ChoiceLoader, Template
 
+from conan import conan_version
 from conans.assets.templates import dict_loader
 from conans.client.cache.editable import EditablePackages
 from conans.client.cache.remote_registry import RemoteRegistry
@@ -173,7 +174,8 @@ class ClientCache(object):
                 distro = None
                 if platform.system() in ["Linux", "FreeBSD"]:
                     import distro
-                content = Template(text).render({"platform": platform, "os": os, "distro": distro})
+                content = Template(text).render({"platform": platform, "os": os, "distro": distro,
+                                                 "conan_version": conan_version})
                 self._new_config.loads(content)
         return self._new_config
 
diff --git a/conans/client/profile_loader.py b/conans/client/profile_loader.py
index b484f68af..669df4f1e 100644
--- a/conans/client/profile_loader.py
+++ b/conans/client/profile_loader.py
@@ -4,6 +4,7 @@ from collections import OrderedDict, defaultdict
 
 from jinja2 import Environment, FileSystemLoader
 
+from conan import conan_version
 from conan.tools.env.environment import ProfileEnvironment
 from conans.errors import ConanException, ConanV2Exception
 from conans.model.conf import ConfDefinition
@@ -124,7 +125,8 @@ def read_profile(profile_name, cwd, default_folder):
         base_path = os.path.dirname(profile_path)
         context = {"platform": platform,
                    "os": os,
-                   "profile_dir": base_path}
+                   "profile_dir": base_path,
+                   "conan_version": conan_version}
         rtemplate = Environment(loader=FileSystemLoader(base_path)).from_string(text)
         text = rtemplate.render(context)
 


Test patch:
diff --git a/conans/test/integration/configuration/conf/test_conf.py b/conans/test/integration/configuration/conf/test_conf.py
index 9310a156b..539eb6150 100644
--- a/conans/test/integration/configuration/conf/test_conf.py
+++ b/conans/test/integration/configuration/conf/test_conf.py
@@ -3,9 +3,9 @@ import platform
 import textwrap
 
 import pytest
-import six
 from mock import patch
 
+from conan import conan_version
 from conans.errors import ConanException
 from conans.util.files import save
 from conans.test.utils.tools import TestClient
@@ -180,10 +180,12 @@ def test_composition_conan_conf_different_data_types_by_cli_arg(client):
 def test_jinja_global_conf(client):
     save(client.cache.new_config_path, "user.mycompany:parallel = {{os.cpu_count()/2}}\n"
                                        "user.mycompany:other = {{platform.system()}}\n"
-                                       "user.mycompany:dist = {{distro.id() if distro else '42'}}\n")
+                                       "user.mycompany:dist = {{distro.id() if distro else '42'}}\n"
+                                       "user.conan:version = {{conan_version}}-{{conan_version>0.1}}")
     client.run("install .")
     assert "user.mycompany:parallel={}".format(os.cpu_count()/2) in client.out
     assert "user.mycompany:other={}".format(platform.system()) in client.out
+    assert f"user.conan:version={conan_version}-True" in client.out
     if platform.system() == "Linux":
         import distro
         assert "user.mycompany:dist={}".format(distro.id()) in client.out
diff --git a/conans/test/integration/configuration/test_profile_jinja.py b/conans/test/integration/configuration/test_profile_jinja.py
index 30aa9ee84..da1a72209 100644
--- a/conans/test/integration/configuration/test_profile_jinja.py
+++ b/conans/test/integration/configuration/test_profile_jinja.py
@@ -1,6 +1,7 @@
 import platform
 import textwrap
 
+from conan import conan_version
 from conans.client.tools import environment_append
 from conans.test.assets.genconanfile import GenConanfile
 from conans.test.utils.tools import TestClient
@@ -71,3 +72,19 @@ def test_profile_template_profile_dir():
                  "anysubfolder/toolchain.cmake": "MyToolchainCMake!!!"})
     client.run("install . -pr=anysubfolder/profile1.jinja")
     assert "conanfile.py: CONTENT: MyToolchainCMake!!!" in client.out
+
+
+def test_profile_version():
+    client = TestClient()
+    tpl1 = textwrap.dedent("""
+        [options]
+        *:myoption={{conan_version}}
+        *:myoption2={{conan_version<13 and conan_version>1.0}}
+        """)
+
+    client.save({"conanfile.py": GenConanfile(),
+                 "profile1.jinja": tpl1})
+    client.run("install . -pr=profile1.jinja")
+    assert f"*:myoption={conan_version}" in client.out
+    assert "*:myoption2=True" in client.out
+