stylegen 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4808a4d66b66694ce4df774bc4fc3cc4048e5eb28849bc9d6e89c191a2c9f32
4
- data.tar.gz: 639451c4ee1913d543c1b10cd3ed6dc7bf8f70b5711ac490ca1644d28b88bd88
3
+ metadata.gz: 1f3c7f3c64e0c2b99ca8ab6fa2f200f4a47af401b9e1c2547983eaed4f6a339e
4
+ data.tar.gz: 870d75d1e851c2cf8d1f8958f0eed6ceff39b2537d28f81e199a6cb8f683caee
5
5
  SHA512:
6
- metadata.gz: 3ac5a414042b33bef240224d8390d79e92466b777a98bd9ffa0dcd20fe63b4b3353823d26dcc129655bd3d534b1eca2b7c9950b2b6e1764b5d7e30e8e1381ac2
7
- data.tar.gz: 8d2e762fef43e854ca222e9fba107fb8df885d2ae4b2d0ab44eed03c6f7e0c5f61138df701954fd479b4dcbe2f1698b18ae4a140423a625bab83ccff95b0eec5
6
+ metadata.gz: 9e18ecf6503daef89a5cc9fdc9f4fda1a90a2872ff701f750d924a2e3d4f4efedc3ca74cd9a4803fd4b3ee2001defc877e07d40feada9cfa96c845fa3c0e79d2
7
+ data.tar.gz: 8365c8bad165c4f518502185e5f639c5e9beebe664c6018d2ea9ec1175bc6f720fa8dc6fc21b879fb76a9dea59e1e071222a360f0235519fcae5a72a087e5440
data/CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## v0.4.0
5
+ ### Added
6
+ - Added support for color descriptions.
7
+
4
8
  ## v0.3.1
5
9
  ### Changed
6
10
  - Increased precision of HEX color conversion to avoid truncation.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stylegen (0.3.1)
4
+ stylegen (0.4.0)
5
5
  dry-inflector (~> 0.2.0)
6
6
  gli (~> 2.1)
7
7
  json_schemer (~> 0.2.0)
data/README.md CHANGED
@@ -68,6 +68,7 @@ colors:
68
68
  alpha: 0.6
69
69
 
70
70
  primary_background:
71
+ description: The color for the main background of your interface.
71
72
  light: "#FFFFFF"
72
73
  dark:
73
74
  # Value for base (non-elevated) level
@@ -37,6 +37,8 @@ colors:
37
37
  alpha: 0.6
38
38
 
39
39
  primary_background:
40
+ # Use `description` to document your colors (optional).
41
+ description: The color for the main background of your interface.
40
42
  light: "#FFFFFF"
41
43
  dark:
42
44
  # Value for base (non-elevated) level
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Stylegen
4
4
  class BaseElevatedColor
5
+ attr_reader :description
6
+
5
7
  def initialize(base, elevated)
6
8
  @base, @elevated = base, elevated
7
9
  end
data/lib/stylegen/data.rb CHANGED
@@ -46,9 +46,13 @@ module Stylegen
46
46
  "#{system_name}Color"
47
47
  end
48
48
 
49
- def colors
50
- @colors ||= @data["colors"].map do |key, value|
51
- [inflector.camelize_lower(key), generate_color(value)]
49
+ def color_entries
50
+ @color_entries ||= @data["colors"].map do |key, value|
51
+ {
52
+ property: inflector.camelize_lower(key),
53
+ description: value["description"],
54
+ color: generate_color(value)
55
+ }
52
56
  end
53
57
  end
54
58
 
@@ -10,8 +10,7 @@ module Stylegen
10
10
  end
11
11
 
12
12
  def generate
13
- template_content = File.read(File.join(__dir__, "resources/template.swift.erb"))
14
- template = Template.new(template_content, @data)
13
+ template = Template.new(@data)
15
14
 
16
15
  file = File.open(@data.output_path, "w")
17
16
  file << template.render
@@ -19,7 +18,7 @@ module Stylegen
19
18
  end
20
19
 
21
20
  def stats
22
- @stats ||= { output_path: @data.output_path, color_count: @data.colors.count }
21
+ @stats ||= { output_path: @data.output_path, color_count: @data.color_entries.count }
23
22
  end
24
23
  end
25
24
  end
