| 1 |
|
defmodule Compose do |
| 2 |
|
import Enum, only: [map: 2, reduce: 3, reject: 2, uniq: 1, join: 2] |
| 3 |
|
import Map, only: [put: 3, merge: 2, drop: 2, get: 3] |
| 4 |
|
|
| 5 |
|
def compose([main | rest]) do |
| 6 |
189 |
active = [main | reject(rest, &(&1[:active] == false))] |
| 7 |
189 |
active |> headers() |> content(active) |
| 8 |
|
end |
| 9 |
|
|
| 10 |
|
defp headers(configs) do |
| 11 |
|
configs |
| 12 |
|
|> map(&strip/1) |
| 13 |
189 |
|> reduce(%{}, &combine/2) |
| 14 |
|
end |
| 15 |
|
|
| 16 |
|
defp combine(config, acc) do |
| 17 |
264 |
tools = fuse(acc, config) |
| 18 |
264 |
merge(acc, config) |> put(:tools, tools) |
| 19 |
|
end |
| 20 |
|
|
| 21 |
|
defp fuse(acc, config) do |
| 22 |
264 |
(tools(acc) ++ tools(config)) |> uniq() |
| 23 |
|
end |
| 24 |
|
|
| 25 |
|
defp tools(config) do |
| 26 |
528 |
get(config, :tools, []) |
| 27 |
|
end |
| 28 |
|
|
| 29 |
|
defp content(merged, configs) do |
| 30 |
189 |
text = configs |> map(&extract/1) |> join("\n\n") |
| 31 |
189 |
put(merged, :content, text) |
| 32 |
|
end |
| 33 |
|
|
| 34 |
264 |
defp strip(config), do: drop(config, [:content]) |
| 35 |
264 |
defp extract(config), do: get(config, :content, "") |
| 36 |
|
end |