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 shorthand syntax in cli to specify host and build in 1 argument #14727

Merged
merged 18 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
26 changes: 20 additions & 6 deletions conan/api/subapi/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,26 @@ def get_default_build(self):
return loader.get_default_build()

def get_profiles_from_args(self, args):
build = [self.get_default_build()] if not args.profile_build else args.profile_build
host = [self.get_default_host()] if not args.profile_host else args.profile_host
profile_build = self.get_profile(profiles=build, settings=args.settings_build,
options=args.options_build, conf=args.conf_build)
profile_host = self.get_profile(profiles=host, settings=args.settings_host,
options=args.options_host, conf=args.conf_host)
all_profiles = [] if not args.profile_all else args.profile_all
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
all_settings = [] if not args.settings_all else args.settings_all
all_options = [] if not args.options_all else args.options_all
all_conf = [] if not args.conf_all else args.conf_all

build_profiles = all_profiles + ([self.get_default_build()] if not args.profile_build else args.profile_build)
build_settings = all_settings + ([] if not args.settings_build else args.settings_build)
build_options = all_options + ([] if not args.options_build else args.options_build)
build_conf = all_conf + ([] if not args.conf_build else args.conf_build)

host_profiles = all_profiles + ([self.get_default_host()] if not args.profile_host else args.profile_host)
host_settings = all_settings + ([] if not args.settings_host else args.settings_host)
host_options = all_options + ([] if not args.options_host else args.options_host)
host_conf = all_conf + ([] if not args.conf_host else args.conf_host)

profile_build = self.get_profile(profiles=build_profiles, settings=build_settings,
options=build_options, conf=build_conf)

profile_host = self.get_profile(profiles=host_profiles, settings=host_settings,
options=host_options, conf=host_conf)
return profile_host, profile_build

def get_profile(self, profiles, settings=None, options=None, conf=None, cwd=None):
Expand Down
60 changes: 40 additions & 20 deletions conan/cli/args.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import argparse

from conan.cli.command import OnceArgument
from conan.errors import ConanException

Expand Down Expand Up @@ -56,45 +58,63 @@ def add_common_install_arguments(parser):


def add_profiles_args(parser):
def profile_args(machine, short_suffix="", long_suffix=""):
def profile_args(machine, short_suffix="", long_suffix="", suppress_help=False):
parser.add_argument("-pr{}".format(short_suffix),
"--profile{}".format(long_suffix),
default=None, action="append",
metavar="PROFILE",
dest='profile_{}'.format(machine),
help='Apply the specified profile to the {} machine'.format(machine))
help=argparse.SUPPRESS if suppress_help else
'Apply the specified profile. '
'By default, or if specifying -pr:h (--profile:host), it applies to the host context. '
'Use -pr:b (--profile:build) to specify the build context, '
'or -pr:a (--profile:all) to specify both contexts at once')

def settings_args(machine, short_suffix="", long_suffix=""):
def settings_args(machine, short_suffix="", long_suffix="", suppress_help=False):
parser.add_argument("-s{}".format(short_suffix),
"--settings{}".format(long_suffix),
action="append",
metavar="SETTINGS",
dest='settings_{}'.format(machine),
help='Settings to build the package, overwriting the defaults'
' ({} machine). e.g.: -s{} compiler=gcc'.format(machine,
short_suffix))

def options_args(machine, short_suffix="", long_suffix=""):
help=argparse.SUPPRESS if suppress_help else
'Settings for the package, overwriting the defaults. '
'By default, or if specifying -s:h (--settings:host), it applies to the host context. '
'Use -s:b (--settings:build) to specify the build context, '
'or -s:a (--settings:all) to specify both contexts at once. '
'Example: -s compiler=gcc')

def options_args(machine, short_suffix="", long_suffix="", suppress_help=False):
parser.add_argument("-o{}".format(short_suffix),
"--options{}".format(long_suffix),
action="append",
metavar="OPTIONS",
dest="options_{}".format(machine),
help='Define options values ({} machine), e.g.:'
' -o{} Pkg:with_qt=true'.format(machine, short_suffix))

