2015-02-05 43 views
0

可變量的作用域在我~/Sites/目錄我有無所不能的Rakefile:Rakefiles:在`load`ed腳本

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 

... 

這個Rake文件的行爲是由頂部的選項和deliver任務(其他網站可能不會使用傑奇建築)完全定義。該文件中的所有其餘任務都是複製粘貼。爲了DRY的利益,我已將複製粘貼的任務移至~/Sites/common.rake,並將此文件與load '../common.rake'一起包含在貢獻文件中。

Pulling git rake aborted! NameError: undefined local variable or method 'local_git_dir' for main:Object /Users/williamentriken/Sites/common.rake:4:in 'block in ' Tasks: TOP => pullgit (See full trace by running task with --trace)

如何使該變量可用於load ed腳本?

+0

你在哪兒叫'load'? – 2015-02-05 21:27:28

+0

該文件中的第一件事 – 2015-02-05 21:51:45

回答

-1

How do I make that variable available to the load ed script?

你不行。這是一個局部變量。局部變量侷限於它們定義的範圍,它們不在其他範圍內。這就是爲什麼他們被稱爲局部變量。