Sha256: b66eb99c35690909b44bd18b41855f9f009cb4987eb44a71a3e6a3a491fdeaaa

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 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
      fullname = root.basename.to_s
      /\A(.+)(?:-\d+(?:\.\d+)*)\z/.if_match(fullname, false) { |m| m[1] }.if_present(fullname)
    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_file.value
    end

    private

    def gemfile_path_uncached
      root.join('Gemfile')
    end

    def gemfile_lock_path_uncached
      gemfile_path.basename_sub { |b| "#{b}.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
      ::EacRubyGemsUtils::Gem::VersionFile.new(version_file_path)
    end

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

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
avm-tools-0.68.0 vendor/eac_ruby_gems_utils/lib/eac_ruby_gems_utils/gem.rb
eac_ruby_gems_utils-0.7.0 lib/eac_ruby_gems_utils/gem.rb
avm-tools-0.67.0 vendor/eac_ruby_gems_utils/lib/eac_ruby_gems_utils/gem.rb