-
-
Notifications
You must be signed in to change notification settings - Fork 24
Add Igniter Helper Functions #569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maennchen
wants to merge
1
commit into
nerves-networking:main
Choose a base branch
from
maennchen:igniter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+170
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.