stylegen 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6880c6848932f2c2f954860a1885cfd20fda16e99f6ea01dd8c5fe5731658c45
4
- data.tar.gz: b7f49e94a5c19dbe464b3c246cf1834b7477b118c006aea1eed9ebd0c548acd6
3
+ metadata.gz: 5c648fda564306d4dc88dab32098c2d6fb0a355e16adb0c352eec6b5ca7cea23
4
+ data.tar.gz: 0517ac0a864f9663f508376d3ab71859a248f82c028fbb14bd2eb33ac8ce6658
5
5
  SHA512:
6
- metadata.gz: d16722e424e033cd0d29b73ffa65358de4bdc490a3bf44736202f403bba405b6d4cb64499646dead5cc6185c71e0708d0977ac9688c4fd3baffd88f69ec6e546
7
- data.tar.gz: 264639f5ff9bf1541f3670cf428c7390ec72d3746144501fa039eae7ce5a4b28f7bd3dfb72cc02ade6ce3ee9db6bdd844646ad1e11657dba9068afd9e70b0c5a
6
+ metadata.gz: 81a4d52f8b5c45938296c7b934069ff6b0906e167d554798da00e893fe38838623ca206d576222dbededddefa6885e2a25e04e29fe56fe93ff2892bda62e0cce
7
+ data.tar.gz: ce866c1b9c3de44234b290d964baa02c819ac814e74541e59f54fa112c0913752ba83281303ab82c5f182ce6f8301a8bf209c1861737ab3e967ad4fe7d2e7cb8
@@ -21,10 +21,7 @@ jobs:
21
21
  steps:
22
22
  - uses: actions/checkout@v2
23
23
  - name: Set up Ruby
24
- # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
25
- # change this to (see https://github.com/ruby/setup-ruby#versioning):
26
- # uses: ruby/setup-ruby@v1
27
- uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
24
+ uses: ruby/setup-ruby@v1
28
25
  with:
29
26
  ruby-version: ${{ matrix.ruby-version }}
30
27
  bundler-cache: true # runs 'bundle install' and caches installed gems automatically
data/.rubocop.yml CHANGED
@@ -3,7 +3,7 @@ require:
3
3
  - rubocop-minitest
4
4
 
5
5
  AllCops:
6
- TargetRubyVersion: 2.4
6
+ TargetRubyVersion: 2.5
7
7
  NewCops: enable
8
8
 
9
9
  Style/StringLiterals:
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ ## v0.3.0
5
+ ### Added
6
+ - SwiftUI support.
7
+
8
+ ## v0.2.0
9
+ ### Added
10
+ - `stylegen init` command.
11
+
12
+ ## v0.1.0
13
+ - Initial release.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stylegen (0.2.0)
4
+ stylegen (0.3.0)
5
5
  dry-inflector (~> 0.2.0)
6
6
  gli (~> 2.1)
7
7
  json_schemer (~> 0.2.0)
@@ -9,38 +9,38 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- ast (2.4.1)
12
+ ast (2.4.2)
13
13
  dry-inflector (0.2.0)
14
14
  ecma-re-validator (0.3.0)
15
15
  regexp_parser (~> 2.0)
16
16
  gli (2.20.0)
17
17
  hana (1.3.7)
18
- json_schemer (0.2.17)
18
+ json_schemer (0.2.18)
19
19
  ecma-re-validator (~> 0.3)
20
20
  hana (~> 1.3)
21
21
  regexp_parser (~> 2.0)
22
22
  uri_template (~> 0.7)
23
- minitest (5.14.3)
23
+ minitest (5.14.4)
24
24
  parallel (1.20.1)
25
- parser (3.0.0.0)
25
+ parser (3.0.1.1)
26
26
  ast (~> 2.4.1)
27
27
  rainbow (3.0.0)
28
28
  rake (13.0.3)
29
- regexp_parser (2.0.3)
30
- rexml (3.2.4)
31
- rubocop (1.8.1)
29
+ regexp_parser (2.1.1)
30
+ rexml (3.2.5)
31
+ rubocop (1.14.0)
32
32
  parallel (~> 1.10)
33
33
  parser (>= 3.0.0.0)
34
34
  rainbow (>= 2.2.2, < 4.0)
35
35
  regexp_parser (>= 1.8, < 3.0)
36
36
  rexml
37
- rubocop-ast (>= 1.2.0, < 2.0)
37
+ rubocop-ast (>= 1.5.0, < 2.0)
38
38
  ruby-progressbar (~> 1.7)
39
39
  unicode-display_width (>= 1.4.0, < 3.0)
40
- rubocop-ast (1.4.0)
41
- parser (>= 2.7.1.5)
42
- rubocop-minitest (0.10.3)
43
- rubocop (>= 0.87, < 2.0)
40
+ rubocop-ast (1.5.0)
41
+ parser (>= 3.0.1.1)
42
+ rubocop-minitest (0.12.1)
43
+ rubocop (>= 0.90, < 2.0)
44
44
  rubocop-rake (0.5.1)
45
45
  rubocop
46
46
  ruby-progressbar (1.11.0)
data/README.md CHANGED
@@ -38,6 +38,9 @@ The generated YAML file will look similar to this:
38
38
  # Path of generated Swift file.
39
39
  output_path: "Colors.swift"
40
40
 
41
+ # Enables SwiftUI support
42
+ # swiftui: false
43
+
41
44
  # Key-value pairs of theme colors.
42
45
  colors:
43
46
 
@@ -101,5 +104,5 @@ colors. The name of the namespacing function gets inferred from the `system_name
101
104
 
102
105
  * ~~`$ stylegen init` command~~
103
106
  * Option to specify target frameworks: AppKit, UIKit, Core Graphics, and SwiftUI
104
- * SwiftUI support
107
+ * ~~SwiftUI support~~
105
108
  * AppKit support
@@ -7,6 +7,9 @@
7
7
  # Path of generated Swift file.
8
8
  output_path: "Colors.swift"
9
9
 
10
+ # Enables SwiftUI support
11
+ # swiftui: false
12
+
10
13
  # Key-value pairs of theme colors.
11
14
  colors:
12
15
 
data/lib/stylegen/data.rb CHANGED
@@ -30,6 +30,10 @@ module Stylegen
30
30
  @data["output_path"]
31
31
  end
32
32
 
33
+ def swiftui?
34
+ @data["swiftui"] || false
35
+ end
36
+
33
37
  def basename
34
38
  File.basename(@data["output_path"])
35
39
  end
@@ -1,126 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "stylegen/data"
4
+ require "stylegen/template"
4
5
 
5
6
  module Stylegen
6
7
  class Generator
7
8
  def initialize(data)
8
9
  @data = Data.new(data)
9
- @file = File.open(@data.output_path, "w")
10
- end
11
-
12
- def out(line)
13
- @file << line << "\n"
14
10
  end
15
11
 
16
12
  def generate
17
- generate_header
18
- generate_struct
19
- generate_colors
20
- generate_extensions
13
+ template_content = File.read(File.join(__dir__, "resources/template.swift.erb"))
14
+ template = Template.new(template_content, @data)
15
+
16
+ file = File.open(@data.output_path, "w")
17
+ file << template.render
18
+ file.close
21
19
  end
22
20
 
23
21
  def stats
24
22
  @stats ||= { output_path: @data.output_path, color_count: @data.colors.count }
25
23
  end
26
-
27
- private
28
-
29
- def generate_header
30
- out "//"
31
- out "// #{@data.basename}"
32
- out "//"
33
- out "// Autogenerated by stylegen (v#{@data.version})"
34
- out "// DO NOT EDIT"
35
- out "//"
36
- out ""
37
- out "import UIKit"
38
- out ""
39
- end
40
-
41
- def generate_struct
42
- out "#{@data.access_level} struct #{@data.struct_name} {"
43
- out ""
44
- out " let uiColor: UIColor"
45
- out ""
46
- out " fileprivate init(white: CGFloat, alpha: CGFloat) {"
47
- out " self.uiColor = UIColor(white: white, alpha: alpha)"
48
- out " }"
49
- out ""
50
- out " fileprivate init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {"
51
- out " self.uiColor = UIColor(red: red, green: green, blue: blue, alpha: alpha)"
52
- out " }"
53
- out ""
54
- out " fileprivate init(_ color: UIColor) {"
55
- out " self.uiColor = color"
56
- out " }"
57
- out ""
58
- out " fileprivate init(light: #{@data.struct_name}, dark: #{@data.struct_name}) {"
59
- out " if #available(iOS 13.0, *) {"
60
- out " self.uiColor = UIColor(dynamicProvider: { (traits: UITraitCollection) -> UIColor in"
61
- out " switch traits.userInterfaceStyle {"
62
- out " case .dark:"
63
- out " return dark.uiColor"
64
- out " default:"
65
- out " return light.uiColor"
66
- out " }"
67
- out " })"
68
- out " } else {"
69
- out " self.uiColor = light.uiColor"
70
- out " }"
71
- out " }"
72
- out ""
73
- out " fileprivate init(base: #{@data.struct_name}, elevated: #{@data.struct_name}) {"
74
- out " if #available(iOS 13.0, *) {"
75
- out " self.uiColor = UIColor(dynamicProvider: { (traits: UITraitCollection) -> UIColor in"
76
- out " switch traits.userInterfaceLevel {"
77
- out " case .elevated:"
78
- out " return elevated.uiColor"
79
- out " default:"
80
- out " return base.uiColor"
81
- out " }"
82
- out " })"
83
- out " } else {"
84
- out " self.uiColor = base.uiColor"
85
- out " }"
86
- out " }"
87
- out ""
88
- out "}"
89
- out ""
90
- end
91
-
92
- def generate_colors
93
- out "#{@data.access_level} extension #{@data.struct_name} {"
94
- out ""
95
-
96
- @data.colors.each do |member, color|
97
- out " static let #{member} = #{color.to_s(@data.struct_name, 4)}"
98
- out ""
99
- end
100
-
101
- out "}"
102
- out ""
103
- end
104
-
105
- def generate_extensions
106
- out "#{@data.access_level} extension UIColor {"
107
- out ""
108
- out " @inline(__always)"
109
- out " static func #{@data.util_method_name}(_ color: #{@data.struct_name}) -> UIColor {"
110
- out " return color.uiColor"
111
- out " }"
112
- out ""
113
- out "}"
114
- out ""
115
-
116
- out "#{@data.access_level} extension CGColor {"
117
- out ""
118
- out " @inline(__always)"
119
- out " static func #{@data.util_method_name}(_ color: #{@data.struct_name}) -> CGColor {"
120
- out " return color.uiColor.cgColor"
121
- out " }"
122
- out ""
123
- out "}"
124
- end
125
24
  end
126
25
  end
@@ -83,6 +83,11 @@
83
83
  "minLength": 1,
84
84
  "description": "Path of generated Swift file."
85
85
  },
86
+ "swiftui": {
87
+ "type": "boolean",
88
+ "default": false,
89
+ "description": "Enables SwiftUI support."
90
+ },
86
91
  "colors": {
87
92
  "type": "object",
88
93
  "description": "Key-value pairs of colors.",
@@ -0,0 +1,97 @@
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
+ }
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "erb"
4
+
5
+ module Stylegen
6
+ class Template
7
+ def initialize(content, data)
8
+ @content = content
9
+ @data = data
10
+ end
11
+
12
+ def render
13
+ ERB.new(@content, nil, "-").result(binding)
14
+ end
15
+ end
16
+ end
@@ -21,7 +21,7 @@ module Stylegen
21
21
  private
22
22
 
23
23
  def schema
24
- @schema ||= JSONSchemer.schema(File.read(File.join(__dir__, "schema.json")))
24
+ @schema ||= JSONSchemer.schema(File.read(File.join(__dir__, "resources/schema.json")))
25
25
  end
26
26
  end
27
27
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Stylegen
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/stylegen.gemspec CHANGED
@@ -27,5 +27,5 @@ Gem::Specification.new do |s|
27
27
  s.add_development_dependency "rubocop-minitest"
28
28
  s.add_development_dependency "rubocop-rake"
29
29
 
30
- s.required_ruby_version = ">= 2.4.0"
30
+ s.required_ruby_version = ">= 2.5.0"
31
31
  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.2.0
4
+ version: 0.3.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-03-19 00:00:00.000000000 Z
11
+ date: 2021-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-inflector
@@ -147,6 +147,7 @@ files:
147
147
  - ".github/workflows/ci.yml"
148
148
  - ".gitignore"
149
149
  - ".rubocop.yml"
150
+ - CHANGELOG.md
150
151
  - Gemfile
151
152
  - Gemfile.lock
152
153
  - LICENSE
@@ -163,7 +164,9 @@ files:
163
164
  - lib/stylegen/colors/light_dark_color.rb
164
165
  - lib/stylegen/data.rb
165
166
  - lib/stylegen/generator.rb
166
- - lib/stylegen/schema.json
167
+ - lib/stylegen/resources/schema.json
168
+ - lib/stylegen/resources/template.swift.erb
169
+ - lib/stylegen/template.rb
167
170
  - lib/stylegen/validator.rb
168
171
  - lib/stylegen/version.rb
169
172
  - stylegen.gemspec
@@ -183,14 +186,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
183
186
  requirements:
184
187
  - - ">="
185
188
  - !ruby/object:Gem::Version
186
- version: 2.4.0
189
+ version: 2.5.0
187
190
  required_rubygems_version: !ruby/object:Gem::Requirement
188
191
  requirements:
189
192
  - - ">="
190
193
  - !ruby/object:Gem::Version
191
194
  version: '0'
192
195
  requirements: []
193
- rubygems_version: 3.0.3
196
+ rubygems_version: 3.0.1
194
197
  signing_key:
195
198
  specification_version: 4
196
199
  summary: Tool for generating styling code for iOS apps