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, Kotlin::Kotlinc, 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.



120
121
122
123
# File 'lib/buildr/core/compile.rb', line 120

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

Class Attribute Details

.languageObject (readonly)

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



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

def language
  @language
end

.packagingObject (readonly)

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



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

def packaging
  @packaging
end

.source_extObject (readonly)

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



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

def source_ext
  @source_ext
end

.sourcesObject (readonly)

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



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

def sources
  @sources
end

.targetObject (readonly)

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



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

def target
  @target
end

.target_extObject (readonly)

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



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

def target_ext
  @target_ext
end

Instance Attribute Details

#optionsObject (readonly)

Options for this compiler.



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

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.



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/buildr/core/compile.rb', line 86

def applies_to?(project, task)
  paths = task.sources + [sources].flatten.map { |src| Array(project.path_to(:source, task.usage, src.to_sym)) }
  paths.flatten!

  paths.each { |path|
    Find.find(path) {|found|
      if (!File.directory?(found)) && found.match(/.*\.#{Array(source_ext).join('|')}/)
        return true
      end
      } if File.exist? path.to_s
    }
  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.



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

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


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

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.



65
66
67
# File 'lib/buildr/core/compile.rb', line 65

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.



143
144
145
# File 'lib/buildr/core/compile.rb', line 143

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.



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

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.



130
131
132
133
134
135
136
137
138
139
# File 'lib/buildr/core/compile.rb', line 130

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