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 5 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
53 changes: 51 additions & 2 deletions conan/tools/env/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections import OrderedDict
from contextlib import contextmanager

from conans.client.generators import relativize_paths
from conans.client.generators import relativize_paths, relativize_generated_file
from conans.client.subsystems import deduce_subsystem, WINDOWS, subsystem_path
from conan.errors import ConanException
from conans.model.recipe_ref import ref_matches
Expand Down Expand Up @@ -520,12 +520,52 @@ 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, generate_deactivate=True):
filepath, filename = os.path.split(file_location)
deactivate_file = os.path.join(filepath, "deactivate_{}".format(filename))
values = self._values.keys()
if len(values) == 0:
# Empty environment, nothing to restore (Easier to handle in Fish)
deactivate = ("""echo "echo Nothing to restore" > {deactivate_file}"""
.format(deactivate_file=deactivate_file))
else:
deactivate = textwrap.dedent("""\
Copy link
Member

Choose a reason for hiding this comment

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

For new one, I'd try to do directly an in-memory or in-tmp-folder deactivate script rather than a local generated one.

echo "echo Restoring environment" > "{deactivate_file}"
for v in {vars}
set is_defined "true"
set value (printenv $v); or set is_defined ""
if test -n "$value" -o -n "$is_defined"
echo set -gx "$v" "$value" >> "{deactivate_file}"
else
echo set -ge "$v" >> "{deactivate_file}"
end
end
""".format(deactivate_file=deactivate_file, vars=" ".join(self._values.keys())))
capture = textwrap.dedent("""\
{deactivate}
""").format(deactivate=deactivate if generate_deactivate else "")
result = [capture]
for varname, varvalues in self._values.items():
value = varvalues.get_str("${name}", self._subsystem, pathsep=self._pathsep)
value = value.replace('"', '\\"')
if value:
result.append('set -gx {} "{}"'.format(varname, value))
else:
result.append('set -ge {}'.format(varname))
Copy link
Member

Choose a reason for hiding this comment

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

I guess it could be simplified by using set -p (prepend), instead of expanding all values.

https://fishshell.com/docs/current/cmds/set.html


content = "\n".join(result)
content = relativize_generated_file(content, self._conanfile, "$script_folder")
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it possible to not use this and relativize paths as the other environment do?

content = f'set script_folder "{os.path.abspath(filepath)}"\n' + content
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 +574,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
1 change: 1 addition & 0 deletions conans/model/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,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'}",
"tools.build:cxxflags": "List of extra CXX flags used by different toolchains like CMakeToolchain, AutotoolsToolchain and MesonToolchain",
Expand Down
5 changes: 5 additions & 0 deletions conans/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@
# "exe": "dpcpp",
# "2021.3": {"path": {"Linux": "/opt/intel/oneapi/compiler/2021.3.0/linux/bin"}}
# }
# TODO: Fish is not yet installed in CI. Uncomment this line whenever it's done
# 'fish': {
# "default": "3.6",
# "3.6": {"path": {"Darwin": f"{homebrew_root}/bin"}}
# }
}


Expand Down
43 changes: 43 additions & 0 deletions conans/test/functional/toolchains/env/test_virtualenv_fish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os

import pytest

from conans.test.assets.genconanfile import GenConanfile
from conans.test.utils.test_files import temp_folder
from conans.test.utils.tools import TestClient
from conans.util.files import save


@pytest.mark.tool("fish")
def test_virtualenv_fish():
cache_folder = os.path.join(temp_folder(), "[sub] folder")
client = TestClient(cache_folder)
conanfile = str(GenConanfile("pkg", "0.1"))
conanfile += """

def package_info(self):
self.buildenv_info.define_path("MYPATH1", "/path/to/ar")
"""
client.save({"conanfile.py": conanfile})
client.run("create .")
save(client.cache.new_config_path, "tools.env.virtualenv:fish=True\n")
client.save({"conanfile.py": GenConanfile("app", "0.1").with_requires("pkg/0.1")})
client.run("install . -s:a os=Linux")

assert not os.path.exists(os.path.join(client.current_folder, "conanbuildenv.sh"))
assert not os.path.exists(os.path.join(client.current_folder, "conanbuildenv.bat"))
assert not os.path.exists(os.path.join(client.current_folder, "conanrunenv.sh"))
assert not os.path.exists(os.path.join(client.current_folder, "conanrunenv.bat"))

assert os.path.exists(os.path.join(client.current_folder, "conanbuildenv.fish"))
assert os.path.exists(os.path.join(client.current_folder, "conanrunenv.fish"))

with open(os.path.join(client.current_folder, "conanbuildenv.fish"), "r") as f:
buildenv = f.read()
assert 'set -gx MYPATH1 "/path/to/ar"' in buildenv

client.run_command("fish -c 'source conanbuildenv.fish && set'")
assert 'MYPATH1 /path/to/ar' in client.out

client.run_command("fish -c 'source conanbuildenv.fish && set && source deactivate_conanbuildenv.fish && set'")
assert str(client.out).count('MYPATH1 /path/to/ar') == 1
uilianries marked this conversation as resolved.
Show resolved Hide resolved