looshi
looshi

Reputation: 1236

Phoenix Framework / Elixir, output relative file path to the console

How to log the relative file location to its project root?

"my-project" is the root directory of the Phoenix Framework project. Inside users/me/apps/my-project/my-file.txt, if I do :

IO.puts __ENV__.file

The console outputs the absolute path: users/me/apps/my-project/my-file.txt.

How to output the relative path, e.g. my-project/my-file.txt ?

Upvotes: 2

Views: 1634

Answers (1)

Dogbert
Dogbert

Reputation: 222060

You can fetch the root directory using File.cwd!() and then do a leading trim of __ENV__.file with it:

defmodule A do
  def a do
    __ENV__.file |> String.trim_leading(File.cwd!)
  end
end

Sample output:

iex(1)> A.a
"/lib/a.ex"

Upvotes: 3

Related Questions