@@ -26,6 +26,9 @@
26
26
  "Color": {
27
27
  "type": "object",
28
28
  "properties": {
29
+ "description": {
30
+ "type": "string"
31
+ },
29
32
  "color": {
30
33
  "$ref": "#/definitions/HexColor"
31
34
  },
@@ -41,6 +44,9 @@
41
44
  "BaseElevatedColor": {
42
45
  "type": "object",
43
46
  "properties": {
47
+ "description": {
48
+ "type": "string"
49
+ },
44
50
  "base": {
45
51
  "$ref": "#/definitions/AbstractColor"
46
52
  },
@@ -54,6 +60,9 @@
54
60
  "LightDarkColor": {
55
61
  "type": "object",
56
62
  "properties": {
63
+ "description": {
64
+ "type": "string"
65
+ },
57
66
  "light": {
58
67
  "$ref": "#/definitions/AbstractColor"
59
68
  },
@@ -107,4 +116,4 @@
107
116
  "output_path",
108
117
  "colors"
109
118
  ]
110
- }
119
+ }
@@ -1,16 +1,157 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "erb"
4
-
5
3
  module Stylegen
6
4
  class Template
7
- def initialize(content, data)
8
- @content = content
5
+ attr_reader :data
6
+
7
+ def initialize(data)
9
8
  @data = data
10
9
  end
11
10
 
12
11
  def render
13
- ERB.new(@content, nil, "-").result(binding)
12
+ result = []
13
+ result << render_header
14
+ result << render_imports
15
+ result << render_struct
16
+ result << render_colors
17
+ result << render_utils
18
+ result.join("\n")
19
+ end
20
+
21
+ private
22
+
23
+ def render_header
24
+ <<~HEREDOC
25
+ //
26
+ // #{data.basename}
27
+ //
28
+ // Autogenerated by stylegen (#{data.version})
29
+ // DO NOT EDIT
30
+ //
31
+ HEREDOC
32
+ end
33
+
34
+ def render_imports
35
+ result = []
36
+ result << "import UIKit"
37
+ result << "import SwiftUI" if data.swiftui?
38
+ result << ""
39
+ result.join("\n")
40
+ end
41
+
42
+ def render_struct
43
+ <<~HEREDOC
44
+ #{data.access_level} struct #{data.struct_name} {
45
+
46
+ let uiColor: UIColor
47
+
48
+ fileprivate init(white: CGFloat, alpha: CGFloat) {
49
+ self.uiColor = UIColor(white: white, alpha: alpha)
50
+ }
51
+
52
+ fileprivate init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
53
+ self.uiColor = UIColor(red: red, green: green, blue: blue, alpha: alpha)
54
+ }
55
+
56
+ fileprivate init(_ color: UIColor) {
57
+ self.uiColor = color
58
+ }
59
+
60
+ fileprivate init(light: #{data.struct_name}, dark: #{data.struct_name}) {
61
+ if #available(iOS 13.0, *) {
62
+ self.uiColor = UIColor(dynamicProvider: { (traits: UITraitCollection) -> UIColor in
63
+ switch traits.userInterfaceStyle {
64
+ case .dark:
65
+ return dark.uiColor
66
+ default:
67
+ return light.uiColor
68
+ }
69
+ })
70
+ } else {
71
+ self.uiColor = light.uiColor
72
+ }
73
+ }
74
+
75
+ fileprivate init(base: #{data.struct_name}, elevated: #{data.struct_name}) {
76
+ if #available(iOS 13.0, *) {
77
+ self.uiColor = UIColor(dynamicProvider: { (traits: UITraitCollection) -> UIColor in
78
+ switch traits.userInterfaceLevel {
79
+ case .elevated:
80
+ return elevated.uiColor
81
+ default:
82
+ return base.uiColor
83
+ }
84
+ })
85
+ } else {
86
+ self.uiColor = base.uiColor
87
+ }
88
+ }
89
+
90
+ }
91
+ HEREDOC
92
+ end
93
+
94
+ def render_colors
95
+ result = []
96
+ result << "// MARK: Colors"
97
+ result << ""
98
+ result << "#{data.access_level} extension #{data.struct_name} {"
99
+ result << ""
100
+
101
+ data.color_entries.each do |entry|
102
+ unless entry[:description].nil?
103
+ entry[:description].strip.lines.each do |line|
104
+ result << " /// #{line.strip}"
105
+ end
106
+ end
107
+
108
+ result << " static let #{entry[:property]} = #{entry[:color].to_s(data.struct_name, 4)}\n"
109
+ end
110
+
111
+ result << "}"
112
+ result << ""
113
+ result.join("\n")
114
+ end
115
+
116
+ def render_utils
117
+ result = []
118
+ result << "// MARK: Utils"
119
+ result << ""
120
+
121
+ if data.swiftui?
122
+ result << <<~HEREDOC
123
+ #{data.access_level} extension Color {
124
+
125
+ @inline(__always)
126
+ static func #{data.util_method_name}(_ color: #{data.struct_name}) -> Color {
127
+ return Color(color.uiColor)
128
+ }
129
+
130
+ }
131
+ HEREDOC
132
+ end
133
+
134
+ result << <<~HEREDOC
135
+ #{data.access_level} extension UIColor {
136
+
137
+ @inline(__always)
138
+ static func #{data.util_method_name}(_ color: #{data.struct_name}) -> UIColor {
139
+ return color.uiColor
140
+ }
141
+
142
+ }
143
+
144
+ #{data.access_level} extension CGColor {
145
+
146
+ @inline(__always)
147
+ static func #{data.util_method_name}(_ color: #{data.struct_name}) -> CGColor {
148
+ return color.uiColor.cgColor
149
+ }
150
+
151
+ }
152
+ HEREDOC
153
+
154
+ result.join("\n")
14
155
  end
