rbs 1.4.0 → 1.6.1

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/dependabot.yml +10 -0
  3. data/CHANGELOG.md +52 -0
  4. data/Gemfile +2 -0
  5. data/Steepfile +9 -1
  6. data/core/builtin.rbs +1 -1
  7. data/core/file.rbs +3 -1
  8. data/core/global_variables.rbs +3 -3
  9. data/core/io/wait.rbs +37 -0
  10. data/core/io.rbs +6 -4
  11. data/core/ractor.rbs +779 -0
  12. data/core/string_io.rbs +3 -5
  13. data/docs/collection.md +116 -0
  14. data/lib/rbs/builtin_names.rb +1 -0
  15. data/lib/rbs/cli.rb +93 -2
  16. data/lib/rbs/collection/cleaner.rb +29 -0
  17. data/lib/rbs/collection/config/lockfile_generator.rb +95 -0
  18. data/lib/rbs/collection/config.rb +85 -0
  19. data/lib/rbs/collection/installer.rb +27 -0
  20. data/lib/rbs/collection/sources/git.rb +147 -0
  21. data/lib/rbs/collection/sources/rubygems.rb +40 -0
  22. data/lib/rbs/collection/sources/stdlib.rb +38 -0
  23. data/lib/rbs/collection/sources.rb +22 -0
  24. data/lib/rbs/collection.rb +13 -0
  25. data/lib/rbs/environment_loader.rb +12 -0
  26. data/lib/rbs/errors.rb +2 -0
  27. data/lib/rbs/repository.rb +13 -7
  28. data/lib/rbs/validator.rb +4 -1
  29. data/lib/rbs/version.rb +1 -1
  30. data/lib/rbs.rb +1 -0
  31. data/sig/builtin_names.rbs +1 -0
  32. data/sig/cli.rbs +5 -0
  33. data/sig/collection/cleaner.rbs +13 -0
  34. data/sig/collection/collections.rbs +112 -0
  35. data/sig/collection/config.rbs +69 -0
  36. data/sig/collection/installer.rbs +15 -0
  37. data/sig/collection.rbs +4 -0
  38. data/sig/environment_loader.rbs +3 -0
  39. data/sig/polyfill.rbs +12 -3
  40. data/sig/repository.rbs +4 -0
  41. data/stdlib/digest/0/digest.rbs +418 -0
  42. data/stdlib/objspace/0/objspace.rbs +406 -0
  43. data/stdlib/openssl/0/openssl.rbs +1 -1
  44. data/stdlib/tempfile/0/tempfile.rbs +270 -0
  45. data/steep/Gemfile.lock +10 -10
  46. metadata +24 -3
data/core/string_io.rbs CHANGED
@@ -164,10 +164,9 @@ class StringIO
164
164
 
165
165
  # See IO#read.
166
166
  #
167
- def read: (?Integer length, ?String outbuf) -> String?
167
+ def read: (?int? length, ?string outbuf) -> String?
168
168
 
169
- def read_nonblock: (Integer len) -> String
170
- | (Integer len, ?String buf) -> String
169
+ def read_nonblock: (int len, ?string buf) -> String
171
170
 
172
171
  def readbyte: () -> Integer
173
172
 
@@ -179,8 +178,7 @@ class StringIO
179
178
  #
180
179
  def readlines: (?String sep, ?Integer limit, ?chomp: boolish) -> ::Array[String]
181
180
 
182
- def readpartial: (Integer maxlen) -> String
183
- | (Integer maxlen, ?String outbuf) -> String
181
+ def readpartial: (int maxlen, ?string outbuf) -> String
184
182
 
185
183
  # Reinitializes the stream with the given *other_StrIO* or *string* and *mode*
186
184
  # (see StringIO#new).
