Last active
April 25, 2020 12:03
-
-
Save eflukx/a9a1a1361d7ba965c5f01a9528037524 to your computer and use it in GitHub Desktop.
Generate include paths for ESP32 ESP-IDF for use in VSCode (c_cpp_properties.json)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
require 'json' | |
APP_ROOT = File.dirname(__FILE__) | |
def dirs_in path | |
Dir.each_child(path) | |
.select{ |e| File.directory? File.join(path, e) } | |
.map{ |dir| File.realpath File.join(path, dir) } | |
end | |
def has_header_files path | |
Dir.each_child(path) | |
.any?{ |e| File.extname(e) == '.h' } | |
end | |
def idf_path *args | |
path = File.join File.realpath(ARGV[0] || ENV['IDF_PATH'] || ''), args | |
rescue Errno::ENOENT | |
raise "idf_path does not exist! (Make sure $IDF_PATH environment is set or use command line parameter.)" | |
end | |
def traverse dirs | |
dirs.map{ |dir| dirs + traverse(dirs_in dir) } | |
end | |
def dirs_with_headers path = idf_path | |
traverse(dirs_in path).flatten.uniq | |
.select{ |d| has_header_files d } | |
end | |
def vscode_includes dwh = dirs_with_headers | |
dwh | |
.map{ |s| s.gsub(/\/include\/.+/, '/include/') }.uniq # trim path to /include part | |
.map{ |s| s.sub idf_path, '${env:IDF_PATH}/' } | |
end | |
def load_local_cpp_properties file = File.join(APP_ROOT, '.vscode', 'c_cpp_properties.json') | |
JSON.parse File.read(file) | |
end | |
include_paths = vscode_includes(dirs_with_headers.select{ |d| d =~ /include/i }) | |
# binding.pry | |
puts JSON.pretty_generate(include_paths) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment