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 foreach ($pair  in $envars_array) {
27     # setting the values
28     $var_name = $pair[0].Trim() # trim spaces on the ends of the name
29     $var_val = $pair[1].Trim() # trim spaces on the ends of the val
30     if ($var_name -eq "PATH") {
31         # trim "%PATH%" or "`$PATH"
32         if ($IsWindows) {
33             $var_val = $var_val.Trim($S + "%PATH%")
34         } else {
35             $var_val = $var_val.Trim($S + "`$PATH")
36         }
37         # apply
38         $env:PATH = $var_val + $S + $env:PATH
39     } else {
40         New-Item -Path "env:$var_name" -Value "$var_val" -Force
41     }
42 }
43 
44 # Allow calling some IDF python tools without specifying the full path
45 # ${IDF_PATH}/tools is already added by 'idf_tools.py export'
46 $IDF_ADD_PATHS_EXTRAS = [IO.Path]::Combine(${IDF_PATH}, "components", "esptool_py", "esptool")
47 $IDF_ADD_PATHS_EXTRAS += ${S} + [IO.Path]::Combine(${IDF_PATH}, "components", "app_update")
48 $IDF_ADD_PATHS_EXTRAS += ${S} + [IO.Path]::Combine(${IDF_PATH}, "components", "espcoredump")
49 $IDF_ADD_PATHS_EXTRAS += ${S} + [IO.Path]::Combine(${IDF_PATH}, "components", "partition_table")
50 $env:PATH = $IDF_ADD_PATHS_EXTRAS + $S + $env:PATH
51 
52 #Compare Path's OLD vs. NEW
53 $NEW_PATH = $env:PATH.split($S) | Select-Object -Unique # array without duplicates
54 $dif_Path = Compare-Object -ReferenceObject $OLD_PATH -DifferenceObject $NEW_PATH -PassThru
55 if ($dif_Path -ne $null) {
56     Write-Output "`nAdded to PATH`n-------------"
57     Write-Output $dif_Path
58 } else {
59     Write-Output "No directories added to PATH:"
60     Write-Output $OLD_PATH
61 }
62 
63 
64 Write-Output "Checking if Python packages are up to date..."
65 
66 Start-Process -Wait -NoNewWindow -FilePath "python" -Args "`"$IDF_PATH/tools/check_python_dependencies.py`""
67 if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } # if error
68 
69 Write-Output "
70 Done! You can now compile ESP-IDF projects.
71 Go to the project directory and run:
72     idf.py build
73 
74 "
75