且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

测试当前目录是否在Rails项目(bash)中

更新时间:2023-11-29 15:25:52

在进一步研究后,我得以深入研究 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