class GGem::Gemspec

Constants

BUILD_TO_DIRNAME
CmdError
DEFAULT_PUSH_HOST
LoadError
NotFoundError
PUSH_HOST_META_KEY

Attributes

gem_file[R]
gem_file_name[R]
name[R]
path[R]
push_host[R]
version[R]
version_tag[R]

Public Class Methods

new(root_path) click to toggle source
# File lib/ggem/gemspec.rb, line 20
def initialize(root_path)
  @root = Pathname.new(File.expand_path(root_path))
  raise NotFoundError unless @root.exist?
  @path = Pathname.new(Dir[File.join(@root, "{,*}.gemspec")].first.to_s)
  raise NotFoundError unless @path.exist?

  @spec        = load_gemspec(@path)
  @name        = @spec.name
  @version     = @spec.version
  @version_tag = "v#{@version}"

  @gem_file_name  = "#{@name}-#{@version}.gem"
  @gem_file       = File.join(BUILD_TO_DIRNAME, @gem_file_name)
  @built_gem_path = @root.join(@gem_file)

  @push_host = get_push_host(@spec)
end

Public Instance Methods

run_build_cmd() click to toggle source
# File lib/ggem/gemspec.rb, line 38
def run_build_cmd
  run_cmd("gem build --verbose #{@path}").tap do
    gem_path = @root.join(@gem_file_name)
    run_cmd("mkdir -p #{@built_gem_path.dirname}")
    run_cmd("mv #{gem_path} #{@built_gem_path}")
  end
end
run_install_cmd() click to toggle source
# File lib/ggem/gemspec.rb, line 46
def run_install_cmd
  run_cmd("gem install #{@built_gem_path}")
end
run_push_cmd() click to toggle source
# File lib/ggem/gemspec.rb, line 50
def run_push_cmd
  run_cmd("gem push #{@built_gem_path} --host #{@push_host}")
end

Private Instance Methods

get_meta(spec) click to toggle source
# File lib/ggem/gemspec.rb, line 89
def get_meta(spec)
  (spec.respond_to?(:metadata) ? spec.metadata : {}) || {}
end
get_push_host(spec) click to toggle source
# File lib/ggem/gemspec.rb, line 83
def get_push_host(spec)
  ENV["GGEM_PUSH_HOST"] ||
  get_meta(spec)[PUSH_HOST_META_KEY] ||
  DEFAULT_PUSH_HOST
end
load_gemspec(path) click to toggle source
# File lib/ggem/gemspec.rb, line 68
def load_gemspec(path)
  eval( # rubocop:disable Security/Eval
    path.read,
    TOPLEVEL_BINDING,
    path.expand_path.to_s,
  )
rescue ScriptError, StandardError => ex
  original_line = ex.backtrace.find{ |line| line.include?(path.to_s) }
  msg =
    "There was a #{ex.class} while loading #{path.basename}: \n#{ex.message}"
  msg << " from\n  #{original_line}" if original_line
  msg << "\n"
  raise LoadError, msg
end
run_cmd(cmd_string) click to toggle source
# File lib/ggem/gemspec.rb, line 56
def run_cmd(cmd_string)
  cmd = Scmd.new(cmd_string)
  cmd.run
  unless cmd.success?
    raise(
      CmdError,
      "#{cmd_string}\n#{cmd.stderr.empty? ? cmd.stdout : cmd.stderr}",
    )
  end
  [cmd_string, cmd.exitstatus, cmd.stdout]
end