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 Fish support for environments #15503

Open
wants to merge 25 commits into
base: develop2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5f19714
Add Fish support for environments
RubenRBS Jan 23, 2024
bf5d21e
Erase global scope flag
RubenRBS Jan 23, 2024
b249c6d
Add fish functional test
RubenRBS Feb 1, 2024
e16bf22
Merge branch 'develop2' of https://github.com/conan-io/conan into rr/…
uilianries Apr 1, 2024
d42b388
Add missing import
uilianries Apr 1, 2024
269b4c8
Merge branch 'develop2' of https://github.com/conan-io/conan into rr/…
uilianries Apr 4, 2024
e703b07
test: consume tool requires using fish
uilianries Apr 9, 2024
88a48b4
Load fish environment
uilianries Apr 11, 2024
c60eb5a
Remove windows subsystems for Windows
uilianries Apr 15, 2024
1ab3a8d
Add more tests for fish env
uilianries Apr 15, 2024
70c34ae
Fix path remove element
uilianries Apr 16, 2024
37eab05
generate script wrapper
uilianries Apr 16, 2024
e32b420
validate define with spaces
uilianries Apr 16, 2024
8cdfd29
Add support path with space
uilianries Apr 16, 2024
966c631
update todo
uilianries Apr 16, 2024
dd517c6
only generates wrappers when needed
uilianries Apr 16, 2024
325fe06
simplify element remove
uilianries Apr 16, 2024
2d202ed
Skip fish test in Windows
uilianries Apr 18, 2024
66ca6a2
Add more tests for fish env
uilianries Apr 18, 2024
f57f32f
Sort the expected files
uilianries Apr 22, 2024
4535b74
Add fish into test configuration
uilianries Apr 22, 2024
32125de
Merge branch 'develop2' into rr/fish-support
RubenRBS May 21, 2024
cd94ce3
Merge branch 'develop2' into rr/fish-support
uilianries Jun 4, 2024
35d682e
Add missing comma
uilianries Jun 4, 2024
d55eff5
Adjust imports with new test folder location
uilianries Jun 4, 2024
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
99 changes: 93 additions & 6 deletions conan/tools/env/environment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import os
import textwrap
import random
import string
from jinja2 import Template
from collections import OrderedDict
from contextlib import contextmanager
from pathlib import Path


from conans.client.generators import relativize_paths
from conans.client.subsystems import deduce_subsystem, WINDOWS, subsystem_path
Expand All @@ -19,16 +24,19 @@ def environment_wrap_command(env_filenames, env_folder, cmd, subsystem=None,
if not env_filenames:
return cmd
filenames = [env_filenames] if not isinstance(env_filenames, list) else env_filenames
bats, shs, ps1s = [], [], []
bats, shs, ps1s, fishs = [], [], [], []

accept = accepted_extensions or ("ps1", "bat", "sh")
# TODO: This implemantation is dirty, improve it
accept = accepted_extensions or ("ps1", "bat", "sh", "fish")
# TODO: This implementation is dirty, improve it
for f in filenames:
f = f if os.path.isabs(f) else os.path.join(env_folder, f)
if f.lower().endswith(".sh"):
if os.path.isfile(f) and "sh" in accept:
f = subsystem_path(subsystem, f)
shs.append(f)
elif f.lower().endswith(".fish"):
if os.path.isfile(f) and "fish" in accept:
fishs.append(f)
elif f.lower().endswith(".bat"):
if os.path.isfile(f) and "bat" in accept:
bats.append(f)
Expand All @@ -39,17 +47,20 @@ def environment_wrap_command(env_filenames, env_folder, cmd, subsystem=None,
path_bat = "{}.bat".format(f)
path_sh = "{}.sh".format(f)
path_ps1 = "{}.ps1".format(f)
path_fish = "{}.fish".format(f)
if os.path.isfile(path_bat) and "bat" in accept:
bats.append(path_bat)
if os.path.isfile(path_ps1) and "ps1" in accept:
ps1s.append(path_ps1)
if os.path.isfile(path_sh) and "sh" in accept:
path_sh = subsystem_path(subsystem, path_sh)
shs.append(path_sh)
if os.path.isfile(path_fish) and "fish" in accept:
fishs.append(path_fish)

if bool(bats + ps1s) + bool(shs) > 1:
if bool(bats + ps1s) + bool(shs) > 1 + bool(fishs) > 1:
raise ConanException("Cannot wrap command with different envs,"
"{} - {}".format(bats+ps1s, shs))
"{} - {} - {}".format(bats+ps1s, shs, fishs))

if bats:
launchers = " && ".join('"{}"'.format(b) for b in bats)
Expand All @@ -62,6 +73,9 @@ def environment_wrap_command(env_filenames, env_folder, cmd, subsystem=None,
elif shs:
launchers = " && ".join('. "{}"'.format(f) for f in shs)
return '{} && {}'.format(launchers, cmd)
elif fishs:
launchers = " && ".join('. \\"{}\\"'.format(f) for f in fishs)
return 'fish -c "{}; and {}"'.format(launchers, cmd)
elif ps1s:
# TODO: at the moment it only works with path without spaces
launchers = " ; ".join('"&\'{}\'"'.format(f) for f in ps1s)
Expand Down Expand Up @@ -520,12 +534,76 @@ def save_sh(self, file_location, generate_deactivate=True):
content = f'script_folder="{os.path.abspath(filepath)}"\n' + content
save(file_location, content)

def save_fish(self, file_location):
"""Save a fish script file with the environment variables defined in the Environment object.

It generates a function to deactivate the environment variables configured in the Environment object.

TODO: Honor append and prepend paths from recipe to the fish shell syntax.
buildenv_info.append_path should be fish set -a
buildenv_info.prepend_path should be fish set -p
buildenv_info.define_path should be fish set

:param file_location: The path to the file to save the fish script.
"""
filepath, filename = os.path.split(file_location)
function_name = f"deactivate_{Path(filename).stem}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the function_name is this one? Why not just the group with the random chars? Is there risk of collision of deactivate_xxxx function name?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It follow the common name used by other virtualenv like: conanbuild.sh -> deactivate_conanbuild.sh. So you can actually can call from your terminal directly, like:

source conanbuild.fish
...
deactivate_conanbuild

In Fish, functions are exported as a command, so you can run directly.

About the random to avoid collision: In case we have multiple dependencies add bin folder to PATH, we will need to undo the configuration after deactivating it. Usually it would be store in a deactivate script, but here we are using a variable. Actually, the random is just an idea, but could be the build folder name too, because is kind unique name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, having something deterministic would make more sense, I probably still didn't understand the issue.

group = "".join(random.choices(string.ascii_letters + string.digits, k=8))
script_content = Template(textwrap.dedent("""
function remove_path
set -l variable_name $argv[1]
set -l to_remove $argv[2]
if set -l index (contains -i $to_remove $$variable_name)
set -e {$variable_name}[$index]
end
end
function {{ function_name }}
echo "Restoring environment"
for var in $conan_{{ group }}_del
set -e $var
end
set -e conan_{{ group }}_del
{% for item in vars_define.keys() %}
if set -q conan_{{ group }}_{{ item }}
remove_path {{ item }} $conan_{{ group }}_{{ item }}
set -e conan_{{ group }}_{{ item }}
end
{% endfor %}
end

{% for item, value in vars_define.items() %}
if not set -q {{ item }}
set -ga conan_{{ group }}_del {{ item }}
set -gx {{ item }} "{{ value }}"
else
set -g conan_{{ group }}_{{ item }} "{{ value }}"
set -pgx {{ item }} "{{ value }}"
end
{% endfor %}
exit 0
"""))
values = self._values.keys()
vars_define = {}
for varname, varvalues in self._values.items():
abs_base_path, new_path = relativize_paths(self._conanfile, "$script_folder")
value = varvalues.get_str("", self._subsystem, pathsep=self._pathsep,
root_path=abs_base_path, script_path=new_path)
value = value.replace('"', '\\"')
vars_define[varname] = value

if values:
content = script_content.render(function_name=function_name, group=group,
vars_define=vars_define)
save(file_location, content)

def save_script(self, filename):
"""
Saves a script file (bat, sh, ps1) with a launcher to set the environment.
If the conf "tools.env.virtualenv:powershell" is set to True it will generate powershell
launchers if Windows.

If the conf "tools.env.virtualenv:fish" is set to True it will generate fish launchers.

:param filename: Name of the file to generate. If the extension is provided, it will generate
the launcher script for that extension, otherwise the format will be deduced
checking if we are running inside Windows (checking also the subsystem) or not.
Expand All @@ -534,18 +612,27 @@ def save_script(self, filename):
if ext:
is_bat = ext == ".bat"
is_ps1 = ext == ".ps1"
is_fish = ext == ".fish"
else: # Need to deduce it automatically
is_bat = self._subsystem == WINDOWS
is_ps1 = self._conanfile.conf.get("tools.env.virtualenv:powershell", check_type=bool)
if is_ps1:
is_fish = self._conanfile.conf.get("tools.env.virtualenv:fish", check_type=bool)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no wrappers for .fish files, are they? If there are no wrappers, this will break when set and used to build any dependency.
This should most likely apply only to end-consumer conanfile, not cache dependencies, and only if not used for conan build or anything like that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, please, take a look in the test test_transitive_tool_requires. It generates a tool requirement, then a second recipe consumes it and runs an executable that prints an environment variable. All using Fish.

if is_fish:
filename = filename + ".fish"
is_bat = False
is_ps1 = False
elif is_ps1:
filename = filename + ".ps1"
is_bat = False
is_fish = False
else:
filename = filename + (".bat" if is_bat else ".sh")

path = os.path.join(self._conanfile.generators_folder, filename)
if is_bat:
self.save_bat(path)
elif is_fish:
self.save_fish(path)
elif is_ps1:
self.save_ps1(path)
else:
Expand Down
12 changes: 12 additions & 0 deletions conans/client/generators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def deactivates(filenames):
bats = []
shs = []
ps1s = []
fishs = []
for env_script in env_scripts:
path = os.path.join(conanfile.generators_folder, env_script)
# Only the .bat and .ps1 are made relative to current script
Expand All @@ -174,6 +175,9 @@ def deactivates(filenames):
bats.append("%~dp0/"+path)
elif env_script.endswith(".sh"):
shs.append(subsystem_path(subsystem, path))
elif env_script.endswith(".fish"):
path = os.path.abspath(path)
fishs.append(path)
elif env_script.endswith(".ps1"):
path = os.path.relpath(path, conanfile.generators_folder)
# This $PSScriptRoot uses the current script directory
Expand All @@ -186,6 +190,14 @@ def sh_content(files):
save(os.path.join(conanfile.generators_folder, filename), sh_content(shs))
save(os.path.join(conanfile.generators_folder, "deactivate_{}".format(filename)),
sh_content(deactivates(shs)))
if fishs:
def fish_content(files):
return ". " + " && . ".join('"{}"'.format(s) for s in files)
fishs = [it for it in fishs if os.path.exists(it)]
if fishs:
filename = "conan{}.fish".format(group)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it seems it is missing the deactivation function (not script)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the deactivation is not an script, but a function exported by Fish in the environment. So we can call the deactivate_conanbuild, but there is no file associated. The function is created on-the-fly: https://github.com/conan-io/conan/pull/15503/files#diff-56b36c23589a66b0bd803c545703684c74400cbb7bd8538b8949ed84613e3fc7R550

generated.append(filename)
save(os.path.join(conanfile.generators_folder, filename), fish_content(fishs))
if bats:
def bat_content(files):
return "\r\n".join(["@echo off"] + ['call "{}"'.format(b) for b in files])
Expand Down
1 change: 1 addition & 0 deletions conans/model/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"tools.apple:enable_arc": "(boolean) Enable/Disable ARC Apple Clang flags",
"tools.apple:enable_visibility": "(boolean) Enable/Disable Visibility Apple Clang flags",
"tools.env.virtualenv:powershell": "If it is set to True it will generate powershell launchers if os=Windows",
"tools.env.virtualenv:fish": "If it is set to True it will generate fish launchers",
# Compilers/Flags configurations
"tools.build:compiler_executables": "Defines a Python dict-like with the compilers path to be used. Allowed keys {'c', 'cpp', 'cuda', 'objc', 'objcxx', 'rc', 'fortran', 'asm', 'hip', 'ispc', 'ld', 'ar'}",
"tools.build:cxxflags": "List of extra CXX flags used by different toolchains like CMakeToolchain, AutotoolsToolchain and MesonToolchain",
Expand Down
4 changes: 4 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@
"path": {'Darwin': f'{homebrew_root}/share/android-ndk'}
}
},
'fish': {
"default": "3.6",
"3.6": {"path": {"Darwin": f"{homebrew_root}/bin"}}
},
"qbs": {"disabled": True},
# TODO: Intel oneAPI is not installed in CI yet. Uncomment this line whenever it's done.
# "intel_oneapi": {
Expand Down