Class: Makefile::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/makefile.rb

Overview

The Rule class defines a single Makefile rule.

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Overloads:

  • #initialize(target, dependencies: [], recipe: []) ⇒ Rule

    Parameters:

    • target (String)

      target

    • dependencies (Array<String>) (defaults to: [])

      ]

    • recipe (Array<String>) (defaults to: [])
  • #initialize(target, dependencies: [], recipe: []) {|rule| ... } ⇒ Rule

    Examples:

    Providing a block to #initialize

    newrule = Makefile::Rule.new("test", dependencies: ["all"]) do |rule|
      rule.recipe = [
        "make test",
        "make cpplint",
      ]
    end

    Parameters:

    • target (String)

      target

    • dependencies (Array<String>) (defaults to: [])

      ]

    • recipe (Array<String>) (defaults to: [])

    Yield Parameters:

Yields:

  • (_self)

Yield Parameters:



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

#dependenciesArray<String>

Returns A list of dependencies that this rule depends on.

Returns:

  • (Array<String>)

    A list of dependencies that this rule depends on.



12
13
14
# File 'lib/makefile.rb', line 12

def dependencies
  @dependencies
end

#recipeArray<String>

Returns A list of commands to execute upon invocation of this rule.

Returns:

  • (Array<String>)

    A list of commands to execute upon invocation of this rule.



16
17
18
# File 'lib/makefile.rb', line 16

def recipe
  @recipe
end

#targetString

Returns The target generated by this rule.

Returns:

  • (String)

    The target generated by this rule.



8
9
10
# File 'lib/makefile.rb', line 8

def target
  @target
end

Instance Method Details

#base_targetString

Returns the base Rule for a Makefile target, including all dependencies.

Returns:

  • (String)

    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_recipeObject



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_dependenciesString, Nil

Returns the name of all dependencies for a given rule, flattened and joined for a Makefule target.

Returns:

  • (String, Nil)

    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

#formatString 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.

Returns:



73
74
75
# File 'lib/makefile.rb', line 73

def format
  [base_target, compounded_recipe].flatten.join("\n")
end