Skip to content

Commit 5fd3239

Browse files
authored
Merge pull request #137 from sue445/feature/add_github_link
Add GitHub link to generated code
2 parents b02df4d + 416bcca commit 5fd3239

12 files changed

+115
-32
lines changed

.rubocop_todo.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2024-09-18 15:36:12 UTC using RuboCop version 1.66.1.
3+
# on 2024-09-22 07:12:45 UTC using RuboCop version 1.66.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -9,7 +9,7 @@
99
# Offense count: 5
1010
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
1111
Metrics/AbcSize:
12-
Max: 64
12+
Max: 67
1313

1414
# Offense count: 1
1515
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
@@ -32,10 +32,10 @@ Metrics/ClassLength:
3232
Metrics/CyclomaticComplexity:
3333
Max: 17
3434

35-
# Offense count: 9
35+
# Offense count: 10
3636
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
3737
Metrics/MethodLength:
38-
Max: 58
38+
Max: 61
3939

4040
# Offense count: 3
4141
# Configuration parameters: AllowedMethods, AllowedPatterns.

_tools/ruby_h_to_go/lib/ruby_h_to_go/argument_definition.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
module RubyHToGo
44
# Proxy class for generating argument in go function
55
class ArgumentDefinition
6+
# @!attribute [r] header_dir
7+
# @return [String]
8+
attr_reader :header_dir
9+
610
extend Forwardable
711

812
def_delegators :@definition, :==, :type, :type=, :name, :name=, :pointer, :pointer=, :pointer?, :length, :length=
913

1014
include GeneratorHelper
1115

1216
# @param definition [RubyHeaderParser::ArgumentDefinition]
13-
def initialize(definition)
17+
# @param header_dir [String]
18+
def initialize(definition:, header_dir:)
1419
@definition = definition
20+
@header_dir = header_dir
1521
end
1622

