Skip to content

Commit 0ea0d4a

Browse files
authored
Merge pull request #222 from ruby-go-gem/feature/print_build_envs
Define `build_envs` task at `GoGem::RakeTask`
2 parents 75dcf40 + 70d9e41 commit 0ea0d4a

File tree

6 files changed

+71
-2
lines changed

6 files changed

+71
-2
lines changed

.github/workflows/build.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,19 @@ jobs:
9999
- uses: ruby/setup-ruby@v1
100100
with:
101101
ruby-version: ruby
102+
bundler-cache: false
102103

103-
- name: export CGO_CFLAGS
104-
run: ruby -e "puts %Q(CGO_CFLAGS=-I#{RbConfig::CONFIG['rubyarchhdrdir']} -I#{RbConfig::CONFIG['rubyhdrdir']})" >> $GITHUB_ENV
104+
# FIXME: setup-go installs cache in `vendor/` and setup-ruby installs cache in `vendor/bundle/`
105+
# If we use the cache in setup-go and setup-ruby at the same time, this doesn't work well because they use the same directory.
106+
# Therefore, the installation location needs to be changed from the setup-ruby default.
107+
- name: bundle install
108+
run: |
109+
set -xe
110+
bundle config --local path ruby-vendor/bundle
111+
bundle install --jobs 4
112+
113+
- name: export CGO_CFLAGS for golangci-lint
114+
run: bundle exec rake go:build_envs[CGO_CFLAGS] >> $GITHUB_ENV
105115

106116
- run: echo $CGO_CFLAGS
107117

_gem/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Following tasks are generated
4949
* `rake go:test`
5050
* `rake go:testrace`
5151
* `rake go:fmt`
52+
* `rake go:build_envs`
5253

5354
#### Example (With config)
5455
```ruby
@@ -68,6 +69,7 @@ Following tasks are generated
6869
* `rake go5:test`
6970
* `rake go5:testrace`
7071
* `rake go5:fmt`
72+
* `rake go5:build_envs`
7173

7274
#### Example (Add additional tasks)
7375
```ruby
@@ -89,6 +91,24 @@ namespace :go do
8991
end
9092
```
9193

94+
#### Example (Use [golangci/golangci-lint-action](https://github.com/golangci/golangci-lint-action))
95+
```yml
96+
jobs:
97+
go-lint:
98+
runs-on: ubuntu-latest
99+
100+
steps:
101+
- uses: actions/checkout@v4
102+
- uses: actions/setup-go@v5
103+
- uses: ruby/setup-ruby@v1
104+
105+
- name: export CGO_CFLAGS for golangci-lint
106+
run: bundle exec rake go:build_envs[CGO_CFLAGS] >> $GITHUB_ENV
107+
108+
- name: Run golangci-lint
109+
uses: golangci/golangci-lint-action@v6
110+
```
111+
92112
#### Available configurations
93113
* `task_namespace` : task namespace (default: `:go`)
94114
* `go_bin_path` : path to go binary (default: `"go"`)

_gem/lib/go_gem/rake_task.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def initialize(gem_name)
8686
define_go_test_task
8787
define_go_testrace_task
8888
define_go_fmt_task
89+
define_go_build_envs_task
8990
end
9091
end
9192

@@ -168,5 +169,19 @@ def define_go_fmt_task
168169
end
169170
end
170171
end
172+
173+
def define_go_build_envs_task
174+
desc "Print build envs for `go build`"
175+
task(:build_envs, [:env_name]) do |_, args|
176+
if args[:env_name]
177+
value = RakeTask.build_env_vars[args[:env_name]]
178+
puts "#{args[:env_name]}=#{value}"
179+
else
180+
RakeTask.build_env_vars.each do |name, value|
181+
puts "#{name}=#{value}"
182+
end
183+
end
184+
end
185+
end
171186
end
172187
end

_gem/sig/go_gem/rake_task.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ module GoGem
3838

3939
def define_go_fmt_task: () -> void
4040

41+
def define_go_build_envs_task: () -> void
42+
4143
def within_target_dir: () { () -> void } -> void
4244

4345
def ext_dir: () -> String

_gem/sig/non-gemify/rake.rbs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
# Monkey path to https://github.com/ruby/gem_rbs_collection/blob/main/gems/rake/13.0/rake.rbs
2+
3+
module Rake
4+
class Task
5+
end
6+
7+
class TaskArguments
8+
include Enumerable[untyped]
9+
10+
def []: (untyped index) -> untyped
11+
def each: () ?{ (untyped, untyped) -> void } -> void
12+
end
13+
14+
module DSL
15+
private
16+
17+
def task: (*untyped args) ?{ (Rake::Task, Rake::TaskArguments) -> void } -> void
18+
| ...
19+
end
20+
end
21+
122
module FileUtils
223
def sh: (Hash[String, String] env, *String cmd, **untyped options) ?{ (bool, Process::Status) -> void } -> void
324
| ...

_gem/spec/go_gem/rake_task_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
it { should be_task_defined("go:test") }
1818
it { should be_task_defined("go:testrace") }
1919
it { should be_task_defined("go:fmt") }
20+
it { should be_task_defined("go:build_envs") }
2021

2122
describe "Add additional tasks" do
2223
include Rake::DSL

0 commit comments

Comments
 (0)