Packages
evision
0.1.26-rc2
1.0.1-rc.0
1.0.0
1.0.0-rc.0
0.2.17
0.2.17-rc2
0.2.17-rc1
0.2.16
0.2.16-pre.2
0.2.16-pre
0.2.15
0.2.14
0.2.13
0.2.12
0.2.11
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.2-rc2
0.2.1
0.2.0
0.1.39
0.1.38
0.1.37
0.1.36
0.1.35
0.1.34
0.1.33
0.1.32
retired
0.1.31
0.1.30
0.1.29
0.1.28
0.1.27
0.1.26
0.1.26-rc3
0.1.26-rc2
0.1.26-rc1
0.1.26-rc0
0.1.25
0.1.24
0.1.23
0.1.22
0.1.21
0.1.20
0.1.19
0.1.18
0.1.17
0.1.16
0.1.15
0.1.14
0.1.13
0.1.12
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
OpenCV-Erlang/Elixir binding.
Current section
Files
Jump to
Current section
Files
lib/mix/tasks/evision.fetch_precompiled.ex
defmodule Mix.Tasks.Evision.Fetch do
@shortdoc "Fetch precompiled NIFs and build the checksums"
@moduledoc """
A task responsible for downloading the precompiled NIFs for a given module.
This task must only be used by package creators who want to ship the
precompiled NIFs. The goal is to download the precompiled packages and
generate a checksum to check-in alongside the project in the the Hex repository.
This is done by passing the `--all` flag.
You can also use the `--only-local` flag to download only the precompiled
package for use during development.
You can use the `--ignore-unavailable` flag to ignore any NIFs that are not available.
This is useful when you are developing a new NIF that does not support all platforms.
This task also accept the `--print` flag to print the checksums.
"""
use Mix.Task
alias Mix.Tasks.Compile.EvisionPrecompiled, as: Precompile
require Logger
@switches [
all: :boolean,
only_local: :boolean,
print: :boolean,
ignore_unavailable: :boolean
]
@impl true
def run([]) do
raise "the module name and a flag is expected. Use \"--all\" or \"--only-local\" flags"
end
@impl true
def run(flags) when is_list(flags) do
{options, _args, _invalid} = OptionParser.parse(flags, strict: @switches)
nif_version = Precompile.get_nif_version()
urls =
cond do
Keyword.get(options, :all) ->
Precompile.available_nif_urls(nif_version)
Keyword.get(options, :only_local) ->
[Precompile.current_target_nif_url(nif_version)]
true ->
raise "you need to specify either \"--all\" or \"--only-local\" flags"
end
result =
Task.async_stream(urls, fn url ->
filename = basename_from_url(url)
cache_to = Path.join([Precompile.cache_dir(), filename])
{:ok, algo, checksum} = Precompile.download!(url, cache_to, true)
Logger.info("downloaded: url=#{url}, file=#{cache_to}, checksum[#{algo}]=#{checksum}")
%{
url: url,
path: filename,
checksum: checksum,
checksum_algo: String.to_atom(algo)
}
end,
timeout: :infinity
)
result = Enum.map(result, fn {:ok, r} -> r end)
if Keyword.get(options, :print) do
result
|> Enum.map(fn map ->
{Path.basename(Map.fetch!(map, :path)), Map.fetch!(map, :checksum)}
end)
|> Enum.sort()
|> Enum.map_join("\n", fn {file, checksum} -> "#{checksum} #{file}" end)
|> IO.puts()
end
write_checksum!(result)
end
defp write_checksum!(result, app \\ Mix.Project.config()[:app]) do
{elixir_file, erlang_file} = Precompile.checksum_file(app)
pairs =
for %{path: path, checksum: checksum, checksum_algo: algo} <- result, into: %{} do
basename = Path.basename(path)
checksum = "#{algo}:#{checksum}"
{basename, checksum}
end
lines =
for {filename, checksum} <- Enum.sort(pairs) do
~s( "#{filename}" => #{inspect(checksum, limit: :infinity)},\n)
end
File.write!(elixir_file, ["%{\n", lines, "}\n"])
lines =
for {filename, checksum} <- Enum.sort(pairs) do
" \"#{filename}\" => #{inspect(checksum, limit: :infinity)}"
end
lines = Enum.join(lines, ",\n")
File.write!(erlang_file, [
"""
-module(checksum).
-export([checksum/0]).
checksum() ->
\#{
""", lines, "\n }.\n"])
end
defp basename_from_url(url) do
uri = URI.parse(url)
uri.path
|> String.split("/")
|> List.last()
end
end