rbs_config 0.1.0 → 0.2.1

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: 61793181f02c7345ef7f22fe57204825aad1575141e9de7b6723c13928a6f25d
4
- data.tar.gz: bc22569815b41ee6b65d8e19816376e0312a074baa3b4997ccdc7dcf9d82368d
3
+ metadata.gz: 214aeb4d1563d548e95f738b906554c68ddd2a2bad39bfb9fc0906a23eeba005
4
+ data.tar.gz: 013072d9063caf8f220ed4dc7ed52470d755e8e80be9265dca1b37136e1299d6
5
5
  SHA512:
6
- metadata.gz: 4299e76aeb9d945dda9fece882aa8c153813d0f9f916b7f6a18faaf644b58dde066411587b7ad68d9fda64a8fe7682976d8273a5e820a6a1dba340f57cc19811
7
- data.tar.gz: d1d0d938d45bef011eefba72b2a35bfcf452f78e02cfb31669bce0d8a4bbb5ade031c587978960404440aac7ca4de18f1cdcdb88f0cc558e122ee42e8e71e1b4
6
+ metadata.gz: 177c407ec2c7f25d4081658816729faf91ccf66f69d38cf5ae5abbc3283625327d77565dc67a56b1e19f2d8ae03bfb3dba6d449f9aa0b7761279e95abbb8883a
7
+ data.tar.gz: 2a3ed93b17a077d1ad9d718e05608f2a6efb2f19bffce73703ba206cfa28d445d096dc2b4b653f0a6532d36797c828ea2fd96de8df5eda3c6d416b0eadbb3b36
data/.rubocop.yml CHANGED
@@ -15,6 +15,9 @@ Style/StringLiteralsInInterpolation:
15
15
  Layout/LineLength:
16
16
  Max: 120
17
17
 
18
+ Metrics/AbcSize:
19
+ Max: 20
20
+
18
21
  Metrics/BlockLength:
19
22
  Exclude:
20
23
  - "spec/**/*_spec.rb"
data/README.md CHANGED
@@ -1,24 +1,34 @@
1
1
  # rbs_config
2
2
 
3
- rbs_config is a RBS generator for [config gem](https://github.com/rubyconfig/config).
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
- Additionally, it would be better to add the following entry to your `rbs_collection.yml`:
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
- gems:
20
- - name: rbs_config
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
- require 'rbs_config/rake_task'
10
+ # frozen_string_literal: true
11
+
12
+ require "rbs_config/rake_task"
13
+
11
14
  RbsConfig::RakeTask.new do |task|
12
- # The class name of configuration object.
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
@@ -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
- signature_root_dir.join("#{class_name.underscore}.rbs").write(
40
- RbsConfig::Config.generate(files: files, class_name: class_name)
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RbsConfig
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/rbs_config.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "rbs_config/config"
4
+ require_relative "rbs_config/rails_config"
4
5
  require_relative "rbs_config/version"
5
6
 
6
7
  module RbsConfig
@@ -2,7 +2,7 @@
2
2
  sources:
3
3
  - type: git
4
4
  name: ruby/gem_rbs_collection
5
- revision: a4c633634493ab7ae73219022f56acff56ab69af
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: a4c633634493ab7ae73219022f56acff56ab69af
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: a4c633634493ab7ae73219022f56acff56ab69af
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: a4c633634493ab7ae73219022f56acff56ab69af
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: a4c633634493ab7ae73219022f56acff56ab69af
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: a4c633634493ab7ae73219022f56acff56ab69af
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: a4c633634493ab7ae73219022f56acff56ab69af
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
@@ -19,4 +19,8 @@ gems:
19
19
  - name: steep
20
20
  ignore: true
21
21
 
22
+ - name: actionpack
23
+ - name: actionview
22
24
  - name: pathname
25
+ - name: rack
26
+ - name: railties
@@ -4,7 +4,7 @@ module RbsConfig
4
4
 
5
5
  class Generator
6
6
  attr_reader class_name: String
7
- attr_reader files: Array[String]
7
+ attr_reader files: Array[Pathname]
8
8
 
9
9
  def initialize: (class_name: String, files: Array[Pathname]) -> void
10
10
  def generate: () -> String
@@ -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
@@ -0,0 +1,5 @@
1
+ class Thor
2
+ module Actions
3
+ def create_file: (String destination, *String? args, ?config: bool) ?{ () -> void } -> untyped
4
+ end
5
+ end
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.0
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-08-23 00:00:00.000000000 Z
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