15
156
  end
16
157
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Stylegen
4
- VERSION = "0.3.1"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stylegen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ramon Torres
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-22 00:00:00.000000000 Z
11
+ date: 2022-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-inflector
@@ -165,7 +165,6 @@ files:
165
165
  - lib/stylegen/data.rb
166
166
  - lib/stylegen/generator.rb
167
167
  - lib/stylegen/resources/schema.json
168
- - lib/stylegen/resources/template.swift.erb
169
168
  - lib/stylegen/template.rb
170
169
  - lib/stylegen/validator.rb
171
170
  - lib/stylegen/version.rb
@@ -193,7 +192,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
192
  - !ruby/object:Gem::Version
194
193
  version: '0'
195
194
  requirements: []
196
- rubygems_version: 3.0.3
195
+ rubyforge_project:
196
+ rubygems_version: 2.7.6
197
197
  signing_key:
198
198
  specification_version: 4
199
199
  summary: Tool for generating styling code for iOS apps
@@ -1,97 +0,0 @@
1
- //
2
- // <%= @data.basename %>
3
- //
4
- // Autogenerated by stylegen (<%= @data.version %>)
5
- // DO NOT EDIT
6
- //
7
-
8
- import UIKit
9
- <% if @data.swiftui? -%>
10
- import SwiftUI
11
- <% end -%>
12
-
13
- <%= @data.access_level %> struct <%= @data.struct_name %> {
14
-
15
- let uiColor: UIColor
16
-
17
- fileprivate init(white: CGFloat, alpha: CGFloat) {
18
- self.uiColor = UIColor(white: white, alpha: alpha)
19
- }
20
-
21
- fileprivate init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
22
- self.uiColor = UIColor(red: red, green: green, blue: blue, alpha: alpha)
23
- }
24
-
25
- fileprivate init(_ color: UIColor) {
26
- self.uiColor = color
27
- }
28
-
29
- fileprivate init(light: <%= @data.struct_name %>, dark: <%= @data.struct_name %>) {
30
- if #available(iOS 13.0, *) {
31
- self.uiColor = UIColor(dynamicProvider: { (traits: UITraitCollection) -> UIColor in
32
- switch traits.userInterfaceStyle {
33
- case .dark:
34
- return dark.uiColor
35
- default:
36
- return light.uiColor
37
- }
38
- })
39
- } else {
40
- self.uiColor = light.uiColor
41
- }
42
- }
43
-
44
- fileprivate init(base: <%= @data.struct_name %>, elevated: <%= @data.struct_name %>) {
45
- if #available(iOS 13.0, *) {
46
- self.uiColor = UIColor(dynamicProvider: { (traits: UITraitCollection) -> UIColor in
47
- switch traits.userInterfaceLevel {
48
- case .elevated:
49
- return elevated.uiColor
50
- default:
51
- return base.uiColor
52
- }
53
- })
54
- } else {
55
- self.uiColor = base.uiColor
56
- }
57
- }
58
-
59
- }
60
-
61
- // MARK: Colors
62
-
63
- <%= @data.access_level %> extension <%= @data.struct_name %> {
64
- <% @data.colors.each do |member, color| %>
65
- static let <%= member %> = <%= color.to_s(@data.struct_name, 4) %>
66
- <% end %>
67
- }
68
-
69
- // MARK: - Extensions
70
- <% if @data.swiftui? %>
71
- <%= @data.access_level %> extension Color {
72
-
73
- @inline(__always)
74
- static func <%= @data.util_method_name %>(_ color: <%= @data.struct_name %>) -> Color {
75
- return Color(color.uiColor)
76
- }
77
-
78
- }
79
- <% end -%>
80
-
81
- <%= @data.access_level %> extension UIColor {
82
-
83
- @inline(__always)
84
- static func <%= @data.util_method_name %>(_ color: <%= @data.struct_name %>) -> UIColor {
85
- return color.uiColor
86
- }
87
-
88
- }
89
-
90
- <%= @data.access_level %> extension CGColor {
91
-
92
- @inline(__always)
93
- static func <%= @data.util_method_name %>(_ color: <%= @data.struct_name %>) -> CGColor {
94
- return color.uiColor.cgColor
95
- }
96
-
97
- }