mirror of
https://github.com/amix/vimrc
synced 2025-06-16 09:35:01 +08:00
Add support for Elixir.
This commit is contained in:
20
sources_non_forked/vim-elixir/autoload/db/adapter/ecto.vim
Normal file
20
sources_non_forked/vim-elixir/autoload/db/adapter/ecto.vim
Normal file
@ -0,0 +1,20 @@
|
||||
let s:path = expand('<sfile>:h')
|
||||
let s:cmd = join(['mix', 'run', '--no-start', '--no-compile', shellescape(s:path.'/get_repos.exs')])
|
||||
|
||||
function! s:repo_list() abort
|
||||
return map(systemlist(s:cmd), 'split(v:val)')
|
||||
endfunction
|
||||
|
||||
function! db#adapter#ecto#canonicalize(url) abort
|
||||
for l:item in s:repo_list()
|
||||
let l:name = get(l:item, 0)
|
||||
let l:url = get(l:item, 1)
|
||||
if !empty(l:name) && 'ecto:'.l:name ==# a:url
|
||||
return l:url
|
||||
endif
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
function! db#adapter#ecto#complete_opaque(url) abort
|
||||
return map(s:repo_list(), 'v:val[0]')
|
||||
endfunction
|
@ -0,0 +1,66 @@
|
||||
defmodule LoadRepos do
|
||||
defp load_apps do
|
||||
:code.get_path()
|
||||
|> Enum.flat_map(fn app_dir ->
|
||||
Path.join(app_dir, "*.app") |> Path.wildcard()
|
||||
end)
|
||||
|> Enum.map(fn app_file ->
|
||||
app_file |> Path.basename() |> Path.rootname(".app") |> String.to_atom()
|
||||
end)
|
||||
|> Enum.map(&Application.load/1)
|
||||
end
|
||||
|
||||
defp configs do
|
||||
for {app, _, _} <- Application.loaded_applications(),
|
||||
repos = Application.get_env(app, :ecto_repos),
|
||||
is_list(repos) and repos != [],
|
||||
repo <- repos,
|
||||
do: {repo, Map.new(repo.config())}
|
||||
end
|
||||
|
||||
defp config_to_url(_, %{url: url}), do: url
|
||||
|
||||
defp config_to_url(repo, config) do
|
||||
host =
|
||||
case Map.fetch(config, :socket_dir) do
|
||||
:error -> Map.fetch!(config, :hostname)
|
||||
{:ok, socket_dir} -> socket_dir
|
||||
end
|
||||
username = Map.get(config, :username)
|
||||
password = Map.get(config, :password)
|
||||
database = Map.get(config, :database)
|
||||
parameters = Map.get(config, :parameters, [])
|
||||
|
||||
%URI{
|
||||
scheme: adapter_to_string(repo.__adapter__),
|
||||
host: "",
|
||||
path: Path.join("/", database),
|
||||
query: encode_options([host: host, user: username, password: password] ++ parameters)
|
||||
}
|
||||
|> URI.to_string()
|
||||
end
|
||||
|
||||
defp adapter_to_string(Ecto.Adapters.Postgres), do: "postgres"
|
||||
defp adapter_to_string(Ecto.Adapters.MySQL), do: "mysql"
|
||||
defp adapter_to_string(mod), do: raise("Unknown adapter #{inspect(mod)}")
|
||||
|
||||
defp encode_options(opts) do
|
||||
cleaned =
|
||||
for {k, v} <- opts, not is_nil(v), do: {k, v}
|
||||
|
||||
URI.encode_query(cleaned)
|
||||
end
|
||||
|
||||
def main do
|
||||
load_apps()
|
||||
|
||||
configs()
|
||||
|> Enum.map(fn {repo, config} ->
|
||||
[inspect(repo), ?\s, config_to_url(repo, config)]
|
||||
end)
|
||||
|> Enum.intersperse(?\n)
|
||||
|> IO.puts()
|
||||
end
|
||||
end
|
||||
|
||||
LoadRepos.main()
|
Reference in New Issue
Block a user