rbs-patch 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7383464ff429fa44f0303636511ef7eb418a9426122e155c787931e7d356ce79
4
+ data.tar.gz: 8ba1b4760aebb8fb6b9883ab7d083958a2ea042ad53cc50f765b1c303cf5d37e
5
+ SHA512:
6
+ metadata.gz: 2c0e2041a6e5248c336110ffddb1fc543f9d60c7658b6b9c6e91ec9f9dff9253fb02a7791b2616c3d0d00cffa64f8fd4e879149a36599b92ea57fdd204fdb75f
7
+ data.tar.gz: f46c3da340a1d7e3075fb78778563cd0d7dda7ad387308baf2de32d2b90e0d063d39941c1f5ed0efd7ab3a4c17cc0a5dd87e9f0c60c9d0537f44bae5157776b4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Koji NAKAMURA
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # RBS::Patch
2
+
3
+ RBS::Patch manages RBS (Ruby Signature) type definitions through patches. It applies incremental changes to existing RBS signatures.
4
+
5
+ ## Supported Operations
6
+
7
+ - **`override`**: Replace an existing method signature
8
+ - **`delete`**: Remove a method signature
9
+ - **`append_after`**: Insert a method signature after a specified method
10
+ - **`prepend_before`**: Insert a method signature before a specified method
11
+
12
+ All operations use RBS annotations (e.g., `%a{patch:override}`), keeping patch files valid RBS syntax.
13
+
14
+ ## Installation
15
+
16
+ Install the gem and add to the application's Gemfile by executing:
17
+
18
+ ```bash
19
+ bundle add rbs-patch
20
+ ```
21
+
22
+ If bundler is not being used to manage dependencies, install the gem by executing:
23
+
24
+ ```bash
25
+ gem install rbs-patch
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ TODO: Write usage instructions here
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kozy4324/rbs-patch.
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
data/exe/rbs-patch ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH << File.join(__dir__, "../lib")
5
+ require "rbs/patch"
6
+ require "pathname"
7
+
8
+ if ARGV.empty?
9
+ puts "Usage: rbs-patch RBS_FILE_PATH [RBS_FILE_PATH ...]"
10
+ puts ""
11
+ puts "Examples:"
12
+ puts " rbs-patch lib/types/ sig/patches/"
13
+ puts " rbs-patch base.rbs patch1.rbs patch2.rbs"
14
+ exit 1
15
+ end
16
+
17
+ patch = RBS::Patch.new
18
+ ARGV.each do |arg|
19
+ patch.apply(path: Pathname(arg))
20
+ end
21
+ puts patch
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RBS
4
+ class Patch
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
data/lib/rbs/patch.rb ADDED
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rbs"
4
+ require "stringio"
5
+ require_relative "patch/version"
6
+
7
+ module RBS
8
+ class Patch # rubocop:disable Style/Documentation
9
+ ANNOTATION_OVERRIDE = "patch:override"
10
+ ANNOTATION_DELETE = "patch:delete"
11
+ ANNOTATION_APPEND_AFTER = /\Apatch:append_after:(.*)\Z/
12
+ ANNOTATION_PREPEND_BEFORE = /\Apatch:prepend_before:(.*)\Z/
13
+
14
+ def initialize
15
+ @env = ::RBS::Environment.new
16
+ end
17
+
18
+ def apply(source = nil, path: nil)
19
+ unless path.nil?
20
+ files = Set[]
21
+ ::RBS::FileFinder.each_file(path, skip_hidden: true) do |path|
22
+ next if files.include?(path)
23
+
24
+ files << path
25
+ apply Buffer.new(name: path, content: path.read(encoding: "UTF-8"))
26
+ end
27
+ return
28
+ end
29
+
30
+ _, dirs, decls = ::RBS::Parser.parse_signature(source)
31
+ @env.add_source(::RBS::Source::RBS.new(source, dirs, decls))
32
+ @env.class_decls.each_value.map do |class_entry|
33
+ class_entry.context_decls.map { _2 }.inject do |decl_a, decl_b|
34
+ decl_b.members.delete_if do |member_b|
35
+ ope, arg = if member_b.annotations.any? { |a| a.string == ANNOTATION_OVERRIDE }
36
+ [:override, nil]
37
+ elsif member_b.annotations.any? { |a| a.string == ANNOTATION_DELETE }
38
+ [:delete, nil]
39
+ elsif (anno = member_b.annotations.find { |a| a.string.match(ANNOTATION_APPEND_AFTER) })
40
+ [:append_after, anno.string.match(ANNOTATION_APPEND_AFTER)[1]]
41
+ elsif (anno = member_b.annotations.find { |a| a.string.match(ANNOTATION_PREPEND_BEFORE) })
42
+ [:prepend_before, anno.string.match(ANNOTATION_PREPEND_BEFORE)[1]]
43
+ end
44
+
45
+ next unless ope
46
+
47
+ case ope
48
+ when :override
49
+ index = decl_a.members.find_index { |member_a| member_a.name == member_b.name }
50
+ if index
51
+ decl_a.members[index] = decl_a.members[index].update(overloads: member_b.overloads)
52
+ true
53
+ else
54
+ false
55
+ end
56
+ when :delete
57
+ decl_a.members.reject! { |member_a| member_a.name == member_b.name }
58
+ when :append_after, :prepend_before
59
+ target_name = arg.to_sym
60
+ index = decl_a.members.find_index { |member_a| member_a.name == target_name }
61
+ if index
62
+ if ope == :append_after
63
+ offset = 1
64
+ annotations = member_b.annotations.reject { |a| a.string.match(ANNOTATION_APPEND_AFTER) }
65
+ else
66
+ offset = 0
67
+ annotations = member_b.annotations.reject { |a| a.string.match(ANNOTATION_PREPEND_BEFORE) }
68
+ end
69
+ decl_a.members.insert(index + offset, member_b.update(annotations:))
70
+ true
71
+ else
72
+ false
73
+ end
74
+ end
75
+ end
76
+ decl_a
77
+ end
78
+ end
79
+ end
80
+
81
+ def to_s
82
+ decls = @env.class_decls.each_value.map do |class_entry|
83
+ decls = class_entry.context_decls.map { _2 }
84
+ decls.each_with_object(decls.first.update(members: [])) do |decl, new_decl|
85
+ # merge multiple class decls into a single one
86
+ new_decl.members.concat decl.members
87
+ end
88
+ end
89
+
90
+ io = ::StringIO.new
91
+ RBS::Writer.new(out: io).write(decls)
92
+ io.rewind
93
+ io.read
94
+ end
95
+ end
96
+ end
data/sig/rbs/patch.rbs ADDED
@@ -0,0 +1,13 @@
1
+ module RBS
2
+ class Patch
3
+ VERSION: String
4
+
5
+ @env: untyped
6
+
7
+ def initialize: () -> void
8
+
9
+ def apply: (String source?, path: Pathname?) -> void
10
+
11
+ def to_s: () -> String
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbs-patch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Koji NAKAMURA
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rbs
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - '='
17
+ - !ruby/object:Gem::Version
18
+ version: 4.0.0.dev.5
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - '='
24
+ - !ruby/object:Gem::Version
25
+ version: 4.0.0.dev.5
26
+ description: RBS::Patch manages RBS (Ruby Signature) type definitions through patches.
27
+ It applies incremental changes to existing RBS signatures.
28
+ email:
29
+ - kozy4324@gmail.com
30
+ executables:
31
+ - rbs-patch
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - LICENSE.txt
36
+ - README.md
37
+ - Rakefile
38
+ - exe/rbs-patch
39
+ - lib/rbs/patch.rb
40
+ - lib/rbs/patch/version.rb
41
+ - sig/rbs/patch.rbs
42
+ homepage: https://github.com/kozy4324/rbs-patch
43
+ licenses:
44
+ - MIT
45
+ metadata:
46
+ allowed_push_host: https://rubygems.org
47
+ homepage_uri: https://github.com/kozy4324/rbs-patch
48
+ source_code_uri: https://github.com/kozy4324/rbs-patch
49
+ changelog_uri: https://github.com/kozy4324/rbs-patch/releases
50
+ rubygems_mfa_required: 'true'
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 3.2.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 4.0.3
66
+ specification_version: 4
67
+ summary: RBS::Patch manages RBS type definitions through patches.
68
+ test_files: []