测试当前目录是否在Rails项目(bash)中 [英] Test if the current directory is inside a Rails project (bash)

查看:47
本文介绍了测试当前目录是否在Rails项目(bash)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法来测试当前目录是否在rails项目中?显然,为了使用rails子命令(generate,scaffold等),Rails本身对此进行了测试.因此,大概有一种简单的方法可以对此进行测试.

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.

我正在寻找与您测试是否类似的方式你在一个Git仓库中.

推荐答案

在进一步研究后,我得以深入研究 rails 命令本身,并基于部分 railties解决方案 gem(< railties>/lib/rails/script_rails_loader.rb ):

Upon further investigation, I was able to dig into the rails command itself and based a solution on part of the railties gem (<railties>/lib/rails/script_rails_loader.rb):

#!/usr/bin/env ruby

require 'pathname'

SCRIPT_RAILS = File.join('script', 'rails')

def self.in_rails_application?
  File.exists?(SCRIPT_RAILS)
end

def self.in_rails_application_subdirectory?(path = Pathname.new(Dir.pwd))
  File.exists?(File.join(path, SCRIPT_RAILS)) || !path.root? && in_rails_application_subdirectory?(path.parent)
end

if in_rails_application? || in_rails_application_subdirectory?
  exit(0)
else
  exit(1)
end

这篇关于测试当前目录是否在Rails项目(bash)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