Skip to content

Conversation

@perseoGI
Copy link
Contributor

@perseoGI perseoGI commented Oct 28, 2025

Changelog: Feature: Help and verbosity options in virtual environments
Docs: TODO

This PR refactors the virtual env scripts generations, adding verbosity options together with a help option to the newer deactivate (and activate) functions and scripts.

PowerShell refactor

The original scripts generation code (activate and deactivate) has only been moved to a jinja2 template, but the functionality and the output code remain exactly the same:

develop2 generated code and this newer refactor:

conanbuild.ps1

& "$PSScriptRoot/conanbuildenv-release-armv8.ps1"

conanbuildenv-release-armv8.ps1

Push-Location $PSScriptRoot
"echo `"Restoring environment`"" | Out-File -FilePath "deactivate_conanbuildenv-release-armv8.ps1"
$vars = (Get-ChildItem env:*).name
$updated_vars = @("CCACHE_BASEDIR", "CCACHE_NOHASHDIR", "PATH")

foreach ($var in $updated_vars)
{
    if ($var -in $vars)
    {
        $var_value = (Get-ChildItem env:$var).value
        Add-Content "deactivate_conanbuildenv-release-armv8.ps1" "`n`$env:$var = `"$var_value`""
    }
    else
    {
        Add-Content "deactivate_conanbuildenv-release-armv8.ps1" "`nif (Test-Path env:$var) { Remove-Item env:$var }"
    }
}
Pop-Location

$env:CCACHE_BASEDIR="C:\Users\perseo"
$env:CCACHE_NOHASHDIR="1"
$env:PATH="C:\Users\perseo\.conan2\p\b\ccacha3ec662f1af2b\p\bin;$env:PATH"

deactivate_conanbuild.ps1

& "$PSScriptRoot\deactivate_conanbuildenv-release-armv8.ps1"

deactivate_conanbuildenv-release-armv8.ps1

echo "Restoring environment"

if (Test-Path env:CCACHE_BASEDIR) { Remove-Item env:CCACHE_BASEDIR }

if (Test-Path env:CCACHE_NOHASHDIR) { Remove-Item env:CCACHE_NOHASHDIR }

$env:PATH = "..."

tools.env:deactivation_mode=function

When the new config is enabled, newer files will be created. These scripts remains stable in functionality compared with the current develop2 branch, but with the addition of built-in powershell script description, which enables calling these functions like:

$ .\conanbuild.ps1 -?
$ .\conanbuild.ps1 -v
$ Get-Help .\conanbuild.ps1 -examples 
# And also the deactivation in-memory function accepts the same parameters
$ deactivate_conanbuild -?
$ deactivate_conanbuild -v
$ Get-Help deactivate_conanbuild -examples 

conanbuild.ps1

<#
.SYNOPSIS
    Activates the Conan build environment for the current shell session.
.DESCRIPTION
    Sets environment variables (like PATH) for the Conan build configuration.
    Defines a 'deactivate_conanbuild' function to safely restore the original environment.
.PARAMETER Verbose
    Print information about the modified variables during activation and restoration.
.EXAMPLE
    .\conanbuild.ps1 -Verbose
.EXAMPLE
    .\conanbuild.ps1 ; deactivate_conanbuild -Verbose
#>
# Requires PowerShell 3.0 or later

# --- Top-Level Script Argument Handling (Enables -Verbose implicitly) ---
[CmdletBinding()]
param()

# 1. Execute the environment setup scripts
# Note: We use @PSBoundParameters to forward all built-in and custom parameters
#       (including -Verbose) to the inner script.
& "$PSScriptRoot/conanbuildenv-release-armv8.ps1" @PSBoundParameters

Write-Verbose 'Environment activated. Run "deactivate_conanbuild" to restore.'


