rbs_rails 0.3.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +18 -0
  3. data/.gitignore +3 -0
  4. data/.gitmodules +0 -0
  5. data/CHANGELOG.md +8 -0
  6. data/Gemfile +3 -2
  7. data/README.md +23 -43
  8. data/Rakefile +11 -2
  9. data/Steepfile +10 -1
  10. data/assets/sig/pg.rbs +5 -0
  11. data/assets/sig/que.rbs +4 -0
  12. data/assets/sig/queue_classic.rbs +4 -0
  13. data/assets/sig/rack.rbs +1 -0
  14. data/assets/sig/rails.rbs +1 -5
  15. data/assets/sig/sidekiq.rbs +4 -0
  16. data/assets/sig/sneakers.rbs +4 -0
  17. data/assets/sig/sucker_punch.rbs +4 -0
  18. data/bin/add-type-params.rb +7 -0
  19. data/bin/gem_rbs +94 -0
  20. data/bin/postprocess.rb +15 -6
  21. data/bin/rbs +29 -2
  22. data/bin/rbs-prototype-rb.rb +59 -6
  23. data/lib/rbs_rails.rb +4 -0
  24. data/lib/rbs_rails/active_record.rb +100 -50
  25. data/lib/rbs_rails/dependency_builder.rb +43 -0
  26. data/lib/rbs_rails/rake_task.rb +83 -0
  27. data/lib/rbs_rails/util.rb +25 -0
  28. data/lib/rbs_rails/version.rb +1 -1
  29. data/rbs_rails.gemspec +2 -1
  30. data/sig/fileutils.rbs +1 -0
  31. data/sig/rake.rbs +6 -0
  32. data/sig/rbs_rails/active_record.rbs +8 -2
  33. data/sig/rbs_rails/dependency_builder.rbs +9 -0
  34. data/sig/rbs_rails/rake_task.rbs +26 -0
  35. data/sig/rbs_rails/util.rbs +11 -0
  36. metadata +34 -13
  37. data/.travis.yml +0 -8
  38. data/assets/sig/action_controller.rbs +0 -49
  39. data/assets/sig/active_record.rbs +0 -137
  40. data/assets/sig/generated/actionpack.rbs +0 -11677
  41. data/assets/sig/generated/actionview.rbs +0 -10491
  42. data/assets/sig/generated/activemodel.rbs +0 -4139
  43. data/assets/sig/generated/activerecord-meta-programming.rbs +0 -98
  44. data/assets/sig/generated/activerecord.rbs +0 -24023
  45. data/assets/sig/generated/activesupport.rbs +0 -12207
  46. data/assets/sig/generated/railties.rbs +0 -4647
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f5dc0321730c8a515b7fb485221b43cc9424994fa6e6c351505fbca3c09ae4e1
4
- data.tar.gz: a930ab4b1cac5c0f819ab2d8d1bc18be47bbca00dc41990e185513f7a2cff1a8
3
+ metadata.gz: 82919f7e04d613a702ad6cadbff8df905f23b54bf7dbdae04122a5af5df09608
4
+ data.tar.gz: 7070d476fe7f15776eed7fa406c423f18ba54077b4293ccb6064c151c7a89754
5
5
  SHA512:
6
- metadata.gz: 5efd9e8eabe42a075f9dcf0d15c5e79d7cec935b8d812161cd20ed675b81691ab4d766bc3f59b8b196b3afb39961599d599082bb5d57166b34400f52c16aa716
7
- data.tar.gz: ede7db12226ec86c8f4617c2fcf6713dfda220572bc0dcad7b6d58c3a372c716d20b15124bc245cb02968fb17ca2dbe97d160d137a026d85054ef6257f5eaa95
6
+ metadata.gz: 92c046d0f404ff63fe86b3fab5bd92dd24731919827c5ee04330cecf26ae158608e4b0babc1097977ef5cddcbd9474983621d188b996cf264cb96a65142adf95
7
+ data.tar.gz: 947c7022e0e49010cdc0fbce0bd453993f3ba0bc3df1fb5d406bce94361c49f8d61044b95ba9fed901e6635a9476b4c699812f2334e5e36552552138baada338
@@ -0,0 +1,18 @@
1
+ name: CI
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ strategy:
8
+ fail-fast: false
9
+ matrix:
10
+ ruby: [2.6, 2.7, '3.0']
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: ${{ matrix.ruby }}
17
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
18
+ - run: bundle exec rake
data/.gitignore CHANGED
@@ -7,3 +7,6 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
  /Gemfile.lock