1723
C_NAME_TO_GO_NAME = {

_tools/ruby_h_to_go/lib/ruby_h_to_go/cli.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,32 @@ def parser
3838
end
3939

4040
def write_type_definitions_to_go_file
41-
type_definitions = parser.extract_type_definitions.map { |d| RubyHToGo::TypeDefinition.new(d) }
41+
type_definitions = parser.extract_type_definitions.map do |definition|
42+
RubyHToGo::TypeDefinition.new(definition:, header_dir:)
43+
end
44+
4245
type_definitions.each do |definition|
43-
definition.write_go_file(dist_dir:, header_dir:)
46+
definition.write_go_file(dist_dir)
4447
end
4548
end
4649

4750
def write_struct_definitions_to_go_file
48-
struct_definitions = parser.extract_struct_definitions.map { |d| RubyHToGo::StructDefinition.new(d) }
51+
struct_definitions = parser.extract_struct_definitions.map do |definition|
52+
RubyHToGo::StructDefinition.new(definition:, header_dir:)
53+
end
54+
4955
struct_definitions.each do |definition|
50-
definition.write_go_file(dist_dir:, header_dir:)
56+
definition.write_go_file(dist_dir)
5157
end
5258
end
5359

5460
def write_function_definitions_to_go_file
55-
function_definitions = parser.extract_function_definitions.map { |d| RubyHToGo::FunctionDefinition.new(d) }
61+
function_definitions = parser.extract_function_definitions.map do |definition|
62+
RubyHToGo::FunctionDefinition.new(definition:, header_dir:)
63+
end
64+
5665
function_definitions.each do |definition|
57-
definition.write_go_file(dist_dir:, header_dir:)
66+
definition.write_go_file(dist_dir)
5867
end
5968
end
6069

_tools/ruby_h_to_go/lib/ruby_h_to_go/function_definition.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,36 @@
33
module RubyHToGo
44
# Proxy class for generating go function
55
class FunctionDefinition
6+
# @!attribute [r] header_dir
7+
# @return [String]
8+
attr_reader :header_dir
9+
610
extend Forwardable
711

812
def_delegators :@definition, :==, :name, :name=, :definition, :definition=, :filepath, :filepath=
913

1014
include GeneratorHelper
1115

1216
# @param definition [RubyHeaderParser::FunctionDefinition]
13-
def initialize(definition)
17+
def initialize(definition:, header_dir:)
1418
@definition = definition
19+
@header_dir = header_dir
1520
end
1621

1722
# @return [RubyHToGo::TyperefDefinition]
1823
def typeref
19-
@typeref ||= RubyHToGo::TyperefDefinition.new(@definition.typeref)
24+
@typeref ||= RubyHToGo::TyperefDefinition.new(definition: @definition.typeref, header_dir:)
2025
end
2126

2227
# @return [Array<RubyHToGo::ArgumentDefinition>]
2328
def args
24-
@args ||= @definition.args.map { |arg| RubyHToGo::ArgumentDefinition.new(arg) }
29+
@args ||= @definition.args.map { |arg| RubyHToGo::ArgumentDefinition.new(definition: arg, header_dir:) }
2530
end
2631

2732
# Write definition as go file
2833
# @param [String] dist_dir
29-
# @param [String] header_dir
30-
def write_go_file(dist_dir:, header_dir:)
31-
go_file_path = File.join(dist_dir, go_file_name(header_dir:, ruby_header_file: filepath))
34+
def write_go_file(dist_dir)
35+
go_file_path = File.join(dist_dir, generate_go_file_name(header_dir:, ruby_header_file: filepath))
3236

3337
generate_initial_go_file(go_file_path)
3438

@@ -44,12 +48,16 @@ def generate_go_content
4448

4549
go_function_typeref = typeref.go_function_typeref
4650

51+
github_url = generate_include_github_url(header_dir:, ruby_header_file: filepath)
52+
4753
go_function_lines = [
4854
"// #{go_function_name} calls `#{name}` in C",
4955
"//",
5056
"// Original definition is following",
5157
"//",
5258
"//\t#{definition}",
59+
"//",
60+
"// ref. #{github_url}",
5361
]
5462

5563
go_function_lines << "func #{go_function_name}(#{go_function_args.join(", ")}) #{go_function_typeref} {"

_tools/ruby_h_to_go/lib/ruby_h_to_go/generator_helper.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@ module GeneratorHelper
66
# @param header_dir [String]
77
# @param ruby_header_file [String]
88
# @return [String]
9-
def go_file_name(header_dir:, ruby_header_file:)
9+
def generate_go_file_name(header_dir:, ruby_header_file:)
1010
ruby_header_file.delete_prefix(header_dir + File::SEPARATOR).gsub(File::SEPARATOR, "_").
1111
gsub(/\.h$/, "_generated.go")
1212
end
1313

14+
# Generate GitHub url in https://github.com/ruby/ruby
15+
# @param header_dir [String]
16+
# @param ruby_header_file [String]
17+
# @return [String]
18+
def generate_include_github_url(header_dir:, ruby_header_file:)
19+
file = ruby_header_file.delete_prefix(header_dir + File::SEPARATOR)
20+
"https://github.com/ruby/ruby/blob/master/include/#{file}"
21+
end
22+
1423
# @param str [String]
1524
# @return [String]
1625
def snake_to_camel(str)

_tools/ruby_h_to_go/lib/ruby_h_to_go/struct_definition.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@
33
module RubyHToGo
44
# Proxy class for generating go type
55
class StructDefinition
6+
# @!attribute [r] header_dir
7+
# @return [String]
8+
attr_reader :header_dir
9+
610
extend Forwardable
711

812
def_delegators :@definition, :==, :name, :name=, :filepath, :filepath=
913

1014
include GeneratorHelper
1115

1216
# @param definition [RubyHeaderParser::StructDefinition]
13-
def initialize(definition)
17+
# @param header_dir [String]
18+
def initialize(definition:, header_dir:)
1419
@definition = definition
20+
@header_dir = header_dir
1521
end
1622

1723
# Write definition as go file
1824
# @param [String] dist_dir
19-
# @param [String] header_dir
20-
def write_go_file(dist_dir:, header_dir:)
21-
go_file_path = File.join(dist_dir, go_file_name(header_dir:, ruby_header_file: filepath))
25+
def write_go_file(dist_dir)
26+
go_file_path = File.join(dist_dir, generate_go_file_name(header_dir:, ruby_header_file: filepath))
2227

2328
generate_initial_go_file(go_file_path)
2429

@@ -29,10 +34,14 @@ def write_go_file(dist_dir:, header_dir:)
2934

3035
# @return [String]
3136
def generate_go_content
37+
github_url = generate_include_github_url(header_dir:, ruby_header_file: filepath)
38+
3239
go_type_name = snake_to_camel(name)
3340

3441
<<~GO
3542
// #{go_type_name} is a type for passing `C.#{name}` in and out of package
43+
//
44+
// ref. #{github_url}
3645
type #{go_type_name} C.#{name}
3746
3847
GO

_tools/ruby_h_to_go/lib/ruby_h_to_go/type_definition.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,26 @@
33
module RubyHToGo
44
# Proxy class for generating go type
55
class TypeDefinition
6+
# @!attribute [r] header_dir
7+
# @return [String]
8+
attr_reader :header_dir
9+
610
extend Forwardable
711

812
def_delegators :@definition, :==, :name, :name=, :filepath, :filepath=
913

1014
include GeneratorHelper
1115

1216
# @param definition [RubyHeaderParser::TypeDefinition]
13-
def initialize(definition)
17+
def initialize(definition:, header_dir:)
1418
@definition = definition
19+
@header_dir = header_dir
1520
end
1621

1722
# Write definition as go file
1823
# @param [String] dist_dir
19-
# @param [String] header_dir
20-
def write_go_file(dist_dir:, header_dir:)
21-
go_file_path = File.join(dist_dir, go_file_name(header_dir:, ruby_header_file: filepath))
24+
def write_go_file(dist_dir)
25+
go_file_path = File.join(dist_dir, generate_go_file_name(header_dir:, ruby_header_file: filepath))
2226

2327
generate_initial_go_file(go_file_path)
2428

@@ -29,10 +33,14 @@ def write_go_file(dist_dir:, header_dir:)
2933

3034
# @return [String]
3135
def generate_go_content
36+
github_url = generate_include_github_url(header_dir:, ruby_header_file: filepath)
37+
3238
go_type_name = snake_to_camel(name)
3339

3440
<<~GO
3541
// #{go_type_name} is a type for passing `C.#{name}` in and out of package
42+
//
43+
// ref. #{github_url}
3644
type #{go_type_name} C.#{name}
3745
3846
GO

_tools/ruby_h_to_go/lib/ruby_h_to_go/typeref_definition.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33
module RubyHToGo
44
# Proxy class for generating typeref in go function
55
class TyperefDefinition
6+
# @!attribute [r] header_dir
7+
# @return [String]
8+
attr_reader :header_dir
9+
610
extend Forwardable
711

812
def_delegators :@definition, :==, :type, :type=, :pointer, :pointer=, :pointer?
913

1014
include GeneratorHelper
1115

1216
# @param definition [RubyHeaderParser::TyperefDefinition]
13-
def initialize(definition)
17+
def initialize(definition:, header_dir:)
1418
@definition = definition
19+
@header_dir = header_dir
1520
end
1621

1722
# @return [String]

_tools/ruby_h_to_go/spec/ruby_h_to_go/function_definition_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
RSpec.describe RubyHToGo::FunctionDefinition do
44
describe "#generate_go_content" do
5-
subject { RubyHToGo::FunctionDefinition.new(definition).generate_go_content }
5+
subject { RubyHToGo::FunctionDefinition.new(definition:, header_dir:).generate_go_content }
6+
7+
let(:header_dir) { "/path/to/include" }
68

79
context "rb_define_method" do
810
let(:definition) do
@@ -27,6 +29,8 @@
2729
// Original definition is following
2830
//
2931
// void rb_define_method(VALUE klass, const char *mid, VALUE (*func)(ANYARGS), int arity)
32+
//
33+
// ref. https://github.com/ruby/ruby/blob/master/include/ruby/internal/method.h
3034
func RbDefineMethod(klass VALUE, mid string, arg3 unsafe.Pointer, arity int) {
3135
char, clean := string2Char(mid)
3236
defer clean()
@@ -65,6 +69,8 @@
6569
// Original definition is following
6670
//
6771
// VALUE rb_block_call(VALUE obj, ID mid, int argc, const VALUE *argv, rb_block_call_func_t proc, VALUE data2)
72+
//
73+
// ref. https://github.com/ruby/ruby/blob/master/include/ruby/internal/iterator.h
6874
func RbBlockCall(obj VALUE, mid ID, argc int, argv *VALUE, proc RbBlockCallFuncT, data2 VALUE) VALUE {
6975
var cArgv C.VALUE
7076
ret := VALUE(C.rb_block_call(C.VALUE(obj), C.ID(mid), C.int(argc), &cArgv, C.rb_block_call_func_t(proc), C.VALUE(data2)))
@@ -101,6 +107,8 @@
101107
// Original definition is following
102108
//
103109
// VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
110+
//
111+
// ref. https://github.com/ruby/ruby/blob/master/include/ruby/internal/eval.h
104112
func RbFuncallv(recv VALUE, mid ID, argc int, argv []VALUE) VALUE {
105113
ret := VALUE(C.rb_funcallv(C.VALUE(recv), C.ID(mid), C.int(argc), toCArray[VALUE, C.VALUE](argv)))
106114
return ret
@@ -133,6 +141,8 @@
133141
// Original definition is following
134142
//
135143
// void *rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
144+
//
145+
// ref. https://github.com/ruby/ruby/blob/master/include/rubythread.h
136146
func RbThreadCallWithGvl(arg1 unsafe.Pointer, data1 unsafe.Pointer) unsafe.Pointer {
137147
ret := unsafe.Pointer(C.rb_thread_call_with_gvl(toCPointer(arg1), toCPointer(data1)))
138148
return ret
@@ -165,6 +175,8 @@
165175
// Original definition is following
166176
//
167177
// int rb_uv_to_utf8(char buf[6], unsigned long uv)
178+
//
179+
// ref. https://github.com/ruby/ruby/blob/master/include/intern/bignum.h
168180
func RbUvToUtf8(buf []Char, uv uint) int {
169181
ret := int(C.rb_uv_to_utf8(toCArray[Char, C.char](buf), C.ulong(uv)))
170182
return ret

_tools/ruby_h_to_go/spec/ruby_h_to_go/generator_helper_spec.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
RSpec.describe RubyHToGo::GeneratorHelper do
44
include RubyHToGo::GeneratorHelper
55

6-
describe "#go_file_name" do
7-
subject { go_file_name(header_dir:, ruby_header_file:) }
6+
describe "#generate_go_file_name" do
7+
subject { generate_go_file_name(header_dir:, ruby_header_file:) }
88

99
let(:header_dir) { "/path/to/include" }
1010
let(:ruby_header_file) { "/path/to/include/ruby/internal/intern/thread.h" }
1111

1212
it { should eq "ruby_internal_intern_thread_generated.go" }
1313
end
14+
15+
describe "#generate_include_github_url" do
16+
subject { generate_include_github_url(header_dir:, ruby_header_file:) }
17+
18+
let(:header_dir) { "/path/to/include" }
19+
let(:ruby_header_file) { "/path/to/include/ruby/internal/intern/thread.h" }
20+
21+
it { should eq "https://github.com/ruby/ruby/blob/master/include/ruby/internal/intern/thread.h" }
22+
end
1423
end

0 commit comments

Comments
 (0)