Module: SwiftCodeGenerator

Defined in:
lib/arkana/swift_code_generator.rb

Overview

Responsible for generating Swift source and test files.

Class Method Summary collapse

Class Method Details

.generate(template_arguments:, config:) ⇒ Object

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



10
11
12
13
14
15
16
# File 'lib/arkana/swift_code_generator.rb', line 10

def self.generate(template_arguments:, config:)
  interface_swift_package_dir = File.join(config.result_path, "#{config.import_name}Interfaces")
  set_up_interfaces_swift_package(interface_swift_package_dir, template_arguments, config)

  swift_package_dir = File.join(config.result_path, config.import_name)
  set_up_swift_package(swift_package_dir, template_arguments, config)
end

.render(template, template_arguments, destination_file) ⇒ Object



52
53
54
55
56
# File 'lib/arkana/swift_code_generator.rb', line 52

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_interfaces_swift_package(path, template_arguments, config) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/arkana/swift_code_generator.rb', line 18

def self.set_up_interfaces_swift_package(path, template_arguments, config)
  dirname = File.dirname(__FILE__)
  readme_template = File.read("#{dirname}/templates/interfaces_readme.erb")
  package_template = File.read("#{dirname}/templates/interfaces_package.swift.erb")
  podspec_template = File.read("#{dirname}/templates/interfaces.podspec.erb")
  sources_dir = File.join(path, "Sources")
  source_template = File.read("#{dirname}/templates/arkana_protocol.swift.erb")
  FileUtils.mkdir_p(path)
  FileUtils.mkdir_p(sources_dir)
  render(podspec_template, template_arguments, File.join(path, "#{config.pod_name.capitalize_first_letter}Interfaces.podspec")) if config.package_manager == "cocoapods"
  render(readme_template, template_arguments, File.join(path, "README.md"))
  render(package_template, template_arguments, File.join(path, "Package.swift"))
  render(source_template, template_arguments, File.join(sources_dir, "#{config.import_name}Interfaces.swift"))
end

.set_up_swift_package(path, template_arguments, config) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/arkana/swift_code_generator.rb', line 33

def self.set_up_swift_package(path, template_arguments, config)
  dirname = File.dirname(__FILE__)
  readme_template = File.read("#{dirname}/templates/readme.erb")
  package_template = File.read("#{dirname}/templates/package.swift.erb")
  sources_dir = File.join(path, "Sources")
  source_template = File.read("#{dirname}/templates/arkana.swift.erb")
  tests_dir = File.join(path, "Tests") if config.should_generate_unit_tests
  tests_template = File.read("#{dirname}/templates/arkana_tests.swift.erb")
  podspec_template = File.read("#{dirname}/templates/arkana.podspec.erb")
  FileUtils.mkdir_p(path)
  FileUtils.mkdir_p(sources_dir)
  FileUtils.mkdir_p(tests_dir) if config.should_generate_unit_tests
  render(readme_template, template_arguments, File.join(path, "README.md"))
  render(package_template, template_arguments, File.join(path, "Package.swift"))
  render(podspec_template, template_arguments, File.join(path, "#{config.pod_name.capitalize_first_letter}.podspec")) if config.package_manager == "cocoapods"
  render(source_template, template_arguments, File.join(sources_dir, "#{config.import_name}.swift"))
  render(tests_template, template_arguments, File.join(tests_dir, "#{config.import_name}Tests.swift")) if config.should_generate_unit_tests
end