Module: KotlinCodeGenerator

Defined in:
lib/arkana/kotlin_code_generator.rb

Overview

Responsible for generating Kotlin source and test files.

Class Method Summary collapse

Class Method Details

.generate(template_arguments:, config:) ⇒ Object

Generates Kotlin code and test files for the given template arguments.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/arkana/kotlin_code_generator.rb', line 10

def self.generate(template_arguments:, config:)
  kotlin_module_dir = config.result_path

  source_set = config.is_kotlin_multiplatform_module ? "commonMain" : "main"
  test_set = config.is_kotlin_multiplatform_module ? "commonTest" : "test"

  kotlin_sources_dir = File.join(kotlin_module_dir, "src", source_set, config.kotlin_sources_path, config.kotlin_package_name.split("."))
  kotlin_tests_dir = File.join(kotlin_module_dir, "src", test_set, config.kotlin_sources_path, config.kotlin_package_name.split("."))

  if config.should_generate_gradle_build_file
    set_up_kotlin_module(kotlin_module_dir, template_arguments, config)
  end

  set_up_kotlin_interfaces(kotlin_sources_dir, template_arguments, config)
  set_up_kotlin_classes(kotlin_sources_dir, kotlin_tests_dir, template_arguments, config)
end

.render(template, template_arguments, destination_file) ⇒ Object



56
57
58
59
60
# File 'lib/arkana/kotlin_code_generator.rb', line 56

def self.render(template, template_arguments, destination_file)
  renderer = ERB.new(template, trim_mode: ">") # Don't automatically add newlines at the end of each template tag
  result = renderer.result(template_arguments.get_binding)
  File.write(destination_file, result)
end

.set_up_kotlin_classes(sources_dir, tests_dir, template_arguments, config) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/arkana/kotlin_code_generator.rb', line 46

def self.set_up_kotlin_classes(sources_dir, tests_dir, template_arguments, config)
  dirname = File.dirname(__FILE__)
  source_template = File.read("#{dirname}/templates/kotlin/arkana.kt.erb")
  tests_template = File.read("#{dirname}/templates/kotlin/arkana_tests.kt.erb")
  FileUtils.mkdir_p(sources_dir)
  FileUtils.mkdir_p(tests_dir) if config.should_generate_unit_tests
  render(source_template, template_arguments, File.join(sources_dir, "#{config.namespace}.kt"))
  render(tests_template, template_arguments, File.join(tests_dir, "#{config.namespace}Test.kt")) if config.should_generate_unit_tests
end

.set_up_kotlin_interfaces(path, template_arguments, config) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/arkana/kotlin_code_generator.rb', line 38

def self.set_up_kotlin_interfaces(path, template_arguments, config)
  dirname = File.dirname(__FILE__)
  sources_dir = path
  source_template = File.read("#{dirname}/templates/kotlin/arkana_protocol.kt.erb")
  FileUtils.mkdir_p(path)
  render(source_template, template_arguments, File.join(sources_dir, "#{config.namespace}Environment.kt"))
end

.set_up_kotlin_module(path, template_arguments, config) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/arkana/kotlin_code_generator.rb', line 27

def self.set_up_kotlin_module(path, template_arguments, config)
  dirname = File.dirname(__FILE__)
  sources_dir = path
  readme_template = File.read("#{dirname}/templates/readme.erb")
  gradle_template_file = config.is_kotlin_multiplatform_module ? "build_kmp.gradle.kts.erb" : "build.gradle.kts.erb"
  source_template = File.read("#{dirname}/templates/kotlin/#{gradle_template_file}")
  FileUtils.mkdir_p(path)
  render(readme_template, template_arguments, File.join(path, "README.md"))
  render(source_template, template_arguments, File.join(sources_dir, "build.gradle.kts"))
end