rbs_config 0.1.0 → 0.2.1
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/.rubocop.yml +3 -0
- data/README.md +16 -6
- data/lib/generators/rbs_config/install_generator.rb +12 -3
- data/lib/rbs_config/rails_config.rb +119 -0
- data/lib/rbs_config/rake_task.rb +12 -4
- data/lib/rbs_config/version.rb +1 -1
- data/lib/rbs_config.rb +1 -0
- data/rbs_collection.lock.yaml +51 -7
- data/rbs_collection.yaml +4 -0
- data/sig/rbs_config/config.rbs +1 -1
- data/sig/rbs_config/rails_config.rbs +21 -0
- data/sig/rbs_config/rake_task.rbs +2 -0
- data/sig/shims/thor.rbs +5 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 214aeb4d1563d548e95f738b906554c68ddd2a2bad39bfb9fc0906a23eeba005
|
4
|
+
data.tar.gz: 013072d9063caf8f220ed4dc7ed52470d755e8e80be9265dca1b37136e1299d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 177c407ec2c7f25d4081658816729faf91ccf66f69d38cf5ae5abbc3283625327d77565dc67a56b1e19f2d8ae03bfb3dba6d449f9aa0b7761279e95abbb8883a
|
7
|
+
data.tar.gz: 2a3ed93b17a077d1ad9d718e05608f2a6efb2f19bffce73703ba206cfa28d445d096dc2b4b653f0a6532d36797c828ea2fd96de8df5eda3c6d416b0eadbb3b36
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -1,24 +1,34 @@
|
|
1
1
|
# rbs_config
|
2
2
|
|
3
|
-
rbs_config is a RBS generator for
|
3
|
+
rbs_config is a RBS generator for Rails configuration.
|
4
|
+
|
5
|
+
Now supports the following configuration:
|
6
|
+
|
7
|
+
* Rails configuration (`Rails.configuration`)
|
8
|
+
* [config gem](https://github.com/rubyconfig/config)
|
4
9
|
|
5
10
|
## Installation
|
6
11
|
|
7
12
|
Add a new entry to your Gemfile and run `bundle install`:
|
8
13
|
|
9
14
|
group :development do
|
10
|
-
gem 'rbs_config'
|
15
|
+
gem 'rbs_config', require: false
|
11
16
|
end
|
12
17
|
|
13
18
|
After the installation, please run rake task generator:
|
14
19
|
|
15
20
|
bundle exec rails g rbs_config:install
|
16
21
|
|
17
|
-
|
22
|
+
And then, please modify `lib/tasks/rbs_config.rake` to fit your application.
|
23
|
+
For example, set it up like this if you're using Rails configuration:
|
24
|
+
|
25
|
+
RbsConfig::RakeTask.new do |task|
|
26
|
+
task.type = :rails
|
27
|
+
task.mapping = { conf_name: Rails.application.config_for(:conf_name) }
|
28
|
+
end
|
18
29
|
|
19
|
-
|
20
|
-
|
21
|
-
ignore: true
|
30
|
+
Then, rbs_config will generate RBS file for `Rails.application.conf_name` from your
|
31
|
+
configuration file.
|
22
32
|
|
23
33
|
## Usage
|
24
34
|
|
@@ -7,13 +7,22 @@ module RbsConfig
|
|
7
7
|
def create_raketask
|
8
8
|
create_file "lib/tasks/rbs_config.rake", <<~RUBY
|
9
9
|
begin
|
10
|
-
|
10
|
+
# frozen_string_literal: true
|
11
|
+
|
12
|
+
require "rbs_config/rake_task"
|
13
|
+
|
11
14
|
RbsConfig::RakeTask.new do |task|
|
12
|
-
# The
|
15
|
+
# The type of configuration (default: :config)
|
16
|
+
# task.type = :config | :rails
|
17
|
+
|
18
|
+
# The class name of configuration object (for :config type)
|
13
19
|
# task.class_name = "Settings"
|
14
20
|
|
15
|
-
# The files to be loaded
|
21
|
+
# The files to be loaded (for :config type)
|
16
22
|
# task.files = [Pathname(Rails.root / "config/settings.yml")]
|
23
|
+
|
24
|
+
# The mapping of configuration (for :rails type)
|
25
|
+
# task.mapping = { foo: Rails.application.config_for(:foo) }
|
17
26
|
end
|
18
27
|
rescue LoadError
|
19
28
|
# failed to load rbs_config. Skip to load rbs_config tasks.
|
@@ -0,0 +1,119 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rbs"
|
4
|
+
require "active_support/ordered_options"
|
5
|
+
require "active_support/core_ext/string/inflections"
|
6
|
+
|
7
|
+
module RbsConfig
|
8
|
+
module RailsConfig
|
9
|
+
def self.generate(mapping:)
|
10
|
+
Generator.new(mapping: mapping).generate
|
11
|
+
end
|
12
|
+
|
13
|
+
class Generator
|
14
|
+
attr_reader :mapping
|
15
|
+
|
16
|
+
def initialize(mapping:)
|
17
|
+
@mapping = mapping
|
18
|
+
end
|
19
|
+
|
20
|
+
def generate
|
21
|
+
classes = generate_classes(mapping)
|
22
|
+
methods = mapping.map do |key, value|
|
23
|
+
"def #{key}: () -> #{stringify_type(key, value)}"
|
24
|
+
end
|
25
|
+
|
26
|
+
format <<~RBS
|
27
|
+
module Rails
|
28
|
+
class Application
|
29
|
+
class Configuration
|
30
|
+
#{classes.join("\n")}
|
31
|
+
#{methods.join("\n")}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.configuration: () -> Application::Configuration | ...
|
36
|
+
end
|
37
|
+
RBS
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def format(rbs)
|
43
|
+
parsed = RBS::Parser.parse_signature(rbs)
|
44
|
+
StringIO.new.tap do |out|
|
45
|
+
RBS::Writer.new(out: out).write(parsed[1] + parsed[2])
|
46
|
+
end.string
|
47
|
+
end
|
48
|
+
|
49
|
+
def generate_classes(config)
|
50
|
+
config.filter_map do |key, value|
|
51
|
+
next unless value.is_a?(ActiveSupport::OrderedOptions)
|
52
|
+
|
53
|
+
classes = generate_classes(value)
|
54
|
+
methods = generate_methods(value)
|
55
|
+
|
56
|
+
<<~RBS
|
57
|
+
class #{key.to_s.camelize}
|
58
|
+
#{classes.join("\n")}
|
59
|
+
#{methods.join("\n")}
|
60
|
+
end
|
61
|
+
RBS
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def generate_methods(config)
|
66
|
+
case config
|
67
|
+
when ActiveSupport::OrderedOptions
|
68
|
+
generate_ordered_options_methods(config)
|
69
|
+
when Hash
|
70
|
+
generate_hash_methods(config)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def generate_ordered_options_methods(config)
|
75
|
+
methods = config.map do |key, value|
|
76
|
+
type = stringify_type(key, value)
|
77
|
+
<<~RBS
|
78
|
+
def #{key}: () -> #{type}
|
79
|
+
def #{key}!: () -> #{type}
|
80
|
+
RBS
|
81
|
+
end
|
82
|
+
|
83
|
+
brace_method_type = config.map do |key, value|
|
84
|
+
"(:#{key}) -> #{stringify_type(key, value)}"
|
85
|
+
end.join(" | ")
|
86
|
+
|
87
|
+
methods + ["def []: #{brace_method_type}"]
|
88
|
+
end
|
89
|
+
|
90
|
+
def generate_hash_methods(config)
|
91
|
+
method_type = config.map do |key, value|
|
92
|
+
"(:#{key} | \"#{key}\") -> #{stringify_type(key, value)}"
|
93
|
+
end.join(" | ")
|
94
|
+
["def []: #{method_type}"]
|
95
|
+
end
|
96
|
+
|
97
|
+
def stringify_type(name, value) # rubocop:disable Metrics/CyclomaticComplexity
|
98
|
+
case value
|
99
|
+
when ActiveSupport::OrderedOptions
|
100
|
+
name.to_s.camelize
|
101
|
+
when Hash
|
102
|
+
pairs = value.map do |k, v|
|
103
|
+
"#{k}: #{stringify_type(k, v)}"
|
104
|
+
end
|
105
|
+
"{ #{pairs.join(", ")} }"
|
106
|
+
when Array
|
107
|
+
types = value.map { |v| stringify_type(name, v) }.uniq
|
108
|
+
"Array[#{types.join(" | ")}]"
|
109
|
+
when NilClass
|
110
|
+
"nil"
|
111
|
+
when TrueClass, FalseClass
|
112
|
+
"bool"
|
113
|
+
else
|
114
|
+
value.class.to_s
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
data/lib/rbs_config/rake_task.rb
CHANGED
@@ -5,16 +5,18 @@ require "rake/tasklib"
|
|
5
5
|
|
6
6
|
module RbsConfig
|
7
7
|
class RakeTask < Rake::TaskLib
|
8
|
-
attr_accessor :class_name, :files, :name, :signature_root_dir
|
8
|
+
attr_accessor :type, :class_name, :files, :mapping, :name, :signature_root_dir
|
9
9
|
|
10
10
|
def initialize(name = :'rbs:config', &block)
|
11
11
|
super()
|
12
12
|
|
13
13
|
@name = name
|
14
|
+
@type = :config
|
14
15
|
@class_name = "Settings"
|
15
16
|
@files = [Pathname(Rails.root / "config/settings.yml"),
|
16
17
|
Pathname(Rails.root / "config/settings.local.yml"),
|
17
18
|
Pathname(Rails.root / "config/settings/production.yml")]
|
19
|
+
@mapping = {}
|
18
20
|
@signature_root_dir = Pathname(Rails.root / "sig/config")
|
19
21
|
|
20
22
|
block&.call(self)
|
@@ -34,11 +36,17 @@ module RbsConfig
|
|
34
36
|
def define_generate_task
|
35
37
|
desc "Generate RBS files for config gem"
|
36
38
|
task "#{name}:generate" do
|
39
|
+
require "rbs_config" # load RbsConfig lazily
|
40
|
+
|
37
41
|
signature_root_dir.mkpath
|
38
42
|
|
39
|
-
|
40
|
-
|
41
|
-
|
43
|
+
rbs = if type == :config
|
44
|
+
RbsConfig::Config.generate(files: files, class_name: class_name)
|
45
|
+
else
|
46
|
+
RbsConfig::RailsConfig.generate(mapping: mapping)
|
47
|
+
end
|
48
|
+
|
49
|
+
signature_root_dir.join("#{class_name.underscore}.rbs").write(rbs)
|
42
50
|
end
|
43
51
|
end
|
44
52
|
|
data/lib/rbs_config/version.rb
CHANGED
data/lib/rbs_config.rb
CHANGED
data/rbs_collection.lock.yaml
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
sources:
|
3
3
|
- type: git
|
4
4
|
name: ruby/gem_rbs_collection
|
5
|
-
revision:
|
5
|
+
revision: 5666e737d2b27a8425b4e3855c1a38cae54989f7
|
6
6
|
remote: https://github.com/ruby/gem_rbs_collection.git
|
7
7
|
repo_dir: gems
|
8
8
|
path: ".gem_rbs_collection"
|
@@ -11,12 +11,28 @@ gems:
|
|
11
11
|
version: '0'
|
12
12
|
source:
|
13
13
|
type: stdlib
|
14
|
+
- name: actionpack
|
15
|
+
version: '6.0'
|
16
|
+
source:
|
17
|
+
type: git
|
18
|
+
name: ruby/gem_rbs_collection
|
19
|
+
revision: 5666e737d2b27a8425b4e3855c1a38cae54989f7
|
20
|
+
remote: https://github.com/ruby/gem_rbs_collection.git
|
21
|
+
repo_dir: gems
|
22
|
+
- name: actionview
|
23
|
+
version: '6.0'
|
24
|
+
source:
|
25
|
+
type: git
|
26
|
+
name: ruby/gem_rbs_collection
|
27
|
+
revision: 5666e737d2b27a8425b4e3855c1a38cae54989f7
|
28
|
+
remote: https://github.com/ruby/gem_rbs_collection.git
|
29
|
+
repo_dir: gems
|
14
30
|
- name: activesupport
|
15
31
|
version: '7.0'
|
16
32
|
source:
|
17
33
|
type: git
|
18
34
|
name: ruby/gem_rbs_collection
|
19
|
-
revision:
|
35
|
+
revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
|
20
36
|
remote: https://github.com/ruby/gem_rbs_collection.git
|
21
37
|
repo_dir: gems
|
22
38
|
- name: ast
|
@@ -24,19 +40,23 @@ gems:
|
|
24
40
|
source:
|
25
41
|
type: git
|
26
42
|
name: ruby/gem_rbs_collection
|
27
|
-
revision:
|
43
|
+
revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
|
28
44
|
remote: https://github.com/ruby/gem_rbs_collection.git
|
29
45
|
repo_dir: gems
|
30
46
|
- name: base64
|
31
47
|
version: '0'
|
32
48
|
source:
|
33
49
|
type: stdlib
|
50
|
+
- name: cgi
|
51
|
+
version: '0'
|
52
|
+
source:
|
53
|
+
type: stdlib
|
34
54
|
- name: concurrent-ruby
|
35
55
|
version: '1.1'
|
36
56
|
source:
|
37
57
|
type: git
|
38
58
|
name: ruby/gem_rbs_collection
|
39
|
-
revision:
|
59
|
+
revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
|
40
60
|
remote: https://github.com/ruby/gem_rbs_collection.git
|
41
61
|
repo_dir: gems
|
42
62
|
- name: date
|
@@ -48,7 +68,7 @@ gems:
|
|
48
68
|
source:
|
49
69
|
type: git
|
50
70
|
name: ruby/gem_rbs_collection
|
51
|
-
revision:
|
71
|
+
revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
|
52
72
|
remote: https://github.com/ruby/gem_rbs_collection.git
|
53
73
|
repo_dir: gems
|
54
74
|
- name: json
|
@@ -80,19 +100,35 @@ gems:
|
|
80
100
|
source:
|
81
101
|
type: git
|
82
102
|
name: ruby/gem_rbs_collection
|
83
|
-
revision:
|
103
|
+
revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
|
84
104
|
remote: https://github.com/ruby/gem_rbs_collection.git
|
85
105
|
repo_dir: gems
|
86
106
|
- name: pathname
|
87
107
|
version: '0'
|
88
108
|
source:
|
89
109
|
type: stdlib
|
110
|
+
- name: rack
|
111
|
+
version: '2.2'
|
112
|
+
source:
|
113
|
+
type: git
|
114
|
+
name: ruby/gem_rbs_collection
|
115
|
+
revision: 5666e737d2b27a8425b4e3855c1a38cae54989f7
|
116
|
+
remote: https://github.com/ruby/gem_rbs_collection.git
|
117
|
+
repo_dir: gems
|
118
|
+
- name: railties
|
119
|
+
version: '6.0'
|
120
|
+
source:
|
121
|
+
type: git
|
122
|
+
name: ruby/gem_rbs_collection
|
123
|
+
revision: 5666e737d2b27a8425b4e3855c1a38cae54989f7
|
124
|
+
remote: https://github.com/ruby/gem_rbs_collection.git
|
125
|
+
repo_dir: gems
|
90
126
|
- name: rainbow
|
91
127
|
version: '3.0'
|
92
128
|
source:
|
93
129
|
type: git
|
94
130
|
name: ruby/gem_rbs_collection
|
95
|
-
revision:
|
131
|
+
revision: 306d1aec19defeb177e217d8aad9812a52bcd5eb
|
96
132
|
remote: https://github.com/ruby/gem_rbs_collection.git
|
97
133
|
repo_dir: gems
|
98
134
|
- name: rbs
|
@@ -111,6 +147,10 @@ gems:
|
|
111
147
|
version: '0'
|
112
148
|
source:
|
113
149
|
type: stdlib
|
150
|
+
- name: tempfile
|
151
|
+
version: '0'
|
152
|
+
source:
|
153
|
+
type: stdlib
|
114
154
|
- name: time
|
115
155
|
version: '0'
|
116
156
|
source:
|
@@ -119,4 +159,8 @@ gems:
|
|
119
159
|
version: '0'
|
120
160
|
source:
|
121
161
|
type: stdlib
|
162
|
+
- name: uri
|
163
|
+
version: '0'
|
164
|
+
source:
|
165
|
+
type: stdlib
|
122
166
|
gemfile_lock_path: Gemfile.lock
|
data/rbs_collection.yaml
CHANGED
data/sig/rbs_config/config.rbs
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
module RbsConfig
|
2
|
+
module RailsConfig
|
3
|
+
def self.generate: (mapping: Hash[untyped, Hash[untyped, untyped] | ActiveSupport::OrderedOptions[untyped, untyped]]) -> String
|
4
|
+
|
5
|
+
class Generator
|
6
|
+
attr_reader mapping: Hash[untyped, Hash[untyped, untyped] | ActiveSupport::OrderedOptions[untyped, untyped]]
|
7
|
+
|
8
|
+
def initialize: (mapping: Hash[untyped, Hash[untyped, untyped] | ActiveSupport::OrderedOptions[untyped, untyped]]) -> void
|
9
|
+
def generate: () -> String
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def format: (String rbs) -> String
|
14
|
+
def generate_classes: (Hash[untyped, untyped] | ActiveSupport::OrderedOptions[untyped, untyped] config) -> Array[String]
|
15
|
+
def generate_methods: (Hash[untyped, untyped] | ActiveSupport::OrderedOptions[untyped, untyped] config) -> Array[string]
|
16
|
+
def generate_ordered_options_methods: (ActiveSupport::OrderedOptions[untyped, untyped] config) -> Array[string]
|
17
|
+
def generate_hash_methods: (Hash[untyped, untyped]) -> Array[string]
|
18
|
+
def stringify_type: (untyped name, untyped value) -> String
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -1,8 +1,10 @@
|
|
1
1
|
module RbsConfig
|
2
2
|
class RakeTask < Rake::TaskLib
|
3
|
+
attr_accessor type: :config | :rails
|
3
4
|
attr_accessor class_name: String
|
4
5
|
attr_accessor files: Array[Pathname]
|
5
6
|
attr_accessor name: Symbol
|
7
|
+
attr_accessor mapping: Hash[untyped, Hash[untyped, untyped]]
|
6
8
|
attr_accessor signature_root_dir: Pathname
|
7
9
|
|
8
10
|
def initialize: (?Symbol name) ? { (RbsConfig::RakeTask) -> void } -> void
|
data/sig/shims/thor.rbs
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rbs_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Takeshi KOMIYA
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-09-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -56,6 +56,7 @@ files:
|
|
56
56
|
- lib/generators/rbs_config/install_generator.rb
|
57
57
|
- lib/rbs_config.rb
|
58
58
|
- lib/rbs_config/config.rb
|
59
|
+
- lib/rbs_config/rails_config.rb
|
59
60
|
- lib/rbs_config/rake_task.rb
|
60
61
|
- lib/rbs_config/version.rb
|
61
62
|
- rbs_collection.lock.yaml
|
@@ -63,8 +64,10 @@ files:
|
|
63
64
|
- rbs_config.gemspec
|
64
65
|
- sig/rbs_config.rbs
|
65
66
|
- sig/rbs_config/config.rbs
|
67
|
+
- sig/rbs_config/rails_config.rbs
|
66
68
|
- sig/rbs_config/rake_task.rbs
|
67
69
|
- sig/shims/rake.rbs
|
70
|
+
- sig/shims/thor.rbs
|
68
71
|
homepage: https://github.com/tk0miya/rbs_config
|
69
72
|
licenses:
|
70
73
|
- MIT
|