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

using LF for .sh generators #6670

Merged
merged 8 commits into from Mar 27, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion conans/client/generators/__init__.py
Expand Up @@ -107,7 +107,8 @@ def write_generators(conanfile, path, output):
output.warn("Generator %s is multifile. Property 'filename' not used"
% (generator_name,))
for k, v in content.items():
v = normalize(v)
if generator.normalize: # To not break existing behavior, to be removed 2.0
v = normalize(v)
output.info("Generator %s created %s" % (generator_name, k))
save(join(path, k), v, only_if_modified=True)
else:
Expand Down
52 changes: 33 additions & 19 deletions conans/client/generators/virtualenv.py
Expand Up @@ -6,7 +6,7 @@

from conans.client.tools.oss import OSInfo
from conans.model import Generator

from conans.util.files import normalize

sh_activate_tpl = Template(textwrap.dedent("""\
#!/usr/bin/env sh
Expand Down Expand Up @@ -41,25 +41,25 @@

cmd_activate_tpl = Template(textwrap.dedent("""\
@echo off

{%- for it in modified_vars %}
SET "CONAN_OLD_{{it}}=%{{it}}%"
{%- endfor %}

FOR /F "usebackq tokens=1,* delims==" %%i IN ("{{ environment_file }}") DO (
CALL SET "%%i=%%j"
)

SET "CONAN_OLD_PROMPT=%PROMPT%"
SET "PROMPT=({{venv_name}}) %PROMPT%"
"""))

cmd_deactivate_tpl = Template(textwrap.dedent("""\
@echo off

SET "PROMPT=%CONAN_OLD_PROMPT%"
SET "CONAN_OLD_PROMPT="

{% for it in modified_vars %}
SET "{{it}}=%CONAN_OLD_{{it}}%"
SET "CONAN_OLD_{{it}}="
Expand All @@ -73,13 +73,13 @@
{%- for it in modified_vars %}
$env:CONAN_OLD_{{it}}=$env:{{it}}
{%- endfor %}

foreach ($line in Get-Content "{{ environment_file }}") {
$var,$value = $line -split '=',2
$value_expanded = $ExecutionContext.InvokeCommand.ExpandString($value)
Set-Item env:\\$var -Value "$value_expanded"
}

function global:_old_conan_prompt {""}
$function:_old_conan_prompt = $function:prompt
function global:prompt { write-host "({{venv_name}}) " -nonewline; & $function:_old_conan_prompt }
Expand All @@ -88,7 +88,7 @@
ps1_deactivate_tpl = Template(textwrap.dedent("""\
$function:prompt = $function:_old_conan_prompt
remove-item function:_old_conan_prompt

{% for it in modified_vars %}
$env:{{it}}=$env:CONAN_OLD_{{it}}
Remove-Item env:CONAN_OLD_{{it}}
Expand All @@ -104,11 +104,15 @@ class VirtualEnvGenerator(Generator):
append_with_spaces = ["CPPFLAGS", "CFLAGS", "CXXFLAGS", "LIBS", "LDFLAGS", "CL", "_LINK_"]
suffix = ""
venv_name = "conanenv"
CMD_FLAVOR = "cmd"
PS1_FLAVOR = "ps1"
SH_FLAVOR = "sh"

def __init__(self, conanfile):
super(VirtualEnvGenerator, self).__init__(conanfile)
self.conanfile = conanfile
self.env = conanfile.env
self.normalize = False

@property
def filename(self):
Expand All @@ -122,9 +126,9 @@ def _variable_placeholder(flavor, name, append_with_spaces):
:return: placeholder for the variable name formatted for a certain execution environment.
(e.g., cmd, ps1, sh).
"""
if flavor == "cmd":
if flavor == VirtualEnvGenerator.CMD_FLAVOR:
return "%{}%".format(name)
if flavor == "ps1":
if flavor == VirtualEnvGenerator.PS1_FLAVOR:
return "$env:%s" % name
# flavor == sh
return "${%s+ $%s}" % (name, name) if append_with_spaces else "${%s+:$%s}" % (name, name)
Expand All @@ -137,10 +141,9 @@ def _format_values(cls, flavor, variables):
:param variables: variables to be formatted
:return:
"""
if flavor in ["cmd", "ps1"]:
path_sep, quote_elements = ":", True
if flavor in [VirtualEnvGenerator.CMD_FLAVOR, VirtualEnvGenerator.PS1_FLAVOR]:
path_sep, quote_elements = ";", False
elif flavor == "sh":
path_sep, quote_elements = ":", True

for name, value in variables:
# activate values
Expand All @@ -156,7 +159,7 @@ def _format_values(cls, flavor, variables):
# PATH="one path":"two paths"
# Unquoted variables joined with pathset may look like: PATH=one path;two paths
value = ["\"%s\"" % v for v in value] if quote_elements else value
if flavor == "sh":
if flavor == VirtualEnvGenerator.SH_FLAVOR:
value = path_sep.join(value) + placeholder
else:
value = path_sep.join(value + [placeholder])
Expand All @@ -182,9 +185,20 @@ def _files(self, flavor, activate_tpl, deactivate_tpl, environment_filename):
deactivate_content = deactivate_tpl.render(modified_vars=modified_vars, new_vars=new_vars)

environment_lines = ["{}={}".format(name, value) for name, value, _ in ret]
# This blank line is important, otherwise the script doens't process last line
environment_lines.append('')

return activate_content, deactivate_content, os.linesep.join(environment_lines)
if flavor == VirtualEnvGenerator.SH_FLAVOR:
# replace CRLF->LF guarantee it is always LF, irrespective of current .py file
activate_content = activate_content.replace("\r\n", "\n")
deactivate_content = deactivate_content.replace("\r\n", "\n")
environment = "\n".join(environment_lines)
else:
activate_content = normalize(activate_content)
deactivate_content = normalize(deactivate_content)
environment = os.linesep.join(environment_lines)

return activate_content, deactivate_content, environment

@property
def content(self):
Expand All @@ -202,8 +216,8 @@ def _call_files(flavor, activate_tpl, deactivate_tpl, file_ext=None):

os_info = OSInfo()
if os_info.is_windows and not os_info.is_posix:
_call_files('cmd', cmd_activate_tpl, cmd_deactivate_tpl, 'bat')
_call_files('ps1', ps1_activate_tpl, ps1_deactivate_tpl)
_call_files("sh", sh_activate_tpl, sh_deactivate_tpl)
_call_files(VirtualEnvGenerator.CMD_FLAVOR, cmd_activate_tpl, cmd_deactivate_tpl, 'bat')
_call_files(VirtualEnvGenerator.PS1_FLAVOR, ps1_activate_tpl, ps1_deactivate_tpl)
_call_files(VirtualEnvGenerator.SH_FLAVOR, sh_activate_tpl, sh_deactivate_tpl)

return result
1 change: 1 addition & 0 deletions conans/model/conan_generator.py
Expand Up @@ -8,6 +8,7 @@ class Generator(object):

def __init__(self, conanfile):
self.conanfile = conanfile
self.normalize = True
self._deps_build_info = conanfile.deps_cpp_info
self._deps_env_info = conanfile.deps_env_info
self._env_info = conanfile.env_info
Expand Down