Finding project root

29 views
Skip to first unread message

mla

unread,
Feb 15, 2014, 1:59:07 PM2/15/14
to perl...@googlegroups.com
What's the recommended way to know what your current Kelp project root is?

I see, for example, that Config uses $self->app->path. e.g.,

            # Template
            Template => {
                paths => [
                    $self->app->path . '/views',
                    $self->app->path . '/../views'
                ]
            },

And path() is determined by FindBin.

That works great if you're starting your app from the project root or t/, but I also have a bin/ directory, etc., where I place tools.

I can just add a "project_root" value to the configuration, of course. Just wondering if there's a way to have Kelp tell me without
needing to hardcode it.

Thanks.


Stefan Geneshky

unread,
Feb 15, 2014, 2:22:39 PM2/15/14
to perl...@googlegroups.com

The recommended way to get app root is $self->app->path. It is also recommended, but not required to run your app from the root.
You'll need to hard code the path, if you want to run your app from somewhere else.

Regards,
Stefan

mla

unread,
Feb 15, 2014, 10:46:50 PM2/15/14
to perl...@googlegroups.com
On Saturday, February 15, 2014 11:22:39 AM UTC-8, Stefan Geneshky wrote:

The recommended way to get app root is $self->app->path. It is also recommended, but not required to run your app from the root.
You'll need to hard code the path, if you want to run your app from somewhere else.

Ok, thanks.

I don't mind running the app from root but I'd really like to be able to run ancillary tools that depend on the config from other directories.
For now my solution is to require an environment variable to be set and to automatically chdir to that when the app is loaded. e.g.,

package MyApp;

use Kelp::Base 'Kelp';

BEGIN {
  my $path = $ENV{PROJECT_ROOT} or croak "PROJECT_ROOT not defined";
  chdir $path or croak "chdir to PROJECT_ROOT '$path' failed: $!";
}

Not great but seemed like the easiest way.
I tried just setting KELP_CONFIG_DIR but then it was failing to find the log/ directory, etc.

Stefan Geneshky

unread,
Feb 15, 2014, 10:50:25 PM2/15/14
to perl...@googlegroups.com

Brad

unread,
Jul 29, 2014, 9:28:34 AM7/29/14
to perl...@googlegroups.com
I guess another option would be to use File::ShareDir::module_dir. This also means you'll need to move your views into "YourApp/lib/auto/YourApp/views".
Then you can use something like

use File::ShareDir;
my $path = File::ShareDir::module_dir('YourApp');

{
   
Template => {
        paths
=> [
           $path
. '/views',
        ],
    },
}

It's a great way to have any app globally find your static files.
Reply all
Reply to author
Forward
0 new messages