Skip to content

Commit 1d6d46f

Browse files
committed
Impl GoGem::RakeTask
1 parent e2abf04 commit 1d6d46f

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

_gem/lib/go_gem/rake_task.rb

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
require "rake"
2+
require "rake/tasklib"
3+
4+
module GoGem
5+
# Provides rake tasks for `go test` with CRuby
6+
#
7+
# @example Without config
8+
# # Rakefile
9+
# require "go_gem/rake_task"
10+
#
11+
# GoGem::RakeTask.new("gem_name")
12+
#
13+
# @example With config
14+
# # Rakefile
15+
# require "go_gem/rake_task"
16+
#
17+
# GoGem::RakeTask.new("gem_name") do |config|
18+
# config.task_namespace = "go5"
19+
# config.go_bin_path = "/path/to/go"
20+
# config.go_test_args = "#{GoGem::RakeTask::DEFAULT_GO_TEST_ARGS} -race"
21+
# end
22+
class RakeTask < ::Rake::TaskLib
23+
# @!attribute [r] gem_name
24+
# @return [String]
25+
attr_reader :gem_name
26+
27+
# @!attribute task_namespace
28+
# @return [Symbol] task namespace (default: `:go`)
29+
attr_accessor :task_namespace
30+
31+
# @!attribute go_bin_path
32+
# @return [String] path to go binary (default: `"go"`)
33+
attr_accessor :go_bin_path
34+
35+
# @!attribute go_test_args
36+
# @return [String] argument passed to `go test` (default: `"-mod=readonly -count=1"`)
37+
attr_accessor :go_test_args
38+
39+
# @param gem_name [String]
40+
# @yield configuration of {RakeTask}
41+
# @yieldparam config [RakeTask]
42+
def initialize(gem_name)
43+
super()
44+
45+
@gem_name = gem_name
46+
47+
@task_namespace = :go
48+
@go_bin_path = "go"
49+
@go_test_args = "-mod=readonly -count=1"
50+
51+
yield(self) if block_given?
52+
53+
namespace(task_namespace) do
54+
desc "Run go test"
55+
task(:test) do
56+
within_ext_dir do
57+
sh RakeTask.build_env_vars, "#{go_bin_path} test #{go_test_args} ./..."
58+
end
59+
end
60+
end
61+
end
62+
63+
# Generate environment variables to build go programs in the Go gem
64+
#
65+
# @return [Hash<String, String>]
66+
def self.build_env_vars
67+
ldflags = "-L#{RbConfig::CONFIG["libdir"]} -l#{RbConfig::CONFIG["RUBY_SO_NAME"]}"
68+
69+
case `#{RbConfig::CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition
70+
when /Free Software Foundation/
71+
ldflags << " -Wl,--unresolved-symbols=ignore-all"
72+
when /clang/
73+
ldflags << " -undefined dynamic_lookup"
74+
end
75+
76+
cflags = "#{RbConfig::CONFIG["CFLAGS"]} -I#{RbConfig::CONFIG["rubyarchhdrdir"]} -I#{RbConfig::CONFIG["rubyhdrdir"]}"
77+
78+
# FIXME: Workaround for GitHub Actions
79+
if ENV["GITHUB_ACTIONS"]
80+
cflags.gsub!("-Wno-self-assign", "")
81+
cflags.gsub!("-Wno-parentheses-equality", "")
82+
cflags.gsub!("-Wno-constant-logical-operand", "")
83+
cflags.gsub!("-Wsuggest-attribute=format", "")
84+
cflags.gsub!("-Wold-style-definition", "")
85+
cflags.gsub!("-Wsuggest-attribute=noreturn", "")
86+
ldflags.gsub!("-Wl,--unresolved-symbols=ignore-all", "")
87+
end
88+
89+
ld_library_path = RbConfig::CONFIG["libdir"]
90+
91+
{
92+
"CGO_CFLAGS" => cflags,
93+
"CGO_LDFLAGS" => ldflags,
94+
"LD_LIBRARY_PATH" => ld_library_path,
95+
}
96+
end
97+
98+
private
99+
100+
# @yield
101+
def within_ext_dir
102+
Dir.chdir(ext_dir) do
103+
yield
104+
end
105+
end
106+
107+
# @return [String]
108+
def ext_dir
109+
File.join("ext", gem_name)
110+
end
111+
end
112+
end

_gem/spec/go_gem/rake_task_spec.rb

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+
RSpec.describe GoGem::RakeTask do
4+
before { Rake::Task.clear }
5+
6+
after { Rake::Task.clear }
7+
8+
describe "defining tasks" do
9+
context "with default params" do
10+
let(:gem_name) { "my_gem" }
11+
12+
subject do
13+
GoGem::RakeTask.new(gem_name)
14+
Rake::Task
15+
end
16+
17+
it { should be_task_defined("go:test") }
18+
end
19+
end
20+
end

_gem/spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "go_gem"
44
require "go_gem/mkmf"
5+
require "go_gem/rake_task"
56

67
require "tmpdir"
78
require "serverspec"

0 commit comments

Comments
 (0)