| 
									
										
										
										
											2024-01-02 00:21:30 -08:00
										 |  |  | #  Copyright 2013-2024 Lawrence Livermore National Security, LLC and other | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  | #  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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-01 14:36:27 -04:00
										 |  |  | 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 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  | function Invoke-SpackCD { | 
					
						
							|  |  |  |     if (Compare-CommonArgs $SpackSubCommandArgs) { | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |         python "$Env:SPACK_ROOT/bin/spack" cd -h | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |         $LOC = $(python "$Env:SPACK_ROOT/bin/spack" location $SpackSubCommandArgs) | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  |         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]) { | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |         python "$Env:SPACK_ROOT/bin/spack" env -h | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         $SubCommandSubCommand = $SpackSubCommandArgs[0] | 
					
						
							|  |  |  |         $SubCommandSubCommandArgs = $SpackSubCommandArgs[1..$SpackSubCommandArgs.Count] | 
					
						
							|  |  |  |         switch ($SubCommandSubCommand) { | 
					
						
							|  |  |  |             "activate" { | 
					
						
							|  |  |  |                 if (Compare-CommonArgs $SubCommandSubCommandArgs) { | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |                     python "$Env:SPACK_ROOT/bin/spack" env activate $SubCommandSubCommandArgs | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 elseif ([bool]($SubCommandSubCommandArgs.Where({$_ -eq "--pwsh"}))) { | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |                     python "$Env:SPACK_ROOT/bin/spack" env activate $SubCommandSubCommandArgs | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 elseif (!$SubCommandSubCommandArgs) { | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |                     python "$Env:SPACK_ROOT/bin/spack" env activate $SubCommandSubCommandArgs | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |                     $SpackEnv = $(python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params env activate "--pwsh" $SubCommandSubCommandArgs) | 
					
						
							| 
									
										
										
										
											2023-09-01 14:36:27 -04:00
										 |  |  |                     Set-SpackEnv $SpackEnv | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             "deactivate" { | 
					
						
							|  |  |  |                 if ([bool]($SubCommandSubCommandArgs.Where({$_ -eq "--pwsh"}))) { | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |                     python"$Env:SPACK_ROOT/bin/spack" env deactivate $SubCommandSubCommandArgs | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 elseif($SubCommandSubCommandArgs) { | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |                     python "$Env:SPACK_ROOT/bin/spack" env deactivate -h | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |                     $SpackEnv = $(python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params env deactivate "--pwsh") | 
					
						
							| 
									
										
										
										
											2023-09-01 14:36:27 -04:00
										 |  |  |                     Set-SpackEnv $SpackEnv | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |             default {python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params $SpackSubCommand $SpackSubCommandArgs} | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function Invoke-SpackLoad { | 
					
						
							|  |  |  |     if (Compare-CommonArgs $SpackSubCommandArgs) { | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |         python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params $SpackSubCommand $SpackSubCommandArgs | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     elseif ([bool]($SpackSubCommandArgs.Where({($_ -eq "--pwsh") -or ($_ -eq "--list")}))) { | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |         python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params $SpackSubCommand $SpackSubCommandArgs | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |         $SpackEnv = $(python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params $SpackSubCommand "--pwsh" $SpackSubCommandArgs) | 
					
						
							| 
									
										
										
										
											2023-09-01 14:36:27 -04:00
										 |  |  |         Set-SpackEnv $SpackEnv | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $SpackCMD_params, $SpackSubCommand, $SpackSubCommandArgs = Read-SpackArgs $args | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (Compare-CommonArgs $SpackCMD_params) { | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |     python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params $SpackSubCommand $SpackSubCommandArgs | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  |     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} | 
					
						
							| 
									
										
										
										
											2023-09-08 13:49:16 -04:00
										 |  |  |     default  {python "$Env:SPACK_ROOT/bin/spack" $SpackCMD_params $SpackSubCommand $SpackSubCommandArgs} | 
					
						
							| 
									
										
										
										
											2023-06-27 21:26:51 -04:00
										 |  |  | } |