Skip to content

Commit

Permalink
some refactoring to allow activating rtps, too
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcWeber committed Jun 26, 2014
1 parent 16bed78 commit 3392ede
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 48 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -114,6 +114,10 @@ call add(scripts, {'name': 'script-php', 'ft_regex':'^\.php$'})
" always activate this color scheme, and set runtimepath
call add(scripts, {'name': 'github:daylerees/colour-schemes', 'addon-info': {'runtimepath': 'vim'} })
" just activate a rtp (also works at runtime with all hooks such as sourcing ftdetect/*.vim files)
call add(scripts, {'activate_this_rtp': 'absolute-rtp-path'})
" tell VAM about all scripts, and immediately activate plugins having the c-dev tag:
call vam#Scripts(scripts, {'tag_regex': 'c-dev'})
Expand Down
103 changes: 63 additions & 40 deletions autoload/vam.vim
Expand Up @@ -107,6 +107,9 @@ endfun
" use join so that you can break the dict into multiple lines. This makes
" reading it much easier
fun! vam#ReadAddonInfo(path)
if !filereadable(a:path)
return {}
endif

" don't add "b" because it'll read dos files as "\r\n" which will fail the
" check and evaluate in eval. \r\n is checked out by some msys git
Expand Down Expand Up @@ -139,9 +142,8 @@ endfun
fun! vam#PluginDirFromName(...)
return call(s:c.plugin_dir_by_name, a:000, {})
endfun
fun! vam#PluginRuntimePath(name)
let info = vam#AddonInfo(a:name)
return vam#PluginDirFromName(a:name).(has_key(info, 'runtimepath') ? '/'.info.runtimepath : '')
fun! vam#PluginRuntimePath(pluginDir, info)
return a:pluginDir.(has_key(a:info, 'runtimepath') ? '/'. a:info.runtimepath : '')
endfun

" adding VAM, so that its contained in list passed to :UpdateActivatedAddons
Expand All @@ -160,12 +162,23 @@ fun! vam#IsPluginInstalled(name)
\ || !empty(glob(fnameescape(d).'/archive/*', 1)))
endfun

" {} if file doesn't exist
" TODO: remove this
fun! vam#AddonInfo(name)
let infoFile = vam#AddonInfoFile(a:name)
return filereadable(infoFile)
\ ? vam#ReadAddonInfo(infoFile)
\ : {}
throw "deprecated"
" use this code instead:
" vam#ReadAddonInfo(vam#AddonInfoFile(vam#PluginDirFromName(name), name))
endfun


fun! vam#ActivateDependencies(opts, dependencies, name)

" activate dependencies merging opts with given repository sources
" sources given in opts will win
call vam#ActivateAddons(keys(a:dependencies),
\ extend(copy(a:opts), {
\ 'plugin_sources' : extend(copy(a:dependencies), get(a:opts, 'plugin_sources',{})),
\ 'requested_by' : [a:name] + get(a:opts, 'requested_by', [])
\ }))
endfun


Expand All @@ -178,44 +191,55 @@ fun! vam#ActivateRecursively(list_of_scripts, ...)
let opts = extend({'run_install_hooks': 1}, a:0 == 0 ? {} : a:1)

for script_ in a:list_of_scripts
let name = script_.name
if !has_key(s:c.activated_plugins, name)
" try to find plugin root / rtp

if has_key(script_, 'activate_this_rtp')
" hack: allow passing {'activate_this_rtp': 'path'} to get all the
" workarounds when activating rtps after Vim has started up
let name = get(script_, 'name', 'unkown, rtp: '. script_.activate_this_rtp)
let rtp = script_.activate_this_rtp
if index(split(&runtimepath, '\v(\\@<!(\\.)*\\)@<!\,'), rtp) > 0
" don't readd rtp
continue
endif
let info = vam#ReadAddonInfo(vam#AddonInfoFile(rtp, ""))
call vam#ActivateDependencies(opts, get(info, 'dependencies', {}), name)

let s:c.activated_plugins['rtp:'.rtp] = 1
else
let name = script_.name
let pluginRoot = vam#PluginDirFromName(name)
if has_key(s:c.activated_plugins, name)
continue
endif
" break circular dependencies..
let s:c.activated_plugins[name] = 0

