Class: Buildr::Compiler::Base
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
Class Attribute Summary collapse
-
.language ⇒ Object
readonly
The compiled language (e.g. :java).
-
.packaging ⇒ Object
readonly
The default packaging type (e.g. :jar).
-
.source_ext ⇒ Object
readonly
Extension for source files (e.g. ‘java’).
-
.sources ⇒ Object
readonly
Source directories to use if none were specified (e.g. ‘java’).
-
.target ⇒ Object
readonly
The target path (e.g. ‘classes’).
-
.target_ext ⇒ Object
readonly
Extension for target files (e.g. ‘class’).
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Options for this compiler.
Class Method Summary collapse
-
.applies_to?(project, task) ⇒ Boolean
Returns true if this compiler applies to any source code found in the listed source directories.
-
.dependencies ⇒ Object
Returns additional dependencies required by this language.
-
.specify(attrs) ⇒ Object
Implementations can use this method to specify various compiler attributes.
-
.to_sym ⇒ Object
The compiler’s identifier (e.g. :javac).
Instance Method Summary collapse
-
#compile(sources, target, dependencies) ⇒ Object
Compile all files lists in sources (files and directories) into target using the specified dependencies.
-
#dependencies ⇒ Object
Returns additional dependencies required by this language.
-
#initialize(project, options) ⇒ Base
constructor
Construct a new compiler with the specified options.
-
#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.
Constructor Details
Class Attribute Details
.language ⇒ Object (readonly)
The compiled language (e.g. :java).
68 69 70 |
# File 'lib/buildr/core/compile.rb', line 68 def language @language end |
.packaging ⇒ Object (readonly)
The default packaging type (e.g. :jar).
78 79 80 |
# File 'lib/buildr/core/compile.rb', line 78 def packaging @packaging end |
.source_ext ⇒ Object (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 |
.sources ⇒ Object (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 |
.target ⇒ Object (readonly)
The target path (e.g. ‘classes’)
74 75 76 |
# File 'lib/buildr/core/compile.rb', line 74 def target @target end |
.target_ext ⇒ Object (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
#options ⇒ Object (readonly)
Options for this compiler.
117 118 119 |
# File 'lib/buildr/core/compile.rb', line 117 def @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.
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 |
.dependencies ⇒ Object
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_sym ⇒ Object
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 |
#dependencies ⇒ Object
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.
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). > oldest } end |