Class: PipelineMerger

Inherits:
Object
  • Object
show all
Includes:
Thor::Shell
Defined in:
lib/ctpl/merger.rb

Instance Method Summary collapse

Constructor Details

#initialize(mainPipelinePath, globalAliasesPath, partialsFolder) ⇒ PipelineMerger

Returns a new instance of PipelineMerger.



6
7
8
9
10
# File 'lib/ctpl/merger.rb', line 6

def initialize(mainPipelinePath, globalAliasesPath, partialsFolder)
  loadGlobalAliases(globalAliasesPath)
  loadMainPipeline(mainPipelinePath)
  loadPartials(partialsFolder)
end

Instance Method Details

#globalAliasesObject



76
77
78
# File 'lib/ctpl/merger.rb', line 76

def globalAliases
  @globalAliases
end

#loadGlobalAliases(globalAliasesPath) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/ctpl/merger.rb', line 43

def loadGlobalAliases(globalAliasesPath)
  if File.exist?(globalAliasesPath)
    say_status 'success', "Global alias file found and loaded from #{globalAliasesPath}", :green

    @globalAliases = File.read(globalAliasesPath)
  else
    say_status 'ok', "No global alias file found at #{globalAliasesPath}", :white
    @globalAliases = ""
  end
end

#loadMainPipeline(mainPipelinePath) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/ctpl/merger.rb', line 54

def loadMainPipeline(mainPipelinePath)
  if File.exist?(mainPipelinePath)
    say_status 'success', "Main pipeline file found and loaded from #{mainPipelinePath}", :green

    @mainPipeline = @globalAliases + "\n" +  File.read(mainPipelinePath)
  else
    say_status 'ok', "No Main pipeline file found at #{mainPipelinePath}", :white
    @mainPipeline = ""
  end
end

#loadPartials(partialsFolder) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/ctpl/merger.rb', line 65

def loadPartials(partialsFolder)
  say_status 'warning', "Partial folder not found at  #{partialsFolder}", :yellow unless Dir.exist?(partialsFolder)

  @partials = {}
  # our partials must start with _
  Dir.glob("#{partialsFolder}/**/_*.yaml") do |f|
    content = File.read(f)
    @partials[f] = @globalAliases + "\n" + content if content
  end
end

#mainPipelineObject



80
81
82
# File 'lib/ctpl/merger.rb', line 80

def mainPipeline
  @mainPipeline
end

#mergeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ctpl/merger.rb', line 12

def merge
  pipeline = {}
  pipeline = parseToYaml(@mainPipeline) unless @mainPipeline == ""

  # that is our one level merger for the special keys we support
  @partials.each do |filename, partialContent|
    say_status 'ok', "merging partial #{filename}", :white
    partialAsHashmap = parseToYaml(partialContent)

    %w(jobs resources resource_types groups).each do |mergeKey|
      if partialAsHashmap.key?(mergeKey)
        pipeline[mergeKey] = [] unless pipeline.key?(mergeKey)
        pipeline[mergeKey].concat partialAsHashmap[mergeKey]
        partialAsHashmap.delete(mergeKey)
      end
    end

    # now put all the others which should not be conflicting. We might do this manually per key to inform
    # when we have conflicts .. since this will override existing keys
    pipeline.merge(partialAsHashmap)
  end

  raise "The pipeline-file is empty, most probably your baseFolder was wrong or it is empty" if pipeline.empty?
  pipeline
end

#parseToYaml(body) ⇒ Object



38
39
40
41
# File 'lib/ctpl/merger.rb', line 38

def parseToYaml(body)
  # add our global aliases before you transform to yaml to ensure we can lookup those
  YAML.load(body)
end