Skip to content

Commit e6dd4f4

Browse files
committed
forma
1 parent 78bc232 commit e6dd4f4

File tree

5 files changed

+171
-64
lines changed

5 files changed

+171
-64
lines changed

lib/code_comparison/languages.ex

Lines changed: 74 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,50 @@ defmodule CodeComparison.Languages do
1010
@spec get_languages_by_topic(String.t()) :: list(%Language{})
1111
def get_languages_by_topic(topic) do
1212
topic_dir = Path.join(base_topics_path(), topic)
13+
1314
case File.ls(topic_dir) do
1415
{:ok, files} ->
1516
files
1617
|> Enum.map(&(String.split(&1, ".") |> List.first()))
17-
|> Enum.map(&language_build(&1, topic)) # language_build handles its own file reads using new path logic
18+
# language_build handles its own file reads using new path logic
19+
|> Enum.map(&language_build(&1, topic))
1820
|> Enum.sort_by(& &1.name)
21+
1922
{:error, reason} ->
20-
IO.inspect("Failed to list languages for topic '#{topic}' at '#{topic_dir}': #{inspect(reason)}", label: "Languages Error")
23+
IO.inspect(
24+
"Failed to list languages for topic '#{topic}' at '#{topic_dir}': #{inspect(reason)}",
25+
label: "Languages Error"
26+
)
27+
2128
[]
2229
end
2330
end
2431

2532
@spec get_language_code(String.t(), String.t()) :: String.t()
2633
def get_language_code(language_name, topic) do
27-
filename = get_filename(language_name, topic) # This will use the updated get_filename
34+
# This will use the updated get_filename
35+
filename = get_filename(language_name, topic)
2836

2937
if filename == nil do
30-
IO.inspect("Filename not found for language '#{language_name}' in topic '#{topic}' when getting code.", label: "Languages Error")
38+
IO.inspect(
39+
"Filename not found for language '#{language_name}' in topic '#{topic}' when getting code.",
40+
label: "Languages Error"
41+
)
42+
3143
""
3244
else
3345
file_path = Path.join([base_topics_path(), topic, filename])
46+
3447
case File.read(file_path) do
35-
{:ok, code} -> code
48+
{:ok, code} ->
49+
code
50+
3651
{:error, reason} ->
37-
IO.inspect("Failed to read language code for '#{language_name}' from '#{file_path}': #{inspect(reason)}", label: "Languages Error")
52+
IO.inspect(
53+
"Failed to read language code for '#{language_name}' from '#{file_path}': #{inspect(reason)}",
54+
label: "Languages Error"
55+
)
56+
3857
""
3958
end
4059
end
@@ -43,20 +62,32 @@ defmodule CodeComparison.Languages do
4362
@spec get_filename(String.t(), String.t()) :: String.t() | nil
4463
defp get_filename(language_name, topic) do
4564
topic_dir = Path.join(base_topics_path(), topic)
65+
4666
case File.ls(topic_dir) do
4767
{:ok, files} ->
4868
Enum.find(files, &(String.split(&1, ".") |> List.first() == language_name))
69+
4970
{:error, reason} ->
50-
IO.inspect("Failed to list files in '#{topic_dir}' to find filename for '#{language_name}': #{inspect(reason)}", label: "Languages Error")
71+
IO.inspect(
72+
"Failed to list files in '#{topic_dir}' to find filename for '#{language_name}': #{inspect(reason)}",
73+
label: "Languages Error"
74+
)
75+
5176
nil
5277
end
5378
end
5479

