Skip to content
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

Better compatibility to Android IDE local packages installation #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
104 changes: 84 additions & 20 deletions autoload/arduino.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if has('win64') || has('win32') || has('win16')
endif
let s:HERE = resolve(expand('<sfile>:p:h:h'))
let s:OS = substitute(system('uname'), '\n', '', '')
let s:ARDUINO_USER_DIR = $HOME . "/.arduino15"
" In neovim, run the shell commands using :terminal to preserve interactivity
if has('nvim')
let s:TERM = 'botright split | terminal! '
Expand Down Expand Up @@ -54,6 +55,13 @@ 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')
if s:FileExists(s:ARDUINO_USER_DIR)
let g:arduino_user_installation = 1
else
let g:arduino_user_installation = 0
endif
endif

if !exists('g:arduino_serial_port_globs')
let g:arduino_serial_port_globs = ['/dev/ttyACM*',
Expand Down Expand Up @@ -115,30 +123,54 @@ function! arduino#GetArduinoCommand(cmd)
return cmd
endfunction

function! arduino#GetBoards()
let arduino_dir = arduino#GetArduinoDir()
function! arduino#GetSubBoards(dir, subdir)
let boards = []
for filename in split(globpath(arduino_dir . '/hardware', '**/boards.txt'), '\n')
for filename in split(globpath(a:dir . '/' . a:subdir, '**/boards.txt'), '\n')
let pieces = split(filename, '/')
let package = pieces[-3]
let arch = pieces[-2]
if len(pieces) == 9
let package = pieces[-5]
let arch = pieces[-3]
let package_version = pieces[-2]
else
let package = pieces[-3]
let arch = pieces[-2]
endif

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)
if exists('package_version')
call add(boards, package . ':' . arch . ':' . board . ':' . package_version)
else
call add(boards, package . ':' . arch . ':' . board)
endif
endif
endfor
endfor
return boards
endfunction

function! arduino#GetBoards()
let arduino_dir = arduino#GetArduinoDir()
if arduino#GetArduinoUserInstallation() == 1
let boards = arduino#GetSubBoards(arduino_dir, 'packages')
else
let boards = arduino#GetSubBoards(arduino_dir, 'hardware')
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

function! arduino#GetBoardOptions(board)
let arduino_dir = arduino#GetArduinoDir()
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 @@ -157,12 +189,15 @@ function! arduino#GetBoardOptions(board)
return options
endfunction

function! arduino#GetProgrammers()
let arduino_dir = arduino#GetArduinoDir()
function! arduino#GetSubProgrammers(dir, subdir)
let programmers = []
for filename in split(globpath(arduino_dir . '/hardware', '**/programmers.txt'), '\n')
for filename in split(globpath(a:dir . '/' . a:subdir, '**/programmers.txt'), '\n')
let pieces = split(filename, '/')
let package = pieces[-3]
if len(pieces) == 9
let package = pieces[-5]
else
let package = pieces[-3]
endif
let lines = readfile(filename)
for line in lines
if line =~? '^[^.]*\.name=.*$'
Expand All @@ -172,6 +207,16 @@ function! arduino#GetProgrammers()
endif
endfor
endfor
return programmers
endfunction

function! arduino#GetProgrammers()
let arduino_dir = arduino#GetArduinoDir()
if arduino#GetArduinoUserInstallation() == 1
let programmers = arduino#GetSubProgrammers(arduino_dir, 'packages')
else
let programmers = arduino#GetSubProgrammers(arduino_dir, 'hardware')
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 +267,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 +469,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