function global:deactivate_conanbuild {
    <#
    .SYNOPSIS
        Restores the environment modified by conanbuild.ps1
    .DESCRIPTION
        Restores the PATH and other environment variables set by the Conan build activation script.
    .PARAMETER Verbose
        Prints information about the restored variables.
    .EXAMPLE
        deactivate_conanbuild -Verbose
    #>
    # CmdletBinding enables -Verbose for this function implicitly.
    [CmdletBinding()]
    param()

    # Call deactivation functions
    & "deactivate_conanbuildenv_release_armv8" @PSBoundParameters
    
    # Cleanup (Remove the function itself)
    Remove-Item -Path function:deactivate_conanbuild -ErrorAction SilentlyContinue
}

conanbuildenv-release-armv8.ps1

function global:deactivate_conanbuildenv_release_armv8 {
    [CmdletBinding()]
    param()
    Write-Host "Restoring environment"
    foreach ($v in @("CCACHE_BASEDIR", "CCACHE_NOHASHDIR", "PATH")) {
        $oldVarName = "_CONAN_OLD_CONANBUILDENV_RELEASE_ARMV8_$v"
        $oldValue = Get-Item -Path "Env:$oldVarName" -ErrorAction SilentlyContinue
        if (Test-Path env:$oldValue) {
            Write-Verbose "Unsetting $v"
            Remove-Item -Path "Env:$v" -ErrorAction SilentlyContinue
        } else {
            Write-Verbose "Restoring $v to $oldValue.Value"
            Set-Item -Path "Env:$v" -Value $oldValue.Value
        }
        Remove-Item -Path "Env:$oldVarName" -ErrorAction SilentlyContinue
    }
    Remove-Item -Path function:deactivate_conanbuildenv_release_armv8 -ErrorAction SilentlyContinue
}
if ($env:CCACHE_BASEDIR) { $env:_CONAN_OLD_CONANBUILDENV_RELEASE_ARMV8_CCACHE_BASEDIR = $env:CCACHE_BASEDIR }
Write-Verbose "Exporting CCACHE_BASEDIR=C:\Users\perseo"
$env:CCACHE_BASEDIR="C:\Users\perseo"

if ($env:CCACHE_NOHASHDIR) { $env:_CONAN_OLD_CONANBUILDENV_RELEASE_ARMV8_CCACHE_NOHASHDIR = $env:CCACHE_NOHASHDIR }
Write-Verbose "Exporting CCACHE_NOHASHDIR=1"
$env:CCACHE_NOHASHDIR="1"

if ($env:PATH) { $env:_CONAN_OLD_CONANBUILDENV_RELEASE_ARMV8_PATH = $env:PATH }
Write-Verbose "Exporting PATH=C:\Users\perseo\.conan2\p\b\ccacha3ec662f1af2b\p\bin;$env:PATH"
$env:PATH="C:\Users\perseo\.conan2\p\b\ccacha3ec662f1af2b\p\bin;$env:PATH"

sh / POSIX refactor

Same as powershell, the sh generation scripts have been refactor into templates, but the original generated code remain the same:

develop2 generated code and this newer refactor:

conanbuild.sh

. "/Users/perseo/tmp/run-old/conanbuildenv-release-armv8.sh"

conanbuildenv-release-armv8.sh

script_folder="/Users/perseo/tmp/run-old"
echo "echo Restoring environment" > "$script_folder/deactivate_conanbuildenv-release-armv8.sh"
for v in CCACHE_BASEDIR CCACHE_NOHASHDIR PATH
do
    is_defined="true"
    value=$(printenv $v) || is_defined="" || true
    if [ -n "$value" ] || [ -n "$is_defined" ]
    then
        echo export "$v='$value'" >> "$script_folder/deactivate_conanbuildenv-release-armv8.sh"
    else
        echo unset $v >> "$script_folder/deactivate_conanbuildenv-release-armv8.sh"
    fi
done


export CCACHE_BASEDIR="/Users/perseo"
export CCACHE_NOHASHDIR="1"
export PATH="/Users/perseo/.conan2/p/b/ccach574746dcc7d69/p/bin:$PATH"

