Class: GoGem::RakeTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
_gem/lib/go_gem/rake_task.rb

Overview

Provides rake tasks for go test with CRuby

Examples:

Without config

# Rakefile
require "go_gem/rake_task"

GoGem::RakeTask.new("gem_name")

With config

# Rakefile
require "go_gem/rake_task"

GoGem::RakeTask.new("gem_name") do |t|
  t.task_namespace = "go5"
  t.go_bin_path = "/path/to/go"
  t.go_test_args = "-mod=readonly"
  t.target_dir = "/dir/to/go-mod/"
end

additional tasks

# Rakefile
require "go_gem/rake_task"

t = GoGem::RakeTask.new("gem_name")

namespace :go do
  desc "Run golangci-lint"
  task :lint do
    t.within_target_dir do
      sh "which golangci-lint" do |ok, _|
        raise "golangci-lint isn't installed. See. https://golangci-lint.run/welcome/install/" unless ok
      end
      sh GoGem::RakeTask.build_env_vars, "golangci-lint run"
    end
  end
end

Constant Summary collapse

DEFAULT_TASK_NAMESPACE =
:go
DEFAULT_GO_BIN_PATH =
"go"
DEFAULT_GO_TEST_ARGS =
"-mod=readonly -count=1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gem_name) {|t| ... } ⇒ RakeTask

Returns a new instance of RakeTask.

Parameters:

  • gem_name (String)

Yields:

Yield Parameters:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File '_gem/lib/go_gem/rake_task.rb', line 73

def initialize(gem_name)
  super()

  @gem_name = gem_name

  @task_namespace = DEFAULT_TASK_NAMESPACE
  @go_bin_path = DEFAULT_GO_BIN_PATH
  @go_test_args = DEFAULT_GO_TEST_ARGS
  @target_dir = ext_dir

  yield(self) if block_given?

  namespace(task_namespace) do
    define_go_test_task
    define_go_testrace_task
    define_go_fmt_task
    define_go_build_envs_task
  end
end

Instance Attribute Details

#cwdString

Returns directory when executing go commands. (default: "ext/#{gem_name}").

Returns:

  • (String)

    directory when executing go commands. (default: "ext/#{gem_name}")



68
# File '_gem/lib/go_gem/rake_task.rb', line 68

attr_accessor :target_dir

#gem_nameString (readonly)

Returns:

  • (String)


52
53
54
# File '_gem/lib/go_gem/rake_task.rb', line 52

def gem_name
  @gem_name
end

#go_bin_pathString

Returns path to go binary (default: "go").

Returns:

  • (String)

    path to go binary (default: "go")



60
61
62
# File '_gem/lib/go_gem/rake_task.rb', line 60

def go_bin_path
  @go_bin_path
end

#go_test_argsString

Returns argument passed to go test (default: "-mod=readonly -count=1").

Returns:

  • (String)

    argument passed to go test (default: "-mod=readonly -count=1")



64
65
66
# File '_gem/lib/go_gem/rake_task.rb', line 64

def go_test_args
  @go_test_args
end

#target_dirObject

Returns the value of attribute target_dir.



68
69
70
# File '_gem/lib/go_gem/rake_task.rb', line 68

def target_dir
  @target_dir
end

#task_namespaceSymbol, String

Returns task namespace (default: :go).

Returns:

  • (Symbol, String)

    task namespace (default: :go)



56
57
58
# File '_gem/lib/go_gem/rake_task.rb', line 56

def task_namespace
  @task_namespace
end

Class Method Details

.build_env_varsHash<String, String>

Generate environment variables to build go programs in the Go gem

Returns:

  • (Hash<String, String>)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File '_gem/lib/go_gem/rake_task.rb', line 96

def self.build_env_vars
  ldflags = "-L#{RbConfig::CONFIG["libdir"]} -l#{RbConfig::CONFIG["RUBY_SO_NAME"]}"

  case `#{RbConfig::CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition
  when /Free Software Foundation/
    ldflags << " -Wl,--unresolved-symbols=ignore-all"
  when /clang/
    ldflags << " -undefined dynamic_lookup"
  end

  cflags = [
    RbConfig::CONFIG["CFLAGS"],
    "-I#{RbConfig::CONFIG["rubyarchhdrdir"]}",
    "-I#{RbConfig::CONFIG["rubyhdrdir"]}",
  ].join(" ")

  # FIXME: Workaround for Ubuntu (GitHub Actions)
  if RUBY_PLATFORM =~ /linux/i
    cflags.gsub!("-Wno-self-assign", "")
    cflags.gsub!("-Wno-parentheses-equality", "")
    cflags.gsub!("-Wno-constant-logical-operand", "")
    cflags.gsub!("-Wsuggest-attribute=format", "")
    cflags.gsub!("-Wold-style-definition", "")
    cflags.gsub!("-Wsuggest-attribute=noreturn", "")
    ldflags.gsub!("-Wl,--unresolved-symbols=ignore-all", "")
  end

  ld_library_path = RbConfig::CONFIG["libdir"].to_s

  {
    "CGO_CFLAGS"      => cflags,
    "CGO_LDFLAGS"     => ldflags,
    "LD_LIBRARY_PATH" => ld_library_path,
  }
end

Instance Method Details

#ext_dirString

Returns:

  • (String)


140
141
142
# File '_gem/lib/go_gem/rake_task.rb', line 140

def ext_dir
  File.join("ext", gem_name)
end

#within_target_dir { ... } ⇒ Object

Yields:



133
134
135
136
137
# File '_gem/lib/go_gem/rake_task.rb', line 133

def within_target_dir
  Dir.chdir(target_dir) do # rubocop:disable Style/ExplicitBlockArgument
    yield
  end
end