buildr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From djspie...@apache.org
Subject svn commit: r917124 - /buildr/trunk/lib/buildr/core/cc.rb
Date Sun, 28 Feb 2010 04:37:16 GMT
Author: djspiewak
Date: Sun Feb 28 04:37:16 2010
New Revision: 917124

URL: http://svn.apache.org/viewvc?rev=917124&view=rev
Log:
Reorganized cc task into a separate class (CCTask)

Modified:
    buildr/trunk/lib/buildr/core/cc.rb

Modified: buildr/trunk/lib/buildr/core/cc.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/core/cc.rb?rev=917124&r1=917123&r2=917124&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/core/cc.rb (original)
+++ buildr/trunk/lib/buildr/core/cc.rb Sun Feb 28 04:37:16 2010
@@ -19,109 +19,121 @@
 require 'buildr/core/compile'
 
 module Buildr
-  module CC
-    include Extension
+  class CCTask < Rake::Task
+    attr_accessor :delay
+    attr_reader :project
+    
+    def initialize(*args)
+      super
+      @delay = 0.2
+      enhance do
+        monitor_and_compile
+      end
+    end
+    
+  private
+    
+    def associate_with(project)
+      @project = project
+    end
     
-    class << self
-      def check_mtime(pattern, old_times)
-        times = {}
-        changed = []
+    def monitor_and_compile
+      # we don't want to actually fail if our dependencies don't succede
+      [:compile, 'test:compile'].each { |name| project.task(name).invoke }
+      
+      main_dirs = project.compile.sources.map(&:to_s)
+      test_dirs = project.task('test:compile').sources.map(&:to_s)
+      res_dirs = project.resources.sources.map(&:to_s)
+      
+      main_ext = Buildr::Compiler.select(project.compile.compiler).source_ext.map(&:to_s)
+      test_ext = Buildr::Compiler.select(project.task('test:compile').compiler).source_ext.map(&:to_s)
+      
+      test_tail = if test_dirs.empty? then '' else ",{#{test_dirs.join ','}}/**/*.{#{test_ext.join
','}}" end
+      res_tail = if res_dirs.empty? then '' else ",{#{res_dirs.join ','}}/**/*" end
+      
+      pattern = "{{#{main_dirs.join ','}}/**/*.{#{main_ext.join ','}}#{test_tail}#{res_tail}}"
+      
+      times, _ = check_mtime pattern, {}     # establish baseline
+      
+      dir_names = (main_dirs + test_dirs + res_dirs).map { |file| strip_filename project,
file }
+      if dir_names.length == 1
+        info "Monitoring directory: #{dir_names.first}"
+      else
+        info "Monitoring directories: [#{dir_names.join ', '}]"
+      end
+      trace "Monitoring extensions: [#{main_ext.join ', '}]"
+      
+      while true
+        sleep delay
         
-        Dir.glob pattern do |fname|
-          times[fname] = File.mtime fname
-          if old_times[fname].nil? || old_times[fname] < File.mtime(fname)
-            changed << fname
+        times, changed = check_mtime pattern, times
+        unless changed.empty?
+          info ''    # better spacing
+          
+          changed.each do |file|
+            info "Detected changes in #{strip_filename project, file}"
+          end
+          
+          in_main = main_dirs.any? do |dir|
+            changed.any? { |file| file.index(dir) == 0 }
+          end
+          
+          in_test = test_dirs.any? do |dir|
+            changed.any? { |file| file.index(dir) == 0 }
           end
+          
+          in_res = res_dirs.any? do |dir|
+            changed.any? { |file| file.index(dir) == 0 }
+          end
+          
+          project.task(:compile).reenable if in_main
+          project.task('test:compile').reenable if in_test
+          
+          project.task(:resources).filter.run if in_res
+          project.task(:compile).invoke
+          project.task('test:compile').invoke
         end
-        
-        # detect deletion (slower than it could be)
-        old_times.each_key do |fname|
-          changed << fname unless times.has_key? fname
+      end
+    end
+    
+    def check_mtime(pattern, old_times)
+      times = {}
+      changed = []
+      
+      Dir.glob pattern do |fname|
+        times[fname] = File.mtime fname
+        if old_times[fname].nil? || old_times[fname] < File.mtime(fname)
+          changed << fname
         end
-        
-        [times, changed]
       end
       
-      def strip_filename(project, name)
-        name.gsub project.base_dir + File::SEPARATOR, ''
+      # detect deletion (slower than it could be)
+      old_times.each_key do |fname|
+        changed << fname unless times.has_key? fname
       end
+      
+      [times, changed]
+    end
+    
+    def strip_filename(project, name)
+      name.gsub project.base_dir + File::SEPARATOR, ''
     end
+  end
+  
+  module CC
+    include Extension
     
     first_time do
       Project.local_task :cc
     end
     
     before_define do |project|
-      project.task :cc do
-        # we don't want to actually fail if our dependencies don't succede
-        [:compile, 'test:compile'].each { |name| project.task(name).invoke }
-        
-        main_dirs = project.compile.sources.map(&:to_s)
-        test_dirs = project.task('test:compile').sources.map(&:to_s)
-        res_dirs = project.resources.sources.map(&:to_s)
-        
-        main_ext = Buildr::Compiler.select(project.compile.compiler).source_ext.map(&:to_s)
-        test_ext = Buildr::Compiler.select(project.task('test:compile').compiler).source_ext.map(&:to_s)
-        
-        test_tail = if test_dirs.empty? then '' else ",{#{test_dirs.join ','}}/**/*.{#{test_ext.join
','}}" end
-        res_tail = if res_dirs.empty? then '' else ",{#{res_dirs.join ','}}/**/*" end
-        
-        pattern = "{{#{main_dirs.join ','}}/**/*.{#{main_ext.join ','}}#{test_tail}#{res_tail}}"
-        
-        times, _ = Buildr::CC.check_mtime pattern, {}     # establish baseline
-        
-        dir_names = (main_dirs + test_dirs + res_dirs).map { |file| Buildr::CC.strip_filename
project, file }
-        if dir_names.length == 1
-          info "Monitoring directory: #{dir_names.first}"
-        else
-          info "Monitoring directories: [#{dir_names.join ', '}]"
-        end
-        trace "Monitoring extensions: [#{main_ext.join ', '}]"
-        
-        while true
-          sleep project.cc.frequency
-          
-          times, changed = Buildr::CC.check_mtime pattern, times
-          unless changed.empty?
-            info ''    # better spacing
-            
-            changed.each do |file|
-              info "Detected changes in #{Buildr::CC.strip_filename project, file}"
-            end
-            
-            in_main = main_dirs.any? do |dir|
-              changed.any? { |file| file.index(dir) == 0 }
-            end
-            
-            in_test = test_dirs.any? do |dir|
-              changed.any? { |file| file.index(dir) == 0 }
-            end
-            
-            in_res = res_dirs.any? do |dir|
-              changed.any? { |file| file.index(dir) == 0 }
-            end
-            
-            project.task(:compile).reenable if in_main
-            project.task('test:compile').reenable if in_test
-            
-            project.task(:resources).filter.run if in_res
-            project.task(:compile).invoke
-            project.task('test:compile').invoke
-          end
-        end
-      end
+      cc = CCTask.define_task :cc
+      cc.send :associate_with, project
     end
     
     def cc
-      @cc ||= CCOptions.new
-    end
-    
-    class CCOptions
-      attr_writer :frequency      # TODO  this is a bad name, maybe "delay"?
-      
-      def frequency
-        @frequency ||= 0.2
-      end
+      task :cc
     end
   end
   



Mime
View raw message