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

Add barebones template to conan new #12802

Merged
merged 13 commits into from
Jan 2, 2023
12 changes: 10 additions & 2 deletions conan/api/subapi/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, conan_api):

@api_method
def get_builtin_template(self, template_name):
from conan.internal.api.new.barebones import barebones_file
from conan.internal.api.new.basic import basic_file
from conan.internal.api.new.alias_new import alias_file
from conan.internal.api.new.cmake_exe import cmake_exe_files
from conan.internal.api.new.cmake_lib import cmake_lib_files
Expand All @@ -29,7 +29,7 @@ def get_builtin_template(self, template_name):
from conan.internal.api.new.bazel_exe import bazel_exe_files
from conan.internal.api.new.autotools_lib import autotools_lib_files
from conan.internal.api.new.autoools_exe import autotools_exe_files
new_templates = {"barebones": barebones_file,
new_templates = {"basic": basic_file,
"cmake_lib": cmake_lib_files,
"cmake_exe": cmake_exe_files,
"meson_lib": meson_lib_files,
Expand Down Expand Up @@ -84,12 +84,20 @@ def _read_files(self, template_folder):

return template_files, non_template_files

@staticmethod
def render_requires(requires):
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(requires, str):
requires = [requires]

return "\n".join([f'self.requires("{require}")' for require in requires])

AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
@staticmethod
def render(template_files, definitions):
result = {}
name = definitions.get("name", "Pkg")
definitions["conan_version"] = __version__
definitions["package_name"] = name.replace("-", "_").replace("+", "_").replace(".", "_")
definitions["render_requires"] = lambda x: NewAPI.render_requires(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)
Expand Down
9 changes: 8 additions & 1 deletion conan/cli/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ def new(conan_api, parser, *args):
except ValueError:
raise ConanException(f"Template definitions must be 'key=value', received {u}")
k = k.replace("-", "") # Remove possible "--name=value"
definitions[k] = v
# For variables that only show up once, no need for list to keep compatible behaviour
if k in definitions:
if isinstance(definitions[k], list):
definitions[k].append(v)
else:
definitions[k] = [definitions[k], v]
else:
definitions[k] = v

files = conan_api.new.get_template(args.template) # First priority: user folder
if not files: # then, try the templates in the Conan home
Expand Down
11 changes: 0 additions & 11 deletions conan/internal/api/new/barebones.py

This file was deleted.

29 changes: 29 additions & 0 deletions conan/internal/api/new/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
_conanfile = '''\
from conan import ConanFile

class BasicConanfile(ConanFile):
name = "{% if name is defined %}{{ name }}{% else %}pkg{% endif %}"
version = "{% if version is defined %}{{ version }}{% else %}1.0{% endif %}"
description = "{% if description is defined %}{{ description }}{% else %}A bare-bones recipe{% endif %}"

# Text about the requirements() method
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
def requirements(self):
{% if requires is defined -%}
{{ render_requires(requires) | indent | indent }}
{% else -%}
# Uncommenting this line will add the zlib/1.2.13 dependency to your project
# self.requires("zlib/1.2.13")
pass
{%- endif %}

# Text about the build() method
def build(self):
pass

# Text about the package() method
def package(self):
pass
'''


basic_file = {"conanfile.py": _conanfile}
14 changes: 14 additions & 0 deletions conans/test/integration/command/new_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ def test_new_missing_definitions(self):
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()
try:
tc.run("new basic")
except Exception as e:
assert False, f"conan new basic should work without any extra arguments: {e}"

tc.run("new basic -d name=mygame -d requires=math/1.0 -d requires=ai/1.0")
tc.assert_in_file_contents("conanfile.py", [
'self.requires("math/1.0")',
'self.requires("ai/1.0")',
'name = "myname"'
])


class TestNewCommandUserTemplate:

Expand Down
10 changes: 10 additions & 0 deletions conans/test/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import zipfile
from collections import OrderedDict
from contextlib import contextmanager
from typing import List
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
from urllib.parse import urlsplit, urlunsplit

import bottle
Expand Down Expand Up @@ -718,6 +719,15 @@ def assert_listed_binary(self, requires, build=False, test=False):
else:
raise AssertionError(f"Cant find {r}-{kind} in {reqs}")

def assert_in_file_contents(self, filename, contents: List[str], path=None):
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
path = path or self.current_folder
file = os.path.join(path, filename)
assert os.path.exists(file)
with open(file) as f:
file_contents = "\n".join(f.readlines())
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
for bit in contents:
assert bit in file_contents

def created_package_id(self, ref):
package_id = re.search(r"{}: Package '(\S+)' created".format(str(ref)),
str(self.out)).group(1)
Expand Down