Class: CapsuleCD::TransformEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/capsulecd/base/transform_engine.rb

Instance Method Summary collapse

Constructor Details

#initializeTransformEngine

Returns a new instance of TransformEngine.



7
8
# File 'lib/capsulecd/base/transform_engine.rb', line 7

def initialize()
end

Instance Method Details

#populate_engine_extensionObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/capsulecd/base/transform_engine.rb', line 25

def populate_engine_extension()
  # lets loop though all the keys, and raise an error if the hooks specified are not available.
  if @type == :repo
    @config.keys.each do |key|
       fail CapsuleCD::Error::EngineTransformUnavailableStep, key + ' cannot be overridden by repo capsule.yml file.' if %w(source_configure source_process_pull_request_payload source_process_push_payload runner_retrieve_payload).include?(key)
    end
  end

  # yeah yeah, metaprogramming is evil. But actually its really just a powerful tool, albeit a really complicated one.
  # Like any tool, you can use it incorrectly. With great power comes.. yada yada.
  # In general metaprogramming is bad because it makes it hard to reason about your code. In this case we're using it
  # to allow other developers to override our engine steps, and/or attach to our hooks. In this case its their
  # responsibility not to f*&^ it all up.

  # http://www.monkeyandcrow.com/blog/building_classes_dynamically/
  # https://rubymonk.com/learning/books/5-metaprogramming-ruby-ascent/chapters/24-eval/lessons/68-class-eval
  # https://www.ruby-forum.com/topic/207350
  @config.each do |step, value|
    next unless %w(source_configure source_process_pull_request_payload source_process_push_payload runner_retrieve_payload build_step test_step package_step source_release release_step).include?(step)

    value.each do |prefix, method_script|
      EngineExtension.class_eval(<<-METHOD
      def #{prefix == 'override' ? '' : prefix+ '_'}#{step};
        #{method_script};
      end
METHOD
)
    end
  end
end

#register_extensionObject

at this point the EngineExtension module should be populated with all the hooks and methods. now we need to add them to the engine.



58
59
60
# File 'lib/capsulecd/base/transform_engine.rb', line 58

def register_extension
  @engine.class.prepend(CapsuleCD::EngineExtension)
end

#transform(engine, config_file, type = :repo) ⇒ Object

type can only be :repo or :global



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/capsulecd/base/transform_engine.rb', line 10

def transform(engine, config_file, type = :repo) #type can only be :repo or :global
  @engine = engine
  @type = type

  if !config_file || !File.exists?(config_file)
    puts "no #{type} configuration file found, no engine hooks applied"
    return
  end

  #parse the config file and generate a module file that we can use as part of our engine
  @config = YAML.load(File.open(config_file).read)
  populate_engine_extension
  register_extension
end