Class: KubeDSL::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/kube-dsl/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **kwargs) ⇒ Generator

Returns a new instance of Generator.



7
8
9
# File 'lib/kube-dsl/generator.rb', line 7

def initialize(*args, **kwargs)
  @builder = Builder.new(*args, **kwargs)
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



5
6
7
# File 'lib/kube-dsl/generator.rb', line 5

def builder
  @builder
end

Instance Method Details

#generate(include_rbi: true) ⇒ Object



11
12
13
14
15
# File 'lib/kube-dsl/generator.rb', line 11

def generate(include_rbi: true)
  generate_resource_files(include_rbi: include_rbi)
  generate_autoload_files
  generate_entrypoint_file(include_rbi: include_rbi)
end

#generate_autoload_filesObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/kube-dsl/generator.rb', line 43

def generate_autoload_files
  builder.each_autoload_file do |path, ruby_code|
    if File.exist?(path)
      puts "Skipping #{path} because it already exists"
    else
      puts "Writing #{path}"
      File.write(path, ruby_code)
    end
  end
end

#generate_entrypoint_file(include_rbi: true, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kube-dsl/generator.rb', line 54

def generate_entrypoint_file(include_rbi: true, &block)
  entrypoint_builder = builder.entrypoint(&block)

  if File.exist?(entrypoint_builder.path)
    puts "Skipping #{entrypoint_builder.path} because it already exists"
  else
    entrypoint_ruby = entrypoint_builder.entrypoint

    puts "Writing #{entrypoint_builder.path}"
    File.write(entrypoint_builder.path, entrypoint_ruby)

    if include_rbi
      entrypoint_rbi = entrypoint_builder.entrypoint_rbi

      puts "Writing #{entrypoint_builder.rbi_path}"
      File.write(entrypoint_builder.rbi_path, entrypoint_rbi)
    end
  end
end

#generate_resource_files(include_rbi: true) ⇒ Object



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
# File 'lib/kube-dsl/generator.rb', line 17

def generate_resource_files(include_rbi: true)
  builder.each_resource_file do |path, res|
    FileUtils.mkdir_p(File.dirname(path))

    if File.exist?(path)
      puts "Skipping #{path} because it already exists"
    else
      puts "Writing #{path}"
      File.write(path, res.to_ruby)
    end

    next unless include_rbi

    rbi_path = File.join('sorbet', 'rbi', *path.split(File::SEPARATOR)[1..-1])
    rbi_path = "#{rbi_path.chomp('.rb')}.rbi"
    FileUtils.mkdir_p(File.dirname(rbi_path))

    if File.exist?(rbi_path)
      puts "Skipping #{rbi_path} because it already exists"
    else
      puts "Writing #{rbi_path}"
      File.write(rbi_path, res.to_rbi)
    end
  end
end