Rakefiles:`load`ed脚本中可用的变量范围

时间:2015-02-05 21:09:14

标签: ruby rake rakefile

在我的~/Sites/目录中,我有无所不能的Rakefile:

desc "Run deliver in each directory"
task :deliver do
  sh 'for a in $(ls ./*/Rakefile); do (cd $(dirname $a); rake -f Rakefile deliver); done'
end

(以及其他有趣的制作任务,以及列出所有任务的愚蠢"默认"任务[见https://github.com/ruby/rake/issues/22]

Sites内的某些目录中包含一个Rakefile。大多数看起来像这样:

# Rsync options
production_server = 'pacificmedicaltraining.net'
production_user = 'training2'
production_www_dir = 'www'
local_www_dir = 'www'
local_git_dir = 'git'

desc "Run all build and deployment tasks, for continuous delivery"
task :deliver => [:pullgit, :buildjekyll, :pushproduction]

desc "Bring deployed server files local"
task :pullgit do
  puts 'Pulling git'
  sh "cd '#{local_git_dir}'; git pull"
  puts 'Pulled'
end

...

此Rakefile的行为完全由顶部的选项和deliver任务定义(其他站点可能不使用Jekyll进行构建)。此文件中的所有剩余任务都是复制粘贴。为了DRY的利益,我已将复制粘贴的任务删除到~/Sites/common.rake,并在贡献文件中包含此文件load '../common.rake'

  

拉扯git   耙子流产了!   NameError:未定义的局部变量或方法' local_git_dir' for main:对象   /Users/williamentriken/Sites/common.rake:4:in'阻止'   任务:TOP => pullgit   (通过使用--trace运行任务查看完整跟踪)

如何将该变量提供给load ed脚本?

1 个答案:

答案 0 :(得分:-1)

  

如何将该变量提供给load ed脚本?

你做不到。这是一个局部变量。局部变量是它们定义的范围的本地变量,它们在其他范围中不存在。这就是为什么它们被称为局部变量。