Skip to content

Commit

Permalink
Merge pull request #386 from evnu/381-fix-external_resources-for-loca…
Browse files Browse the repository at this point in the history
…l-deps

Extend rustler_mix to handle local dependencies with external_resources
  • Loading branch information
evnu authored Oct 5, 2021
2 parents b1e09e2 + 8094e86 commit cf3bff2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ See [`UPGRADE.md`](./UPGRADE.md) for additional help when upgrading to newer ver
### Fixed

- Fix codegen problem for untagged enums (#370)
- Fix handling local dependencies with `@external_resources` (#381)

## [0.22.0] - 2021-06-22

Expand Down
68 changes: 62 additions & 6 deletions rustler_mix/lib/rustler/compiler/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,78 @@ defmodule Rustler.Compiler.Config do
end

defp build(opts) do
crate = Keyword.fetch!(opts, :crate)

resources =
opts
|> Keyword.get(:path)
|> external_resources()
|> external_resources(crate)

opts = Keyword.put(opts, :external_resources, resources)

struct!(Config, opts)
end

defp external_resources(crate_path) do
"#{crate_path}/**/*"
defp external_resources(crate_path, crate) do
crate_str = to_string(crate)

metadata =
case System.cmd("cargo", ~w(metadata --format-version=1), cd: crate_path) do
{metadata, 0} ->
metadata

{_error, code} ->
System.stop(code)
end

json = Jason.decode!(metadata)

packages = json["packages"]

crate_spec = get_spec(packages, crate_str)

packages
|> gather_local_crates([crate_spec], [crate_path], MapSet.new([crate_str]))
|> Enum.flat_map(&expand_paths/1)
end

defp expand_paths(path) do
path
|> Path.join("**/*")
|> Path.wildcard()
|> Enum.reject(fn path ->
String.starts_with?(path, "#{crate_path}/target/")
end)
|> Enum.reject(&String.starts_with?(&1, "#{path}/target/"))
end

defp local_crate?(package) do
package["path"]
end

defp gather_local_crates(_, [], paths, _visited) do
paths
end

defp gather_local_crates(packages, [current_spec | rest], paths_acc, visited) do
local_deps =
current_spec["dependencies"]
|> Enum.filter(&local_crate?/1)
|> Enum.reject(fn dep -> MapSet.member?(visited, dep["name"]) end)

paths = Enum.map(local_deps, & &1["path"]) ++ paths_acc

as_specs = Enum.map(local_deps, &get_spec(packages, &1["name"]))

visited =
local_deps
|> MapSet.new(& &1["name"])
|> MapSet.union(visited)

gather_local_crates(packages, as_specs ++ rest, paths, visited)
end

defp get_spec(packages, name) do
packages
|> Enum.filter(&(&1["name"] == name))
|> List.first()
end

defp build_mode(env) when env in [:prod, :bench], do: :release
Expand Down
3 changes: 2 additions & 1 deletion rustler_mix/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ defmodule Rustler.Mixfile do
defp deps do
[
{:toml, "~> 0.5.2", runtime: false},
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false}
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
{:jason, "~> 1.2", runtime: false}
]
end

Expand Down
1 change: 1 addition & 0 deletions rustler_mix/mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"earmark": {:hex, :earmark, "1.4.4", "4821b8d05cda507189d51f2caeef370cf1e18ca5d7dfb7d31e9cafe6688106a4", [:mix], [], "hexpm", "1f93aba7340574847c0f609da787f0d79efcab51b044bb6e242cae5aca9d264d"},
"earmark_parser": {:hex, :earmark_parser, "1.4.13", "0c98163e7d04a15feb62000e1a891489feb29f3d10cb57d4f845c405852bbef8", [:mix], [], "hexpm", "d602c26af3a0af43d2f2645613f65841657ad6efc9f0e361c3b6c06b578214ba"},
"ex_doc": {:hex, :ex_doc, "0.24.2", "e4c26603830c1a2286dae45f4412a4d1980e1e89dc779fcd0181ed1d5a05c8d9", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "e134e1d9e821b8d9e4244687fb2ace58d479b67b282de5158333b0d57c6fb7da"},
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
"makeup_elixir": {:hex, :makeup_elixir, "0.15.1", "b5888c880d17d1cc3e598f05cdb5b5a91b7b17ac4eaf5f297cb697663a1094dd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "db68c173234b07ab2a07f645a5acdc117b9f99d69ebf521821d89690ae6c6ec8"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
Expand Down

0 comments on commit cf3bff2

Please sign in to comment.