@@ -0,0 +1,116 @@
1
+ # RBS Collection manager
2
+
3
+ `rbs collection` sub command manages third party gems' RBS. In short, it is `bundler` for RBS.
4
+
5
+ ## Requirements
6
+
7
+ * `git(1)`
8
+ * `Gemfile.lock`
9
+
10
+
11
+ ## Usage
12
+
13
+ ### Setup
14
+
15
+ First, generate the configuration file, `rbs_collection.yaml`, with `rbs collection init`.
16
+
17
+ ```console
18
+ $ rbs collection init
19
+ created: rbs_collection.yaml
20
+
21
+ $ cat rbs_collection.yaml
22
+ # Download sources
23
+ sources:
24
+ - name: ruby/gem_rbs_collection
25
+ remote: https://github.com/ruby/gem_rbs_collection.git
26
+ revision: main
27
+ repo_dir: gems
28
+
29
+ # A directory to install the downloaded RBSs
30
+ path: .gem_rbs_collection
31
+
32
+ gems:
33
+ # Skip loading rbs gem's RBS.
34
+ # It's unnecessary if you don't use rbs as a library.
35
+ - name: rbs
36
+ ignore: true
37
+ ```
38
+
39
+ I also recommend updating `.gitignore`.
40
+
41
+ ```console
42
+ $ echo /.gem_rbs_collection/ >> .gitignore
43
+ ```
44
+
45
+ ### Install dependencies
46
+
47
+ Then, install gems' RBS with `rbs collection install`! It copies RBS from [the gem RBS repository](https://github.com/ruby/gem_rbs_collection) to `.gem_rbs_collection/` directory by default.
48
+ I recommend to ignore `.gem_rbs_collection/` from version control system, such as Git.
49
+
50
+ ```console
51
+ $ rbs collection install
52
+ Installing ast:2.4 (ruby/gem_rbs_collection@4b1a2a2f64c)
53
+ ...
54
+ It's done! 42 gems's RBSs now installed.
55
+ ```
56
+
57
+ Finally the third party RBSs are available! `rbs` commands, such as `rbs validate`, automatically load the third party RBSs.
58
+
59
+ ### Other commands
60
+
61
+ `rbs collection` has two more commands.
62
+
63
+ * `rbs collection update` updates `rbs_collection.lock.yaml`.
64
+ * `rbs collection clean` removes unnecessary rbs from `.gem_rbs_collection` directory.
65
+
66
+ ## Configuration
67
+
68
+ Configure `rbs collection` with editing `rbs_collection.yaml`.
69
+
70
+ ```yaml
71
+ # Download sources.
72
+ # You can add own collection git repository.
73
+ sources:
74
+ - name: ruby/gem_rbs_collection
75
+ remote: https://github.com/ruby/gem_rbs_collection.git
76
+ revision: main
77
+ repo_dir: gems
78
+
79
+ # A directory to install the downloaded RBSs
80
+ path: .gem_rbs_collection
81
+
82
+ gems:
83
+ # If the Gemfile.lock doesn't contain csv gem but you use csv gem,
84
+ # you can write the gem name explicitly to install RBS of the gem.
85
+ - name: csv
86
+
87
+ # If the Gemfile.lock contains nokogiri gem but you don't want to use the RBS,
88
+ # you can ignore the gem.
89
+ # `rbs collection` avoids to install nokogiri gem's RBS by this change.
90
+ # It is useful if the nokogiri RBS has a problem, such as compatibility issue with other RBS.
91
+ - name: nokogiri
92
+ ignore: true
93
+ ```
94
+
95
+ ## Files / Directories
96
+
97
+ * `rbs_collection.yaml`
98
+ * The configuration file.
99
+ * You need to edit it if:
100
+ * You don't want to ignore gem's RBS.
101
+ * You want to add gem's RBS explicitly.
102
+ * You can change the file path with `--collection` option. e.g. `rbs --collection another_conf.yaml collection install`.
103
+ * `rbs_collection.lock.yaml`
104
+ * RBS installs and loads RBS files with this file.
105
+ * It is auto-generated file. Do not edit this file.
106
+ * I recommend to manage it with VCS such as git.
107
+ * `.gem_rbs_collection/`
108
+ * RBS installs third party RBS files to the directory.
109
+ * I recommend to ignore it from VCS.
110
+ * You can change the path with `path` option of `rbs_collection.yaml` file.
111
+
112
+
113
+ ## How it works
114
+
115
+ `rbs collection` is integrated with Bundler.
116
+ `rbs collection install` command generates `gem_rbs_collection.lock.yaml` from `gem_rbs_collection.yaml` and `Gemfile.lock`. It uses `Gemfile.lock` to detects dependencies.
@@ -51,5 +51,6 @@ module RBS
51
51
  Regexp = Name.define(:Regexp)