deactivate_conanbuild.sh

. "/Users/perseo/tmp/run-old/deactivate_conanbuildenv-release-armv8.sh"

deactivate_conanbuildenv-release-armv8.sh

echo Restoring environment
unset CCACHE_BASEDIR
unset CCACHE_NOHASHDIR
export PATH='...'

tools.env:deactivation_mode=function

Same as powershell, when the new config is enabled, it will generate the same scripts as in develop2 but with -h/--help and -v/--verbose options, which can be handy to check what the scripts are actually doing with your environment.

$ . conanbuild.sh --help
$ source conanbuild.sh -v
$ deactivate_conanbuild -h
$ deactivate_conanbuild -v

conanbuild.sh

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
    printf "%s [-v|--verbose]\n" "$0"
    printf "  Activate Conan build environment\n"
    printf "  -v, --verbose   Print information about the modified variables\n"
    return 0
fi

conan_verbose=false; [ "$1" = "-v" ] || [ "$1" = "--verbose" ] && conan_verbose=true

. "/Users/perseo/tmp/run-new/conanbuildenv-release-armv8.sh"


deactivate_conanbuild() {
    if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
        printf "deactivate_conanbuild [-v|--verbose]\n"
        printf "  Restores the environment modified by conanbuild.sh\n"
        printf "  -v, --verbose   Print information about the restored variables\n"
        return 0
    fi
    conan_verbose=false; [ "$1" = "-v" ] || [ "$1" = "--verbose" ] && conan_verbose=true
    # Call deactivation functions
    "deactivate_conanbuildenv_release_armv8"
    # Remove the function itself
    unset -f deactivate_conanbuild
}

conanbuildenv-release-armv8.sh

# sh-like function to restore environment
deactivate_conanbuildenv_release_armv8 () {
    echo "Restoring environment"
    conan_verbose=${conan_verbose:-false}
    for v in CCACHE_BASEDIR CCACHE_NOHASHDIR PATH; do
        old_var="_CONAN_OLD_CONANBUILDENV_RELEASE_ARMV8_${v}"
        # Use eval for indirect expansion (POSIX safe)
        eval "is_set=\${${old_var}+x}"
        if [ -n "${is_set}" ]; then
            eval "old_value=\${${old_var}}"
            "${conan_verbose}" && echo "Restoring ${v} to ${old_value}"
            eval "export ${v}=\${old_value}"
        else
            "${conan_verbose}" && echo "Unsetting ${v}"
            unset "${v}"
        fi
        unset "${old_var}"
    done
    unset -f deactivate_conanbuildenv_release_armv8
}
conan_verbose=${conan_verbose:-false}

if [ -n "${CCACHE_BASEDIR+x}" ]; then export _CONAN_OLD_CONANBUILDENV_RELEASE_ARMV8_CCACHE_BASEDIR="${CCACHE_BASEDIR}";fi
${conan_verbose} && echo "Exporting CCACHE_BASEDIR=/Users/perseo"
export CCACHE_BASEDIR="/Users/perseo"

if [ -n "${CCACHE_NOHASHDIR+x}" ]; then export _CONAN_OLD_CONANBUILDENV_RELEASE_ARMV8_CCACHE_NOHASHDIR="${CCACHE_NOHASHDIR}";fi
${conan_verbose} && echo "Exporting CCACHE_NOHASHDIR=1"
export CCACHE_NOHASHDIR="1"

if [ -n "${PATH+x}" ]; then export _CONAN_OLD_CONANBUILDENV_RELEASE_ARMV8_PATH="${PATH}";fi
${conan_verbose} && echo "Exporting PATH=/Users/perseo/.conan2/p/b/ccach574746dcc7d69/p/bin:$PATH"
export PATH="/Users/perseo/.conan2/p/b/ccach574746dcc7d69/p/bin:$PATH"

@perseoGI perseoGI marked this pull request as ready for review October 31, 2025 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant