Class: FigmaTemplateGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/admiral-tools-figma/helper/figma/figma_template_generator/figma_template_generator.rb

Instance Method Summary collapse

Instance Method Details

#generate(styles:, style_types:, template_path:, output_path:) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/admiral-tools-figma/helper/figma/figma_template_generator/figma_template_generator.rb', line 6

def generate(styles:, style_types:, template_path:, output_path:)
  require 'liquid'
  Liquid::Template.register_filter(StringFilters)

  styles = styles.select { |s| (style_types.nil? || style_types.include?(s.style_type)) }
  template_data = StylesTemplate.new(styles: styles).to_hash

  dir_name = File.dirname(output_path)
  FileUtils.mkdir_p(dir_name) unless File.directory?(dir_name)

  template = File.read(template_path)
  output = Liquid::Template.parse(template).render(template_data)
  output_file = File.open(output_path, 'w')
  output_file.write(output)
  output_file.close unless output.nil?
end