stylegen 0.3.1 → 0.5.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 +4 -4
- data/.deepsource.toml +5 -0
- data/.github/workflows/ci.yml +6 -5
- data/.rubocop.yml +0 -17
- data/CHANGELOG.md +9 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +3 -3
- data/README.md +1 -0
- data/Rakefile +3 -3
- data/bin/stylegen +1 -1
- data/lib/stylegen/cli/app.rb +13 -13
- data/lib/stylegen/cli/template.yaml +2 -0
- data/lib/stylegen/cli.rb +1 -1
- data/lib/stylegen/colors/base_elevated_color.rb +5 -2
- data/lib/stylegen/colors/color.rb +14 -6
- data/lib/stylegen/colors/light_dark_color.rb +3 -2
- data/lib/stylegen/colors.rb +3 -3
- data/lib/stylegen/data.rb +46 -19
- data/lib/stylegen/generator.rb +5 -6
- data/lib/stylegen/resources/schema.json +10 -1
- data/lib/stylegen/template.rb +155 -5
- data/lib/stylegen/validator.rb +3 -3
- data/lib/stylegen/version.rb +1 -1
- data/lib/stylegen.rb +5 -5
- data/stylegen.gemspec +18 -18
- data/test/helper.rb +4 -4
- data/test/test_base_elevated_color.rb +5 -5
- data/test/test_color.rb +28 -28
- data/test/test_light_dark_color.rb +5 -5
- metadata +4 -4
- data/lib/stylegen/resources/template.swift.erb +0 -97
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31f78139e4e4008bf37ab9d48bc8e00309b7a04d98be8e0a2d66a281afde0780
|
4
|
+
data.tar.gz: e08e613e70ef4ca0f96989051db35496783caf8c4de3245fb78600d6a72cd15a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa6e4794fb3fa16e907aa5fe272fce946b225de69565ca92dee659dd2ddaae63e6ae141c320366bb05ff8dade91ae5b3acd1cb8ca5f0641ab44cfa909971a4bc
|
7
|
+
data.tar.gz: d0b5c306bb2edc0e1c017ba35df7dfb6445d02fe66b12cc7560999461c7ce4a2549378a47b371466b49771cb7300fb360a50ca6345f62e48c23137d1a58644d2
|
data/.deepsource.toml
ADDED
data/.github/workflows/ci.yml
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
name: CI
|
2
|
-
|
3
2
|
on:
|
4
3
|
push:
|
5
|
-
branches:
|
4
|
+
branches:
|
5
|
+
- master
|
6
6
|
pull_request:
|
7
|
-
branches:
|
7
|
+
branches:
|
8
|
+
- master
|
8
9
|
|
9
10
|
jobs:
|
10
11
|
test:
|
11
|
-
|
12
12
|
runs-on: ubuntu-latest
|
13
13
|
strategy:
|
14
14
|
matrix:
|
@@ -17,9 +17,10 @@ jobs:
|
|
17
17
|
- "2.6"
|
18
18
|
- "2.7"
|
19
19
|
- "3.0"
|
20
|
+
- "3.1"
|
20
21
|
|
21
22
|
steps:
|
22
|
-
- uses: actions/checkout@
|
23
|
+
- uses: actions/checkout@v3
|
23
24
|
- name: Set up Ruby
|
24
25
|
uses: ruby/setup-ruby@v1
|
25
26
|
with:
|
data/.rubocop.yml
CHANGED
@@ -6,17 +6,6 @@ AllCops:
|
|
6
6
|
TargetRubyVersion: 2.5
|
7
7
|
NewCops: enable
|
8
8
|
|
9
|
-
Style/StringLiterals:
|
10
|
-
Enabled: true
|
11
|
-
EnforcedStyle: double_quotes
|
12
|
-
|
13
|
-
Metrics/ClassLength:
|
14
|
-
Enabled: true
|
15
|
-
Max: 150
|
16
|
-
|
17
|
-
Naming/MethodParameterName:
|
18
|
-
Enabled: false
|
19
|
-
|
20
9
|
Metrics/MethodLength:
|
21
10
|
Enabled: false
|
22
11
|
|
@@ -25,9 +14,3 @@ Metrics/AbcSize:
|
|
25
14
|
|
26
15
|
Style/Documentation:
|
27
16
|
Enabled: false
|
28
|
-
|
29
|
-
Style/ParallelAssignment:
|
30
|
-
Enabled: false
|
31
|
-
|
32
|
-
Minitest/AssertInDelta:
|
33
|
-
Enabled: false
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
# Changelog
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
|
4
|
+
## v0.5.0
|
5
|
+
### Changed
|
6
|
+
- Switched generated code from `struct` to `class`.
|
7
|
+
- SwiftUI: Use `Color(uiColor:)` initializer when running on iOS 15+.
|
8
|
+
|
9
|
+
## v0.4.0
|
10
|
+
### Added
|
11
|
+
- Added support for color descriptions.
|
12
|
+
|
4
13
|
## v0.3.1
|
5
14
|
### Changed
|
6
15
|
- Increased precision of HEX color conversion to avoid truncation.
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
stylegen (0.
|
4
|
+
stylegen (0.5.0)
|
5
5
|
dry-inflector (~> 0.2.0)
|
6
6
|
gli (~> 2.1)
|
7
7
|
json_schemer (~> 0.2.0)
|
@@ -13,9 +13,9 @@ GEM
|
|
13
13
|
dry-inflector (0.2.0)
|
14
14
|
ecma-re-validator (0.3.0)
|
15
15
|
regexp_parser (~> 2.0)
|
16
|
-
gli (2.
|
16
|
+
gli (2.21.0)
|
17
17
|
hana (1.3.7)
|
18
|
-
json_schemer (0.2.
|
18
|
+
json_schemer (0.2.24)
|
19
19
|
ecma-re-validator (~> 0.3)
|
20
20
|
hana (~> 1.3)
|
21
21
|
regexp_parser (~> 2.0)
|
data/README.md
CHANGED
data/Rakefile
CHANGED
data/bin/stylegen
CHANGED
data/lib/stylegen/cli/app.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'yaml'
|
4
|
+
require 'gli'
|
5
5
|
|
6
|
-
require
|
7
|
-
require
|
6
|
+
require 'stylegen/validator'
|
7
|
+
require 'stylegen/generator'
|
8
8
|
|
9
9
|
module Stylegen
|
10
10
|
module CLI
|
11
11
|
class App
|
12
12
|
extend GLI::App
|
13
13
|
|
14
|
-
program_desc
|
14
|
+
program_desc 'CLI tool for managing colors in iOS apps'
|
15
15
|
|
16
16
|
version Stylegen::VERSION
|
17
17
|
|
@@ -19,30 +19,30 @@ module Stylegen
|
|
19
19
|
|
20
20
|
# Commands
|
21
21
|
|
22
|
-
desc
|
22
|
+
desc 'Generates a sample theme.yaml file in the current directory'
|
23
23
|
command :init do |c|
|
24
24
|
c.action do
|
25
|
-
exit_now!("'theme.yaml' already exists!") if File.exist?(
|
25
|
+
exit_now!("'theme.yaml' already exists!") if File.exist?('theme.yaml')
|
26
26
|
|
27
|
-
template = File.read(File.join(__dir__,
|
28
|
-
File.write(
|
27
|
+
template = File.read(File.join(__dir__, 'template.yaml'))
|
28
|
+
File.write('theme.yaml', template)
|
29
29
|
|
30
30
|
puts "Generated 'theme.yaml'."
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
desc
|
34
|
+
desc 'Generates the Swift colors file'
|
35
35
|
command :build do |c|
|
36
36
|
c.action do
|
37
|
-
exit_now!("'theme.yaml' not found. Create one with 'stylegen init'.") unless File.exist?(
|
37
|
+
exit_now!("'theme.yaml' not found. Create one with 'stylegen init'.") unless File.exist?('theme.yaml')
|
38
38
|
|
39
|
-
data = File.open(
|
39
|
+
data = File.open('theme.yaml') { |file| YAML.safe_load(file) }
|
40
40
|
|
41
41
|
validator = Validator.new
|
42
42
|
|
43
43
|
unless validator.valid?(data)
|
44
44
|
message = []
|
45
|
-
message <<
|
45
|
+
message << 'theme.yaml contains one or more errors:'
|
46
46
|
|
47
47
|
validator.validate(data).each do |e|
|
48
48
|
message << " #{e}"
|
data/lib/stylegen/cli.rb
CHANGED
@@ -2,12 +2,15 @@
|
|
2
2
|
|
3
3
|
module Stylegen
|
4
4
|
class BaseElevatedColor
|
5
|
+
attr_reader :description
|
6
|
+
|
5
7
|
def initialize(base, elevated)
|
6
|
-
@base
|
8
|
+
@base = base
|
9
|
+
@elevated = elevated
|
7
10
|
end
|
8
11
|
|
9
12
|
def to_s(struct_name, indent = 0)
|
10
|
-
indent_prefix =
|
13
|
+
indent_prefix = ' ' * indent
|
11
14
|
|
12
15
|
result = []
|
13
16
|
result << "#{struct_name}("
|
@@ -4,8 +4,13 @@ module Stylegen
|
|
4
4
|
class Color
|
5
5
|
attr_reader :red, :green, :blue, :alpha
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
MAX_PRECISION = 16
|
8
|
+
|
9
|
+
def initialize(red, green, blue, alpha)
|
10
|
+
@red = red
|
11
|
+
@green = green
|
12
|
+
@blue = blue
|
13
|
+
@alpha = alpha
|
9
14
|
end
|
10
15
|
|
11
16
|
def self.from_hex(hex, alpha = nil)
|
@@ -21,9 +26,12 @@ module Stylegen
|
|
21
26
|
raise ArgumentError, "Invalid color syntax: #{hex}"
|
22
27
|
end
|
23
28
|
|
24
|
-
|
25
|
-
|
26
|
-
|
29
|
+
Color.new(
|
30
|
+
r.round(MAX_PRECISION),
|
31
|
+
g.round(MAX_PRECISION),
|
32
|
+
b.round(MAX_PRECISION),
|
33
|
+
alpha || 1.0
|
34
|
+
)
|
27
35
|
end
|
28
36
|
|
29
37
|
def grayscale?
|
@@ -34,7 +42,7 @@ module Stylegen
|
|
34
42
|
if grayscale?
|
35
43
|
"#{struct_name}(white: #{@red}, alpha: #{@alpha})"
|
36
44
|
else
|
37
|
-
indent_prefix =
|
45
|
+
indent_prefix = ' ' * indent
|
38
46
|
|
39
47
|
result = []
|
40
48
|
result << "#{struct_name}("
|
@@ -3,11 +3,12 @@
|
|
3
3
|
module Stylegen
|
4
4
|
class LightDarkColor
|
5
5
|
def initialize(light, dark)
|
6
|
-
@light
|
6
|
+
@light = light
|
7
|
+
@dark = dark
|
7
8
|
end
|
8
9
|
|
9
10
|
def to_s(struct_name, indent = 0)
|
10
|
-
indent_prefix =
|
11
|
+
indent_prefix = ' ' * indent
|
11
12
|
|
12
13
|
result = []
|
13
14
|
result << "#{struct_name}("
|
data/lib/stylegen/colors.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require 'stylegen/colors/color'
|
4
|
+
require 'stylegen/colors/light_dark_color'
|
5
|
+
require 'stylegen/colors/base_elevated_color'
|
data/lib/stylegen/data.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require 'dry/inflector'
|
4
|
+
require 'stylegen/version'
|
5
|
+
require 'stylegen/colors'
|
6
6
|
|
7
7
|
module Stylegen
|
8
8
|
class Data
|
@@ -14,12 +14,31 @@ module Stylegen
|
|
14
14
|
@inflector ||= Dry::Inflector.new
|
15
15
|
end
|
16
16
|
|
17
|
+
def file_header
|
18
|
+
header = @data['header'] || <<~HEADER
|
19
|
+
//
|
20
|
+
// {{STYLEGEN_FILENAME}}
|
21
|
+
//
|
22
|
+
// Autogenerated by stylegen ({{STYLEGEN_VERSION}})
|
23
|
+
// DO NOT EDIT
|
24
|
+
//
|
25
|
+
HEADER
|
26
|
+
|
27
|
+
replacements = {
|
28
|
+
'STYLEGEN_FILENAME' => basename,
|
29
|
+
'STYLEGEN_VERSION' => version,
|
30
|
+
'STYLEGEN_YEAR' => Date.today.year
|
31
|
+
}
|
32
|
+
|
33
|
+
header.strip.gsub(/{{(\w+)}}/) { replacements[Regexp.last_match(1)] || '' }
|
34
|
+
end
|
35
|
+
|
17
36
|
def version
|
18
37
|
Stylegen::VERSION
|
19
38
|
end
|
20
39
|
|
21
40
|
def system_name
|
22
|
-
@data[
|
41
|
+
@data['system_name'] || 'Theme'
|
23
42
|
end
|
24
43
|
|
25
44
|
def util_method_name
|
@@ -27,28 +46,36 @@ module Stylegen
|
|
27
46
|
end
|
28
47
|
|
29
48
|
def output_path
|
30
|
-
@data[
|
49
|
+
@data['output_path']
|
31
50
|
end
|
32
51
|
|
33
52
|
def swiftui?
|
34
|
-
@data[
|
53
|
+
@data['swiftui'] || false
|
35
54
|
end
|
36
55
|
|
37
56
|
def basename
|
38
|
-
File.basename(@data[
|
57
|
+
File.basename(@data['output_path'])
|
39
58
|
end
|
40
59
|
|
41
60
|
def access_level
|
42
|
-
@data[
|
61
|
+
@data['access_level'] || 'internal'
|
62
|
+
end
|
63
|
+
|
64
|
+
def effective_access_level
|
65
|
+
access_level == 'internal' ? '' : "#{access_level} "
|
43
66
|
end
|
44
67
|
|
45
68
|
def struct_name
|
46
69
|
"#{system_name}Color"
|
47
70
|
end
|
48
71
|
|
49
|
-
def
|
50
|
-
@
|
51
|
-
|
72
|
+
def color_entries
|
73
|
+
@color_entries ||= @data['colors'].map do |key, value|
|
74
|
+
{
|
75
|
+
property: inflector.camelize_lower(key),
|
76
|
+
description: value['description'],
|
77
|
+
color: generate_color(value)
|
78
|
+
}
|
52
79
|
end
|
53
80
|
end
|
54
81
|
|
@@ -57,17 +84,17 @@ module Stylegen
|
|
57
84
|
def generate_color(data)
|
58
85
|
if data.is_a?(String)
|
59
86
|
Color.from_hex(data)
|
60
|
-
elsif data.key?(
|
61
|
-
Color.from_hex(data[
|
62
|
-
elsif data.key?(
|
87
|
+
elsif data.key?('color')
|
88
|
+
Color.from_hex(data['color'], data['alpha'])
|
89
|
+
elsif data.key?('light')
|
63
90
|
LightDarkColor.new(
|
64
|
-
generate_color(data[
|
65
|
-
generate_color(data[
|
91
|
+
generate_color(data['light']),
|
92
|
+
generate_color(data['dark'])
|
66
93
|
)
|
67
|
-
elsif data.key?(
|
94
|
+
elsif data.key?('base')
|
68
95
|
BaseElevatedColor.new(
|
69
|
-
generate_color(data[
|
70
|
-
generate_color(data[
|
96
|
+
generate_color(data['base']),
|
97
|
+
generate_color(data['elevated'])
|
71
98
|
)
|
72
99
|
end
|
73
100
|
end
|
data/lib/stylegen/generator.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'stylegen/data'
|
4
|
+
require 'stylegen/template'
|
5
5
|
|
6
6
|
module Stylegen
|
7
7
|
class Generator
|
@@ -10,16 +10,15 @@ module Stylegen
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def generate
|
13
|
-
|
14
|
-
template = Template.new(template_content, @data)
|
13
|
+
template = Template.new(@data)
|
15
14
|
|
16
|
-
file = File.open(@data.output_path,
|
15
|
+
file = File.open(@data.output_path, 'w')
|
17
16
|
file << template.render
|
18
17
|
file.close
|
19
18
|
end
|
20
19
|
|
21
20
|
def stats
|
22
|
-
@stats ||= { output_path: @data.output_path, color_count: @data.
|
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
|
+
}
|
data/lib/stylegen/template.rb
CHANGED
@@ -1,16 +1,166 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "erb"
|
4
|
-
|
5
3
|
module Stylegen
|
4
|
+
# rubocop:disable Metrics/ClassLength
|
6
5
|
class Template
|
7
|
-
|
8
|
-
|
6
|
+
attr_reader :data
|
7
|
+
|
8
|
+
def initialize(data)
|
9
9
|
@data = data
|
10
10
|
end
|
11
11
|
|
12
12
|
def render
|
13
|
-
|
13
|
+
result = []
|
14
|
+
result << render_header
|
15
|
+
result << render_imports
|
16
|
+
result << render_struct
|
17
|
+
result << render_colors
|
18
|
+
result << render_utils
|
19
|
+
result.join("\n")
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def render_header
|
25
|
+
@data.file_header << "\n"
|
26
|
+
end
|
27
|
+
|
28
|
+
def render_imports
|
29
|
+
result = []
|
30
|
+
result << 'import UIKit'
|
31
|
+
result << 'import SwiftUI' if data.swiftui?
|
32
|
+
result << ''
|
33
|
+
result.join("\n")
|
34
|
+
end
|
35
|
+
|
36
|
+
def render_struct
|
37
|
+
<<~HEREDOC.lstrip
|
38
|
+
#{data.effective_access_level} final class #{data.struct_name} {
|
39
|
+
|
40
|
+
let rawValue: UIColor
|
41
|
+
|
42
|
+
private init(_ rawValue: UIColor) {
|
43
|
+
self.rawValue = rawValue
|
44
|
+
}
|
45
|
+
|
46
|
+
private convenience init(white: CGFloat, alpha: CGFloat) {
|
47
|
+
self.init(
|
48
|
+
UIColor(white: white, alpha: alpha)
|
49
|
+
)
|
50
|
+
}
|
51
|
+
|
52
|
+
private convenience init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
|
53
|
+
self.init(
|
54
|
+
UIColor(red: red, green: green, blue: blue, alpha: alpha)
|
55
|
+
)
|
56
|
+
}
|
57
|
+
|
58
|
+
private convenience init(light: #{data.struct_name}, dark: #{data.struct_name}) {
|
59
|
+
if #available(iOS 13.0, *) {
|
60
|
+
self.init(
|
61
|
+
UIColor(dynamicProvider: { (traits: UITraitCollection) -> UIColor in
|
62
|
+
switch traits.userInterfaceStyle {
|
63
|
+
case .dark:
|
64
|
+
return dark.rawValue
|
65
|
+
default:
|
66
|
+
return light.rawValue
|
67
|
+
}
|
68
|
+
})
|
69
|
+
)
|
70
|
+
} else {
|
71
|
+
self.init(light.rawValue)
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
private convenience init(base: #{data.struct_name}, elevated: #{data.struct_name}) {
|
76
|
+
if #available(iOS 13.0, *) {
|
77
|
+
self.init(
|
78
|
+
UIColor(dynamicProvider: { (traits: UITraitCollection) -> UIColor in
|
79
|
+
switch traits.userInterfaceLevel {
|
80
|
+
case .elevated:
|
81
|
+
return elevated.rawValue
|
82
|
+
default:
|
83
|
+
return base.rawValue
|
84
|
+
}
|
85
|
+
})
|
86
|
+
)
|
87
|
+
} else {
|
88
|
+
self.init(base.rawValue)
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
}
|
93
|
+
HEREDOC
|
94
|
+
end
|
95
|
+
|
96
|
+
def render_colors
|
97
|
+
result = []
|
98
|
+
result << '// MARK: Colors'
|
99
|
+
result << ''
|
100
|
+
result << "#{data.effective_access_level} extension #{data.struct_name} {".lstrip
|
101
|
+
result << ''
|
102
|
+
|
103
|
+
data.color_entries.each do |entry|
|
104
|
+
unless entry[:description].nil?
|
105
|
+
entry[:description].strip.lines.each do |line|
|
106
|
+
result << " /// #{line.strip}"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
result << " static let #{entry[:property]} = #{entry[:color].to_s(data.struct_name, 4)}\n"
|
111
|
+
end
|
112
|
+
|
113
|
+
result << '}'
|
114
|
+
result << ''
|
115
|
+
result.join("\n")
|
116
|
+
end
|
117
|
+
|
118
|
+
def render_utils
|
119
|
+
result = []
|
120
|
+
result << '// MARK: Utils'
|
121
|
+
result << ''
|
122
|
+
|
123
|
+
if data.swiftui?
|
124
|
+
result << <<~HEREDOC.lstrip
|
125
|
+
#{data.effective_access_level} extension Color {
|
126
|
+
|
127
|
+
@inline(__always)
|
128
|
+
static func #{data.util_method_name}(_ color: #{data.struct_name}) -> Color {
|
129
|
+
if #available(iOS 15.0, *) {
|
130
|
+
return Color(uiColor: color.rawValue)
|
131
|
+
} else {
|
132
|
+
return Color(color.rawValue)
|
133
|
+
}
|
134
|
+
}
|
135
|
+
|
136
|
+
}
|
137
|
+
HEREDOC
|
138
|
+
end
|
139
|
+
|
140
|
+
result << <<~HEREDOC.lstrip
|
141
|
+
#{data.effective_access_level} extension UIColor {
|
142
|
+
|
143
|
+
@inline(__always)
|
144
|
+
static func #{data.util_method_name}(_ color: #{data.struct_name}) -> UIColor {
|
145
|
+
return color.rawValue
|
146
|
+
}
|
147
|
+
|
148
|
+
}
|
149
|
+
HEREDOC
|
150
|
+
|
151
|
+
result << <<~HEREDOC.lstrip
|
152
|
+
#{data.effective_access_level} extension CGColor {
|
153
|
+
|
154
|
+
@inline(__always)
|
155
|
+
static func #{data.util_method_name}(_ color: #{data.struct_name}) -> CGColor {
|
156
|
+
return color.rawValue.cgColor
|
157
|
+
}
|
158
|
+
|
159
|
+
}
|
160
|
+
HEREDOC
|
161
|
+
|
162
|
+
result.join("\n")
|
14
163
|
end
|
15
164
|
end
|
165
|
+
# rubocop:enable Metrics/ClassLength
|
16
166
|
end
|
data/lib/stylegen/validator.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'json_schemer'
|
4
4
|
|
5
5
|
module Stylegen
|
6
6
|
class Validator
|
@@ -12,7 +12,7 @@ module Stylegen
|
|
12
12
|
errors = []
|
13
13
|
|
14
14
|
schema.validate(config).each do |v|
|
15
|
-
errors << JSONSchemer::Errors.pretty(v) unless v[
|
15
|
+
errors << JSONSchemer::Errors.pretty(v) unless v['type'] == 'schema'
|
16
16
|
end
|
17
17
|
|
18
18
|
errors
|
@@ -21,7 +21,7 @@ module Stylegen
|
|
21
21
|
private
|
22
22
|
|
23
23
|
def schema
|
24
|
-
@schema ||= JSONSchemer.schema(File.read(File.join(__dir__,
|
24
|
+
@schema ||= JSONSchemer.schema(File.read(File.join(__dir__, 'resources/schema.json')))
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
data/lib/stylegen/version.rb
CHANGED
data/lib/stylegen.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
3
|
+
require 'stylegen/version'
|
4
|
+
require 'stylegen/colors'
|
5
|
+
require 'stylegen/generator'
|
6
|
+
require 'stylegen/data'
|
7
|
+
require 'stylegen/validator'
|
data/stylegen.gemspec
CHANGED
@@ -1,31 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require './lib/stylegen/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
|
-
s.name =
|
6
|
+
s.name = 'stylegen'
|
7
7
|
s.version = Stylegen::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
|
-
s.authors = [
|
10
|
-
s.email = [
|
11
|
-
s.homepage =
|
12
|
-
s.description = s.summary =
|
9
|
+
s.authors = ['Ramon Torres']
|
10
|
+
s.email = ['raymondjavaxx@gmail.com']
|
11
|
+
s.homepage = 'https://github.com/raymondjavaxx/stylegen'
|
12
|
+
s.description = s.summary = 'Tool for generating styling code for iOS apps'
|
13
13
|
s.files = `git ls-files`.split("\n")
|
14
14
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
15
|
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
16
|
-
s.require_paths = [
|
17
|
-
s.license =
|
16
|
+
s.require_paths = ['lib']
|
17
|
+
s.license = 'MIT'
|
18
18
|
|
19
|
-
s.add_runtime_dependency
|
20
|
-
s.add_runtime_dependency
|
21
|
-
s.add_runtime_dependency
|
19
|
+
s.add_runtime_dependency 'dry-inflector', '~> 0.2.0'
|
20
|
+
s.add_runtime_dependency 'gli', '~> 2.1'
|
21
|
+
s.add_runtime_dependency 'json_schemer', '~> 0.2.0'
|
22
22
|
|
23
|
-
s.add_development_dependency
|
24
|
-
s.add_development_dependency
|
25
|
-
s.add_development_dependency
|
26
|
-
s.add_development_dependency
|
27
|
-
s.add_development_dependency
|
28
|
-
s.add_development_dependency
|
23
|
+
s.add_development_dependency 'bundler'
|
24
|
+
s.add_development_dependency 'minitest', '~> 5.14'
|
25
|
+
s.add_development_dependency 'rake'
|
26
|
+
s.add_development_dependency 'rubocop'
|
27
|
+
s.add_development_dependency 'rubocop-minitest'
|
28
|
+
s.add_development_dependency 'rubocop-rake'
|
29
29
|
|
30
|
-
s.required_ruby_version =
|
30
|
+
s.required_ruby_version = '>= 2.5.0'
|
31
31
|
end
|
data/test/helper.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative 'helper'
|
4
4
|
|
5
5
|
class TestBaseElevatedColor < MiniTest::Test
|
6
6
|
def test_to_string
|
7
7
|
color = Stylegen::BaseElevatedColor.new(
|
8
|
-
Stylegen::Color.from_hex(
|
9
|
-
Stylegen::Color.from_hex(
|
8
|
+
Stylegen::Color.from_hex('#000000'),
|
9
|
+
Stylegen::Color.from_hex('#333333')
|
10
10
|
)
|
11
11
|
|
12
12
|
# Default indentation
|
@@ -18,7 +18,7 @@ class TestBaseElevatedColor < MiniTest::Test
|
|
18
18
|
)
|
19
19
|
CODE
|
20
20
|
|
21
|
-
assert_equal expected, color.to_s(
|
21
|
+
assert_equal expected, color.to_s('ThemeColor')
|
22
22
|
|
23
23
|
# Additional indentation
|
24
24
|
|
@@ -29,6 +29,6 @@ class TestBaseElevatedColor < MiniTest::Test
|
|
29
29
|
)
|
30
30
|
CODE
|
31
31
|
|
32
|
-
assert_equal expected, color.to_s(
|
32
|
+
assert_equal expected, color.to_s('ThemeColor', 4)
|
33
33
|
end
|
34
34
|
end
|
data/test/test_color.rb
CHANGED
@@ -1,40 +1,40 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative 'helper'
|
4
4
|
|
5
5
|
class TestColor < MiniTest::Test
|
6
6
|
def test_parsing
|
7
|
-
color = Stylegen::Color.from_hex(
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
color = Stylegen::Color.from_hex('#FF8000')
|
8
|
+
assert_in_delta(1.0, color.red)
|
9
|
+
assert_in_delta(0.5019607843137255, color.green)
|
10
|
+
assert_in_delta(0.0, color.blue)
|
11
|
+
assert_in_delta(1.0, color.alpha)
|
12
12
|
|
13
13
|
# Optional pound sign
|
14
|
-
color = Stylegen::Color.from_hex(
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
color = Stylegen::Color.from_hex('FF8000')
|
15
|
+
assert_in_delta(1.0, color.red)
|
16
|
+
assert_in_delta(0.5019607843137255, color.green)
|
17
|
+
assert_in_delta(0.0, color.blue)
|
18
|
+
assert_in_delta(1.0, color.alpha)
|
19
19
|
|
20
20
|
# Specify alpha
|
21
|
-
color = Stylegen::Color.from_hex(
|
22
|
-
|
21
|
+
color = Stylegen::Color.from_hex('#FF8000', 0.5)
|
22
|
+
assert_in_delta(0.5, color.alpha)
|
23
23
|
end
|
24
24
|
|
25
25
|
def test_parsing_shorthand_syntax
|
26
|
-
color = Stylegen::Color.from_hex(
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
color = Stylegen::Color.from_hex('#FC0')
|
27
|
+
assert_in_delta(1.0, color.red)
|
28
|
+
assert_in_delta(0.8000000000000002, color.green)
|
29
|
+
assert_in_delta(0.0, color.blue)
|
30
|
+
assert_in_delta(1.0, color.alpha)
|
31
31
|
|
32
32
|
# Optional pound sign
|
33
|
-
color = Stylegen::Color.from_hex(
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
33
|
+
color = Stylegen::Color.from_hex('FC0')
|
34
|
+
assert_in_delta(1.0, color.red)
|
35
|
+
assert_in_delta(0.8000000000000002, color.green)
|
36
|
+
assert_in_delta(0.0, color.blue)
|
37
|
+
assert_in_delta(1.0, color.alpha)
|
38
38
|
end
|
39
39
|
|
40
40
|
def test_grayscale
|
@@ -46,7 +46,7 @@ class TestColor < MiniTest::Test
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def test_to_string
|
49
|
-
color = Stylegen::Color.from_hex(
|
49
|
+
color = Stylegen::Color.from_hex('#00FF00')
|
50
50
|
|
51
51
|
expected = <<~CODE.strip
|
52
52
|
ThemeColor(
|
@@ -57,11 +57,11 @@ class TestColor < MiniTest::Test
|
|
57
57
|
)
|
58
58
|
CODE
|
59
59
|
|
60
|
-
assert_equal expected, color.to_s(
|
60
|
+
assert_equal expected, color.to_s('ThemeColor')
|
61
61
|
|
62
62
|
# Grayscale
|
63
|
-
color = Stylegen::Color.from_hex(
|
64
|
-
expected =
|
65
|
-
assert_equal expected, color.to_s(
|
63
|
+
color = Stylegen::Color.from_hex('#FFFFFF')
|
64
|
+
expected = 'ThemeColor(white: 1.0, alpha: 1.0)'
|
65
|
+
assert_equal expected, color.to_s('ThemeColor')
|
66
66
|
end
|
67
67
|
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative 'helper'
|
4
4
|
|
5
5
|
class TestLightDarkColor < MiniTest::Test
|
6
6
|
def test_to_string
|
7
7
|
color = Stylegen::LightDarkColor.new(
|
8
|
-
Stylegen::Color.from_hex(
|
9
|
-
Stylegen::Color.from_hex(
|
8
|
+
Stylegen::Color.from_hex('#FFFFFF'),
|
9
|
+
Stylegen::Color.from_hex('#333333')
|
10
10
|
)
|
11
11
|
|
12
12
|
# Default indentation
|
@@ -18,7 +18,7 @@ class TestLightDarkColor < MiniTest::Test
|
|
18
18
|
)
|
19
19
|
CODE
|
20
20
|
|
21
|
-
assert_equal expected, color.to_s(
|
21
|
+
assert_equal expected, color.to_s('ThemeColor')
|
22
22
|
|
23
23
|
# Additional indentation
|
24
24
|
|
@@ -29,6 +29,6 @@ class TestLightDarkColor < MiniTest::Test
|
|
29
29
|
)
|
30
30
|
CODE
|
31
31
|
|
32
|
-
assert_equal expected, color.to_s(
|
32
|
+
assert_equal expected, color.to_s('ThemeColor', 4)
|
33
33
|
end
|
34
34
|
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.
|
4
|
+
version: 0.5.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:
|
11
|
+
date: 2022-12-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-inflector
|
@@ -144,6 +144,7 @@ executables:
|
|
144
144
|
extensions: []
|
145
145
|
extra_rdoc_files: []
|
146
146
|
files:
|
147
|
+
- ".deepsource.toml"
|
147
148
|
- ".github/workflows/ci.yml"
|
148
149
|
- ".gitignore"
|
149
150
|
- ".rubocop.yml"
|
@@ -165,7 +166,6 @@ files:
|
|
165
166
|
- lib/stylegen/data.rb
|
166
167
|
- lib/stylegen/generator.rb
|
167
168
|
- lib/stylegen/resources/schema.json
|
168
|
-
- lib/stylegen/resources/template.swift.erb
|
169
169
|
- lib/stylegen/template.rb
|
170
170
|
- lib/stylegen/validator.rb
|
171
171
|
- lib/stylegen/version.rb
|
@@ -193,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
193
193
|
- !ruby/object:Gem::Version
|
194
194
|
version: '0'
|
195
195
|
requirements: []
|
196
|
-
rubygems_version: 3.
|
196
|
+
rubygems_version: 3.1.2
|
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
|
-
}
|