Rakefile多任务 - 如何循环和创建任务并执行它们

时间:2013-12-10 02:59:59

标签: ruby sass compass-sass rakefile

我有以下文件夹结构,我需要运行compass watch

|   config.rb
|   rakefile.rb
+---folder1
|   +---css 
|   +---img
|   +---js
|   \---sass
|           
+---folder2
|   +---css 
|   +---img
|   +---js  
|   \---sass
|           
\---folder3
    +---css
    +---img
    +---js
    \---sass

我将config.rb保留在外面,因为它包含相同的js,img,css文件夹配置,并具有相同的:output设置。

这是我的rakefile.rb来监视所有文件夹:

desc 'Compile folder1 sass'
task :watch_1 do
puts 'Watching folder1 sass...'
system 'compass watch --sass-dir folder1/sass --css-dir folder1/css -c config.rb'
end

desc 'Compile folder2 sass'
task :watch_2 do
puts 'Watching folder2 sass...'
system 'compass watch --sass-dir folder2/sass --css-dir folder2/css -c config.rb'
end

desc 'Compile folder3 sass'
task :watch_3 do
puts 'Watching folder3 sass...'
system 'compass watch --sass-dir folder3/sass --css-dir folder3/css -c config.rb'
end

# Watch all sass folder to compile css.
multitask :watch_all => [:watch_1, :watch_2, :watch_3] do
puts 'Watching all...'
end

我的问题是......当我有新文件夹时,如何循环并在rakefile.rb中运行任务?我四处看看,但我仍然坚持创造任务。这是我的伪代码:

$folders = ['folder1', 'folder2', ... , 'folderN'];
foreach $folder in $folders do
    system 'compass watch --sass-dir $folder/sass --css-dir $folder/css -c config.rb'
end

2 个答案:

答案 0 :(得分:0)

你可以用ruby-way来实现..使用“each”循环。例如:

folders = ['folder1', 'folder2', ... , 'folderN']
folders.each do |folder|
  system 'compass watch --sass-dir folder/sass --css-dir folder/css -c config.rb'
end

或使用块:

folders = ['folder1', 'folder2', ... , 'folderN']
folders.each { |folder| system 'compass watch --sass-dir folder/sass --css-dir folder/css -c config.rb' }

这是你要找的答案吗?

答案 1 :(得分:0)

到目前为止我拥有的是这个。到目前为止它的工作正常。但是当我对文件夹1 scss进行更改时,一些奇怪的行为,终端窗口报告“相同的folder1 / css / main.css”虽然调查文件folder1 / css / main.css,但更改已正确编译。除此之外,这里的rakefile.rb代码对我有用:

threads = []
folders = ['folder1', 'folder2', 'folder3']

for folder in folders
    threads << Thread.new(folder) { |thefolder|
        puts "Watching #{thefolder}/sass"
        system "compass watch --sass-dir #{thefolder}/sass --css-dir #{thefolder}/css -c config.rb"
    }
end

threads.each { |thethread| thethread.join }