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.



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

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

Class Attribute Details

.languageObject (readonly)

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



68
69
70
# File 'lib/buildr/core/compile.rb', line 68

def language
  @language
end

.packagingObject (readonly)

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



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

def packaging
  @packaging
end

.source_extObject (readonly)

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



72
73
74
# File 'lib/buildr/core/compile.rb', line 72

def source_ext
  @source_ext
end

.sourcesObject (readonly)

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



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

def sources
  @sources
end

.targetObject (readonly)

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



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

def target
  @target
end

.target_extObject (readonly)

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



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

def target_ext
  @target_ext
end

Instance Attribute Details

#optionsObject (readonly)

Options for this compiler.



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

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)


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

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.any? { |path| !Dir["#{path}/**/*.{#{ext_glob}}"].empty? }
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.



103
104
105
# File 'lib/buildr/core/compile.rb', line 103

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


94
95
96
97
98
# File 'lib/buildr/core/compile.rb', line 94

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.



63
64
65
# File 'lib/buildr/core/compile.rb', line 63

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.



134
135
136
# File 'lib/buildr/core/compile.rb', line 134

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.



140
141
142
# File 'lib/buildr/core/compile.rb', line 140

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)


121
122
123
124
125
126
127
128
129
130
# File 'lib/buildr/core/compile.rb', line 121

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