summaryrefslogtreecommitdiff
path: root/bin/spack.ps1
blob: e08f1fc53d720488844f3afc720677516ab540a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#  Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
#  Spack Project Developers. See the top-level COPYRIGHT file for details.

#  SPDX-License-Identifier: (Apache-2.0 OR MIT)
# #######################################################################

function Compare-CommonArgs {
    $CMDArgs = $args[0]
    # These aruments take precedence and call for no futher parsing of arguments
    # invoke actual Spack entrypoint with that context and exit after
    "--help", "-h", "--version", "-V" | ForEach-Object {
        $arg_opt = $_
        if(($CMDArgs) -and ([bool]($CMDArgs.Where({$_ -eq $arg_opt})))) {
            return $true
        }
    }
    return $false
}

function Read-SpackArgs {
    $SpackCMD_params = @()
    $SpackSubCommand = $NULL
    $SpackSubCommandArgs = @()
    $args_ = $args[0]
    $args_ | ForEach-Object {
        if (!$SpackSubCommand) {
            if($_.SubString(0,1) -eq "-")
            {
                $SpackCMD_params += $_
            }
            else{
                $SpackSubCommand = $_
            }
        }
        else{
            $SpackSubCommandArgs += $_
        }
    }
    return $SpackCMD_params, $SpackSubCommand, $SpackSubCommandArgs
}

function Set-SpackEnv {
    # This method is responsible
    # for processing the return from $(spack <command>)
    # which are returned as System.Object[]'s containing
    # a list of env commands
    # Invoke-Expression can only handle one command at a time
    # so we iterate over the list to invoke the env modification
    # expressions one at a time
    foreach($envop in $args[0]){
        Invoke-Expression $envop
    }
}


function Invoke-SpackCD {
    if (Compare-CommonArgs $SpackSubCommandArgs) {
        python "$Env:SPACK_ROOT/bin/spack" cd -h
    }
    else {
        $LOC = $(python "$Env:SPACK_ROOT/bin/spack" location $SpackSubCommandArgs)
        if (($NULL -ne $LOC)){
            if ( Test-Path -Path $LOC){
                Set-Location $LOC
            }
            else{
                exit 1
            }
        }
        else {
            exit 1
        }
    }
}

function Invoke-SpackEnv {
    if (Compare-CommonArgs $SpackSubCommandArgs[0]) {
        python "$Env:SPACK_ROOT/bin/spack" env -h
    }
    else {
        $SubCommandSubCommand = $SpackSubCommandArgs[0]
        $SubCommandSubCommandArgs = $SpackSubCommandArgs[1..$SpackSubCommandArgs.Count]
        switch ($SubCommandSubCommand) {
            "activate" {
                if (Compare-CommonArgs $SubCommandSubCommandArgs) {
                    python "$Env:SPACK_ROOT/bin/spack" env activate $SubCommandSubCommandArgs
                }
                elseif ([bool]($SubCommandSubCommandArgs.Where({$_ -eq "--pwsh"}))) {
                    python "$Env:SPACK_ROOT/bin/spack" env activate $SubCommandSubCommandArgs
                }
                elseif (!$SubCommandSubCommandArgs) {
                    python "$Env:SPACK_ROOT/bin/spack" env activate $SubCommandSubCommandArgs
                }
                else {
                    $SpackEnv = $(python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params env activate "--pwsh" $SubCommandSubCommandArgs)
                    Set-SpackEnv $SpackEnv
                }
            }
            "deactivate" {
                if ([bool]($SubCommandSubCommandArgs.Where({$_ -eq "--pwsh"}))) {
                    python"$Env:SPACK_ROOT/bin/spack" env deactivate $SubCommandSubCommandArgs
                }
                elseif($SubCommandSubCommandArgs) {
                    python "$Env:SPACK_ROOT/bin/spack" env deactivate -h
                }
                else {
                    $SpackEnv = $(python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params env deactivate "--pwsh")
                    Set-SpackEnv $SpackEnv
                }
            }
            default {python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params $SpackSubCommand $SpackSubCommandArgs}
        }
    }
}

function Invoke-SpackLoad {
    if (Compare-CommonArgs $SpackSubCommandArgs) {
        python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params $SpackSubCommand $SpackSubCommandArgs
    }
    elseif ([bool]($SpackSubCommandArgs.Where({($_ -eq "--pwsh") -or ($_ -eq "--list")}))) {
        python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params $SpackSubCommand $SpackSubCommandArgs
    }
    else {
        $SpackEnv = $(python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params $SpackSubCommand "--pwsh" $SpackSubCommandArgs)
        Set-SpackEnv $SpackEnv
    }
}


$SpackCMD_params, $SpackSubCommand, $SpackSubCommandArgs = Read-SpackArgs $args

if (Compare-CommonArgs $SpackCMD_params) {
    python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params $SpackSubCommand $SpackSubCommandArgs
    exit $LASTEXITCODE
}

# Process Spack commands with special conditions
# all other commands are piped directly to Spack
switch($SpackSubCommand)
{
    "cd"     {Invoke-SpackCD}
    "env"    {Invoke-SpackEnv}
    "load"   {Invoke-SpackLoad}
    "unload" {Invoke-SpackLoad}
    default  {python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params $SpackSubCommand $SpackSubCommandArgs}
}