Skip to content

Commit 5124c39

Browse files
author
Aleksei Matiushkin
committed
Utils.get_app_name/1 now understands the application name expressed via module attribute.
1 parent 6deb263 commit 5124c39

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

lib/sobelow/utils.ex

+17-1
Original file line numberDiff line numberDiff line change
@@ -912,10 +912,26 @@ defmodule Sobelow.Utils do
912912
ast = ast(filepath)
913913
{_, project_block} = Macro.prewalk(ast, [], &extract_project_block/2)
914914
{_, app_name} = Macro.prewalk(project_block, [], &extract_app_name/2)
915-
if is_atom(app_name), do: Atom.to_string(app_name), else: app_name
915+
binarize_app_name(app_name, ast)
916916
end
917917
end
918918

919+
defp binarize_app_name(app_name, _) when is_binary(app_name), do: app_name
920+
defp binarize_app_name(app_name, _) when is_atom(app_name), do: Atom.to_string(app_name)
921+
922+
defp binarize_app_name({:@, _, [{module_attribute, _, _}]}, ast) do
923+
ast
924+
|> Macro.prewalk([], fn
925+
{:@, _, [{^module_attribute, _, [name]}]}, [] ->
926+
{[], name}
927+
928+
ast, acc ->
929+
{ast, acc}
930+
end)
931+
|> elem(1)
932+
|> binarize_app_name(ast)
933+
end
934+
919935
defp extract_project_block({:def, _, [{:project, _, _}, [do: block]]} = ast, _) do
920936
{ast, block}
921937
end

test/fixtures/utils/mix.exs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
defmodule FooBar.Mixfile do
2+
use Mix.Project
3+
4+
@app :foo_bar
5+
def project do
6+
[
7+
app: @app
8+
]
9+
end
10+
end

test/utils_test.exs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule SobelowTest.UtilsTest do
2+
use ExUnit.Case
3+
4+
test "Utils.get_app_name/1 understands module attributes" do
5+
assert Sobelow.Utils.get_app_name("./test/fixtures/utils/mix.exs") == "foo_bar"
6+
end
7+
end

0 commit comments

Comments
 (0)