10
+ /test/app/sig/rbs_rails
11
+ /test/app/sig/app
12
+ /test/app/sig/path_helpers.rbs
File without changes
@@ -0,0 +1,8 @@
1
+ # Change log
2
+
3
+ ## master (unreleased)
4
+
5
+ ## 0.7.0 (2020-12-28)
6
+
7
+ * **[BREAKING]** Re-structure signature directory. [#86](https://github.com/pocke/rbs_rails/pull/86)
8
+ * Generate ActiveRecord models with namespaces and superclasses. [#87](https://github.com/pocke/rbs_rails/pull/87)
data/Gemfile CHANGED
@@ -4,5 +4,6 @@ source "https://rubygems.org"
4
4
  gemspec
5
5
 
6
6
  gem "rake", "~> 12.0"
7
- gem 'rbs', '>= 0.6.0'
8
- gem 'steep', '>= 0.19.0'
7
+ gem 'rbs', '1.0.0'
8
+ gem 'steep', '>= 0.34.0', git: 'https://github.com/soutaro/steep.git'
9
+ gem 'minitest'
data/README.md CHANGED
@@ -7,7 +7,7 @@ RBS files generator for Ruby on Rails.
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem 'rbs_rails'
10
+ gem 'rbs_rails', require: false
11
11
  ```
12
12
 
13
13
  And then execute:
@@ -20,56 +20,23 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- ### For Active Record models
24
-
25
- It has two tasks.
26
-
27
- * `copy_signature_files`: Copy type definition files for Rails from rbs_rails.
28
- * `generate_rbs_for_model`: Generate RBS files from model classes.
23
+ Put the following code to `lib/tasks/rbs.rake`.
29
24
 
30
25
  ```ruby
31
- # Rakefile
32
-
33
- task copy_signature_files: :environment do
34
- require 'rbs_rails'
35
-
36
- to = Rails.root.join('sig/rbs_rails/')
37
- to.mkpath unless to.exist?
38
- RbsRails.copy_signatures(to: to)
39
- end
40
-
41
- task generate_rbs_for_model: :environment do
42
- require 'rbs_rails'
43
-
44
- out_dir = Rails.root / 'sig'
45
- out_dir.mkdir unless out_dir.exist?
46
-
47
- Rails.application.eager_load!
26
+ require 'rbs_rails/rake_task'
48
27
 
49
- ActiveRecord::Base.descendants.each do |klass|
50
- next if klass.abstract_class?
28
+ RbsRails::RakeTask.new
29
+ ```
51
30
 
52
- path = out_dir / "app/models/#{klass.name.underscore}.rbs"
53
- FileUtils.mkdir_p(path.dirname)
31
+ Then, the following four tasks are available.
54
32
 
55
- sig = RbsRails::ActiveRecord.class_to_rbs(klass)
56
- path.write sig
57
- end
58
- end
59
- ```
33
+ * `rbs_rails:copy_signature_files`: Copy RBS files for rbs_rails
34
+ * `rbs_rails:generate_rbs_for_models`: Generate RBS files for Active Record models
35
+ * `rbs_rails:generate_rbs_for_path_helpers`: Generate RBS files for path helpers
36
+ * `rbs_rails:all`: Execute all tasks of RBS Rails
60
37
 
61
- ### For path helpers
62
38
 
63
- ```ruby
64
- # Rakefile
65
39
 
66
- task generate_rbs_for_path_helpers: :environment do
67
- require 'rbs_rails'
68
- out_path = Rails.root.join 'sig/path_helpers.rbs'
69
- rbs = RbsRails::PathHelpers.generate
70
- out_path.write rbs
71
- end
72
- ```
73
40
 
74
41
  ### Steep integration
75
42
 
@@ -83,12 +50,25 @@ target :app do
83
50
 
84
51
  check 'app'
85
52
 
53
+ repo_path "path/to/rbs_repo"
54
+
86
55
  library 'pathname'
87
56
  library 'logger'
88
57
  library 'mutex_m'
58
+ library 'date'
59
+
60
+ library 'activesupport'
61
+ library 'actionpack'
62
+ library 'activejob'
63
+ library 'activemodel'
64
+ library 'actionview'
65
+ library 'activerecord'
66
+ library 'railties'
89
67
  end
90
68
  ```
91
69
 
70
+ You need to put RBS repo to `path/to/rbs_repo`. See https://github.com/ruby/gem_rbs
71
+
92
72
  ## Development
93
73
 
94
74
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/Rakefile CHANGED
@@ -1,5 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
- task :default => [:rbs_validate, :steep]
2
+ require 'rake/testtask'
3
+
4
+ task :default => [:rbs_validate, :steep, :test]
3
5
 
4
6
  desc 'run Steep'
5
7
  task :steep do
@@ -7,5 +9,12 @@ task :steep do
7
9
  end
8
10
 
9
11
  task :rbs_validate do
10
- sh 'bin/rbs validate'
12
+ rbs = Pathname(__dir__).join('bin/rbs').to_s
13
+ sh "ruby #{rbs} validate --silent"
14
+ end
15
+
16
+ Rake::TestTask.new do |test|
17
+ test.libs << 'test'
18
+ test.test_files = Dir['test/**/*_test.rb']
19
+ test.verbose = true
11
20
  end
data/Steepfile CHANGED
@@ -3,9 +3,18 @@ target :lib do
3
3
  signature 'assets/sig'
4
4
 
5
5
  check "lib" # Directory name
6
+ repo_path ENV['RBS_REPO_DIR'] || './gem_rbs/gems'
6
7
 
7
8
  library "pathname"
8
9
  library "logger"
9
10
  library "mutex_m"
10
- # library "strong_json" # Gems
11
+ library "date"
12
+
13
+ library 'activesupport'
14
+ library 'actionpack'
15
+ library 'activejob'
16
+ library 'activemodel'
17
+ library 'actionview'
18
+ library 'activerecord'
19
+ library 'railties'
11
20
  end
@@ -0,0 +1,5 @@
1
+ module PG
2
+ class Connection
3
+ def async_exec: (*untyped) -> untyped
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ module Que
2
+ class Job
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ class QC
2
+ class Queue
3
+ end
4
+ end
@@ -4,6 +4,7 @@ module Rack
4
4
 
5
5
  class Response
6
6
  module Helpers
7
+ def location: () -> untyped
7
8
  end
8
9
  end
9
10
 
@@ -1,5 +1,5 @@
1
1
  module Rails
2
- overload def self.env: () -> ActiveSupport::StringInquirer
2
+ def self.env: () -> ActiveSupport::StringInquirer | ...
3
3
 
4
4
  module Dom
5
5
  module Testing
@@ -8,7 +8,3 @@ module Rails
8
8
  end
9
9
  end
10
10
  end
11
-
12
- class Rails::Application
13
- def routes: () -> untyped
14
- end
@@ -0,0 +1,4 @@
1
+ module Sidekiq
2
+ module Worker
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Sneakers
2
+ module Worker
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module SuckerPunch
2
+ module Job
3
+ end
4
+ end
@@ -1,5 +1,12 @@
1
1
  #!ruby
2
2
 
3
+ require 'bundler/inline'
4
+
5
+ gemfile do
6
+ source 'https://rubygems.org'
7
+ gem 'rbs', '1.0.0'
8
+ end
9
+
3
10
  require 'rbs'
4
11
 
5
12
  def args(n)
@@ -0,0 +1,94 @@
1
+ #!ruby
2
+
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'securerandom'
6
+ require 'pathname'
7
+
8
+ TOKEN = ENV.fetch('GITHUB_ACCESS_TOKEN')
9
+ VERSION = "6.0.3.2"
10
+
11
+ def req(query)
12
+ http = Net::HTTP.new("api.github.com", 443)
13
+ http.use_ssl = true
14
+ header = {
15
+ "Authorization" => "Bearer #{TOKEN}",
16
+ 'Content-Type' => 'application/json',
17
+ 'User-Agent' => 'gem_rbs client',
18
+ }
19
+ resp = http.request_post('/graphql', JSON.generate(query), header)
20
+ JSON.parse(resp.body, symbolize_names: true).tap do |content|
21
+ raise content[:errors].inspect if content[:errors]
22
+ end
23
+ end
24
+
25
+ class QueryBuilder
26
+ attr_reader :variables
27
+
28
+ def initialize
29
+ @queries = []
30
+ @variables = {}
31
+ end
32
+
33
+ def add(query, variables)
34
+ query = query.dup
35
+ variables = variables.transform_keys do |key|
36
+ next key unless @variables.key?(key)
37
+
38
+ new_key = key + '_' + SecureRandom.hex(8)
39
+ query.gsub!(key, new_key)
40
+ new_key
41
+ end
42
+
43
+ @queries << query
44
+ @variables.merge!(variables)
45
+ end
46
+
47
+ def query
48
+ # TODO: variable type
49
+ "query(#{variables.keys.map { |v| "$#{v}: String!" }.join(',')}) { #{@queries.join("\n")} }"
50
+ end
51
+ end
52
+
53
+ gems = %w[activesupport actionpack activejob activemodel actionview activerecord railties]
54
+
55
+ builder = QueryBuilder.new
56
+ gems.each do |gem|
57
+ path = "main:gems/#{gem}/#{VERSION}"
58
+ builder.add(<<~GRAPHQL, { 'path' => path })
59
+ #{gem}:repository(owner: "ruby", name: "gem_rbs") {
60
+ object(expression: $path) {
61
+ ... on Tree {
62
+ entries {
63
+ name
64
+ object {
65
+ ... on Blob {
66
+ isTruncated
67
+ text
68
+ }
69
+ }
70
+ }
71
+ }
72
+ }
73
+ }
74
+ GRAPHQL
75
+ end
76
+
77
+ resp = req(query: builder.query, variables: builder.variables)
78
+
79
+ resp[:data].each do |gem_name, gem_value|
80
+ gem_value.dig(:object, :entries).each do |entry|
81
+ fname = entry[:name]
82
+ if fname.end_with?('.rbs')
83
+ content =
84
+ if entry.dig(:object, :isTruncated)
85
+ `curl -H 'Accept: application/vnd.github.v3.raw' -H Authorization: token #{TOKEN} https://api.github.com/repos/ruby/gem_rbs/contents/gems/#{gem_name}/#{VERSION}/#{fname}`
86
+ else
87
+ entry.dig(:object, :text)
88
+ end
89
+ dir = Pathname("gem_rbs/gems/#{gem_name}/#{VERSION}")
90
+ dir.mkpath
91
+ dir.join(fname).write(content)
92
+ end
93
+ end
94
+ end
@@ -2,21 +2,26 @@
2
2
 
3
3
  # TODO: Expose me to user
4
4
 
5
+ require 'bundler/inline'
6
+
7
+ gemfile do
8
+ source 'https://rubygems.org'
9
+ gem 'rbs', '1.0.0'
10
+ end
11
+
5
12
  require 'rbs'
6
13
  require 'rbs/cli'
7
14
  require 'optparse'
8
15
 
9
16
  def env(options:)
10
- loader = RBS::EnvironmentLoader.new()
11
- options.setup(loader)
17
+ loader = options.loader
12
18
  RBS::Environment.from_loader(loader).resolve_type_names
13
19
  end
14
20
 
15
21
  def parse_option(argv)
16
22
  opt = OptionParser.new
17
23
  options = RBS::CLI::LibraryOptions.new
18
- cli = RBS::CLI.new(stdout: nil, stderr: nil)
19
- cli.library_parse(opt, options: options)
24
+ options.setup_library_options(opt)
20
25
 
21
26
  return opt.parse(argv), options
22
27
  end
@@ -79,8 +84,10 @@ def may_eql_member?(a, b)
79
84
  a.name.to_s.split('::').last == b.name.to_s.split('::').last
80
85
  end
81
86
 
82
- def update!(update_targets:)
87
+ def update!(update_targets:, only:)
83
88
  update_targets.group_by { |decl, _concern| decl.location.name }.each do |fname, target_decls|
89
+ next unless only.match?(fname)
90
+
84
91
  tree = RBS::Parser.parse_signature(File.read(fname))
85
92
  target_decls.each do |target_decl, concern|
86
93
  catch(:break) do
@@ -109,6 +116,8 @@ def run(argv)
109
116
  builder = RBS::DefinitionBuilder.new(env: env)
110
117
  matcher = FileMatcher.new(targets: targets)
111
118
 
119
+ only = ENV['ONLY']&.then { Regexp.new(_1) } || //
120
+
112
121
  update_targets = []
113
122
 
114
123
  env.class_decls.each do |_name, entry|
@@ -122,7 +131,7 @@ def run(argv)
122
131
  end
123
132
  end
124
133
 
125
- update!(update_targets: update_targets)
134
+ update!(update_targets: update_targets, only: only)
126
135
  end
127
136
 
128
137
  run(ARGV)
data/bin/rbs CHANGED
@@ -1,3 +1,30 @@
1
- #!/bin/bash
1
+ #!ruby
2
+
3
+ require 'pathname'
4
+ root = Pathname(__dir__) / '../'
5
+
6
+ def v(require)
7
+ if v = ENV['RAILS_VERSION']
8
+ "#{require}:#{v}"
9
+ else
10
+ require
11
+ end
12
+ end
13
+
14
+ def repo
15
+ ENV['RBS_REPO_DIR'] || Pathname(__dir__).join('../gem_rbs/gems').to_s
16
+ end
17
+
18
+ exec(
19
+ 'rbs',
20
+ # Require stdlibs
21
+ '-rlogger', '-rpathname', '-rmutex_m', '-rdate',
22
+ "--repo=#{repo}",
23
+ # Require Rails libraries
24
+ v('-ractivesupport'), v('-ractionpack'), v('-ractivejob'), v('-ractivemodel'), v('-ractionview'), v('-ractiverecord'), v('-rrailties'),
25
+ # Load signatures that are bundled in rbs_rails
26
+ '-I' + root.join('sig').to_s, '-I' + root.join('assets/sig').to_s,
27
+ # Expand arguments
28
+ *ARGV,
29
+ )
2
30
 
3
- rbs -rlogger -rpathname -rmutex_m -Isig -Iassets/sig $@