Class: GemBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_builder.rb

Overview

Builds nitrocop gems — both base (fallback) and platform-specific variants.

Base gem: no binary, users on unsupported platforms get a helpful error. Platform gem: includes precompiled binary in libexec/.

Constant Summary collapse

GEM_SOURCE =
File.expand_path("..", __dir__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version:, platform: nil, binary_path: nil) ⇒ GemBuilder

Returns a new instance of GemBuilder.

Parameters:

  • version (String)

    gem version (e.g. “0.1.0”)

  • platform (String, nil) (defaults to: nil)

    platform string (e.g. “arm64-darwin”), nil for base gem

  • binary_path (String, nil) (defaults to: nil)

    path to compiled binary, required for platform gems



18
19
20
21
22
# File 'lib/gem_builder.rb', line 18

def initialize(version:, platform: nil, binary_path: nil)
  @version = version
  @platform = platform
  @binary_path = binary_path
end

Instance Attribute Details

#binary_pathObject (readonly)

Returns the value of attribute binary_path.



13
14
15
# File 'lib/gem_builder.rb', line 13

def binary_path
  @binary_path
end

#platformObject (readonly)

Returns the value of attribute platform.



13
14
15
# File 'lib/gem_builder.rb', line 13

def platform
  @platform
end

#versionObject (readonly)

Returns the value of attribute version.



13
14
15
# File 'lib/gem_builder.rb', line 13

def version
  @version
end

Instance Method Details

#assemble(dir) ⇒ Object

Assembles gem contents into the given directory. Useful for testing. Returns the directory path.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gem_builder.rb', line 30

def assemble(dir)
  FileUtils.cp_r(File.join(GEM_SOURCE, "lib"), dir)
  FileUtils.cp_r(File.join(GEM_SOURCE, "exe"), dir)

  if platform?
    raise "Binary not found: #{binary_path}" unless File.file?(binary_path)

    libexec = File.join(dir, "libexec")
    FileUtils.mkdir_p(libexec)
    FileUtils.cp(binary_path, File.join(libexec, "nitrocop"))
    FileUtils.chmod(0o755, File.join(libexec, "nitrocop"))
  end

  patch_version(dir)
  write_gemspec(dir)
  dir
end

#build(output_dir: Dir.pwd) ⇒ Object

Builds the .gem file and moves it to output_dir. Returns the path to the built .gem file.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gem_builder.rb', line 50

def build(output_dir: Dir.pwd)
  Dir.mktmpdir("nitrocop-gem-") do |tmpdir|
    assemble(tmpdir)

    Dir.chdir(tmpdir) do
      system("gem", "build", "nitrocop.gemspec", exception: true)
    end

    gem_file = Dir.glob(File.join(tmpdir, "nitrocop-*.gem")).first
    dest = File.join(output_dir, File.basename(gem_file))
    FileUtils.mv(gem_file, dest)
    dest
  end
end

#gemspec_contentObject

Returns the gemspec content as a string.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gem_builder.rb', line 66

def gemspec_content
  lines = []
  lines << '# frozen_string_literal: true'
  lines << ''
  lines << 'require_relative "lib/nitrocop"'
  lines << ''
  lines << 'Gem::Specification.new do |spec|'
  lines << '  spec.name     = "nitrocop"'
  lines << '  spec.version  = Nitrocop::VERSION'
  lines << "  spec.platform = \"#{platform}\"" if platform?
  lines << '  spec.authors  = ["6"]'
  lines << ''
  lines << '  spec.summary     = "Fast Ruby linter targeting RuboCop compatibility"'
  if platform?
    lines << '  spec.description = "A Ruby linter written in Rust that reads your existing .rubocop.yml " \\'
    lines << '                     "and runs 900+ cops. " \\'
    lines << "                     \"Platform variant: #{platform}.\""
  else
    lines << '  spec.description = "A Ruby linter written in Rust that reads your existing .rubocop.yml " \\'
    lines << '                     "and runs 900+ cops."'
  end
  lines << '  spec.homepage    = "https://github.com/6/nitrocop"'
  lines << '  spec.license     = "MIT"'
  lines << ''
  lines << '  spec.required_ruby_version = ">= 3.1.0"'
  lines << ''
  lines << '  spec.metadata["source_code_uri"] = spec.homepage'
  lines << '  spec.metadata["changelog_uri"]   = "#{spec.homepage}/releases"'
  lines << ''
  lines << '  spec.files       = Dir["lib/**/*", "exe/**/*", "libexec/**/*"]'
  lines << '  spec.bindir      = "exe"'
  lines << '  spec.executables = ["nitrocop"]'
  lines << 'end'
  lines.join("\n") + "\n"
end

#platform?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/gem_builder.rb', line 24

def platform?
  !platform.nil?
end