Skip to content

Commit 84d382f

Browse files
committed
Fix patch_for_go_gem
1 parent a97564c commit 84d382f

File tree

2 files changed

+29
-77
lines changed

2 files changed

+29
-77
lines changed

_tools/patch_for_go_gem/patch_for_go_gem.rb

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def perform
3131
create_go_mod
3232
update_gem_name_c
3333
update_extconf_rb
34+
update_gemspec
3435
end
3536

3637
private
@@ -125,65 +126,45 @@ def update_gem_name_c
125126
save_file(file_path: gem_name_c_path, content:)
126127
end
127128

128-
def update_extconf_rb # rubocop:disable Metrics/MethodLength
129+
def update_extconf_rb
129130
extconf_rb_path = File.join(ext_dir, "extconf.rb")
130131

131132
content = File.read(extconf_rb_path)
132133

133-
unless content.include?(<<~RUBY)
134-
require "mkmf"
135-
136-
find_executable("go")
137-
RUBY
138-
134+
unless content.include?(%(require "go_gem/mkmf"))
139135
content.gsub!(<<~RUBY, <<~RUBY)
140136
require "mkmf"
141137
RUBY
142138
require "mkmf"
143-
144-
find_executable("go")
145-
146-
# rubocop:disable Style/GlobalVars
147-
$objs = []
148-
def $objs.empty?; false; end
149-
# rubocop:enable Style/GlobalVars
150-
139+
require "go_gem/mkmf"
151140
RUBY
152141
end
153142

154-
unless content.include?(<<~RUBY)
155-
create_makefile("#{gem_name}/#{gem_name}")
156-
157-
case `\#{CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition
158-
RUBY
159-
143+
unless content.include?(%(create_go_makefile("#{gem_name}/#{gem_name}")))
160144
content.gsub!(<<~RUBY, <<~RUBY)
161145
create_makefile("#{gem_name}/#{gem_name}")
162146
RUBY
163-
create_makefile("#{gem_name}/#{gem_name}")
147+
create_go_makefile("#{gem_name}/#{gem_name}")
148+
RUBY
149+
end
164150

165-
case `\#{CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition
166-
when /Free Software Foundation/
167-
ldflags = "-Wl,--unresolved-symbols=ignore-all"
168-
when /clang/
169-
ldflags = "-undefined dynamic_lookup"
170-
end
151+
save_file(file_path: extconf_rb_path, content:)
152+
end
171153

172-
current_dir = File.expand_path(".")
154+
def update_gemspec
155+
content = File.read(gemspec_file)
173156

174-
File.open("Makefile", "a") do |f|
175-
f.write <<~MAKEFILE.gsub(/^ {8}/, "\t")
176-
$(DLLIB): Makefile $(srcdir)/*.go
177-
cd $(srcdir); \
178-
CGO_CFLAGS='$(INCFLAGS)' CGO_LDFLAGS='\#{ldflags}' \\
179-
go build -p 4 -buildmode=c-shared -o \#{current_dir}/$(DLLIB)
180-
MAKEFILE
181-
end
157+
return if content.include?(%(.add_dependency "go_gem")) || content.include?(%(.add_runtime_dependency "go_gem"))
182158

183-
RUBY
184-
end
159+
content =~ /Gem::Specification\.new\s+do\s+\|(.+)\|/
160+
spec_var_name = ::Regexp.last_match(1)
185161

186-
save_file(file_path: extconf_rb_path, content:)
162+
content.gsub!(/^end\n/, <<~RUBY)
163+
#{spec_var_name}.add_dependency "go_gem"
164+
end
165+
RUBY
166+
167+
save_file(file_path: gemspec_file, content:)
187168
end
188169

189170
# @param file_path [String]

_tools/patch_for_go_gem/spec/patch_for_go_gem_spec.rb

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -88,47 +88,18 @@ module github.com/username/#{gem_name}
8888
end
8989

9090
describe file(File.join(gem_name, "ext", gem_name, "extconf.rb")) do
91-
let(:content1) do
92-
<<~RUBY
93-
require "mkmf"
94-
95-
find_executable("go")
96-
97-
# rubocop:disable Style/GlobalVars
98-
$objs = []
99-
def $objs.empty?; false; end
100-
# rubocop:enable Style/GlobalVars
101-
RUBY
102-
end
103-
104-
let(:content2) do
105-
<<~RUBY
106-
create_makefile("#{gem_name}/#{gem_name}")
107-
108-
case `\#{CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition
109-
when /Free Software Foundation/
110-
ldflags = "-Wl,--unresolved-symbols=ignore-all"
111-
when /clang/
112-
ldflags = "-undefined dynamic_lookup"
113-
end
114-
115-
current_dir = File.expand_path(".")
91+
it { should be_file }
92+
it { should exist }
11693

117-
File.open("Makefile", "a") do |f|
118-
f.write <<~MAKEFILE.gsub(/^ {8}/, "\t")
119-
$(DLLIB): Makefile $(srcdir)/*.go
120-
cd $(srcdir); \
121-
CGO_CFLAGS='$(INCFLAGS)' CGO_LDFLAGS='\#{ldflags}' \\
122-
go build -p 4 -buildmode=c-shared -o \#{current_dir}/$(DLLIB)
123-
MAKEFILE
124-
end
125-
RUBY
126-
end
94+
its(:content) { should include %(require "go_gem/mkmf") }
95+
its(:content) { should include %(create_go_makefile("#{gem_name}/#{gem_name}")) }
96+
its(:content) { should_not include %(create_makefile("#{gem_name}/#{gem_name}")) }
97+
end
12798

99+
describe file(File.join(gem_name, "#{gem_name}.gemspec")) do
128100
it { should be_file }
129101
it { should exist }
130102

131-
its(:content) { should include content1 }
132-
its(:content) { should include content2 }
103+
its(:content) { should include %(spec.add_dependency "go_gem") }
133104
end
134105
end

0 commit comments

Comments
 (0)