5580
@spec get_language(list(%Language{}), String.t()) :: %Language{}
5681
def get_language(topic_languages, current_language_name) do
5782
if topic_languages == [] do
58-
%Language{ # Using alias for brevity
59-
name: "", code: "", topic: "", commiter_name: "", commiter_url: "", path: ""
83+
# Using alias for brevity
84+
%Language{
85+
name: "",
86+
code: "",
87+
topic: "",
88+
commiter_name: "",
89+
commiter_url: "",
90+
path: ""
6091
}
6192
else
6293
# The topic should ideally be consistent for all languages in topic_languages.
@@ -67,14 +98,17 @@ defmodule CodeComparison.Languages do
6798
found_language_struct = Enum.find(topic_languages, &(&1.name == current_language_name))
6899

69100
cond do
70-
found_language_struct -> # Language found in the pre-loaded list
101+
# Language found in the pre-loaded list
102+
found_language_struct ->
71103
found_language_struct |> put_commit_values()
72-
true -> # Language not found by name, or current_language_name is empty. Default to first.
73-
# This path also handles if current_language_name was for a language not in this topic.
74-
# The original code's `Enum.member?` check followed by `language_build` or `List.first`
75-
# had a subtle difference. `language_build` would reload the code.
76-
# Here, we assume `topic_languages` are complete structs.
77-
# If `current_language_name` isn't in the list, we take the first.
104+
105+
# Language not found by name, or current_language_name is empty. Default to first.
106+
true ->
107+
# This path also handles if current_language_name was for a language not in this topic.
108+
# The original code's `Enum.member?` check followed by `language_build` or `List.first`
109+
# had a subtle difference. `language_build` would reload the code.
110+
# Here, we assume `topic_languages` are complete structs.
111+
# If `current_language_name` isn't in the list, we take the first.
78112
topic_languages |> List.first() |> put_commit_values()
79113
end
80114
end
@@ -84,7 +118,8 @@ defmodule CodeComparison.Languages do
84118
def get_language_by_topic(topic, current_language_name) do
85119
# This function, as per existing code, rebuilds the language struct.
86120
# It's used in HomeLive mount and language change event.
87-
language_build(current_language_name, topic) # This now uses new path logic internally
121+
# This now uses new path logic internally
122+
language_build(current_language_name, topic)
88123
|> put_commit_values()
89124
end
90125

@@ -96,15 +131,22 @@ defmodule CodeComparison.Languages do
96131
if filename do
97132
"topics/#{topic}/#{filename}"
98133
else
99-
IO.inspect("Filename is nil for language '#{language_name}' in topic '#{topic}' during language_build. Path will be empty.", label: "Languages Warning")
100-
"" # Empty path if filename couldn't be determined
134+
IO.inspect(
135+
"Filename is nil for language '#{language_name}' in topic '#{topic}' during language_build. Path will be empty.",
136+
label: "Languages Warning"
137+
)
138+
139+
# Empty path if filename couldn't be determined
140+
""
101141
end
102142

103143
Language.build(%{
104144
name: language_name,
105-
code: get_language_code(language_name, topic), # Uses new path logic and error handling
145+
# Uses new path logic and error handling
146+
code: get_language_code(language_name, topic),
106147
topic: topic,
107-
path: String.replace(github_path, " ", "%20") # Path for GitHub API
148+
# Path for GitHub API
149+
path: String.replace(github_path, " ", "%20")
108150
})
109151
end
110152

@@ -114,20 +156,25 @@ defmodule CodeComparison.Languages do
114156
# Ensure path is not empty before calling GitHub API
115157
if language.path && language.path != "" do
116158
case Application.get_env(:code_comparison, :github)[:token] do
117-
nil -> put_empty_commit_values(language)
118-
_ -> # Pass the actual language struct, not just path
119-
put_commit_values_with_api(language, Github.get_last_commit(language.path))
159+
nil ->
160+
put_empty_commit_values(language)
161+
162+
# Pass the actual language struct, not just path
163+
_ ->
164+
put_commit_values_with_api(language, Github.get_last_commit(language.path))
120165
end
121166
else
122-
put_empty_commit_values(language) # Path is empty, skip GitHub call
167+
# Path is empty, skip GitHub call
168+
put_empty_commit_values(language)
123169
end
124170
end
125171

126172
# Renamed to avoid clash and clarify it's the one calling API
127173
defp put_commit_values_with_api(language, {:ok, body}) do
128174
# Assuming body structure is a list of commits
129-
first_commit = List.first(body) # Safe navigation for author needed
130-
175+
# Safe navigation for author needed
176+
first_commit = List.first(body)
177+
131178
author_name = first_commit |> get_in(["author", "login"])
132179
author_url = first_commit |> get_in(["author", "html_url"])
133180

lib/code_comparison/topics.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ defmodule CodeComparison.Topics do
66
# topics_dir is expected to be at the root of the application.
77
# Application.app_dir(:code_comparison) should point to the app's root directory (e.g., /app or /code_comparison).
88
topics_dir = Path.join(Application.app_dir(:code_comparison), "topics")
9-
9+
1010
case File.ls(topics_dir) do
11-
{:ok, files} -> Enum.sort(files)
11+
{:ok, files} ->
12+
Enum.sort(files)
13+
1214
{:error, reason} ->
13-
IO.inspect("Failed to list topics directory '#{topics_dir}': #{inspect(reason)}", label: "Topics Error")
14-
[] # Return empty list on error to prevent crash, error will be logged.
15+
IO.inspect("Failed to list topics directory '#{topics_dir}': #{inspect(reason)}",
16+
label: "Topics Error"
17+
)
18+
19+
# Return empty list on error to prevent crash, error will be logged.
20+
[]
1521
end
1622
end
1723
end

lib/code_comparison_web/live/home_live.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ defmodule CodeComparisonWeb.HomeLive do
1515
socket
1616
|> assign(selected_topic: nil)
1717
|> assign(topics: [])
18-
|> assign(language1: CodeComparison.Structs.Language.build(%{})) # Default empty struct
19-
|> assign(language2: CodeComparison.Structs.Language.build(%{})) # Default empty struct
18+
# Default empty struct
19+
|> assign(language1: CodeComparison.Structs.Language.build(%{}))
20+
# Default empty struct
21+
|> assign(language2: CodeComparison.Structs.Language.build(%{}))
2022
|> assign(languages: [])
23+
2124
{:ok, socket}
2225
else
23-
first_topic = List.first(topics) # Safe as topics is not empty
24-
languages = Languages.get_languages_by_topic(first_topic) # This can be []
26+
# Safe as topics is not empty
27+
first_topic = List.first(topics)
28+
# This can be []
29+
languages = Languages.get_languages_by_topic(first_topic)
2530

2631
# If languages is empty, get_language_by_topic will return an empty Language struct
2732
# because Languages.get_language (which it calls) returns an empty struct for an empty list.
@@ -40,6 +45,7 @@ defmodule CodeComparisonWeb.HomeLive do
4045
|> assign(language1: language1_struct)
4146
|> assign(language2: language2_struct)
4247
|> assign(languages: languages)
48+
4349
{:ok, socket}
4450
end
4551
end

test/code_comparison/languages_test.exs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ defmodule CodeComparison.LanguagesTest do
7171
# Let's refine the assertion to only care about the name, which is safe from put_commit_values side effects.
7272
selected_lang1 = Languages.get_language(languages, "Lang1")
7373
assert selected_lang1.name == "Lang1"
74-
assert selected_lang1.topic == "Topic1" # Topic is also set by language_build
74+
# Topic is also set by language_build
75+
assert selected_lang1.topic == "Topic1"
7576

7677
selected_lang2_by_default = Languages.get_language(languages, "NonExistentLang")
77-
assert selected_lang2_by_default.name == "Lang1" # Defaults to first
78+
# Defaults to first
79+
assert selected_lang2_by_default.name == "Lang1"
7880
assert selected_lang2_by_default.topic == "Topic1"
7981
end
8082
end

0 commit comments

Comments
 (0)