|
| 1 | +require 'fileutils' |
1 | 2 | require 'arduino_ci/display_manager'
|
2 | 3 | require 'arduino_ci/arduino_installation'
|
3 | 4 |
|
@@ -159,6 +160,12 @@ def install_library(library_name)
|
159 | 160 | result[:success]
|
160 | 161 | end
|
161 | 162 |
|
| 163 | + # generate the (very likely) path of a library given its name |
| 164 | + def library_path(library_name) |
| 165 | + sketchbook = get_pref("sketchbook.path") |
| 166 | + File.join(sketchbook, library_name) |
| 167 | + end |
| 168 | + |
162 | 169 | # update the library index
|
163 | 170 | def update_library_index
|
164 | 171 | # install random lib so the arduino IDE grabs a new library index
|
@@ -193,5 +200,42 @@ def verify_sketch(path)
|
193 | 200 | run("--verify", path, err: :out)
|
194 | 201 | end
|
195 | 202 |
|
| 203 | + # ensure that the given library is installed, or symlinked as appropriate |
| 204 | + # return the path of the prepared library, or nil |
| 205 | + def install_local_library(library_path) |
| 206 | + library_name = File.basename(library_path) |
| 207 | + destination_path = File.join(@installation.lib_dir, library_name) |
| 208 | + |
| 209 | + # things get weird if the sketchbook contains the library. |
| 210 | + # check that first |
| 211 | + if File.exist? destination_path |
| 212 | + uhoh = "There is already a library '#{library_name}' in the library directory" |
| 213 | + return destination_path if destination_path == library_path |
| 214 | + |
| 215 | + # maybe it's a symlink? that would be OK |
| 216 | + if File.symlink?(destination_path) |
| 217 | + return destination_path if File.readlink(destination_path) == library_path |
| 218 | + puts "#{uhoh} and it's not symlinked to #{library_path}" |
| 219 | + return nil |
| 220 | + end |
| 221 | + |
| 222 | + puts "#{uhoh}. It may need to be removed manually." |
| 223 | + return nil |
| 224 | + end |
| 225 | + |
| 226 | + # install the library |
| 227 | + FileUtils.ln_s(library_path, destination_path) |
| 228 | + destination_path |
| 229 | + end |
| 230 | + |
| 231 | + def each_library_example(installed_library_path) |
| 232 | + example_path = File.join(installed_library_path, "examples") |
| 233 | + examples = Pathname.new(example_path).children.select(&:directory?).map(&:to_path).map(&File.method(:basename)) |
| 234 | + examples.each do |e| |
| 235 | + proj_file = File.join(example_path, e, "#{e}.ino") |
| 236 | + puts "Considering #{proj_file}" |
| 237 | + yield e if File.exist?(proj_file) |
| 238 | + end |
| 239 | + end |
196 | 240 | end
|
197 | 241 | end
|
0 commit comments