52
52
  TrueClass = Name.define(:TrueClass)
53
53
  FalseClass = Name.define(:FalseClass)
54
+ Numeric = Name.define(:Numeric)
54
55
  end
55
56
  end
data/lib/rbs/cli.rb CHANGED
@@ -6,6 +6,7 @@ module RBS
6
6
  class CLI
7
7
  class LibraryOptions
8
8
  attr_accessor :core_root
9
+ attr_accessor :config_path
9
10
  attr_reader :repos
10
11
  attr_reader :libs
11
12
  attr_reader :dirs
@@ -16,6 +17,7 @@ module RBS
16
17
 
17
18
  @libs = []
18
19
  @dirs = []
20
+ @config_path = Collection::Config::PATH
19
21
  end
20
22
 
21
23
  def loader
@@ -25,6 +27,8 @@ module RBS
25
27
  end
26
28
 
27
29
  loader = EnvironmentLoader.new(core_root: core_root, repository: repository)
30
+ lock = config_path&.then { |p| Collection::Config.lockfile_of(p) }
31
+ loader.add_collection(lock) if lock
28
32
 
29
33
  dirs.each do |dir|
30
34
  loader.add(path: Pathname(dir))
@@ -52,6 +56,14 @@ module RBS
52
56
  self.core_root = nil
53
57
  end
54
58
 
59
+ opts.on('--collection PATH', "File path of collection configration (default: #{Collection::Config::PATH})") do |path|
60
+ self.config_path = Pathname(path).expand_path
61
+ end
62
+
63
+ opts.on('--no-collection', 'Ignore collection configration') do
64
+ self.config_path = nil
65
+ end
66
+
55
67
  opts.on("--repo DIR", "Add RBS repository") do |dir|
56
68
  repos << dir
57
69
  end
@@ -68,7 +80,7 @@ module RBS
68
80
  @stderr = stderr
69
81
  end
70
82
 
71
- COMMANDS = [:ast, :list, :ancestors, :methods, :method, :validate, :constant, :paths, :prototype, :vendor, :parse, :test]
83
+ COMMANDS = [:ast, :list, :ancestors, :methods, :method, :validate, :constant, :paths, :prototype, :vendor, :parse, :test, :collection]
72
84
 
73
85
  def parse_logging_options(opts)
74
86
  opts.on("--log-level LEVEL", "Specify log level (defaults to `warn`)") do |level|
@@ -819,11 +831,90 @@ EOB
819
831
 
820
832
  # @type var out: String
821
833
  # @type var err: String
822
- out, err, status = Open3.capture3(env_hash, *args)
834
+ out, err, status = __skip__ = Open3.capture3(env_hash, *args)
823
835
  stdout.print(out)
824
836
  stderr.print(err)
825
837
 
826
838
  status
827
839
  end
