How to get project's root dir path

6,582 views
Skip to first unread message

eksperimental

unread,
Sep 23, 2015, 5:15:32 AM9/23/15
to elixir-l...@googlegroups.com
Instead of doing relative paths from the current file, i would like to
know if there is a way to know the project's root folder.
if it has to be used with Mix it is ok, since it's to be used in test,
but if there is an option that doesn't rely on Mix, even better.

thanks in advance

José Valim

unread,
Sep 23, 2015, 6:09:52 AM9/23/15
to elixir-l...@googlegroups.com
Can you give us what you want to retrieve from the path? Depending on what you want achieve the answer may differ. However, if the project is being compiled with Mix, "File.cwd!" will always point to the root directory because Mix always runs from root.



José Valim
Skype: jv.ptec
Founder and Director of R&D


--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/20150923161523.2acccbbc.eksperimental%40autistici.org.
For more options, visit https://groups.google.com/d/optout.

eksperimental

unread,
Sep 23, 2015, 7:14:20 AM9/23/15
to elixir-l...@googlegroups.com
> Can you give us what you want to retrieve from the path?
Sure..
My code is exactly like this.

defmodule ExDocTest do
use ExUnit.Case, async: true
require ExDoc.Retriever

@root_dir Path.expand("../../../../", __DIR__)
@tmp_dir "#{@root_dir}/test/tmp"
@all_output_dir Path.expand("#{@tmp_dir}/doc_all", __DIR__)
@beam_dir "#{@tmp_dir}/ebin"
@all_beam_dir "#{@tmp_dir}/ebin_all"
@elixir_beam_dir
Path.expand("#{@root_dir}/../elixir/lib/elixir/ebin", __DIR__)

the File.cwd!/0 trick worked perfectly
you will just tell me if there is a neater way to achieve this.
thanks you José

Michael Schaefermeyer

unread,
Sep 23, 2015, 9:13:32 AM9/23/15
to elixir-lang-talk, eksper...@autistici.org
Hey,

since José already gave away my top secret cwd trick here is how I'd define those other directories: Checkout Path.join/1 and Path.join/2. Especially with the sigil_w function I find it to be very readable:

@root_dir       File.cwd!
@tmp_dir        Path.join(~w(#{@root_dir} test tmp))
@all_output_dir Path.join(@tmp_dir, "doc_all")
@beam_dir       Path.join(@tmp_dir, "ebin")
# ...
 
Hope that is what you were looking for.

eksperimental

unread,
Sep 23, 2015, 9:19:17 AM9/23/15
to elixir-l...@googlegroups.com
beautiful! thanks a lot.

Onorio Catenacci

unread,
Sep 23, 2015, 10:07:17 PM9/23/15
to elixir-lang-talk, eksper...@autistici.org
Cool pointer Michael!


--
Onorio
Reply all
Reply to author
Forward
0 new messages