Skip to content

Commit

Permalink
add find_msvs custom command to the nu_msvs module (#881)
Browse files Browse the repository at this point in the history
I changed `export-env {}` at the beginning to a custom command to avoid
having `export-env` run when loading the module
  because:
1. I couldn't use `nu_msvs activate` because it depends on env vars
being set that were hidden with `nu_msvs deactivate`
2. It seems that `export-env` executed at `use`, leaves `$env.FILE_PWD`
available in the environment which I think may be
     a bug. So, putting it in a custom command avoids that.
  • Loading branch information
fdncred committed Jun 18, 2024
1 parent 92f20ff commit ddbebf1
Showing 1 changed file with 68 additions and 59 deletions.
127 changes: 68 additions & 59 deletions modules/virtual_environments/nu_msvs/nu_msvs.nu
Original file line number Diff line number Diff line change
@@ -1,70 +1,79 @@
export-env {
$env.MSVS_BASE_PATH = $env.Path
$env.PATH_VAR = (if "Path" in $env { "Path" } else { "PATH" })

let info = (
if not (which vswhere | is-empty) {
(vswhere -format json | from json)
} else {
('{"installationPath": [""]}' | from json)
}
)

$env.MSVS_ROOT = $info.installationPath.0

$env.MSVS_MSVC_ROOT = (
if not ($'($env.MSVS_ROOT)\VC\Tools\MSVC\' | path exists) {
""
} else if (ls ($'($env.MSVS_ROOT)\VC\Tools\MSVC\*' | into glob) | is-empty) {
""
} else {
((ls ($'($env.MSVS_ROOT)\VC\Tools\MSVC\*' | into glob)).name.0)
})

$env.MSVS_MSDK_ROOT = (registry query --hklm 'SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v10.0' InstallationFolder | get value)

$env.MSVS_MSDK_VER = (registry query --hklm 'SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v10.0' ProductVersion | get value) + '.0'

$env.MSVS_INCLUDE_PATH = ([
$'($env.MSVS_ROOT)\Include\($env.MSVS_MSDK_VER)\cppwinrt\winrt',
$'($env.MSVS_MSVC_ROOT)\Include',
$'($env.MSVS_MSDK_ROOT)Include\($env.MSVS_MSDK_VER)\cppwinrt\winrt',
$'($env.MSVS_MSDK_ROOT)Include\($env.MSVS_MSDK_VER)\shared',
$'($env.MSVS_MSDK_ROOT)Include\($env.MSVS_MSDK_VER)\ucrt',
$'($env.MSVS_MSDK_ROOT)Include\($env.MSVS_MSDK_VER)\um',
$'($env.MSVS_MSDK_ROOT)Include\($env.MSVS_MSDK_VER)\winrt'
] | str join (char esep))

let esep_path_converter = {
from_string: { |s| $s | split row (char esep) }
to_string: { |v| $v | path expand | str join (char esep) }
}

$env.ENV_CONVERSIONS = {
Path: $esep_path_converter
DYLD_FALLBACK_LIBRARY_PATH: $esep_path_converter
PSModulePath: $esep_path_converter
MSVS_BASE_PATH: $esep_path_converter
MSVS_INCLUDE_PATH: $esep_path_converter
INCLUDE: $esep_path_converter
LIB: $esep_path_converter
def --env find_msvs [] {
export-env {
$env.MSVS_BASE_PATH = $env.Path
$env.PATH_VAR = (if "Path" in $env { "Path" } else { "PATH" })

let info = (
if not (which vswhere | is-empty) {
(vswhere -format json | from json)
} else {
('{"installationPath": [""]}' | from json)
}
)

$env.MSVS_ROOT = $info.installationPath.0

$env.MSVS_MSVC_ROOT = (
if not ($'($env.MSVS_ROOT)\VC\Tools\MSVC\' | path exists) {
""
} else if (ls ($'($env.MSVS_ROOT)\VC\Tools\MSVC\*' | into glob) | is-empty) {
""
} else {
((ls ($'($env.MSVS_ROOT)\VC\Tools\MSVC\*' | into glob)).name.0)
})

$env.MSVS_MSDK_ROOT = (registry query --hklm 'SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v10.0' InstallationFolder | get value)

$env.MSVS_MSDK_VER = (registry query --hklm 'SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v10.0' ProductVersion | get value) + '.0'

$env.MSVS_INCLUDE_PATH = ([
$'($env.MSVS_ROOT)\Include\($env.MSVS_MSDK_VER)\cppwinrt\winrt',
$'($env.MSVS_MSVC_ROOT)\Include',
$'($env.MSVS_MSDK_ROOT)Include\($env.MSVS_MSDK_VER)\cppwinrt\winrt',
$'($env.MSVS_MSDK_ROOT)Include\($env.MSVS_MSDK_VER)\shared',
$'($env.MSVS_MSDK_ROOT)Include\($env.MSVS_MSDK_VER)\ucrt',
$'($env.MSVS_MSDK_ROOT)Include\($env.MSVS_MSDK_VER)\um',
$'($env.MSVS_MSDK_ROOT)Include\($env.MSVS_MSDK_VER)\winrt'
] | str join (char esep))

let esep_path_converter = {
from_string: { |s| $s | split row (char esep) }
to_string: { |v| $v | path expand | str join (char esep) }
}

$env.ENV_CONVERSIONS = {
Path: $esep_path_converter
DYLD_FALLBACK_LIBRARY_PATH: $esep_path_converter
PSModulePath: $esep_path_converter
MSVS_BASE_PATH: $esep_path_converter
MSVS_INCLUDE_PATH: $esep_path_converter
INCLUDE: $esep_path_converter
LIB: $esep_path_converter
}

# Debugging Info
# print $"MSVS_BASE_PATH: ($env.MSVS_BASE_PATH)"
# print $"PATH_VAR: ($env.PATH_VAR)"
# print $"MSVS_ROOT: ($env.MSVS_ROOT)"
# print $"MSVS_MSVC_ROOT: ($env.MSVS_MSVC_ROOT)"
# print $"MSVS_MSDK_ROOT: ($env.MSVS_MSDK_ROOT)"
# print $"MSVS_MSDK_VER: ($env.MSVS_MSDK_VER)"
# print $"MSVS_INCLUDE_PATH: ($env.MSVS_INCLUDE_PATH)"
}

# Debugging Info
# print $"MSVS_BASE_PATH: ($env.MSVS_BASE_PATH)"
# print $"PATH_VAR: ($env.PATH_VAR)"
# print $"MSVS_ROOT: ($env.MSVS_ROOT)"
# print $"MSVS_MSVC_ROOT: ($env.MSVS_MSVC_ROOT)"
# print $"MSVS_MSDK_ROOT: ($env.MSVS_MSDK_ROOT)"
# print $"MSVS_MSDK_VER: ($env.MSVS_MSDK_VER)"
# print $"MSVS_INCLUDE_PATH: ($env.MSVS_INCLUDE_PATH)"
}

export def --env activate [
--host (-h): string = x64, # Host architecture, must be x64 or x86 (case insensitive)
--target (-t): string = x64, # Target architecture, must be x64 or x86 (case insensitive)
--sdk (-s): string = latest # Version of Windows SDK, must be "latest" or a valid version string
] {
# I changed export-env {} to a custom command to avoid having export-env run when loading the module
# because:
# 1. I couldn't use activate because it depends on env vars being set that were hidden with deactivate
# 2. It seems that export-env executed at `use`, leaves `$env.FILE_PWD` available in the environment
# which I think may be a bug. So, putting it in a custom command avoids that.
find_msvs

if (($env.MSVS_ROOT | is-empty) or ($env.MSVS_MSVC_ROOT | is-empty)) {
print "Either Microsoft Visual Studio or MSVC is valid."
return
Expand Down

0 comments on commit ddbebf1

Please sign in to comment.