rbs 1.5.1 → 1.6.0

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: d4c3e2d7c96d7bfa810d993696088e8e4d7cd17301c5a96f925b38a6d2d6ef0c
4
- data.tar.gz: f1a618f362782267bcbb221314f0070f0d6e6be4cd2436e3bf904c7bcbace039
3
+ metadata.gz: fb7050ff50eb28c830dd0b36c6f36a5d1913342daf67096313b2abf6d5285dbe
4
+ data.tar.gz: 4ee896f45c1857cea2f15a6cc69fd976c15055a36bb77cec51c669dfb56cc79e
5
5
  SHA512:
6
- metadata.gz: c98f6bdf67e2fc41f421ccd778a029b74f5ecc121f79e3c0740fb2d5b7eb03d8ac95116ba2bfa4193d6aeee2fb1d819f5144d6e6c883e1e332ee4fa96f41cbb7
7
- data.tar.gz: 515b1091e01179d3b472e8a7dc16fb520767a2b2d7617bebd14fce0d806ff6368ee633234de91cc49c0814673d55f5b44cff87bb5bbb5098f25b46002d14d7b7
6
+ metadata.gz: 3aa3aa5be6922a746a36012cb3c5ba624a44d0ed5dac05790942ebf44bb7f4f44a107209465a69143282b33e9a18b7b4f820d093b6228f0a091c0fb190268e32
7
+ data.tar.gz: fa46604d3d6b154555084d4d77861dbc8eae77d54f88486cf6df691da4b605576569f7d55acac1deeb3b30202ea561c9cf221ac44c8aab6acbe0cd483f7d786d
@@ -0,0 +1,10 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "bundler"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "daily"
7
+ - package-ecosystem: "bundler"
8
+ directory: "/steep"
9
+ schedule:
10
+ interval: "daily"
data/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 1.6.0 (2021-09-05)
6
+
7
+ This release includes a preview of `rbs collection` commands, which is _bundler for RBS_.
8
+ The command helps you manage RBS files from gem_rbs_collection or other repositories.
9
+
10
+ This feature is a preview, and any feedback is welcome!
11
+
12
+ ## Signature updates
13
+
14
+ * objspace ([\#763](https://github.com/ruby/rbs/pull/763), [\#776](https://github.com/ruby/rbs/pull/776))
15
+ * tempfile ([\#767](https://github.com/ruby/rbs/pull/767), [\#775](https://github.com/ruby/rbs/pull/775))
16
+ * `IO#set_encoding_by_bom` ([\#106](https://github.com/ruby/rbs/pull/106))
17
+ * `OpenSSL::PKey::EC#dh_compute_key` ([\#775](https://github.com/ruby/rbs/pull/775))
18
+
19
+ ## Library changes
20
+
21
+ * Add `rbs collection` ([\#589](https://github.com/ruby/rbs/pull/589), [\#772](https://github.com/ruby/rbs/pull/772), [\#773](https://github.com/ruby/rbs/pull/773))
22
+
23
+ ## Miscellaneous
24
+
25
+ * Let `bin/annotate-with-rdoc` process nested constants/classes ([\#766](https://github.com/ruby/rbs/pull/766), [\#768](https://github.com/ruby/rbs/pull/768))
26
+ * Stop printing version mismatch message in CI ([\#777](https://github.com/ruby/rbs/pull/777))
27
+ * Update Steep and fix type errors ([\#770](https://github.com/ruby/rbs/pull/770), [\#774](https://github.com/ruby/rbs/pull/774))
28
+ * Add dependabot configuration ([\#771](https://github.com/ruby/rbs/pull/771))
29
+
5
30
  ## 1.5.1 (2021-08-22)
6
31
 
7
32
  ### Miscellaneous
data/Gemfile CHANGED
@@ -16,6 +16,7 @@ gem 'stackprof'
16
16
  gem "goodcheck"
17
17
  gem "dbm"
18
18
  gem 'digest'
19
+ gem 'tempfile'
19
20
 
20
21
  # Test gems
21
22
  gem "rbs-amber", path: "test/assets/test-gem"
data/Steepfile CHANGED
@@ -1,13 +1,21 @@
1
+ D = Steep::Diagnostic
2
+
1
3
  target :lib do
2
4
  signature "sig"
3
5
  check "lib"
4
6
  ignore "lib/rbs/parser.rb"
5
7
  ignore "lib/rbs/prototype", "lib/rbs/test", "lib/rbs/test.rb"
6
8
 
7
- library "set", "pathname", "json", "logger", "monitor", "tsort"
9
+ library "set", "pathname", "json", "logger", "monitor", "tsort", "uri"
8
10
  signature "stdlib/strscan/0/"
9
11
  signature "stdlib/rubygems/0/"
10
12
  signature "stdlib/optparse/0/"
13
+
14
+ configure_code_diagnostics do |config|
15
+ config[D::Ruby::MethodDefinitionMissing] = :hint
16
+ config[D::Ruby::ElseOnExhaustiveCase] = :hint
17
+ config[D::Ruby::FallbackAny] = :hint
18
+ end
11
19
  end
12
20
 
13
21
  # target :lib do
data/core/io.rbs CHANGED
@@ -635,6 +635,8 @@ class IO < Object
635
635
  def set_encoding: (?String | Encoding ext_or_ext_int_enc) -> self
636
636
  | (?String | Encoding ext_or_ext_int_enc, ?String | Encoding int_enc) -> self
637
637
 
638
+ def set_encoding_by_bom: () -> Encoding?
639
+
638
640
  # Returns status information for *ios* as an object of type `File::Stat` .
639
641
  #
640
642
  # ```ruby
@@ -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