Bundle install运行在错误的目录下。

5
我正在为一些内部项目构建一个基于thor的简单生成器,但似乎无法从正确的目录运行bundle install。
当我运行新的[APP_NAME]函数时,它应该创建目录和文件,然后运行bundle install来安装应用程序所需的gems。
生成器函数的源代码:
def create
  puts "Creating application #{name}"

  directory 'application', "#{name}"

  Dir.chdir("#{Dir.pwd}/#{name}") do
    puts `bundle install`
  end
end

以下是运行调用此创建方法的命令时的控制台输出:

$ bundle exec bin/my_gem new test_app
Creating application test_app
      create  test_app
      create  test_app/Gemfile
      create  test_app/Guardfile
      create  test_app/README.md
      create  test_app/app/controllers
      create  test_app/app/helpers
      create  test_app/app/models
      create  test_app/app/views
Using thor (0.14.6) 
Using my_gem (0.0.1) 
Using bundler (1.1.3) 
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

如您所见,它正在运行bundle install,但是它是在我的当前目录(thorbundlermy_gem)中运行,而不是在test_app目录(guardguard-coffeescriptguard-less等)中运行。
运行其他命令,如lspwd会给出预期的结果:
Gemfile
Guardfile
README.md
app

并且

/Users/davidlumley/Development/Gems/my_gem/test_app

不确定是否有任何区别,但我使用RVM来管理我的Ruby。


“不确定是否有任何区别,但我使用RVM来管理我的Rubies。” - 是的 :) - djlumley
当然可以管理Ruby,但是通过使用RVM,您应该创建gemset并使用该gemset目录来安装gem,这样就不会出现您现在面临的问题了。 - Kashiftufail
2个回答

20

看起来你的应用程序已经在使用Bundler,而且你遇到了一个内部使用Bundler的问题。尝试这样做:

Bundler.with_clean_env do
  puts `bundle install`
end

我猜测发生的情况是您的外部打包程序将 BUNDLE_GEMFILE 环境变量设置为您应用程序的 Gemfile,然后您的内部打包程序继承了它。


-1
请尝试这个,我肯定会帮到你的。
      rvm gemset create app_name


      rvm use ruby-1.9.2@app_name

你用过哪些 Ruby 版本?

然后安装 bundler gem。

      gem install bundler.           

然后运行 bundle install 和启动服务器...

我目前正在使用一个新的gemset,其中安装了thorbundler。问题不在于bundle没有被安装,而是我的生成器没有在适当的目录下运行命令,因此它使用了错误的Gemfile。 - djlumley
哦,我曾经遇到过这个问题,然后我使用了这个技巧。通过命令行进入应用程序目录,逐个安装所有的 gem,并确保每个 gem 安装在正确的 gemset 目录中。 - Kashiftufail
我正在尝试自动化这个过程,因此我正在尝试以编程方式运行bundle install,类似于Rails在其生成器中的方式。 - djlumley

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接