代码之家  ›  专栏  ›  技术社区  ›  TerryP

如何使用递归rake?-或合适的替代品

  •  6
  • TerryP  · 技术社区  · 14 年前

    我希望我的项目顶级Rakefile使用树中更深的rakefiles构建东西;i、 e.顶层的rakefile说明如何构建项目(大图),底层的rakefile说明如何构建特定模块(本地图)。

    当然,只要可以在任务之间共享,就有一组共享的配置来详细说明执行这些操作的细节:因此,主要是保持对需要构建的内容的描述,尽可能接近正在构建的源代码。例如,应使用/Source/Module/Rakefile中的说明构建/Source/Module/code.foo和cie;and/Rakefile了解模块之间的依赖关系。

    我不在乎它是使用多个rake进程(alarecursivemake),还是只创建单独的构建环境。无论哪种方式,它都应该具有足够的自包含性,以便由队列进行处理:以便可以同时构建非依赖模块。

    问题是,你到底是怎么用Rake做的!?我在网上和文档中都找不到任何有意义的东西。我尝试创建一个新的Rake::Application对象并进行设置,但不管怎样 methods

    recursive

    4 回复  |  直到 14 年前
        1
  •  3
  •   Derick Bailey    14 年前

    require 从其他rake文件中删除rake文件,并从所需文件中运行任务。

    将其放入rakefile.rb:

    require 'foo/rakefile.rb'
    
    task :default => [:bar]
    

    task :bar do
      puts "baz"
    end
    

    运行根级rakefile,它将在屏幕上打印“baz”。你应该能够组织你的rake文件和公共代码,不管你想用这个想法

        2
  •  2
  •   siva008    11 年前

    正如我在前一篇文章中提到的,这里的代码片段几乎支持递归rake行为,包括选项传递、干运行、跟踪和顶级任务。

    #!/usr/bin/env ruby
    
    begin
      require 'rubygems'
      gem 'rake'
    rescue LoadError
    end
    
    $sub_directories = []
    $top_level_task_provided__skip_subdir = 0
    module Rake
       REDUCE_COMPAT = true if ARGV.include?("--reduce-compat")
       RAKE_OPTS = ARGV.clone.join(" ") 
       module DSL
         def subdirs dirs
            puts "DIRS: #{dirs}"
            $sub_directories = dirs
         end
       end
    end
    
    require 'rake'
    
    Rake.application.standard_exception_handling do
       Rake.application.init
       $top_level_task_provided__skip_subdir = 1 if Rake.application.top_level_tasks.size >= 1 and Rake.application.top_level_tasks[0] != "default"
       Rake.application.load_rakefile
       if Rake.application.tasks.size == 0 and $sub_directories.size != 0 then
          desc "Default task when only subdirs is present in a rakefile"
          task :default do
             puts "Default subdir task"
          end
       end
       Rake.application.top_level
    end
    
    if $top_level_task_provided__skip_subdir == 0 then
        $sub_directories.each do |d|
            unless rake_system("cd #{d} && pwd && ruby #{File.expand_path $0} #{Rake::RAKE_OPTS}") then
                exit(1)
            end
        end
    end
    
        3
  •  0
  •   garno    14 年前

    如果我理解的很好,那么您的问题是您需要从不同的RakeFile调用另一个Rake任务中的一个Rake任务。

    您可以按照需要的方式在/Source/Module/Rakefile中构建所需的任务,并在/Rakefile中创建另一个Rakefile并以这种方式运行任务。

    desc 'Description'
    task :foo do
        %x[rake -f /Source/Module/RakeFile foo:bar]
    end
    

    -f 让您定义耙锉的位置

    rake -f /path/to/Rakefile -R /path/to/rakelib foo:bar
    

    我希望这对你有帮助。

        4
  •  0
  •   siva008    11 年前

    我也有类似的需求,并创建了以下函数

    rakeutils.rb:

    def subdirs dirs
        dirs.each do |d|
          task d do |t|
            sh("cd #{t.name} && pwd && rake")
          end
        end
        task :default => dirs
    end
    

    在Rakefiles中,无论我想在哪里拥有这个功能,我都会把它包括进去。。。例如

    耙子:

    require 'rakeutils'
    
    task :test1 do
      puts "Hi"
    end
    
    task :default => :test1
    subdirs ["ab", "cd"]