Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new conan new defaults #17186

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions conan/api/subapi/new.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fnmatch
import os

from jinja2 import Template, StrictUndefined
from jinja2 import Template, StrictUndefined, UndefinedError, Environment, meta

from conan.errors import ConanException
from conans.util.files import load
Expand Down Expand Up @@ -118,11 +118,35 @@ def as_name(ref):
definitions["package_name"] = as_package_name(name)
definitions["as_name"] = as_name
definitions["names"] = lambda x: ", ".join(r.split("/", 1)[0] for r in x)
for k, v in template_files.items():
k = Template(k, keep_trailing_newline=True, undefined=StrictUndefined).render(
**definitions)
v = Template(v, keep_trailing_newline=True, undefined=StrictUndefined).render(
**definitions)
if v:
result[k] = v
if "name" not in definitions:
definitions["name"] = "mypkg"
if "version" not in definitions:
definitions["version"] = "0.1"
version = definitions.get("version")
if isinstance(version, list):
raise ConanException(f"version argument can't be multiple: {version}")

try:
for k, v in template_files.items():
k = Template(k, keep_trailing_newline=True, undefined=StrictUndefined).render(
**definitions)
v = Template(v, keep_trailing_newline=True, undefined=StrictUndefined).render(
**definitions)
if v:
result[k] = v
except UndefinedError:
template_vars = []
for templ_str in template_files.values():
env = Environment()
ast = env.parse(templ_str)
template_vars.extend(meta.find_undeclared_variables(ast))

injected_vars = {"conan_version", "package_name", "as_name"}
optional_vars = {"requires", "tool_requires", "output_root_dir"}
template_vars = list(set(template_vars) - injected_vars - optional_vars)
template_vars.sort()

raise ConanException("Missing definitions for the template. "
"Required definitions are: {}"
.format(", ".join("'{}'".format(var) for var in template_vars)))
return result
22 changes: 1 addition & 21 deletions conan/cli/commands/new.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
import shutil

from jinja2 import Environment, meta, exceptions

from conan.api.output import ConanOutput
from conan.cli.command import conan_command
from conan.errors import ConanException
Expand Down Expand Up @@ -57,25 +55,7 @@ def new(conan_api, parser, *args):
if not template_files and not non_template_files:
raise ConanException("Template doesn't exist or not a folder: {}".format(args.template))

try:
template_files = conan_api.new.render(template_files, definitions)
except exceptions.UndefinedError:
def get_template_vars():
template_vars = []
for _, templ_str in template_files.items():
env = Environment()
ast = env.parse(templ_str)
template_vars.extend(meta.find_undeclared_variables(ast))

injected_vars = {"conan_version", "package_name", "as_name"}
optional_vars = {"requires", "tool_requires", "output_root_dir"}
template_vars = list(set(template_vars) - injected_vars - optional_vars)
template_vars.sort()
return template_vars

raise ConanException("Missing definitions for the template. "
"Required definitions are: {}"
.format(", ".join("'{}'".format(var) for var in get_template_vars())))
template_files = conan_api.new.render(template_files, definitions)

# Saving the resulting files
output = ConanOutput()
Expand Down
27 changes: 16 additions & 11 deletions test/integration/command/new_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ def test_new_cmake_exe(self):
client.run("export . --user=myuser --channel=testing")
assert "pkg/1.3@myuser/testing" in client.out

def test_new_missing_definitions(self):
client = TestClient()
client.run("new cmake_lib", assert_error=True)
assert "Missing definitions for the template. Required definitions are: 'name', 'version'" in client.out
client.run("new cmake_lib -d name=myname", assert_error=True)
assert "Missing definitions for the template. Required definitions are: 'name', 'version'" in client.out
client.run("new cmake_lib -d version=myversion", assert_error=True)
assert "Missing definitions for the template. Required definitions are: 'name', 'version'" in client.out

def test_new_basic_template(self):
tc = TestClient()
tc.run("new basic")
Expand All @@ -56,6 +47,20 @@ def test_new_basic_template(self):
assert 'self.requires("ai/1.0")' in conanfile
assert 'name = "mygame"' in conanfile

def test_new_defaults(self):
c = TestClient()
for t in ("cmake_lib", "cmake_exe", "meson_lib", "meson_exe", "msbuild_lib", "msbuild_exe",
"bazel_lib", "bazel_exe", "autotools_lib", "autotools_exe"):
c.run(f"new {t} -f")
conanfile = c.load("conanfile.py")
assert 'name = "mypkg"' in conanfile
assert 'version = "0.1"' in conanfile

c.run(f"new {t} -d name=mylib -f")
conanfile = c.load("conanfile.py")
assert 'name = "mylib"' in conanfile
assert 'version = "0.1"' in conanfile


class TestNewCommandUserTemplate:

Expand Down Expand Up @@ -151,5 +156,5 @@ def test_duplicated(self):
assert "ERROR: name argument can't be multiple: ['hello', '0.1']" in client.out

# It will create a list and assign to it, but it will not fail ugly
client.run("new cmake_lib -d name=pkg -d version=0.1 -d version=0.2")
assert "['0.1', '0.2']" in client.load("conanfile.py")
client.run("new cmake_lib -d name=pkg -d version=0.1 -d version=0.2", assert_error=True)
assert "ERROR: version argument can't be multiple: ['0.1', '0.2']" in client.out