840
+
841
+ def run_collection(args, options)
842
+ warn "warning: rbs collection is experimental, and the behavior may change until RBS v2.0"
843
+
844
+ opts = collection_options(args)
845
+ params = {}
846
+ opts.order args.drop(1), into: params
847
+ config_path = options.config_path or raise
848
+ lock_path = Collection::Config.to_lockfile_path(config_path)
849
+
850
+ case args[0]
851
+ when 'install'
852
+ unless params[:frozen]
853
+ Collection::Config.generate_lockfile(config_path: config_path, gemfile_lock_path: Pathname('./Gemfile.lock'))
854
+ end
855
+ Collection::Installer.new(lockfile_path: lock_path, stdout: stdout).install_from_lockfile
856
+ when 'update'
857
+ # TODO: Be aware of argv to update only specified gem
858
+ Collection::Config.generate_lockfile(config_path: config_path, gemfile_lock_path: Pathname('./Gemfile.lock'), with_lockfile: false)
859
+ Collection::Installer.new(lockfile_path: lock_path, stdout: stdout).install_from_lockfile
860
+ when 'init'
861
+ if config_path.exist?
862
+ puts "#{config_path} already exists"
863
+ exit 1
864
+ end
865
+
866
+ config_path.write(<<~'YAML')
867
+ # Download sources
868
+ sources:
869
+ - name: ruby/gem_rbs_collection
870
+ remote: https://github.com/ruby/gem_rbs_collection.git
871
+ revision: main
872
+ repo_dir: gems
873
+
874
+ # A directory to install the downloaded RBSs
875
+ path: .gem_rbs_collection
876
+
877
+ gems:
878
+ # Skip loading rbs gem's RBS.
879
+ # It's unnecessary if you don't use rbs as a library.
880
+ - name: rbs
881
+ ignore: true
882
+ YAML
883
+ stdout.puts "created: #{config_path}"
884
+ when 'clean'
885
+ unless lock_path.exist?
886
+ puts "#{lock_path} should exist to clean"
887
+ exit 1
888
+ end
889
+ Collection::Cleaner.new(lockfile_path: lock_path)
890
+ when 'help'
891
+ puts opts.help
892
+ else
893
+ puts opts.help
894
+ exit 1
895
+ end
896
+ end
897
+
898
+ def collection_options(args)
899
+ OptionParser.new do |opts|
900
+ opts.banner = <<~HELP
901
+ Usage: rbs collection [install|update|init|clean|help]
902
+
903
+ Manage RBS collection, which contains third party RBS.
904
+
905
+ Examples:
906
+
907
+ # Initialize the configration file
908
+ $ rbs collection init
909
+
910
+ # Generate the lock file and install RBSs from the lock file
911
+ $ rbs collection install
912
+
913
+ # Update the RBSs
914
+ $ rbs collection update
915
+ HELP
916
+ opts.on('--frozen') if args[0] == 'install'
917
+ end
918
+ end
828
919
  end
829
920
  end
