lib/stylegen/generator.rb in stylegen-0.2.0 vs lib/stylegen/generator.rb in stylegen-0.3.0
- old
+ new
@@ -1,126 +1,25 @@
# frozen_string_literal: true
require "stylegen/data"
+require "stylegen/template"
module Stylegen
class Generator
def initialize(data)
@data = Data.new(data)
- @file = File.open(@data.output_path, "w")
end
- def out(line)
- @file << line << "\n"
- end
-
def generate
- generate_header
- generate_struct
- generate_colors
- generate_extensions
+ template_content = File.read(File.join(__dir__, "resources/template.swift.erb"))
+ template = Template.new(template_content, @data)
+
+ file = File.open(@data.output_path, "w")
+ file << template.render
+ file.close
end
def stats
@stats ||= { output_path: @data.output_path, color_count: @data.colors.count }
- end
-
- private
-
- def generate_header
- out "//"
- out "// #{@data.basename}"
- out "//"
- out "// Autogenerated by stylegen (v#{@data.version})"
- out "// DO NOT EDIT"
- out "//"
- out ""
- out "import UIKit"
- out ""
- end
-
- def generate_struct
- out "#{@data.access_level} struct #{@data.struct_name} {"
- out ""
- out " let uiColor: UIColor"
- out ""
- out " fileprivate init(white: CGFloat, alpha: CGFloat) {"
- out " self.uiColor = UIColor(white: white, alpha: alpha)"
- out " }"
- out ""
- out " fileprivate init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {"
- out " self.uiColor = UIColor(red: red, green: green, blue: blue, alpha: alpha)"
- out " }"
- out ""
- out " fileprivate init(_ color: UIColor) {"
- out " self.uiColor = color"
- out " }"
- out ""
- out " fileprivate init(light: #{@data.struct_name}, dark: #{@data.struct_name}) {"
- out " if #available(iOS 13.0, *) {"
- out " self.uiColor = UIColor(dynamicProvider: { (traits: UITraitCollection) -> UIColor in"
- out " switch traits.userInterfaceStyle {"
- out " case .dark:"
- out " return dark.uiColor"
- out " default:"
- out " return light.uiColor"
- out " }"
- out " })"
- out " } else {"
- out " self.uiColor = light.uiColor"
- out " }"
- out " }"
- out ""
- out " fileprivate init(base: #{@data.struct_name}, elevated: #{@data.struct_name}) {"
- out " if #available(iOS 13.0, *) {"
- out " self.uiColor = UIColor(dynamicProvider: { (traits: UITraitCollection) -> UIColor in"
- out " switch traits.userInterfaceLevel {"
- out " case .elevated:"
- out " return elevated.uiColor"
- out " default:"
- out " return base.uiColor"
- out " }"
- out " })"
- out " } else {"
- out " self.uiColor = base.uiColor"
- out " }"
- out " }"
- out ""
- out "}"
- out ""
- end
-
- def generate_colors
- out "#{@data.access_level} extension #{@data.struct_name} {"
- out ""
-
- @data.colors.each do |member, color|
- out " static let #{member} = #{color.to_s(@data.struct_name, 4)}"
- out ""
- end
-
- out "}"
- out ""
- end
-
- def generate_extensions
- out "#{@data.access_level} extension UIColor {"
- out ""
- out " @inline(__always)"
- out " static func #{@data.util_method_name}(_ color: #{@data.struct_name}) -> UIColor {"
- out " return color.uiColor"
- out " }"
- out ""
- out "}"
- out ""
-
- out "#{@data.access_level} extension CGColor {"
- out ""
- out " @inline(__always)"
- out " static func #{@data.util_method_name}(_ color: #{@data.struct_name}) -> CGColor {"
- out " return color.uiColor.cgColor"
- out " }"
- out ""
- out "}"
end
end
end