Is there an easy way to test if the current directory is inside a rails project? Clearly Rails itself tests this in order to use the rails subcommands (generate, scaffold, etc.), so presumably there's a straight-forward way to test this.
I'm looking for something similar to how you would test if you're inside a Git repo.
pwd : To check the current working directory, pwd(present working directory) method is used. 5. chdir : To change the current working directory, chdir method is used.
__dir__ is a alias to a function As the ruby documentation says __dir__ is equal to File.dirname(File.realpath(__FILE__))
Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .
I know you already have a solution in ruby, but here's one written in bash, which should be a bit faster.
Assuming you're using Bundler:
grep 'rails' 'Gemfile' >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "It's a Rails project!"
fi
This is what that snippet does:
grep 'rails' 'Gemfile'
will return a status code of 2 if a file named 'Gemfile' doesn't exist in the current directory. grep
will return a status code of 1 if the found a 'Gemfile' but it doesn't contain 'rails'.
For Rails 2 without Bundler this should work:
if [ -f script/rails ]; then
echo "It's a Rails 2 project!"
fi
All that does is test if the script/rails file exists.
I hope this helps someone who's hoping to do it bash instead of ruby.
There are no file or directory specific to Rails. so you can't really know if you are or not in a rails directory.
The better solution can be to know if you have rails or railities gem dependencies in your Gemfile.
bundle list | grep 'rail'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With