Skip to content

Commit f2ddb93

Browse files
authored
Merge pull request #142 from sue445/refactor
Refactor ruby_h_to_go
2 parents 719be80 + 5c77ae9 commit f2ddb93

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

_tools/ruby_h_to_go/lib/ruby_h_to_go/cli.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def write_type_definitions_to_go_file
4343
end
4444

4545
type_definitions.each do |definition|
46+
next unless target_header_file?(definition.filepath)
47+
4648
definition.write_go_file(dist_dir)
4749
end
4850
end
@@ -53,6 +55,8 @@ def write_struct_definitions_to_go_file
5355
end
5456

5557
struct_definitions.each do |definition|
58+
next unless target_header_file?(definition.filepath)
59+
5660
definition.write_go_file(dist_dir)
5761
end
5862
end
@@ -73,6 +77,8 @@ def write_function_definitions_to_go_file
7377
end
7478

7579
function_definitions.each do |definition|
80+
next unless target_header_file?(definition.filepath)
81+
7682
definition.write_go_file(dist_dir)
7783
end
7884
end
@@ -83,14 +89,15 @@ def clean_generated_files
8389
end
8490

8591
def copy_go_files
86-
# FIXME: This is a temporary process until all possible contents of `ruby/*.go` are replaced
87-
# with automatically generated files. (Currently output to `dist/` as it is incomplete)
92+
src_dir = File.expand_path("../../../../ruby", __dir__)
93+
return if src_dir == dist_dir
94+
8895
%w[
8996
c_types.go
9097
types.go
9198
wrapper.go
9299
].each do |file|
93-
FileUtils.cp(File.join(__dir__, "..", "..", "..", "..", "ruby", file), dist_dir)
100+
FileUtils.cp(File.join(src_dir, file), dist_dir)
94101
end
95102
end
96103

@@ -108,5 +115,11 @@ def go_fmt
108115
system("go fmt", exception: true)
109116
end
110117
end
118+
119+
# @param filename [String]
120+
# @return [Boolean]
121+
def target_header_file?(filename)
122+
File.extname(filename) == ".h"
123+
end
111124
end
112125
end

_tools/ruby_h_to_go/lib/ruby_header_parser/data.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,25 @@ def function_arg_pointer_hint(function_name:, index:)
2626
# @param function_name [String]
2727
# @return [Boolean]
2828
def should_generate_function?(function_name)
29-
return false if data["function"]["deny_name"].any? { |format| format === function_name } # rubocop:disable Style/CaseEquality
29+
return false if data["function"]["exclude_name"].any? { |format| format === function_name } # rubocop:disable Style/CaseEquality
3030

31-
data["function"]["allow_name"].any? { |format| format === function_name } # rubocop:disable Style/CaseEquality
31+
data["function"]["include_name"].any? { |format| format === function_name } # rubocop:disable Style/CaseEquality
3232
end
3333

3434
# Whether generate C struct to go
3535
# @param struct_name [String]
3636
# @return [Boolean]
3737
def should_generate_struct?(struct_name)
38-
data["struct"]["allow_name"].any? { |format| format === struct_name } # rubocop:disable Style/CaseEquality
38+
data["struct"]["include_name"].any? { |format| format === struct_name } # rubocop:disable Style/CaseEquality
3939
end
4040

4141
# Whether generate C type to go
4242
# @param type_name [String]
4343
# @return [Boolean]
4444
def should_generate_type?(type_name)
45-
data["type"]["allow_name"].any? { |format| format === type_name } # rubocop:disable Style/CaseEquality
45+
return false if data["type"]["exclude_name"].any? { |format| format === type_name } # rubocop:disable Style/CaseEquality
46+
47+
data["type"]["include_name"].any? { |format| format === type_name } # rubocop:disable Style/CaseEquality
4648
end
4749
end
4850
end

_tools/ruby_h_to_go/lib/ruby_header_parser/data.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
function:
2-
allow_name:
2+
include_name:
33
- !ruby/regexp /^rb_/i
44
- !ruby/regexp /^rstring_/i
55

6-
deny_name:
6+
exclude_name:
77
# deprecated functions
88
- rb_check_safe_str
99
- rb_clear_constant_cache
@@ -41,15 +41,17 @@ function:
4141
3: array
4242

4343
struct:
44-
allow_name:
44+
include_name:
4545
- !ruby/regexp /^rb_/i
4646
- re_registers
4747

4848
type:
49-
allow_name:
49+
include_name:
5050
- !ruby/regexp /^rb_/i
5151
- !ruby/regexp /^st_/i
5252
- ID
5353
- VALUE
5454
- regex_t
5555
- OnigPosition
56+
57+
exclude_name: []

0 commit comments

Comments
 (0)