Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions lib/vintage_net/generator.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# SPDX-FileCopyrightText: 2025 Jonatan Männchen
#
# SPDX-License-Identifier: Apache-2.0
#
with {:module, Igniter} <- Code.ensure_loaded(Igniter) do
defmodule VintageNet.Generator do
@moduledoc """
Igniter Helpers for VintageNet.
"""

alias Igniter.Code.Common
alias Igniter.Code.List
alias Igniter.Code.Tuple
alias Igniter.Project.Config

@spec add_regulatory_domain(igniter :: Igniter.t(), regulatory_domain :: String.t()) ::
Igniter.t()
def add_regulatory_domain(igniter, regulatory_domain) do
Config.configure_new(
igniter,
"target.exs",
:vintage_net,
[:regulatory_domain],
regulatory_domain
)
end

@spec configure_interface(
igniter :: Igniter.t(),
name :: String.t(),
type :: module(),
config :: Keyword.t(),
update :: (Sourceror.Zipper.t() -> {:ok, Sourceror.Zipper.t()} | :error)
) :: Igniter.t()
def configure_interface(igniter, name, type, config, update \\ &{:ok, &1}) do
config =
config
|> Keyword.put(:type, type)
|> Enum.sort_by(fn
# Put type first
{:type, value} -> {1, :type, value}
{key, value} -> {2, key, value}
end)
|> then(&{:%{}, [], &1})

new_entry_ast = {name, config}

Config.configure(
igniter,
"target.exs",
:vintage_net,
[:interfaces],
{:code, [new_entry_ast]},
updater: fn zipper ->
zipper
|> move_to_interface_config(name)
|> case do
{:ok, zipper} -> Common.within(zipper, update)
:error -> List.append_to_list(zipper, new_entry_ast)
end
end
)
end

@spec move_to_interface_config(Sourceror.Zipper.t(), name :: String.t()) ::
{:ok, Sourceror.Zipper.t()} | :error
def move_to_interface_config(zipper, name) do
zipper
|> List.move_to_list_item(fn zipper ->
with true <- Tuple.tuple?(zipper),
{:ok, first} <- Tuple.tuple_elem(zipper, 0) do
Common.nodes_equal?(first, name)
else
_ ->
false
end
end)
|> case do
{:ok, zipper} ->
Tuple.tuple_elem(zipper, 1)

:error ->
:error
end
end
end
end
2 changes: 2 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ defmodule VintageNet.MixProject do
{:beam_notify, "~> 1.0 or ~> 0.2.0"},
{:muontrap, "~> 1.0 or ~> 0.5.1 or ~> 0.6.0"},
{:property_table, "~> 0.2.0 or ~> 0.3.0"},
# Bootstrap dependencies
{:igniter, "~> 0.6.4", optional: true},
# Build dependencies
{:credo, "~> 1.2", only: :test, runtime: false},
{:credo_binary_patterns, "~> 0.2.2", only: :test, runtime: false},
Expand Down
15 changes: 15 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,26 @@
"erlex": {:hex, :erlex, "0.2.7", "810e8725f96ab74d17aac676e748627a07bc87eb950d2b83acd29dc047a30595", [:mix], [], "hexpm", "3ed95f79d1a844c3f6bf0cea61e0d5612a42ce56da9c03f01df538685365efb0"},
"ex_doc": {:hex, :ex_doc, "0.38.1", "bae0a0bd5b5925b1caef4987e3470902d072d03347114ffe03a55dbe206dd4c2", [:mix], [{:earmark_parser, "~> 1.4.44", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "754636236d191b895e1e4de2ebb504c057fe1995fdfdd92e9d75c4b05633008b"},
"file_system": {:hex, :file_system, "1.1.0", "08d232062284546c6c34426997dd7ef6ec9f8bbd090eb91780283c9016840e8f", [:mix], [], "hexpm", "bfcf81244f416871f2a2e15c1b515287faa5db9c6bcf290222206d120b3d43f6"},
"finch": {:hex, :finch, "0.19.0", "c644641491ea854fc5c1bbaef36bfc764e3f08e7185e1f084e35e0672241b76d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "fc5324ce209125d1e2fa0fcd2634601c52a787aff1cd33ee833664a5af4ea2b6"},
"glob_ex": {:hex, :glob_ex, "0.1.11", "cb50d3f1ef53f6ca04d6252c7fde09fd7a1cf63387714fe96f340a1349e62c93", [:mix], [], "hexpm", "342729363056e3145e61766b416769984c329e4378f1d558b63e341020525de4"},
"hpax": {:hex, :hpax, "1.0.3", "ed67ef51ad4df91e75cc6a1494f851850c0bd98ebc0be6e81b026e765ee535aa", [:mix], [], "hexpm", "8eab6e1cfa8d5918c2ce4ba43588e894af35dbd8e91e6e55c817bca5847df34a"},
"igniter": {:hex, :igniter, "0.6.4", "81b7442be9ae0b9107b715144f3633f72788644d13ffded612cb508861b2c726", [:mix], [{:glob_ex, "~> 0.1.7", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:owl, "~> 0.11", [hex: :owl, repo: "hexpm", optional: false]}, {:phx_new, "~> 1.7", [hex: :phx_new, repo: "hexpm", optional: true]}, {:req, "~> 0.5", [hex: :req, repo: "hexpm", optional: false]}, {:rewrite, ">= 1.1.1 and < 2.0.0-0", [hex: :rewrite, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.4", [hex: :sourceror, repo: "hexpm", optional: false]}, {:spitfire, ">= 0.1.3 and < 1.0.0-0", [hex: :spitfire, repo: "hexpm", optional: false]}], "hexpm", "e999fc4b883e5eacc07ca2823273880ab30108c7e6a0d1542a3c8e00605aef46"},
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
"makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"},
"makeup_elixir": {:hex, :makeup_elixir, "1.0.1", "e928a4f984e795e41e3abd27bfc09f51db16ab8ba1aebdba2b3a575437efafc2", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "7284900d412a3e5cfd97fdaed4f5ed389b8f2b4cb49efc0eb3bd10e2febf9507"},
"makeup_erlang": {:hex, :makeup_erlang, "1.0.2", "03e1804074b3aa64d5fad7aa64601ed0fb395337b982d9bcf04029d68d51b6a7", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "af33ff7ef368d5893e4a267933e7744e46ce3cf1f61e2dccf53a111ed3aa3727"},
"mime": {:hex, :mime, "2.0.7", "b8d739037be7cd402aee1ba0306edfdef982687ee7e9859bee6198c1e7e2f128", [:mix], [], "hexpm", "6171188e399ee16023ffc5b76ce445eb6d9672e2e241d2df6050f3c771e80ccd"},
"mint": {:hex, :mint, "1.7.1", "113fdb2b2f3b59e47c7955971854641c61f378549d73e829e1768de90fc1abf1", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "fceba0a4d0f24301ddee3024ae116df1c3f4bb7a563a731f45fdfeb9d39a231b"},
"muontrap": {:hex, :muontrap, "1.6.1", "4a81a159f64e4c7bf01162a7863559d634bc48929218690ada309a9a98a9ac22", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "8ad31072402bebed3f554c9a463aa272c6dd964168c9cb81385f8711f068ed47"},
"nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.2", "8efba0122db06df95bfaa78f791344a89352ba04baedd3849593bfce4d0dc1c6", [:mix], [], "hexpm", "4b21398942dda052b403bbe1da991ccd03a053668d147d53fb8c4e0efe09c973"},
"nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"},
"owl": {:hex, :owl, "0.12.2", "65906b525e5c3ef51bab6cba7687152be017aebe1da077bb719a5ee9f7e60762", [:mix], [{:ucwidth, "~> 0.2", [hex: :ucwidth, repo: "hexpm", optional: true]}], "hexpm", "6398efa9e1fea70a04d24231e10dcd66c1ac1aa2da418d20ef5357ec61de2880"},
"property_table": {:hex, :property_table, "0.3.1", "f71381dea834f9c62eff043c626e7af0ef5697258cb961040927e086c7ecb9b6", [:mix], [], "hexpm", "3ebc06667d3a95101a7dba5e9db800395bd5de6b8debbc5a064f78bb1ec5668b"},
"req": {:hex, :req, "0.5.10", "a3a063eab8b7510785a467f03d30a8d95f66f5c3d9495be3474b61459c54376c", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "8a604815743f8a2d3b5de0659fa3137fa4b1cffd636ecb69b30b2b9b2c2559be"},
"rewrite": {:hex, :rewrite, "1.1.2", "f5a5d10f5fed1491a6ff48e078d4585882695962ccc9e6c779bae025d1f92eda", [:mix], [{:glob_ex, "~> 0.1", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.0", [hex: :sourceror, repo: "hexpm", optional: false]}, {:text_diff, "~> 0.1", [hex: :text_diff, repo: "hexpm", optional: false]}], "hexpm", "7f8b94b1e3528d0a47b3e8b7bfeca559d2948a65fa7418a9ad7d7712703d39d4"},
"sourceror": {:hex, :sourceror, "1.10.0", "38397dedbbc286966ec48c7af13e228b171332be1ad731974438c77791945ce9", [:mix], [], "hexpm", "29dbdfc92e04569c9d8e6efdc422fc1d815f4bd0055dc7c51b8800fb75c4b3f1"},
"spitfire": {:hex, :spitfire, "0.2.0", "0de1f519a23f65bde40d316adad53c07a9563f25cc68915d639d8a509a0aad8a", [:mix], [], "hexpm", "743daaee2d81a0d8095431729f478ce49b47ea8943c7d770de86704975cb7775"},
"telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"},
"text_diff": {:hex, :text_diff, "0.1.0", "1caf3175e11a53a9a139bc9339bd607c47b9e376b073d4571c031913317fecaa", [:mix], [], "hexpm", "d1ffaaecab338e49357b6daa82e435f877e0649041ace7755583a0ea3362dbd7"},
}
66 changes: 66 additions & 0 deletions test/vintage_net/generator_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# SPDX-FileCopyrightText: 2025 Jonatan Männchen
#
# SPDX-License-Identifier: Apache-2.0
#
defmodule VintageNet.GeneratorTest do
use ExUnit.Case, async: true

import Igniter.Test

alias Igniter.Code.Common, as: CodeCommon
alias Igniter.Code.Map, as: CodeMap
alias VintageNet.Generator

doctest Generator

describe inspect(&Generator.add_regulatory_domain/2) do
test "adds regulatory domain to the config" do
assert {:ok, igniter, _} =
test_project()
|> Generator.add_regulatory_domain("US")
|> assert_creates("config/target.exs", """
import Config
config :vintage_net, regulatory_domain: "US"
""")
|> apply_igniter()

igniter
|> Generator.add_regulatory_domain("US")
|> assert_unchanged()
end
end

describe inspect(&Generator.configure_interface/5) do
test "adds interface to the config" do
# Increments num in config
update = fn zipper ->
CodeMap.set_map_key(zipper, :num, 1, fn zipper ->
case CodeCommon.expand_literal(zipper) do
{:ok, num} ->
{:ok, CodeCommon.replace_code(zipper, num + 1)}

:error ->
:error
end
end)
end

assert {:ok, igniter, _} =
test_project()
|> Generator.configure_interface("wlan0", VintageNetWiFi, [num: 1], update)
|> assert_creates("config/target.exs", """
import Config
config :vintage_net, interfaces: [{"wlan0", %{type: VintageNetWiFi, num: 1}}]
""")
|> apply_igniter()

igniter
|> Generator.configure_interface("wlan0", VintageNetWiFi, [num: 1], update)
|> assert_has_patch("config/target.exs", """
1 1 |import Config
2 - |config :vintage_net, interfaces: [{"wlan0", %{type: VintageNetWiFi, num: 1}}]
2 + |config :vintage_net, interfaces: [{"wlan0", %{type: VintageNetWiFi, num: 2}}]
""")
end
end
end