Skip to content

Commit

Permalink
Add deactivate_conanvcvars file with message (#15557)
Browse files Browse the repository at this point in the history
* Add deactivate_conanvcvars file with message

* Add deactivate_conanvcvars test

* make function private

* change ps check

* add powershell test
  • Loading branch information
juansblanco committed Feb 1, 2024
1 parent 5cf93f3 commit 748b417
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
23 changes: 19 additions & 4 deletions conan/tools/microsoft/visual.py
Expand Up @@ -6,9 +6,9 @@
from conan.errors import ConanException, ConanInvalidConfiguration
from conan.tools.scm import Version
from conan.tools.intel.intel_cc import IntelCC
from conans.util.files import save

CONAN_VCVARS_BAT = "conanvcvars.bat"
CONAN_VCVARS_PS1 = "conanvcvars.ps1"
CONAN_VCVARS = "conanvcvars"


def check_min_vs(conanfile, version, raise_invalid=True):
Expand Down Expand Up @@ -151,7 +151,9 @@ def generate(self, scope="build"):
{vcvars}
""")
from conan.tools.env.environment import create_env_script
create_env_script(conanfile, content, CONAN_VCVARS_BAT, scope)
conan_vcvars_bat = f"{CONAN_VCVARS}.bat"
create_env_script(conanfile, content, conan_vcvars_bat, scope)
_create_deactivate_vcvars_file(conanfile, conan_vcvars_bat)

is_ps1 = conanfile.conf.get("tools.env.virtualenv:powershell", check_type=bool, default=False)
if is_ps1:
Expand All @@ -167,10 +169,23 @@ def generate(self, scope="build"):
Pop-Location
write-host conanvcvars.ps1: Activated environment}}
""")
create_env_script(conanfile, content_ps1, CONAN_VCVARS_PS1, scope)
conan_vcvars_ps1 = f"{CONAN_VCVARS}.ps1"
create_env_script(conanfile, content_ps1, conan_vcvars_ps1, scope)
_create_deactivate_vcvars_file(conanfile, conan_vcvars_ps1)



def _create_deactivate_vcvars_file(conanfile, filename):
deactivate_filename = f"deactivate_{filename}"
message = f"[{deactivate_filename}]: vcvars env cannot be deactivated"
is_ps1 = filename.endswith(".ps1")
if is_ps1:
content = f"Write-Host {message}"
else:
content = f"echo {message}"
path = os.path.join(conanfile.generators_folder, deactivate_filename)
save(path, content)

def vs_ide_version(conanfile):
"""
Gets the VS IDE version as string. It'll use the ``compiler.version`` (if exists) and/or the
Expand Down
32 changes: 32 additions & 0 deletions conans/test/integration/toolchains/microsoft/vcvars_test.py
Expand Up @@ -126,3 +126,35 @@ class TestConan(ConanFile):

vcvars = client.load("conanvcvars.bat")
assert 'vcvarsall.bat" amd64 8.1 -vcvars_ver=14.3' in vcvars

@pytest.mark.skipif(platform.system() != "Windows", reason="Requires Windows")
def test_deactivate_vcvars_message():
client = TestClient()
conanfile = textwrap.dedent("""
from conan import ConanFile
class TestConan(ConanFile):
generators = "VCVars"
settings = "os", "compiler", "arch", "build_type"
""")
client.save({"conanfile.py": conanfile})
client.run('install . -s compiler.version=193')
client.run_command(r'conanbuild.bat')
assert "[vcvarsall.bat] Environment initialized" in client.out
client.run_command(r'deactivate_conanvcvars.bat')
assert "vcvars env cannot be deactivated" in client.out

@pytest.mark.skipif(platform.system() != "Windows", reason="Requires Windows Powershell")
def test_deactivate_vcvars_with_powershell():
client = TestClient()
conanfile = textwrap.dedent("""
from conan import ConanFile
class TestConan(ConanFile):
generators = "VCVars"
settings = "os", "compiler", "arch", "build_type"
""")
client.save({"conanfile.py": conanfile})
client.run('install . -c tools.env.virtualenv:powershell=True')
client.run_command(r'powershell.exe ".\conanbuild.ps1"')
assert "conanvcvars.ps1: Activated environment" in client.out
client.run_command(r'powershell.exe ".\deactivate_conanvcvars.ps1"')
assert "vcvars env cannot be deactivated" in client.out

0 comments on commit 748b417

Please sign in to comment.