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

      build_tag = GoGem::Util.ruby_minor_version_build_tag
      sh GoGem::RakeTask.build_env_vars, "golangci-lint run --build-tags #{build_tag} --modules-download-mode=readonly"
    end
  end
end

Constant Summary collapse

DEFAULT_TASK_NAMESPACE =

rubocop:enable Layout/LineLength

: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:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File '_gem/lib/go_gem/rake_task.rb', line 81

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
    define_go_build_tag_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}")



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

attr_accessor :target_dir

#gem_nameString (readonly)

Returns:

  • (String)


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

def gem_name
  @gem_name
end

#go_bin_pathString

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

Returns:

  • (String)

    path to go binary (default: "go")



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

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")



72
73
74
# File '_gem/lib/go_gem/rake_task.rb', line 72

def go_test_args
  @go_test_args
end

#target_dirObject

Returns the value of attribute target_dir.



76
77
78
# File '_gem/lib/go_gem/rake_task.rb', line 76

def target_dir
  @target_dir
end

#task_namespaceSymbol, String

Returns task namespace (default: :go).

Returns:

  • (Symbol, String)

    task namespace (default: :go)



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

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>)


105
106
107
108
109
110
111
112
113
114
115
116
117
# File '_gem/lib/go_gem/rake_task.rb', line 105

def self.build_env_vars
  ldflags = GoGem::Util.generate_ldflags
  cflags = GoGem::Util.generate_cflags

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

  {
    "GOFLAGS"         => GoGem::Util.generate_goflags,
    "CGO_CFLAGS"      => cflags,
    "CGO_LDFLAGS"     => ldflags,
    "LD_LIBRARY_PATH" => ld_library_path,
  }
end

Instance Method Details

#ext_dirString

Returns ext dir name (e.g.ext/<GEM_NAME>/).

Returns:

  • (String)

    ext dir name (e.g.ext/<GEM_NAME>/)



129
130
131
# File '_gem/lib/go_gem/rake_task.rb', line 129

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

#within_target_dir { ... } ⇒ Object

Change current working dir inside ext/<GEM_NAME>/

Yields:



122
123
124
125
126
# File '_gem/lib/go_gem/rake_task.rb', line 122

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