idf_cmd_init.ps1 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # This script is called from a Windows shortcut, with
  2. # the working directory set to an ESP-IDF directory.
  3. # Its purpose is to support using the "IDF Tools Directory" method of
  4. # installation for ESP-IDF versions older than IDF v4.1.
  5. # It does the same thing as "export.ps1" in IDF v4.1.
  6. Param
  7. (
  8. [String]$IdfGitDir,
  9. [String]$IdfPythonDir
  10. )
  11. $IDF_PATH = "."
  12. $isEspIdfRoot = (Test-Path "$IDF_PATH/tools/idf.py")
  13. if (-not $isEspIdfRoot) {
  14. Write-Output "Usage: idf_cmd_init.ps1 ^<Python directory^> ^<Git directory^>"
  15. Write-Output "This script must be invoked from ESP-IDF directory."
  16. }
  17. # Strip quotes
  18. $IdfGitDir = $IdfGitDir.Trim("`"")
  19. $IdfPythonDir = $IdfPythonDir.Trim("`"")
  20. # Add Python and Git paths to PATH
  21. $env:PATH = "$IdfGitDir;$IdfPythonDir;$env:PATH"
  22. Write-Output "Using Python in $IdfPythonDir"
  23. python.exe --version
  24. Write-Output "Using Git in $IdfGitDir"
  25. git.exe --version
  26. # Check if this is a recent enough copy of ESP-IDF.
  27. # If so, use export.ps1 provided there.
  28. $isExport = (Test-Path "$IDF_PATH/export.ps1")
  29. if ($isExport){
  30. . $IDF_PATH/export.ps1
  31. }
  32. else {
  33. Write-Output "IDF version does not include export.ps1. Using the fallback version."
  34. if ((Test-Path "$IDF_PATH/tools/tools.json")){
  35. $IDF_TOOLS_JSON_PATH = "$IDF_PATH/tools/tools.json"
  36. }
  37. else{
  38. Write-Output "IDF version does not include tools/tools.json. Using the fallback version."
  39. $IDF_TOOLS_JSON_PATH = "$PSScriptRoot/tools_fallback.json"
  40. }
  41. if ((Test-Path "$IDF_PATH/tools/idf_tools.py")){
  42. $IDF_TOOLS_PY_PATH = "$IDF_PATH/tools/idf_tools.py"
  43. }
  44. else{
  45. Write-Output "IDF version does not include tools/idf_tools.py. Using the fallback version."
  46. $IDF_TOOLS_PY_PATH = "$PSScriptRoot/idf_tools_fallback.py"
  47. }
  48. Write-Output "Setting IDF_PATH: $IDF_PATH"
  49. $env:IDF_PATH = $IDF_PATH
  50. Write-Output "Adding ESP-IDF tools to PATH..."
  51. $OLD_PATH = $env:Path.split(";") | Select-Object -Unique # array without duplicates
  52. # using idf_tools.py to get $envars_array to set
  53. $envars_raw = (python.exe "$IDF_TOOLS_PY_PATH" --tools-json "$IDF_TOOLS_JSON_PATH" export --format key-value)
  54. if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } # if error
  55. $envars_array # will be filled like:
  56. # [
  57. # [vname1, vval1], [vname2, vval2], ...
  58. # ]
  59. foreach ($line in $envars_raw) {
  60. $pair = $line.split("=") # split in name, val
  61. $var_name = $pair[0].Trim() # trim spaces on the ends of the name
  62. $var_val = $pair[1].Trim() # trim spaces on the ends of the val
  63. $var_val = $var_val -replace "%(.+)%", "`$env:`$1" # convert var syntax to PS using RegEx
  64. $var_val = $ExecutionContext.InvokeCommand.ExpandString($var_val) # expand variables to values
  65. $envars_array += (, ($var_name, $var_val))
  66. }
  67. foreach ($pair in $envars_array) {
  68. # setting the values
  69. $var_name = $pair[0].Trim() # trim spaces on the ends of the name
  70. $var_val = $pair[1].Trim() # trim spaces on the ends of the val
  71. Set-Item -Path "Env:$var_name" -Value "$var_val"
  72. }
  73. #Compare Path's OLD vs. NEW
  74. $NEW_PATH = $env:Path.split(";") | Select-Object -Unique # array without duplicates
  75. $dif_Path = Compare-Object -ReferenceObject $OLD_PATH -DifferenceObject $NEW_PATH -PassThru
  76. if ($dif_Path -ne $null) {
  77. Write-Output $dif_Path
  78. }
  79. else {
  80. Write-Output "No directories added to PATH:"
  81. Write-Output $OLD_PATH
  82. }
  83. Write-Output "Checking if Python packages are up to date..."
  84. Start-Process -Wait -NoNewWindow -FilePath "python" -Args "`"$IDF_PATH/tools/check_python_dependencies.py`""
  85. if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } # if error
  86. Write-Output "
  87. Done! You can now compile ESP-IDF projects.
  88. Go to the project directory and run:
  89. idf.py build
  90. "
  91. }