【问题标题】:Rakefile multitask- How to for loop and create task and execute themRakefile 多任务 - 如何循环和创建任务并执行它们
【发布时间】:2013-12-10 14:18:24
【问题描述】:

我需要在以下文件夹结构上运行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

【问题讨论】:

  • 为什么需要这样分解文件?
  • 这是一个后端供应商的要求,因为该文件夹将用于不同的国家、不同的服务器、不同的开发人员、不同的权限等使用。

标签: ruby sass compass-sass rakefile


【解决方案1】:

您可以使用“每个”循环以 ruby​​ 方式进行操作。例如:

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' }

这是您正在寻找的答案吗?

【讨论】:

  • 谢谢@iSvetlanko!我这样做了,但该命令仅对第一个文件夹运行。我想我需要用 Rake 进行某种多任务处理。但我不知道如何将任务添加到任务数组并使用多任务执行它们。
  • 啊,@zen-c,我的错!我没有意识到这一点。试试这个:find_folders = File.join("**", "folder*"); all_folders = Dir.glob(find_folders) ; folders.each { |folder| <your stuff> }。如果您从 folder1/2../N 的父目录执行它,它会起作用。您还可以硬编码连接中文件夹的路径。
【解决方案2】:

到目前为止,我所拥有的是这个。到目前为止它工作正常。但是一些奇怪的行为是,当我对文件夹 1 scss 进行更改时,终端窗口会报告“相同的文件夹 1/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 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多