Skip to content

Instantly share code, notes, and snippets.

@dux
Created April 24, 2017 23:26
Show Gist options
  • Save dux/990bec5c7a7a8adb5fe4100c10bd8c9b to your computer and use it in GitHub Desktop.
Save dux/990bec5c7a7a8adb5fe4100c10bd8c9b to your computer and use it in GitHub Desktop.
checks, builds, installes localy, publishes rubygems
#!/usr/bin/env ruby
require 'rubygems'
require 'colorize'
require 'awesome_print'
require 'slop'
require 'irb'
require 'yaml'
module Sys
extend self
def die(text)
puts text.red
exit
end
def run(command)
puts command.blue
system command
end
end
class Gemify
attr_reader :gemfile, :spec, :klass
def initialize
@klass = `ls | grep .gemspec`.split('.gemspec').first
@spec = Gem::Specification::load("#{klass}.gemspec")
@gemfile = '%s-%s.gem' % [@klass, @spec.version]
Sys.die '.gemspec not found' unless @klass
# get credentions for pushing to rubygems
cred = YAML.load File.read('%s/.gem/credentials' % ENV.fetch('HOME'))
puts 'Gem: %s (%s, push as %s from ~/.gem/credentials)' % [@klass.yellow, @spec.version, (cred[:name] || cred[:rubygems_api_key]).yellow]
check!
end
def check!
loca_req = './lib/%s.rb' % @klass
Sys.die 'File %s not found. That is main auto-require, you have to have it.' % loca_req unless File.exists?(loca_req)
Sys.die 'Gemspec name is not "%s", it is "%s".' % [loca_req, @spec.name] unless @spec.name == klass
end
def build
Sys.run 'gem build %s.gemspec' % @klass
end
def push
Sys.run 'gem push %s' % @gemfile
puts 'https://rubygems.org/gems/%s'.yellow % @klass
end
def install
Sys.run 'gem install -l %s' % @gemfile
Sys.run 'gem cleanup %s' % @klass
Sys.run 'gem list %s' % @klass
end
def remove
`rm *.gem`
puts 'removed'
end
def files
@spec.files
end
def minor
old_version = File.read('./.version').gsub(/[\s]/,'') rescue '0.0.0'
v_parts = old_version.split('.')
v_parts.push(v_parts.pop.to_i + 1)
version = v_parts.join('.')
puts 'Version: %s -> %s' % [old_version, version.yellow]
File.write('./.version', version)
end
def console
puts instance_variables
binding.irb
end
end
###
@gem = Gemify.new
opts = Slop.parse do |o|
o.bool '-b', '--build', 'build gem'
o.bool '-p', '--push', 'push to rubygems (gem push %s)' % @gem.gemfile
o.bool '-i', '--install', 'install localy and cleanup (gem install -l %s' % @gem.gemfile
o.bool '-r', '--remove', 'delete .gem files'
o.bool '-f', '--files', 'show files in gem'
o.bool '-m', '--minor', 'increase minor in .version by 1'
o.bool '-c', '--console', 'get pry console to inspec spec'
end
unless ARGV[0]
puts opts
system 'gem list %s' % @gem.klass
system 'gem list %s --remote --all' % @gem.klass
exit
end
opts.build? && @gem.build
opts.push? && @gem.push
opts.install? && @gem.install
opts.files? && ap(@gem.files)
opts.minor? && @gem.minor
opts.console? && Proc.new { ARGV = []; @gem.console }.call
opts.remove? && @gem.remove
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment