Class: Buildr::Compiler::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/buildr/core/compile.rb

Overview

Base class for all compilers, with common functionality. Extend and over-ride as you see fit (see Javac as an example).

Direct Known Subclasses

Javac, Groovy::Groovyc, Scala::Scalac

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, options) ⇒ Base

Construct a new compiler with the specified options. Note that options may change before the compiler is run.



126
127
128
129
# File 'lib/buildr/core/compile.rb', line 126

def initialize(project, options)
  @project = project
  @options = options
end

Class Attribute Details

.languageObject (readonly)

The compiled language (e.g. :java).



75
76
77
# File 'lib/buildr/core/compile.rb', line 75

def language
  @language
end

.packagingObject (readonly)

The default packaging type (e.g. :jar).



85
86
87
# File 'lib/buildr/core/compile.rb', line 85

def packaging
  @packaging
end

.source_extObject (readonly)

Extension for source files (e.g. ‘java’). Defaults to language.



79
80
81
# File 'lib/buildr/core/compile.rb', line 79

def source_ext
  @source_ext
end

.sourcesObject (readonly)

Source directories to use if none were specified (e.g. ‘java’). Defaults to #language.



77
78
79
# File 'lib/buildr/core/compile.rb', line 77

def sources
  @sources
end

.targetObject (readonly)

The target path (e.g. ‘classes’)



81
82
83
# File 'lib/buildr/core/compile.rb', line 81

def target
  @target
end

.target_extObject (readonly)

Extension for target files (e.g. ‘class’).



83
84
85
# File 'lib/buildr/core/compile.rb', line 83

def target_ext
  @target_ext
end

Instance Attribute Details

#optionsObject (readonly)

Options for this compiler.



132
133
134
# File 'lib/buildr/core/compile.rb', line 132

def options
  @options
end

Class Method Details

.applies_to?(project, task) ⇒ Boolean

Returns true if this compiler applies to any source code found in the listed source directories. For example, Javac returns true if any of the source directories contains a .java file. The default implementation looks to see if there are any files in the specified path with the extension #source_ext.

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/buildr/core/compile.rb', line 91

def applies_to?(project, task)
  paths = task.sources + [sources].flatten.map { |src| Array(project.path_to(:source, task.usage, src.to_sym)) }
  paths.flatten!
  ext_glob = Array(source_ext).join(',')
  
  paths.each { |path| 
    Find.find(path) {|found|
      if (!File.directory?(found)) && found.match(/.*\.#{Array(source_ext).join('|')}/)
        return true
      end
      } if File.exist? path
    }
  false
end

.dependenciesObject

Returns additional dependencies required by this language. For example, since the test framework picks on these, you can use the JUnit framework with Scala. Defaults to obtaining a list of artifact specifications from the REQUIRES constant.



118
119
120
# File 'lib/buildr/core/compile.rb', line 118

def dependencies
  []
end

.specify(attrs) ⇒ Object

Implementations can use this method to specify various compiler attributes. For example:

specify :language=>:java, :target=>'classes', :target_ext=>'class', :packaging=>:jar


109
110
111
112
113
# File 'lib/buildr/core/compile.rb', line 109

def specify(attrs)
  attrs[:sources] ||= attrs[:language].to_s
  attrs[:source_ext] ||= attrs[:language].to_s
  attrs.each { |name, value| instance_variable_set("@#{name}", value) }
end

.to_symObject

The compiler’s identifier (e.g. :javac). Inferred from the class name.



70
71
72
# File 'lib/buildr/core/compile.rb', line 70

def to_sym
  @symbol ||= name.split('::').last.downcase.to_sym
end

Instance Method Details

#compile(sources, target, dependencies) ⇒ Object

Compile all files lists in sources (files and directories) into target using the specified dependencies.



149
150
151
# File 'lib/buildr/core/compile.rb', line 149

def compile(sources, target, dependencies)
  raise 'Not implemented'
end

#dependenciesObject

Returns additional dependencies required by this language. For example, since the test framework picks on these, you can use the JUnit framework with Scala.



155
156
157
# File 'lib/buildr/core/compile.rb', line 155

def dependencies
  self.class.dependencies
end

#needed?(sources, target, dependencies) ⇒ Boolean

Determines if the compiler needs to run by checking if the target files exist, and if any source files or dependencies are newer than corresponding target files.

Returns:

  • (Boolean)


136
137
138
139
140
141
142
143
144
145
# File 'lib/buildr/core/compile.rb', line 136

def needed?(sources, target, dependencies)
  map = compile_map(sources, target)
  return false if map.empty?
  return true unless File.exist?(target.to_s)
  source_files_not_yet_compiled = map.select { |source, target| !File.exist?(target) }.to_a
  trace "Compile needed because source file #{source_files_not_yet_compiled[0][0]} has no corresponding #{source_files_not_yet_compiled[0][1]}" unless source_files_not_yet_compiled.empty?
  return true if map.any? { |source, target| !File.exist?(target) || File.stat(source).mtime > File.stat(target).mtime }
  oldest = map.map { |source, target| File.stat(target).mtime }.min
  return dependencies.any? { |path| file(path).timestamp > oldest }
end