Hi All,

I have recently had some issues with the Gettext library (the issues are 
irrelevant for this suggestion) and thought that maybe the Gettext 
Extraction could be rebuilt using compiler tracers.

The basic idea was to collect all modules and gettext calls via a compiler 
tracer and replace the existing extractor with that. (See example in 
Attachment)

However, there seems to be no way to access the args for a call for any of 
the following trace events: remote_function, remote_macro, local_function, 
local_macro.

Did I overlook something or is there currently no way to access the arg 
values?

If not, I think it could be a great addition to somehow make those values 
accessible.

I'm playing around for the first time with compiler tracers. Therefore I'm 
sorry if this request doesn't make sense or this is a completely wrong 
approach.

Thanks & Best,
Jonatan

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/a51ab9f7-30d3-4fa6-a7a3-b3653bc52470n%40googlegroups.com.
diff --git a/lib/gettext/compile_tracer.ex b/lib/gettext/compile_tracer.ex
new file mode 100644
index 0000000..d0f1567
--- /dev/null
+++ b/lib/gettext/compile_tracer.ex
@@ -0,0 +1,26 @@
+defmodule Gettext.CompileTracer do
+  def trace({:remote_macro, _meta, Gettext, :__using__, 1}, %Macro.Env{module: module}) do
+    :ets.insert(:gettext_modules, {module})
+
+    :ok
+  end
+
+  def trace({:remote_macro, _meta, gettext_module, :dpgettext_noop, 3}, %Macro.Env{
+        module: module,
+        file: file,
+        line: line
+      }) do
+    if :ets.member(:gettext_modules, gettext_module) do
+      args = "???"
+
+      :ets.insert(
+        :gettext_calls,
+        {:dpgettext_noop, 1, args, Mix.Project.get().project[:app], module, file, line}
+      )
+    end
+
+    :ok
+  end
+
+  def trace(_event, _env), do: :ok
+end
diff --git a/lib/mix/tasks/gettext.extract.ex b/lib/mix/tasks/gettext.extract.ex
index 97f4263..ea974c6 100644
--- a/lib/mix/tasks/gettext.extract.ex
+++ b/lib/mix/tasks/gettext.extract.ex
@@ -83,12 +83,17 @@ defmodule Mix.Tasks.Gettext.Extract do
     end
   end
 
-  defp extract(app, gettext_config) do
-    Gettext.Extractor.enable()
+  defp extract(_app, _gettext_config) do
+    :ets.new(:gettext_modules, [:set, :named_table, :public])
+    :ets.new(:gettext_calls, [:set, :named_table, :public])
+
+    Code.put_compiler_option(:tracers, [Gettext.CompileTracer])
+
     force_compile()
-    Gettext.Extractor.pot_files(app, gettext_config)
-  after
-    Gettext.Extractor.disable()
+
+    # take collected information and write POT
+    IO.inspect(:ets.tab2list(:gettext_modules))
+    IO.inspect(:ets.tab2list(:gettext_calls))
   end
 
   defp force_compile do

Reply via email to