1 #!/usr/bin/env pwsh
2 $S = [IO.Path]::PathSeparator # path separator. WIN:';', UNIX:":"
3 
4 $IDF_PATH = $PSScriptRoot
5 
6 Write-Output "Setting IDF_PATH: $IDF_PATH"
7 $env:IDF_PATH = $IDF_PATH
8 
9 Write-Output "Adding ESP-IDF tools to PATH..."
10 $OLD_PATH = $env:PATH.split($S) | Select-Object -Unique # array without duplicates
11 # using idf_tools.py to get $envars_array to set
12 $envars_raw = python $IDF_PATH/tools/idf_tools.py export --format key-value
13 if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } # if error
14 
15 $envars_array = @() # will be filled like:
16 #               [
17 #                    [vname1, vval1], [vname2, vval2], ...
18 #               ]
19 foreach ($line  in $envars_raw) {
20     $pair = $line.split("=") # split in name, val
21     $var_name = $pair[0].Trim() # trim spaces on the ends of the name
22     $var_val = $pair[1].Trim() # trim spaces on the ends of the val
23     $envars_array += (, ($var_name, $var_val))
24 }
25 
26 if ($IsWindows -eq $null) {
27     # $IsWindows was added in PowerShell Core 6 and PowerShell 7 together with multi-platform support. # I.E. if this
28     # internal variable is not set then PowerShell 5 is used and # the platform cannot be # anything else than Windows.
29     $IsWindows = $true
30 }
31 
32 foreach ($pair  in $envars_array) {
33     # setting the values
34     $var_name = $pair[0].Trim() # trim spaces on the ends of the name
35     $var_val = $pair[1].Trim() # trim spaces on the ends of the val
36     if ($var_name -eq "PATH") {
37         # trim "%PATH%" or "`$PATH"
38         if ($IsWindows) {
39             $var_val = $var_val.Trim($S + "%PATH%")
40         } else {
41             $var_val = $var_val.Trim($S + "`$PATH")
42         }
43         # apply
44         $env:PATH = $var_val + $S + $env:PATH
45     } else {
46         New-Item -Path "env:$var_name" -Value "$var_val" -Force
47     }
48 }
49 
50 # Allow calling some IDF python tools without specifying the full path
51 # ${IDF_PATH}/tools is already added by 'idf_tools.py export'
52 $IDF_ADD_PATHS_EXTRAS = [IO.Path]::Combine(${IDF_PATH}, "components", "esptool_py", "esptool")
53 $IDF_ADD_PATHS_EXTRAS += ${S} + [IO.Path]::Combine(${IDF_PATH}, "components", "app_update")
54 $IDF_ADD_PATHS_EXTRAS += ${S} + [IO.Path]::Combine(${IDF_PATH}, "components", "espcoredump")
55 $IDF_ADD_PATHS_EXTRAS += ${S} + [IO.Path]::Combine(${IDF_PATH}, "components", "partition_table")
56 $env:PATH = $IDF_ADD_PATHS_EXTRAS + $S + $env:PATH
57 
58 #Compare Path's OLD vs. NEW
59 $NEW_PATH = $env:PATH.split($S) | Select-Object -Unique # array without duplicates
60 $dif_Path = Compare-Object -ReferenceObject $OLD_PATH -DifferenceObject $NEW_PATH -PassThru
61 if ($dif_Path -ne $null) {
62     Write-Output "`nAdded to PATH`n-------------"
63     Write-Output $dif_Path
64 } else {
65     Write-Output "No directories added to PATH:"
66     Write-Output $OLD_PATH
67 }
68 
69 
70 Write-Output "Checking if Python packages are up to date..."
71 
72 Start-Process -Wait -NoNewWindow -FilePath "python" -Args "`"$IDF_PATH/tools/check_python_dependencies.py`""
73 if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } # if error
74 
75 Write-Output "
76 Done! You can now compile ESP-IDF projects.
77 Go to the project directory and run:
78     idf.py build
79 
80 "
81