Module: Distrib::Ruby::Gemspec

Defined in:
lib/distrib/ruby/gemspec.rb

Class Method Summary collapse

Class Method Details

.build!(gemspec_path, **kwargs) {|Gem::Specification| ... } ⇒ Gem::Specification

Builds up a Gem::Specification.

Examples:

require 'distrib/ruby/gemspec'

Distrib::Ruby::Gemspec.build!(__FILE__) do |gemspec|
  gemspec.add_dependency 'liquid'
end

Parameters:

  • gemspec_path (String)

    the path to the gemspec file

  • kwargs (Hash)

    keyword arguments for the gemspec

Yields:

  • (Gem::Specification)

    the gemspec object

Returns:

  • (Gem::Specification)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/distrib/ruby/gemspec.rb', line 23

def self.build!(gemspec_path, **kwargs, &block)
  gemspec_path = Pathname(gemspec_path)

  gemspec = Gem::Specification.new do |s|
    s.name = (kwargs[:name] || gemspec_path.basename('.*')).to_s
    s.version = (kwargs[:version] || File.read('VERSION').chomp).to_s
    s.date = (kwargs[:date] || File.mtime('VERSION').strftime('%Y-%m-%d')).to_s
    s.license = 'Unlicense' if File.exist?('UNLICENSE')

    authors = (File.readlines('AUTHORS') rescue []).map(&:chomp)
    s.authors = authors.map { |entry| entry[/^\*\s+(.+?)\s+</, 1] }
    s.email = authors.map { |entry| entry[/<([^>]+)>/, 1] }

    s.files = %w[AUTHORS CHANGES.md README.md UNLICENSE VERSION]
    s.files.reject! { |f| !File.exist?(f) }
    s.files += Dir['lib/**/*.rb']
    s.files += Dir['ext/**/*.{rb,rs,lock,toml}']
    s.extensions = Dir['ext/*/extconf.rb']

    s.required_ruby_version = '>= 4.0'  # just a default

    s.add_dependency 'rb_sys' if File.exist?('Cargo.toml')

    s.add_development_dependency 'distrib',       '~> 0' unless s.name == 'distrib'
    s.add_development_dependency 'rake',          '~> 13'
    s.add_development_dependency 'rake-compiler', '~> 1.3' if File.exist?('Cargo.toml')
    s.add_development_dependency 'rspec',         '~> 3.13' if Dir.exist?('spec')
    s.add_development_dependency 'yard' ,         '~> 0.9'
  end

  yield gemspec if block_given?

  gemspec. = (gemspec. || {}).transform_keys(&:to_s)

  if !gemspec..has_key?('documentation_uri')
    gemspec.['documentation_uri'] =
      "https://rubydoc.info/gems/#{gemspec.name}"
  end

  if !gemspec..has_key?('homepage_uri')
    gemspec.['homepage_uri'] =
      gemspec.homepage || "https://rubygems.org/gems/#{gemspec.name}"
  end

  gemspec
end