Skip to content

Better compatibility to Android IDE local packages installation #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 91 additions & 33 deletions autoload/arduino.vim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ function! arduino#InitializeConfig()
if !exists('g:arduino_run_headless')
let g:arduino_run_headless = executable('Xvfb') ? 1 : 0
endif
if !exists('g:arduino_user_installation')
let g:arduino_user_installation = 0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want, you could default this to 1 if s:FileExists($HOME . ".arduino15"). If you do, you should probably pull $HOME . ".arduino15" out into a constant or a function

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

endif

if !exists('g:arduino_serial_port_globs')
let g:arduino_serial_port_globs = ['/dev/ttyACM*',
Expand Down Expand Up @@ -118,19 +121,36 @@ endfunction
function! arduino#GetBoards()
let arduino_dir = arduino#GetArduinoDir()
let boards = []
for filename in split(globpath(arduino_dir . '/hardware', '**/boards.txt'), '\n')
let pieces = split(filename, '/')
let package = pieces[-3]
let arch = pieces[-2]
let lines = readfile(filename)
for line in lines
if line =~? '^[^.]*\.build\.board=.*$'
let linesplit = split(line, '\.')
let board = linesplit[0]
call add(boards, package . ':' . arch . ':' . board)
endif
if arduino#GetArduinoUserInstallation() == 1
for filename in split(globpath(arduino_dir . '/packages', '**/boards.txt'), '\n')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the difference in logic here (searching /packages/ instead of /hardware, pulling out the package version, etc) due to the user installation? I would expect that boards might exist in the /packages dir even if system installed, and that boards would exist under /hardware in a user installation. Is this the case?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you got the point correct: I realized with your plugin, that all my hardware, etc. (installed by the arduino ide into the user dir) wasn't available.
That's why I started to dig into the code.

let pieces = split(filename, '/')
let package = pieces[-5]
let arch = pieces[-3]
let package_version = pieces[-2]
let lines = readfile(filename)
for line in lines
if line =~? '^[^.]*\.build\.board=.*$'
let linesplit = split(line, '\.')
let board = linesplit[0]
call add(boards, package . ':' . arch . ':' . board . ':' . package_version)
endif
endfor
endfor
endfor
else
for filename in split(globpath(arduino_dir . '/hardware', '**/boards.txt'), '\n')
let pieces = split(filename, '/')
let package = pieces[-3]
let arch = pieces[-2]
let lines = readfile(filename)
for line in lines
if line =~? '^[^.]*\.build\.board=.*$'
let linesplit = split(line, '\.')
let board = linesplit[0]
call add(boards, package . ':' . arch . ':' . board)
endif
endfor
endfor
endif
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you take the shared logic here and factor it out into a function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return boards
endfunction

Expand All @@ -139,6 +159,10 @@ function! arduino#GetBoardOptions(board)
let board_pieces = split(a:board, ':')
let filename = arduino_dir . '/hardware/' . board_pieces[0] .
\ '/' . board_pieces[1] . '/boards.txt'
if !filereadable(filename)
let filename = arduino_dir . '/packages/' . board_pieces[0] .
\ '/hardware/' . board_pieces[1] . '/' . board_pieces[3] . '/boards.txt'
endif
let lines = readfile(filename)
let pattern = '^' . board_pieces[2] . '\.menu\.\([^.]*\)\.\([^.]*\)='
let options = {}
Expand All @@ -160,18 +184,33 @@ endfunction
function! arduino#GetProgrammers()
let arduino_dir = arduino#GetArduinoDir()
let programmers = []
for filename in split(globpath(arduino_dir . '/hardware', '**/programmers.txt'), '\n')
let pieces = split(filename, '/')
let package = pieces[-3]
let lines = readfile(filename)
for line in lines
if line =~? '^[^.]*\.name=.*$'
let linesplit = split(line, '\.')
let programmer = linesplit[0]
call add(programmers, package . ':' . programmer)
endif
if arduino#GetArduinoUserInstallation() == 1
for filename in split(globpath(arduino_dir . '/packages', '**/programmers.txt'), '\n')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question about if this is specific to a user installation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is.

let pieces = split(filename, '/')
let package = pieces[-5]
let lines = readfile(filename)
for line in lines
if line =~? '^[^.]*\.name=.*$'
let linesplit = split(line, '\.')
let programmer = linesplit[0]
call add(programmers, package . ':' . programmer)
endif
endfor
endfor
endfor
else
for filename in split(globpath(arduino_dir . '/hardware', '**/programmers.txt'), '\n')
let pieces = split(filename, '/')
let package = pieces[-3]
let lines = readfile(filename)
for line in lines
if line =~? '^[^.]*\.name=.*$'
let linesplit = split(line, '\.')
let programmer = linesplit[0]
call add(programmers, package . ':' . programmer)
endif
endfor
endfor
endif
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also looks like a lot of shared logic that could be factored out into a function

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return sort(programmers)
endfunction

Expand Down Expand Up @@ -222,9 +261,15 @@ endfunction
" Callback from board selection. Sets the board and prompts for any options
function! arduino#SelectBoard(board)
let options = arduino#GetBoardOptions(a:board)
call arduino#SetBoard(a:board)
let board_pieces = split(a:board, ':')
if len(board_pieces) == 4
let board = board_pieces[0] . ':' . board_pieces[1] . ':' . board_pieces[2]
else
let board = a:board
endif
call arduino#SetBoard(board)
let s:callback_data = {
\ 'board': a:board,
\ 'board': board,
\ 'available_opts': options,
\ 'opts': {},
\ 'active_option': '',
Expand Down Expand Up @@ -418,18 +463,31 @@ function! arduino#GetArduinoDir()
if exists('g:arduino_dir')
return g:arduino_dir
endif
let executable = arduino#GetArduinoExecutable()
let arduino_cmd = arduino#FindExecutable(executable)
let arduino_dir = fnamemodify(arduino_cmd, ':h')
if s:OS == 'Darwin'
let arduino_dir = fnamemodify(arduino_dir, ':h') . '/Java'
endif
if !s:FileExists(arduino_dir . '/hardware/arduino/')
throw "Could not find arduino directory. Please set g:arduino_dir"
if arduino#GetArduinoUserInstallation() == 1
let arduino_dir = $HOME . '/.arduino15'
if !s:FileExists(arduino_dir)
throw "Could not find arduino directory. Please set g:arduino_dir"
endif
else
let executable = arduino#GetArduinoExecutable()
let arduino_cmd = arduino#FindExecutable(executable)
let arduino_dir = fnamemodify(arduino_cmd, ':h')
if s:OS == 'Darwin'
let arduino_dir = fnamemodify(arduino_dir, ':h') . '/Java'
endif
if !s:FileExists(arduino_dir . '/hardware/arduino/')
throw "Could not find arduino directory. Please set g:arduino_dir"
endif
endif
return arduino_dir
endfunction

function! arduino#GetArduinoUserInstallation()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you actually need this function. Since you do the exists() check at the top of the file, any time you're calling this function you can instead just read out the value of g:arduino_user_installation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user should be able to configure the directory, because it can be configured in the arduino ide as well.
So I think I'll need this check to verify the existence of the directory.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I'm misunderstanding, but at the top of the file you're doing

if !exists('g:arduino_user_installation')
  if s:FileExists(s:ARDUINO_USER_DIR)
    let g:arduino_user_installation = 1
  else
    let g:arduino_user_installation = 0
  endif
endif

So by the time this method is called, the only way exists('g:arduino_user_installation') will be false is if the user has manually run unlet g:arduino_user_installation somewhere. It seems like you could just access g:arduino_user_installation directly instead of calling this getter.

if exists('g:arduino_user_installation')
return g:arduino_user_installation
endif
endfunction

" Ctrlp extension {{{1
if exists('g:ctrlp_ext_vars')
let g:arduino_ctrlp_enabled = 1
Expand Down
8 changes: 8 additions & 0 deletions doc/arduino.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Overview:~
|arduino_serial_tmux|..........Tmux command to open serial debugger.
|arduino_serial_port|..........Location of the serial port.
|arduino_serial_port_globs|....Globs to auto-search for serial port.
|arduino_user_installation|....Use user installation of arduino ide.

-------------------------------------------------------------------------------
Detailed descriptions and default values:~
Expand Down Expand Up @@ -110,6 +111,13 @@ Search these patterns to find a likely serial port to upload to. >
\'/dev/tty.usbserial*']
<

*'g:arduino_user_installation'*
Set this variable to 1 if you want to use the the arduino user folder normally
located under ~/.arduino15.
If you're using a non default path you also have to set 'g:arduino_dir'. >
let g:arduino_user_installation = 0


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you'll want a closing < here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

===============================================================================
COMMANDS *arduino-commands*
*:ArduinoChooseBoard*
Expand Down