Sha256: f1841067c4918cafc665945ddb88484cd3bfcf1a4363dd606683a9d02c8f1534

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'
require 'eac_ruby_utils/envs'

module EacRubyGemsUtils
  class Gem
    require_sub __FILE__
    enable_simple_cache

    GEMSPEC_EXTNAME = '.gemspec'

    common_constructor :root do
      @root = ::Pathname.new(root).expand_path
    end

    def to_s
      name
    end

    def bundle(*args)
      ::EacRubyGemsUtils::Gem::Command.new(self, %w[bundle] + args).envvar_gemfile
    end

    def gemfile_lock_gem_version(gem_name)
      gemfile_lock_content.specs.find { |gem| gem.name == gem_name }.if_present(&:version)
    end

    def gemfile_lock_content
      ::Bundler::LockfileParser.new(::Bundler.read_file(gemfile_lock_path))
    end

    def name
      name_by_gemspec || name_by_path
    end

    def name_by_gemspec
      gemspec_path.if_present { |v| v.basename(GEMSPEC_EXTNAME).to_path }
    end

    def name_by_path
      root.basename.to_s
    end

    def namespace_parts
      name.split('-')
    end

    def rake(*args)
      raise "File \"#{rakefile_path}\" does not exist" unless rakefile_path.exist?

      bundle('exec', 'rake', '--rakefile', rakefile_path, *args)
    end

    def version
      /VERSION\s*=\s*[\'\"]([^\'\"]+)[\'\"]/.if_match(version_file.read) { |m| m[1] }
    end

    private

    def gemfile_path_uncached
      root.join('Gemfile')
    end

    def gemfile_lock_path_uncached
      root.join('Gemfile.lock')
    end

    def gemspec_path_uncached
      ::Pathname.glob("#{root.to_path}/*#{GEMSPEC_EXTNAME}").first
    end

    def rakefile_path_uncached
      root.join('Rakefile')
    end

    def version_file_uncached
      root.join('lib', *namespace_parts, 'version.rb')
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
avm-tools-0.58.0 vendor/eac_ruby_gems_utils/lib/eac_ruby_gems_utils/gem.rb
avm-tools-0.57.0 vendor/eac_ruby_gems_utils/lib/eac_ruby_gems_utils/gem.rb
eac_ruby_gems_utils-0.6.0 lib/eac_ruby_gems_utils/gem.rb
avm-tools-0.56.0 vendor/eac_ruby_gems_utils/lib/eac_ruby_gems_utils/gem.rb