let infoFile = vam#AddonInfoFile(name)
let infoFile = vam#AddonInfoFile(pluginRoot, name)
if !filereadable(infoFile) && !vam#IsPluginInstalled(name)
if empty(vam#install#Install([script_], opts))
unlet s:c.activated_plugins[name]
continue
endif
endif
let info = vam#AddonInfo(name)
let dependencies = get(info,'dependencies', {})

" activate dependencies merging opts with given repository sources
" sources given in opts will win
call vam#ActivateAddons(keys(dependencies),
\ extend(copy(opts), {
\ 'plugin_sources' : extend(copy(dependencies), get(opts, 'plugin_sources',{})),
\ 'requested_by' : [name] + get(opts, 'requested_by', [])
\ }))
let info = vam#ReadAddonInfo(infoFile)
call vam#ActivateDependencies(opts, get(info, 'dependencies', {}), name)

let s:c.activated_plugins[name] = 1
" source plugin/* files ?
let rtp = vam#PluginRuntimePath(name)
call add(opts.new_runtime_paths, rtp)
let rtp = vam#PluginRuntimePath(pluginRoot, info)
endif

let s:c.activated_plugins[name] = 1
call add(opts.new_runtime_paths, rtp)

if s:c.debug_activation
" activation takes place later (-> new_runtime_paths), but messages will be in order
" XXX Lengths of “as it was requested by” and “which was requested by”
" match
call vam#Log('Will activate '.name.(empty(get(opts, 'requested_by'))?
\ (' as it was specified by user.'):
\ ("\n as it was requested by ".
\ join(opts.requested_by, "\n which was requested by ").'.')))
endif
if s:c.debug_activation
" activation takes place later (-> new_runtime_paths), but messages will be in order
" XXX Lengths of “as it was requested by” and “which was requested by”
" match
call vam#Log('Will activate '.name.(empty(get(opts, 'requested_by'))?
\ (' as it was specified by user.'):
\ ("\n as it was requested by ".
\ join(opts.requested_by, "\n which was requested by ").'.')))
endif
endfor
endfun
Expand Down Expand Up @@ -326,7 +350,8 @@ fun! vam#ActivateAddons(...) abort
let opts.to_be_activated = to_be_activated

for a in args[0]
let to_be_activated[a.name] = a
let to_be_activated[has_key(a, 'name') ? a.name : 'rtp:'.a.activate_this_rtp] = a
" a.name
endfor

call call('vam#ActivateRecursively', args)
Expand Down Expand Up @@ -600,7 +625,7 @@ fun! vam#SourceMissingPlugins()
call vam#SourceFiles(fs)
endfun

fun! vam#AddonInfoFile(name)
fun! vam#AddonInfoFile(pluginRoot, name)
" history:
" 1) plugin-info.txt was the first name (deprecated)
" 2) a:name-addon-info.txt was the second recommended name (maybe deprecated - no hurry)
Expand All @@ -609,15 +634,13 @@ fun! vam#AddonInfoFile(name)
" - json says all about its contents (Let's hope all browsers still render
" it in a readable way

let p = vam#PluginDirFromName(a:name)
let default = p.'/addon-info.json'
let choices = [ default , p.'/plugin-info.txt', p.'/'.a:name.'-addon-info.txt']
let choices = [ a:pluginRoot.'/addon-info.json' , a:pluginRoot.'/plugin-info.txt', a:pluginRoot.'/'.a:name.'-addon-info.txt']
for f in choices
if filereadable(f)
return f
endif
endfor
return default
return choices[0]
endfun

" looks like an error but is not. Catches users attention. Logs to :messages
Expand Down Expand Up @@ -686,7 +709,7 @@ command! -nargs=* -complete=customlist,vam#bisect#BisectCompletion VAMBisect :ca

fun! s:RunInstallHooks(plugins)
for name in a:plugins
call vam#install#RunHook('post-install', vam#AddonInfo(name), vam#install#GetRepo(name, {}), vam#PluginDirFromName(name), {})
call vam#install#RunHook('post-install', vam#ReadAddonInfo(vam#AddonInfoFile(vam#PluginDirFromName(name), name)), vam#install#GetRepo(name, {}), vam#PluginDirFromName(name), {})
endfor
endfun
command! -nargs=+ -complete=customlist,vam#install#InstalledAddonCompletion RunInstallHooks :call s:RunInstallHooks([<f-args>])
Expand Down
19 changes: 11 additions & 8 deletions autoload/vam/install.vim
Expand Up @@ -223,13 +223,13 @@ fun! vam#install#Install(toBeInstalledList, ...)

