Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
blob: 1e366967bc177812d92da445ba250fea243b524d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
defmodule Mix.Tasks.Pandoc.Install do
  @moduledoc """
  Installs pandoc under `_build`.

  ```bash
  $ mix pandoc.install
  $ mix pandoc.install --if-missing
  ```

  By default, it installs #{Pandoc.latest_version()} but you can configure it
  in your config files, such as:

      config :pandoc, :version, "#{Pandoc.latest_version()}"

  ## Options

    * `--runtime-config` - load the runtime configuration before executing
      command

    * `--if-missing` - install only if the given version does not exist
  """

  @shortdoc "Installs pandoc under _build"
  @compile {:no_warn_undefined, Mix}

  use Mix.Task

  @impl true
  def run(args) do
    valid_options = [runtime_config: :boolean, if_missing: :boolean]

    {opts, base_url} =
      case OptionParser.parse_head!(args, strict: valid_options) do
        {opts, []} ->
          {opts, Pandoc.default_base_url()}

        {opts, [base_url]} ->
          {opts, base_url}

        {_, _} ->
          Mix.raise("""
          Invalid arguments to pandoc.install, expected one of:

              mix pandoc.install
              mix pandoc.install 'https://github.com/jgm/pandoc/releases/download/$version/pandoc-$version-$target.tar.gz'
              mix pandoc.install --runtime-config
              mix pandoc.install --if-missing
          """)
      end

    if opts[:runtime_config], do: Mix.Task.run("app.config")

    if opts[:if_missing] && latest_version?() do
      :ok
    else
      if function_exported?(Mix, :ensure_application!, 1) do
        Mix.ensure_application!(:inets)
        Mix.ensure_application!(:ssl)
      end

      Mix.Task.run("loadpaths")
      Pandoc.install(base_url)
    end
  end

  defp latest_version?() do
    version = Pandoc.configured_version()
    match?({:ok, ^version}, Pandoc.bin_version())
  end
end