Class: Makefile::Rule
- Inherits:
-
Object
- Object
- Makefile::Rule
- Defined in:
- lib/makefile.rb
Overview
The Rule class defines a single Makefile rule.
Instance Attribute Summary collapse
-
#dependencies ⇒ Array<String>
A list of dependencies that this rule depends on.
-
#recipe ⇒ Array<String>
A list of commands to execute upon invocation of this rule.
-
#target ⇒ String
The target generated by this rule.
Instance Method Summary collapse
-
#base_target ⇒ String
The base Rule for a Makefile target, including all dependencies.
- #compounded_recipe ⇒ Object
-
#flatten_dependencies ⇒ String, Nil
The name of all dependencies for a given rule, flattened and joined for a Makefule target.
-
#format ⇒ String
(also: #to_s)
Format this rule as a Makefile rule.
-
#initialize(target, dependencies: [], recipe: []) {|_self| ... } ⇒ Rule
constructor
Create a new Makefile rule.
Constructor Details
#initialize(target, dependencies: [], recipe: []) ⇒ Rule #initialize(target, dependencies: [], recipe: []) {|rule| ... } ⇒ Rule
Create a new Makefile rule. When a block is provided then it is called and passed the newly created Rule object.
39 40 41 42 43 44 45 |
# File 'lib/makefile.rb', line 39 def initialize(target, dependencies: [], recipe: [], &block) @target = target @dependencies = dependencies @recipe = recipe yield(self) if block end |
Instance Attribute Details
#dependencies ⇒ Array<String>
Returns A list of dependencies that this rule depends on.
12 13 14 |
# File 'lib/makefile.rb', line 12 def dependencies @dependencies end |
#recipe ⇒ Array<String>
Returns A list of commands to execute upon invocation of this rule.
16 17 18 |
# File 'lib/makefile.rb', line 16 def recipe @recipe end |
#target ⇒ String
Returns The target generated by this rule.
8 9 10 |
# File 'lib/makefile.rb', line 8 def target @target end |
Instance Method Details
#base_target ⇒ String
Returns the base Rule for a Makefile target, including all dependencies.
56 57 58 |
# File 'lib/makefile.rb', line 56 def base_target ["#{target}:", dependencies].flatten.compact.join("\s") end |
#compounded_recipe ⇒ Object
60 61 62 63 64 65 |
# File 'lib/makefile.rb', line 60 def compounded_recipe Array(recipe) .compact .map { |line| "\t" + line.gsub("\n", "\n\t") + "\n" } .join end |
#flatten_dependencies ⇒ String, Nil
Returns the name of all dependencies for a given rule, flattened and joined for a Makefule target.
49 50 51 52 |
# File 'lib/makefile.rb', line 49 def flatten_dependencies return nil if dependencies.empty? dependencies.flatten.join("\s") end |
#format ⇒ String Also known as: to_s
Format this rule as a Makefile rule.
Recipes that have multiline statements will have tabs inserted after each newline to ensure that the recipe is parsed as part of a single makefile rule.
73 74 75 |
# File 'lib/makefile.rb', line 73 def format [base_target, compounded_recipe].flatten.join("\n") end |