Class: PipelineCompiler

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

Instance Method Summary collapse

Constructor Details

#initialize(mainPipelinePath, globalAliasesPath, partialsFolder, verbose = false) ⇒ PipelineCompiler

Returns a new instance of PipelineCompiler.



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

def initialize(mainPipelinePath, globalAliasesPath, partialsFolder, verbose = false)
  loadGlobalAliases(globalAliasesPath)
  loadMainPipeline(mainPipelinePath)
  loadPartials(partialsFolder)
  @verbose = verbose
end

Instance Method Details

#globalAliasesObject



82
83
84
# File 'lib/ctpl/compiler.rb', line 82

def globalAliases
  @globalAliases
end

#loadGlobalAliases(globalAliasesPath) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/ctpl/compiler.rb', line 49

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



60
61
62
63
64
65
66
67
68
69
# File 'lib/ctpl/compiler.rb', line 60

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



71
72
73
74
75
76
77
78
79
80
# File 'lib/ctpl/compiler.rb', line 71

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



86
87
88
# File 'lib/ctpl/compiler.rb', line 86

def mainPipeline
  @mainPipeline
end

#mergeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ctpl/compiler.rb', line 13

def merge
  pipeline = {}
  say_status 'ok', "Parsing main/global pipeline file", :white if @verbose

  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', "starting to handle partial: #{filename}", :white
    partialAsHashmap = parseToYaml(partialContent)

    say_status 'step', File.basename(filename, '.yaml') + ": merging hashmaps jobs, resources, types and groups", :white if @verbose
    %w(jobs resources resource_types groups).each do |mergeKey|
      say_status 'step', File.basename(filename, '.yaml') + ": merging #{mergeKey}", :white if @verbose
      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
    say_status 'step', File.basename(filename, '.yaml') + ": merging all the rest of the partial", :white if @verbose
    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



44
45
46
47
# File 'lib/ctpl/compiler.rb', line 44

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