Finding a project's root directory

I

Intransition

As I continue to work project development tools I repeatedly run into
the case of needing a reliable way to identify the project's root
directory, i.e. to ascend up the directories until it is found.

So far the best idea of come up with is to look for .git/, .hg/ or as
a fallback, README*.

Have any ideas for more reliable means?
 
J

Jeremy Hinegardner

As I continue to work project development tools I repeatedly run into
the case of needing a reliable way to identify the project's root
directory, i.e. to ascend up the directories until it is found.

So far the best idea of come up with is to look for .git/, .hg/ or as
a fallback, README*.

Have any ideas for more reliable means?

I use the parent directory of the lib/ directory as project root.

-jeremy
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

As I continue to work project development tools I repeatedly run into
the case of needing a reliable way to identify the project's root
directory, i.e. to ascend up the directories until it is found.

So far the best idea of come up with is to look for .git/, .hg/ or as
a fallback, README*.


In cases where I actually need this, I usually have a "root" method defined
on the class/module that represents the project. This is usually calculated
from __FILE__ based off the file where the "root" method is defined. I
prefer to have this be the only usage of __FILE__ in the entire project, if
possible.
 
I

Intransition

I use the parent directory of the lib/ directory as project root.

Thanks Jeremy. I'm using lib/ as the fallback in place of README*.

I also added a .root/ check to .git and .hg, so if all else fails
people can add that to their project.
 
I

Intransition

In cases where I actually need this, I usually have a "root" method defin= ed
on the class/module that represents the project. =A0This is usually calcu= lated
from __FILE__ based off the file where the "root" method is defined. =A0I
prefer to have this be the only usage of __FILE__ in the entire project, = if
possible.

I need to find the root external to the project itself. In fact I
would be very weary of ever looking up a project's root from within
the project's code. Different install setups will not necessarily keep
a project intact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top