|
| 1 | +defmodule Mix.Tasks.Nvim.Install do |
| 2 | + use Mix.Task |
| 3 | + |
| 4 | + import Mix.Generator |
| 5 | + @shortdoc "Installs the host to provided nvim config path." |
| 6 | + |
| 7 | + def run(argv) do |
| 8 | + {_opts, argv} = OptionParser.parse!(argv) |
| 9 | + |
| 10 | + case argv do |
| 11 | + [] -> |
| 12 | + Mix.raise "Expected NVIM_CONFIG_PATH to be given, please use \"mix nvim.install NVIM_CONFIG_PATH\"" |
| 13 | + [nvim_config_path | _] -> |
| 14 | + create_file Path.join(nvim_config_path, "plugin/elixir_host.vim"), elixir_host_plugin_vim_text, force: true |
| 15 | + |
| 16 | + remote_plugin_path = Path.join(nvim_config_path, "rplugin/elixir") |
| 17 | + |
| 18 | + create_directory Path.join(remote_plugin_path, "scripts") |
| 19 | + create_directory Path.join(remote_plugin_path, "apps") |
| 20 | + create_file Path.join(remote_plugin_path, "mix.exs"), apps_mixfile_text, force: true |
| 21 | + create_file Path.join([remote_plugin_path, "config", "config.exs"]), apps_config_text, force: true |
| 22 | + |
| 23 | + host_path = Path.join([remote_plugin_path, "apps","host"]) |
| 24 | + |
| 25 | + create_file Path.join(host_path, "mix.exs"), host_mixfile_text, force: true |
| 26 | + create_file Path.join([host_path, "config", "config.exs"]), host_config_text, force: true |
| 27 | + |
| 28 | + print_successful_info |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + defp print_successful_info do |
| 33 | + Mix.shell.info [:green, """ |
| 34 | +
|
| 35 | + Elixir host succesfully installed. |
| 36 | + """] |
| 37 | + end |
| 38 | + |
| 39 | + embed_text :host_mixfile, ~s""" |
| 40 | + defmodule Host.Mixfile do |
| 41 | + use Mix.Project |
| 42 | +
|
| 43 | + def project do |
| 44 | + [app: :host, |
| 45 | + version: "#{Mix.Project.config[:version]}", |
| 46 | + build_path: "../../_build", |
| 47 | + config_path: "../../config/config.exs", |
| 48 | + deps_path: "../../deps", |
| 49 | + lockfile: "../../mix.lock", |
| 50 | + elixir: "~> 1.3", |
| 51 | + deps: deps, |
| 52 | + aliases: aliases, |
| 53 | + escript: escript] |
| 54 | + end |
| 55 | +
|
| 56 | + def application do |
| 57 | + [applications: [:logger, :nvim], env: [plugin_module: NVim.Host.Plugin]] |
| 58 | + end |
| 59 | +
|
| 60 | + def escript do |
| 61 | + [main_module: NVim.Host, emu_args: "-noinput"] |
| 62 | + end |
| 63 | +
|
| 64 | + defp deps do |
| 65 | + [{:nvim, "#{Mix.Project.config[:version]}"}] |
| 66 | + end |
| 67 | +
|
| 68 | + defp aliases do |
| 69 | + ["nvim.build_host": ["deps.get", "nvim.build_host"]] |
| 70 | + end |
| 71 | + end |
| 72 | + """ |
| 73 | + embed_text :apps_mixfile, """ |
| 74 | + defmodule Elixir.Mixfile do |
| 75 | + use Mix.Project |
| 76 | +
|
| 77 | + def project do |
| 78 | + [apps_path: "apps", |
| 79 | + deps: []] |
| 80 | + end |
| 81 | + end |
| 82 | + """ |
| 83 | + |
| 84 | + embed_text :apps_config, """ |
| 85 | + use Mix.Config |
| 86 | + import_config "../apps/*/config/config.exs" |
| 87 | + """ |
| 88 | + |
| 89 | + embed_text :host_config, ~S""" |
| 90 | + use Mix.Config |
| 91 | +
|
| 92 | + config :logger, |
| 93 | + backends: [{LoggerFileBackend, :error_log}], |
| 94 | + level: :error |
| 95 | +
|
| 96 | + config :logger, :error_log, |
| 97 | + path: Path.expand("#{__DIR__}/../neovim_elixir_host.log"), |
| 98 | + level: :error |
| 99 | + """ |
| 100 | + |
| 101 | + embed_text :elixir_host_plugin_vim, """ |
| 102 | + let s:nvim_path = expand('<sfile>:p:h:h') |
| 103 | + let s:xdg_home_path = expand('<sfile>:p:h:h:h') |
| 104 | +
|
| 105 | + function! s:RequireElixirHost(host) |
| 106 | + try |
| 107 | + let channel_id = rpcstart(s:nvim_path . '/rplugin/elixir/apps/host/host',[]) |
| 108 | + if rpcrequest(channel_id, 'poll') == 'ok' |
| 109 | + return channel_id |
| 110 | + endif |
| 111 | + catch |
| 112 | + endtry |
| 113 | + throw 'Failed to load elixir host.' . expand('<sfile>') . |
| 114 | + \ ' More information can be found in elixir host log file.' |
| 115 | + endfunction |
| 116 | +
|
| 117 | + call remote#host#Register('elixir', '{scripts/*.exs,apps/*}', function('s:RequireElixirHost')) |
| 118 | +
|
| 119 | + function! UpdateElixirPlugins() |
| 120 | + execute '!cd ' . s:nvim_path . '/rplugin/elixir/apps/host && MIX_ENV=prod mix do deps.get, nvim.build_host --xdg-home-path ' . s:xdg_home_path . ' --vim-rc-path ' . s:nvim_path . '/init.vim' |
| 121 | + endfunction |
| 122 | + command! UpdateElixirPlugins call UpdateElixirPlugins() |
| 123 | + """ |
| 124 | +end |
0 commit comments