@@ -0,0 +1,29 @@
1
+ module RBS
2
+ module Collection
3
+ class Cleaner
4
+ attr_reader :lock
5
+
6
+ def initialize(lockfile_path:)
7
+ @lock = Config.from_path(lockfile_path)
8
+ end
9
+
10
+ def clean
11
+ lock.repo_path.glob('*/*') do |dir|
12
+ *_, gem_name, version = dir.to_s.split('/')
13
+ gem_name or raise
14
+ version or raise
15
+ next if needed? gem_name, version
16
+
17
+ FileUtils.remove_entry_secure(dir.to_s)
18
+ end
19
+ end
20
+
21
+ def needed?(gem_name, version)
22
+ gem = lock.gem(gem_name)
23
+ return false unless gem
24
+
25
+ gem['version'] == version
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,95 @@
1
+ module RBS
2
+ module Collection
3
+
4
+ # This class represent the configration file.
5
+ class Config
6
+ class LockfileGenerator
7
+ attr_reader :config, :lock, :gemfile_lock, :lock_path
8
+
9
+ def self.generate(config_path:, gemfile_lock_path:, with_lockfile: true)
10
+ new(config_path: config_path, gemfile_lock_path: gemfile_lock_path, with_lockfile: with_lockfile).generate
11
+ end
12
+
13
+ def initialize(config_path:, gemfile_lock_path:, with_lockfile:)
14
+ @config = Config.from_path config_path
15
+ @lock_path = Config.to_lockfile_path(config_path)
16
+ @lock = Config.from_path(lock_path) if lock_path.exist? && with_lockfile
17
+ @gemfile_lock = Bundler::LockfileParser.new(gemfile_lock_path.read)
18
+ end
19
+
20
+ def generate
21
+ config.gems.each do |gem|
22
+ assign_gem(gem_name: gem['name'], version: gem['version'])
23
+ end
24
+
25
+ gemfile_lock_gems do |spec|
26
+ assign_gem(gem_name: spec.name, version: spec.version)
27
+ end
28
+ remove_ignored_gems!
29
+
30
+ config.dump_to(lock_path)
31
+ config
32
+ end
33
+
34
+ private def assign_gem(gem_name:, version:)
35
+ locked = lock&.gem(gem_name)
36
+ specified = config.gem(gem_name)
37
+
38
+ return if specified&.dig('ignore')
39
+ return if specified&.dig('source') # skip if the source is already filled
40
+
41
+ if locked
42
+ # If rbs_collection.lock.yaml contain the gem, use it.
43
+ upsert_gem specified, locked
44
+ else
45
+ # Find the gem from gem_collection.
46
+ source = find_source(gem_name: gem_name)
47
+ return unless source
48
+
49
+ installed_version = version
50
+ best_version = find_best_version(version: installed_version, versions: source.versions({ 'name' => gem_name }))
51
+ # @type var new_content: RBS::Collection::Config::gem_entry
52
+ new_content = {
53
+ 'name' => gem_name,
54
+ 'version' => best_version.to_s,
55
+ 'source' => source.to_lockfile,
56
+ }
57
+ upsert_gem specified, new_content
58
+ end
59
+ end
60
+
61
+ private def upsert_gem(old, new)
62
+ if old
63
+ old.merge! new
64
+ else
65
+ config.add_gem new
66
+ end
67
+ end
68
+
69
+ private def remove_ignored_gems!
70
+ config.gems.reject! { |gem| gem['ignore'] }
71
+ end
72
+
73
+ private def gemfile_lock_gems(&block)
74
+ gemfile_lock.specs.each do |spec|
75
+ yield spec
76
+ end
77
+ end
78
+
79
+ private def find_source(gem_name:)
80
+ sources = config.sources
81
+
82
+ sources.find { |c| c.has?({ 'name' => gem_name, 'revision' => nil } ) }
83
+ end
84
+
85
+ private def find_best_version(version:, versions:)
86
+ candidates = versions.map { |v| Gem::Version.create(v) or raise }
87
+ return candidates.max || raise unless version
88
+
89
+ v = Gem::Version.create(version) or raise
90
+ Repository.find_best_version(v, candidates)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,85 @@
1
+ module RBS
2
+ module Collection
3
+
4
+ # This class represent the configration file.
5
+ class Config
6
+ class CollectionNotAvailable < StandardError
7
+ def initialize
8
+ super <<~MSG
9
+ rbs collection is not initialized.
10
+ Run `rbs collection install` to install RBSs from collection.
11
+ MSG
12
+ end
13
+ end
14
+
15
+ PATH = Pathname('rbs_collection.yaml')
16
+
17
+ # Generate a rbs lockfile from Gemfile.lock to `config_path`.
18
+ # If `with_lockfile` is true, it respects existing rbs lockfile.
19
+ def self.generate_lockfile(config_path:, gemfile_lock_path:, with_lockfile: true)
20
+ LockfileGenerator.generate(config_path: config_path, gemfile_lock_path: gemfile_lock_path, with_lockfile: with_lockfile)
21
+ end
22
+
23
+ def self.from_path(path)
24
+ new(YAML.load(path.read), config_path: path)
25
+ end
26
+
27
+ def self.lockfile_of(config_path)
28
+ lock_path = to_lockfile_path(config_path)
29
+ from_path lock_path if lock_path.exist?
30
+ end
31
+
32
+ def self.to_lockfile_path(config_path)
33
+ config_path.sub_ext('.lock' + config_path.extname)
34
+ end
35
+
36
+ def initialize(data, config_path:)
37
+ @data = data
38
+ @config_path = config_path
39
+ end
40
+
41
+ def add_gem(gem)
42
+ gems << gem
43
+ end
44
+
45
+ def gem(gem_name)
46
+ gems.find { |gem| gem['name'] == gem_name }
47
+ end
48
+
49
+ def repo_path
50
+ @config_path.dirname.join @data['path']
51
+ end
52
+
53
+ def sources
54
+ @sources ||= (
55
+ @data['sources']
56
+ .map { |c| Sources.from_config_entry(c) }
57
+ .push(Sources::Stdlib.instance)
58
+ .push(Sources::Rubygems.instance)
59
+ )
60
+ end
61
+
62
+ def dump_to(io)
63
+ YAML.dump(@data, io)
64
+ end
65
+
66
+ def gems
67
+ @data['gems'] ||= []
68
+ end
69
+
70
+ # It raises an error when there are non-available libraries
71
+ def check_rbs_availability!
72
+ raise CollectionNotAvailable unless repo_path.exist?
73
+
74
+ gems.each do |gem|
75
+ case gem['source']['type']
76
+ when 'git'
77
+ meta_path = repo_path.join(gem['name'], gem['version'], Sources::Git::METADATA_FILENAME)
78
+ raise CollectionNotAvailable unless meta_path.exist?
79
+ raise CollectionNotAvailable unless gem == YAML.load(meta_path.read)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end