check for api key in config file to fix #23

This commit is contained in:
Alan Hamlett 2015-05-01 16:08:02 -07:00
parent 3218aabd97
commit ec353d8fe6

View file

@ -27,17 +27,24 @@ let s:VERSION = '3.0.9'
let s:old_cpo = &cpo
set cpo&vim
" To be backwards compatible, rename config file
" Globals
let s:plugin_directory = expand("<sfile>:p:h") . '/'
let s:config_file = expand("$HOME/.wakatime.cfg")
let s:config_file_already_setup = 0
" For backwards compatibility, rename wakatime.conf to wakatime.cfg
if !filereadable(s:config_file)
if filereadable(expand("$HOME/.wakatime"))
exec "silent !mv" expand("$HOME/.wakatime") expand("$HOME/.wakatime.conf")
endif
if filereadable(expand("$HOME/.wakatime.conf"))
if !filereadable(expand("$HOME/.wakatime.cfg"))
if !filereadable(s:config_file)
let contents = ['[settings]'] + readfile(expand("$HOME/.wakatime.conf"), '')
call writefile(contents, expand("$HOME/.wakatime.cfg"))
call writefile(contents, s:config_file)
call delete(expand("$HOME/.wakatime.conf"))
endif
endif
endif
" Set default python binary location
if !exists("g:wakatime_PythonBinary")
@ -49,27 +56,45 @@ let s:VERSION = '3.0.9'
let g:wakatime_HeartbeatFrequency = 2
endif
" Globals
let s:plugin_directory = expand("<sfile>:p:h") . '/'
let s:config_file_exists = 0
" }}}
" Function Definitions {{{
function! s:StripWhitespace(str)
return substitute(a:str, '^\s*\(.\{-}\)\s*$', '\1', '')
endfunction
function! s:SetupConfigFile()
if !s:config_file_exists
if !s:config_file_already_setup
" Create config file if does not exist
if !filereadable(expand("$HOME/.wakatime.cfg"))
if !filereadable(s:config_file)
let key = input("[WakaTime] Enter your wakatime.com api key: ")
if key != ''
call writefile(['[settings]', 'debug = false', printf("api_key = %s", key), 'hidefilenames = false', 'ignore =', ' ^COMMIT_EDITMSG$', ' ^TAG_EDITMSG$'], expand("$HOME/.wakatime.cfg"))
call writefile(['[settings]', 'debug = false', printf("api_key = %s", key), 'hidefilenames = false', 'ignore =', ' COMMIT_EDITMSG$', ' TAG_EDITMSG$'], s:config_file)
endif
" Make sure config file has api_key
else
let found_api_key = 0
let lines = readfile(s:config_file)
for line in lines
let group = split(line, '=')
if len(group) == 2 && s:StripWhitespace(group[0]) == 'api_key' && s:StripWhitespace(group[1]) != ''
let found_api_key = 1
endif
endfor
if !found_api_key
let key = input("[WakaTime] Enter your wakatime.com api key: ")
let lines = lines + [join(['api_key', key], '=')]
call writefile(lines, s:config_file)
endif
endif
let s:config_file_already_setup = 1
echo "[WakaTime] Setup complete! Visit http://wakatime.com to view your logged time."
endif
endif
let s:config_file_exists = 1
endif
endfunction
function! s:GetCurrentFile()
@ -100,11 +125,11 @@ let s:VERSION = '3.0.9'
else
exec 'silent !' . join(cmd, ' ') . ' &'
endif
call s:SetLastAction(a:time, a:time, targetFile)
call s:SetLastHeartbeat(a:time, a:time, targetFile)
endif
endfunction
function! s:GetLastAction()
function! s:GetLastHeartbeat()
if !filereadable(expand("$HOME/.wakatime.data"))
return [0, 0, '']
endif
@ -115,18 +140,10 @@ let s:VERSION = '3.0.9'
return last
endfunction
function! s:SetLastAction(time, last_update, targetFile)
function! s:SetLastHeartbeat(time, last_update, targetFile)
call writefile([substitute(printf('%d', a:time), ',', '.', ''), substitute(printf('%d', a:last_update), ',', '.', ''), a:targetFile], expand("$HOME/.wakatime.data"))
endfunction
function! s:GetChar()
let c = getchar()
if c =~ '^\d\+$'
let c = nr2char(c)
endif
return c
endfunction
function! s:EnoughTimePassed(now, last)
let prev = a:last[0]
if a:now - prev > g:wakatime_HeartbeatFrequency * 60
@ -140,23 +157,16 @@ let s:VERSION = '3.0.9'
" Event Handlers {{{
function! s:normalAction()
function! s:handleActivity(is_write)
call s:SetupConfigFile()
let targetFile = s:GetCurrentFile()
let now = localtime()
let last = s:GetLastAction()
if s:EnoughTimePassed(now, last) || targetFile != last[2]
call s:Api(targetFile, now, 0, last)
let last = s:GetLastHeartbeat()
if a:is_write || s:EnoughTimePassed(now, last) || targetFile != last[2]
call s:Api(targetFile, now, a:is_write, last)
endif
endfunction
function! s:writeAction()
let targetFile = s:GetCurrentFile()
let now = localtime()
let last = s:GetLastAction()
call s:Api(targetFile, now, 1, last)
endfunction
" }}}
@ -164,10 +174,10 @@ let s:VERSION = '3.0.9'
augroup Wakatime
autocmd!
autocmd BufEnter * call s:normalAction()
autocmd VimEnter * call s:normalAction()
autocmd BufWritePost * call s:writeAction()
autocmd CursorMoved,CursorMovedI * call s:normalAction()
autocmd BufEnter * call s:handleActivity(0)
autocmd VimEnter * call s:handleActivity(0)
autocmd BufWritePost * call s:handleActivity(1)
autocmd CursorMoved,CursorMovedI * call s:handleActivity(0)
augroup END
" }}}