call vam#install#Checkout(pluginDir, repository)

let infoFile = vam#AddonInfoFile(name)
let infoFile = vam#AddonInfoFile(pluginDir, name)
if has_key(repository, 'addon-info') && !filereadable(infoFile)
call writefile([string(repository['addon-info'])], infoFile)
endif

" install dependencies
let info = vam#AddonInfo(name)
let info = vam#ReadAddonInfo(infoFile)

let dependencies = get(info, 'dependencies', {})

Expand Down Expand Up @@ -336,7 +336,7 @@ fun! vam#install#UpdateAddon(name)
let hook_opts={'oldVersion': oldVersion, 'newVersion': newVersion, 'sdescr': sdescr}
if r isnot# 'unknown'
if r is# 'updated'
call vam#install#RunHook('post-scms-update', vam#AddonInfo(a:name), {}, pluginDir, hook_opts)
call vam#install#RunHook('post-scms-update', vam#ReadAddonInfo(vam#AddonInfoFile(pluginDir, a:name)), {}, pluginDir, hook_opts)
endif
return r
endif
Expand Down Expand Up @@ -377,15 +377,16 @@ fun! vam#install#UpdateAddon(name)
echom "Updating plugin ".a:name." because ".(newVersion == '?' ? 'version is unknown' : 'there is a different version')

let hook_opts={'oldVersion': oldVersion, 'newVersion': newVersion}
call vam#install#RunHook('pre-update', vam#AddonInfo(a:name), repository, pluginDir, hook_opts)
call vam#install#RunHook('pre-update', vam#ReadAddonInfo(vam#AddonInfoFile(pluginDir, a:name)), repository, pluginDir, hook_opts)

" checkout new version (checkout into empty location - same as installing):
if isdirectory(pluginDir)
call vam#utils#RmFR(pluginDir)
endif
call vam#install#Checkout(pluginDir, repository)

call vam#install#RunHook('post-update', vam#AddonInfo(a:name), repository, pluginDir, hook_opts)
" addon info file name could have changed here ..
call vam#install#RunHook('post-update', vam#ReadAddonInfo(vam#AddonInfoFile(pluginDir, a:name)), repository, pluginDir, hook_opts)

return 'updated'
elseif oldVersion == newVersion
Expand Down Expand Up @@ -573,7 +574,9 @@ fun! vam#install#UninstallAddons(list)
endfun

fun! vam#install#HelpTags(name)
let d=vam#PluginRuntimePath(a:name).'/doc'
let pluginDir = vam#PluginDirFromName(a:name)
let info = vam#ReadAddonInfo(vam#AddonInfoFile(pluginDir, a:name))
let d=vam#PluginRuntimePath(pluginDir, info).'/doc'
if isdirectory(d) | exec 'helptags '.fnameescape(d) | endif
endfun

Expand Down Expand Up @@ -664,7 +667,7 @@ fun! vam#install#MergePluginFiles(plugins, skip_pattern)
endif
endfor

let runtimepaths = map(copy(a:plugins), 'vam#PluginRuntimePath(v:val)')
let runtimepaths = map(copy(a:plugins), 'vam#PluginRuntimePath(vam#PluginDirFromName(v:val), vam#ReadAddonInfo(vam#AddonInfoFile(vam#PluginDirFromName(v:val), v:val)))')

" 1)
for r in runtimepaths
Expand Down Expand Up @@ -762,7 +765,7 @@ fun! vam#install#MergePluginFiles(plugins, skip_pattern)
endfun

fun! vam#install#UnmergePluginFiles()
let path = fnamemodify(vam#PluginRuntimePath('vim-addon-manager'),':h')
let path = fnamemodify(vam#PluginRuntimePath('vim-addon-manager', {}),':h')
for merged in vam#GlobInDir(path, '{,*/}*/plugin-merged')
echo "unmerging ".merged
call rename(merged, substitute(merged,'-merged$','',''))
Expand Down

0 comments on commit 3392ede

Please sign in to comment.