def conf_args(machine, short_suffix="", long_suffix=""):
help=argparse.SUPPRESS if suppress_help else
'Define options values. '
'By default, or if specifying -o:h (--options:host), it applies to the host context. '
'Use -o:b (--options:build) to specify the build context, '
'or -o:a (--options:all) to specify both contexts at once. '
'Example: -o pkg:with_qt=true')

def conf_args(machine, short_suffix="", long_suffix="", suppress_help=False):
parser.add_argument("-c{}".format(short_suffix),
"--conf{}".format(long_suffix),
action="append",
metavar="CONF",
dest='conf_{}'.format(machine),
help='Configuration to build the package, overwriting the defaults'
' ({} machine). e.g.: -c{} '
'tools.cmake.cmaketoolchain:generator=Xcode'.format(machine,
short_suffix))
help=argparse.SUPPRESS if suppress_help else
'Configuration to build the package, overwriting the defaults'
'By default, or if specifying -c:h (--conf:host), it applies to the host context. '
'Use -c:b (--conf:build) to specify the build context, '
'or -c:a (--conf:all) to specify both contexts at once. '
'Example: -c tools.cmake.cmaketoolchain:generator=Xcode')

for item_fn in [options_args, profile_args, settings_args, conf_args]:
item_fn("host", "",
"") # By default it is the HOST, the one we are building binaries for
item_fn("build", ":b", ":build")
item_fn("host", ":h", ":host")
# By default, it is the HOST, the one we are building binaries for
item_fn("host", "", "")
item_fn("build", ":b", ":build", suppress_help=True)
item_fn("host", ":h", ":host", suppress_help=True)
item_fn("all", ":a", ":all", suppress_help=True)


def add_reference_args(parser):
Expand Down
13 changes: 13 additions & 0 deletions conans/test/integration/command/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ def test_ignore_paths_when_listing_profiles():
assert ignore_path not in c.out


def test_shorthand_syntax():
tc = TestClient()
tc.save({"profile": "[conf]\nuser.profile=True"})
tc.run("profile show -o:a=both_options=True -pr:a=profile -s:a=os=WindowsCE -s:a=os.platform=conan -c:a=user.conf.cli=True")

# All of them show up twice, once per context
assert tc.out.count("both_options=True") == 2
assert tc.out.count("os=WindowsCE") == 2
assert tc.out.count("os.platform=conan") == 2
assert tc.out.count("user.conf.cli=True") == 2
assert tc.out.count("user.profile=True") == 2


def test_profile_show_json():
c = TestClient()
c.save({"myprofilewin": "[settings]\nos=Windows",
Expand Down
36 changes: 36 additions & 0 deletions conans/test/integration/graph/version_ranges/test_version_range.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
import re

from conans.util.files import load
from conans.test.utils.tools import TestClient, GenConanfile


def test_version_range():
tc = TestClient()

with tc.chdir("utils"):
tc.run("new cmake_lib -d name=utils -d version=0.1.0")
tc.run("create .")

with tc.chdir("liba"):
tc.run("new cmake_lib -d name=liba -d version=0.1.0 -d requires=utils/[>=0.1.0]")
tc.run("create .")

with tc.chdir("libc"):
tc.run("new cmake_lib -d name=libc -d version=0.1.0 -d requires=liba/[>=0.1.0] -d requires=utils/[>=0.1.0]")
new_conanfile = re.sub("self.requires",
"self.test_requires",
load(os.path.join(tc.current_folder, "conanfile.py")))
new_conanfile = re.sub("def requirements", "def build_requirements", new_conanfile)
tc.save({"conanfile.py": new_conanfile})

# As per the report, this should fail when compiling libc.cpp, but it doesnt, it can find utils.h
tc.run("create . -tf=")


def test_tool_requires_conflict():
tc = TestClient()
tc.save({"gcc/conanfile.py": GenConanfile("gcc"),
"tool_a/conanfile.py": GenConanfile("tool_a", "1.0").with_tool_requires("gcc/1.0"),
"tool_b/conanfile.py": GenConanfile()})

6 changes: 5 additions & 1 deletion conans/test/unittests/cli/common_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ def argparse_args():
return MagicMock(
profile_build=None,
profile_host=None,
profile_all=None,
settings_build=None,
settings_host=None,
settings_all=None,
options_build=None,
options_host=None,
options_all=None,
conf_build=None,
conf_host=None
conf_host=None,
conf_all=None,
)


Expand Down