Skip to content

Commit 71c3d90

Browse files
authored
Merge pull request #276 from ruby-go-gem/fix-pkg-go-dev
Fixed https://pkg.go.dev/ didn't generate documentation for auto-generated code
2 parents b859b55 + d2e73fc commit 71c3d90

File tree

7 files changed

+68
-9
lines changed

7 files changed

+68
-9
lines changed

_tools/ruby_h_to_go/config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This tag appears in https://pkg.go.dev/github.com/ruby-go-gem/go-gem-wrapper/ruby
2+
default_tag: ruby_3_3
3+
4+
available_tags:
5+
- ruby_3_3
6+
- ruby_3_4

_tools/ruby_h_to_go/lib/ruby_h_to_go.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
require "forwardable"
44
require "ruby_header_parser"
55
require "go_gem/util"
6+
require "yaml"
67

78
require_relative "ruby_h_to_go/type_helper"
89

910
require_relative "ruby_h_to_go/argument_definition"
1011
require_relative "ruby_h_to_go/cli"
12+
require_relative "ruby_h_to_go/config"
1113
require_relative "ruby_h_to_go/go_util"
1214
require_relative "ruby_h_to_go/enum_definition"
1315
require_relative "ruby_h_to_go/function_definition"
@@ -17,4 +19,8 @@
1719

1820
# Generate Go binding from ruby.h
1921
module RubyHToGo
22+
# @return [RubyHToGo::Config]
23+
def self.config
24+
@config ||= RubyHToGo::Config.new
25+
end
2026
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module RubyHToGo
4+
# Manage config.yml
5+
class Config
6+
def initialize
7+
@config = YAML.load_file(File.expand_path("../../config.yml", __dir__))
8+
end
9+
10+
# @return [String]
11+
def default_tag
12+
@config["default_tag"]
13+
end
14+
15+
# @return [Array<String>]
16+
def available_tags
17+
@config["available_tags"]
18+
end
19+
end
20+
end

_tools/ruby_h_to_go/lib/ruby_h_to_go/go_util.rb

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module RubyHToGo
44
# helper methods for generating go code
5-
module GoUtil
5+
module GoUtil # rubocop:disable Metrics/ModuleLength
66
# @param str [String]
77
# @return [String]
88
def self.snake_to_camel(str)
@@ -13,19 +13,38 @@ def self.snake_to_camel(str)
1313

1414
# Generate initial go file whether not exists
1515
# @param go_file_path [String]
16-
def self.generate_initial_go_file(go_file_path)
16+
def self.generate_initial_go_file(go_file_path) # rubocop:disable Metrics/MethodLength
1717
return if File.exist?(go_file_path)
1818

19-
ruby_build_tag = GoGem::Util.ruby_minor_version_build_tag
20-
21-
File.binwrite(go_file_path, <<~GO)
19+
header = +<<~GO
2220
// THE AUTOGENERATED LICENSE. ALL THE RIGHTS ARE RESERVED BY ROBOTS.
2321
2422
// WARNING: This file has automatically been generated
2523
// Code generated by ruby_h_to_go. DO NOT EDIT.
2624
27-
//go:build #{ruby_build_tag}
25+
GO
26+
27+
ruby_build_tag = GoGem::Util.ruby_minor_version_build_tag
28+
29+
header <<
30+
if ruby_build_tag == RubyHToGo.config.default_tag
31+
other_tags = RubyHToGo.config.available_tags - [RubyHToGo.config.default_tag]
32+
condition = other_tags.map { |tag| "!#{tag}" }.join(" && ")
2833

34+
<<~GO
35+
// FIXME: https://pkg.go.dev/ doesn't support custom build tag.
36+
// Therefore, if no build tag is passed, treat it as the default tag
37+
//go:build #{ruby_build_tag} || (#{condition})
38+
39+
GO
40+
else
41+
<<~GO
42+
//go:build #{ruby_build_tag}
43+
44+
GO
45+
end
46+
47+
header << <<~GO
2948
package ruby
3049
3150
/*
@@ -38,6 +57,8 @@ def self.generate_initial_go_file(go_file_path)
3857
)
3958
4059
GO
60+
61+
File.binwrite(go_file_path, header)
4162
end
4263

4364
C_TYPE_TO_GO_TYPE = {

ruby/enum_ruby_3_3_generated.go

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ruby/function_ruby_3_3_generated.go

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ruby/type_ruby_3_3_generated.go

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)