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).
75 76 77 |
# File 'lib/buildr/core/compile.rb', line 75 def language @language end |
.packaging ⇒ Object (readonly)
The default packaging type (e.g. :jar).
85 86 87 |
# File 'lib/buildr/core/compile.rb', line 85 def packaging @packaging end |
.source_ext ⇒ Object (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 |
.sources ⇒ Object (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 |
.target ⇒ Object (readonly)
The target path (e.g. ‘classes’)
81 82 83 |
# File 'lib/buildr/core/compile.rb', line 81 def target @target end |
.target_ext ⇒ Object (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
#options ⇒ Object (readonly)
Options for this compiler.
132 133 134 |
# File 'lib/buildr/core/compile.rb', line 132 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.
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 |
.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.
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_sym ⇒ Object
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 |
#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.
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.
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). > oldest } end |