0

全部,

在特定目录中创建一些文件后,我使用以下命令。它给我一条错误消息。

命令

desc "new project_name", "new a project with a name"
def new(project_name = 'my_project')
    directory 'doc', project_name
    path =  Dir.pwd + '/' + project_name
    system("cd #{path}")
    run('bundle install')

    #But this command is working that makes me so confusing
    #run("cd #{path} && bundle install")

    #puts "Initializing complete"
end

错误

create  aaaaaaa/views/index.slim
create  aaaaaaa/views/layout.slim
run  bundle install from "."
Could not locate Gemfile

我看到在初始化新项目时直接运行此命令的rails,bundle installrails如何做到这一点?我如何使这个命令工作。

4

2 回答 2

3

system创建一个子进程然后退出。子进程更改目录然后完成,但您的脚本仍然认为它位于进程认为它所在的任何工作目录中。

If you want to change the working directory of the process running the script, use Dir.chdir().

于 2011-10-17T03:15:13.903 回答
0

Rails 实际上使用了一个小技巧,将 bundle install 命令“打印”到 shell 并运行它。注意输出中的反引号。

print `"#{Gem.ruby}" -rubygems "#{Gem.bin_path('bundler', 'bundle')}" #{command}`

我还认为系统命令不一定会改变进程的当前工作目录(就 Thor 的运行命令而言)。

于 2011-10-17T03:08:47.823 回答