Module: Grably::Core::ProductExpand

Extended by:
ProductFilter
Defined in:
lib/grably/core/product.rb

Overview

Product expansion rules. Expand is mapping from anything(almost) to list of products

We have following expansion rules:

  • Symbol can represent following entities:

    • ‘:task_deps` - all all backets from all task prerequisites

    • ‘:last_job` - result of last executed job

    • any other symbol - backet of task with that name

  • Hash describes mapping from product expandable expression to filter in form of ‘{ expr => filter }`. Filter can be either string or lambda (proc).

    • If filter is a String it can be:

      • glob pattern (‘gN`)

      • negative glob pattern, e.g. ‘!` is prepended (`!gN`)

      • filter with substitution (can be in two forms):

      • ‘base:glob` - select every file starting with `base`, strip `base` from beginning, apply glob to the rest of path

      • ‘new_base:old_base:glob` - select every file starting with base, strip `old_base`, apply glob to the rest of path, add `new_base`

      Every operation is performed over Product ‘dst`, not `src`.

    `glob` means any glob (either simple glob or negative glob)
    
    • If filter is ‘Proc`:

      • this proc must have arity of 2

      • first argument is expanded expression ‘expr`

      • second argument is contexted expand function (which means it have proper task argument)

  • ‘Array` expansion is expanding each element

  • ‘String` expands as glob pattern If string is path to file, then it expands into single element array of products. Else (i.e. string is directory) it expands into array of products representing directory content.

  • Task expands to its backet. Behaving strictly we can only get task backet of self, i.e. target_task == context_task, or if target_task is context_task prerequisite (direct or indirect).

  • Product expands to self

Constant Summary collapse

METHOD_TABLE =

We define method table for expand rules. Key is object class, value is method.

Initially we just define set of Class to Symbol mappings. Then calling ProductExpand.singleton_method on each method name. As bonus following this technique we will get NameError immediately after module load.

Hash[*{
  Symbol => :expand_symbol,
  Hash => :expand_hash,
  Array => :expand_array,
  String => :expand_string,
  Proc => :expand_proc,
  Grably::Core::Task => :expand_task,
  Product => :expand_product
}
.flat_map { |k, v| [k, ProductExpand.singleton_method(v)] }]
.freeze

Constants included from ProductFilter

Grably::Core::ProductFilter::FILTER_STRING_GROUPS, Grably::Core::ProductFilter::FILTER_STRING_REGEXP

Class Method Summary collapse

Methods included from ProductFilter

filter_products, generate_glob_filter, generate_string_filter, parse_string_filter

Class Method Details

.check_dependencies(target_task, context_task) ⇒ Object

Ensure that target task is among context_task dependencies



211
212
213
214
# File 'lib/grably/core/product.rb', line 211

def check_dependencies(target_task, context_task)
  return true if target_task == context_task
  context_task.all_prerequisite_tasks.include?(target_task)
end

.expand(srcs, task = nil) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/grably/core/product.rb', line 110

def expand(srcs, task = nil)
  # Wrap o in Array to simplify processing flow
  srcs = [srcs] unless srcs.is_a? Array
  # First typed expand will be array expand. So we will get array as
  # result
  typed_expand(srcs, task)
end

.expand_array(elements, task) ⇒ Object



137
138
139
# File 'lib/grably/core/product.rb', line 137

def expand_array(elements, task)
  elements.flat_map { |e| typed_expand(e, task) }
end

.expand_hash(hash, task) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/grably/core/product.rb', line 128

def expand_hash(hash, task)
  hash.flat_map do |expr, filter|
    # If got string generate lambda representing filter operation
    filter = generate_string_filter(filter) if filter.is_a? String
    raise 'Filter is not a proc %s'.format(filter) unless filter.is_a?(Proc)
    filter.call(typed_expand(expr, task), ->(o) { expand(o, task) })
  end
end

.expand_proc(proc, task) ⇒ Object



154
155
156
# File 'lib/grably/core/product.rb', line 154

def expand_proc(proc, task)
  proc.call(->(o) { expand(o, task) })
end

.expand_product(product, _) ⇒ Object



170
171
172
# File 'lib/grably/core/product.rb', line 170

def expand_product(product, _)
  product
end

.expand_string(expr, _task) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/grably/core/product.rb', line 141

def expand_string(expr, _task)
  unless File.exist?(expr)
    warn "'#{expr}' does not exist. Can't expand path"
    return []
  end
  # Will expand recursively over directory content.
  if File.directory?(expr)
    expand_dir(expr)
  else
    Product.new(expr)
  end
end

.expand_symbol(symbol, task) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/grably/core/product.rb', line 118

def expand_symbol(symbol, task)
  case symbol
  when :task_deps
    typed_expand(task.prerequisites.map(&:to_sym), task)
  else
    task_ref = Task[symbol]
    typed_expand(task_ref, task)
  end
end

.expand_task(target_task, context_task) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/grably/core/product.rb', line 158

def expand_task(target_task, context_task)
  # Behaving strictly we can only get task backet of
  # self, i.e. target_task == context_task, or if target_task is
  # context_task prerequisite (direct or indirect).
  unless check_dependencies(target_task, context_task)
    raise(ArgumentError,
          'Target task [%s] is not in context task [%s] prerequisites'
              .format(target_task.name, context_task.name))
  end
  target_task.bucket
end

.typed_expand(element, task) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/grably/core/product.rb', line 194

def typed_expand(element, task)
  # Fetching expand rule for element type
  method_refs = METHOD_TABLE.select { |c, _| element.is_a?(c) }
  raise 'Multiple expands found for %s. Expands %s'.format(element, method_refs.keys) if method_refs.size > 1
  method_ref = method_refs.values.first
  unless method_ref
    err = 'No expand for type: %s. Element is \'%s\''
    raise err.format(element.class, element)
  end
  # Every expand_%something% method follows same contract, so we just
  # passing standard set of arguments
  method_ref.call(element, task)
end