Created
February 13, 2014 14:30
-
-
Save tobynet/8976000 to your computer and use it in GitHub Desktop.
The badge generator for ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
# Implementations | |
module CodeBadge | |
DEFAULT_README = 'README.md' | |
class << self | |
# TODO: Move tempaltes to badges.yml | |
def template(type, user_name, project_name, gem_name = project_name, branch = "master") | |
case type | |
when :gem_version | |
"[![Gem Version](https://badge.fury.io/rb/#{gem_name}.png)](http://badge.fury.io/rb/#{gem_name})" | |
when :travis | |
"[![Build Status](https://travis-ci.org/#{user_name}/#{project_name}.png?branch=#{branch})](https://travis-ci.org/#{user_name}/#{project_name})" | |
when :gemnasium | |
"[![Dependency Status](https://gemnasium.com/#{user_name}/#{project_name}.png)](https://gemnasium.com/#{user_name}/#{project_name})" | |
when :codeclimate | |
"[![Code Climate](https://codeclimate.com/github/#{user_name}/#{project_name}.png)](https://codeclimate.com/github/#{user_name}/#{project_name})" | |
when :coveralls | |
"[![Coverage Status](https://coveralls.io/repos/#{user_name}/#{project_name}/badge.png?branch=#{branch})](https://coveralls.io/r/#{user_name}/#{project_name})" | |
end | |
end | |
def all_templates(*opts) | |
[:gem_version, :travis, :gemnasium, :codeclimate, :coveralls].map{ |type| | |
template(type, *opts) | |
}.join(" ") | |
end | |
# e.g. old README.md => new README.md | |
# | |
# old README.md: | |
# | |
# # FooBar | |
# | |
# ## Installation | |
# | |
# new README.md: | |
# | |
# # FooBar | |
# [badges here] | |
# | |
# ## Installation | |
def apply_badges_to_the_readme(readme_filename = DEFAULT_README, *opts) | |
# Read from the readme | |
text = File.read(readme_filename) | |
badges = all_templates(*opts) | |
if /^# .+\n/ =~ text | |
# Check overlappings | |
if /^# .+\n\[\!\[/ =~ text | |
raise "Badges may already exists in #{readme_filename}." | |
end | |
# Insert badges | |
new_text = text.gsub(/(^# .+\n)/){ $1 + badges + "\n" } | |
# Save to the readme | |
File.write(readme_filename, new_text) | |
end | |
end | |
end | |
end | |
# def run_test | |
# require 'minitest/autorun' | |
# begin | |
# require 'minitest/pride' | |
# rescue LoadError | |
# end | |
# | |
# # TODO: Write spec to follows: | |
# describe CodeBadge do | |
# it '.foobar' do | |
# CodeBadge.foobar('foobar').must_equal 6 | |
# CodeBadge.foobar('だるい').must_equal 3 | |
# end | |
# end | |
# end | |
# Parse options | |
require 'optparse' | |
program_name = File.basename(__FILE__) | |
config = {} | |
opt = OptionParser.new do |opts| | |
opts.banner = 'CodeBadge: The badge generateor for README.md' | |
opts.define_head "Usage: #{program_name} USER_NAME PROJECT_NAME" | |
opts.separator <<-EOD | |
Examples: | |
# Output texts for badges | |
$ #{program_name} toooooooby/kagami | |
: | |
# Apply a patch to the README | |
$ #{program_name} toooooooby/kagami -p | |
$ #{program_name} toooooooby kagami --patch --readme=readme.mkd | |
Options: | |
EOD | |
opts.on('-s', '--sample', 'Run sample code as usage or examples') do |v| | |
cmd = "#{program_name} toooooooby kagami" | |
puts "$ #{cmd}" | |
system(*cmd.split) | |
exit | |
end | |
opts.on('-p', '--patch', 'Apply a patch to README.md') do |v| | |
config[:patch_mode] = true | |
end | |
opts.on('-r', "--readme=README", | |
"Specify the readme filename. Default: '#{CodeBadge::DEFAULT_README}'") do |v| | |
config[:readme_filename] = v | |
end | |
# opts.on_tail('-t', '--test', 'Run test **FOR DEVELOPER** ') do | |
# run_test | |
# exit | |
# end | |
opts.on_tail('-h', '--help', 'Show this message') do | |
puts opts | |
exit | |
end | |
end | |
opt.parse! | |
# Set user_name and project_name | |
case ARGV.size | |
when 1 | |
# e.g. toooooooby/kagami | |
user_name, project_name = ARGV.shift.split("/") | |
when 2..Float::INFINITY | |
# e.g. toooooooby kagami | |
user_name = ARGV.shift | |
project_name = ARGV.shift | |
else | |
# e.g. [NO ARGUMENTS] | |
puts opt | |
exit | |
end | |
begin | |
if config[:patch_mode] | |
CodeBadge.apply_badges_to_the_readme( | |
config[:readme_filename] || CodeBadge::DEFAULT_README, | |
user_name, project_name) | |
else | |
puts CodeBadge.all_templates(user_name, project_name) | |
end | |
rescue => e | |
STDERR.puts "[ERROR] " + e.message | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment