Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gemify #77

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more concise version of this would be:

# See http://help.github.com/ignore-files/ for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
#   git config --global core.excludesfile ~/.gitignore

# Ignore all of the generated gem stuff
/pkg
/*.gem

# Ignore bundler config
/.bundle
/Gemfile.lock

# Ignore all bundler caching
/vendor/cache
/vendor/ruby

# Ignore all tempfiles
/tmp

# Ignores that should be in the global gitignore
/coverage
/doc

Based off the Rails template gitignore.

3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: ruby
rvm:
- 2.0.0
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in shields_io.gemspec
gemspec
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,60 @@ Well here's a hint, if you need to resize the badge yourself in Photoshop make s

In Illustrator, it's [a little different](http://f.cl.ly/items/071J0Q2m0D38250g2s1F/shields_resize_illustrator.mov) (4.8 MB .mov video).


## Installation (ruby class / command line)

[![Build Status](https://travis-ci.org/OriPekelman/shields.io.png?branch=gemify)](https://travis-ci.org/OriPekelman/shields.io)

There is a small ruby class / command line tool to generate the images from a template.
It can output a parametrized SVG, or any format (gif/png/jpg) etc..

Add this line to your application's Gemfile:

gem 'shields_io'

And then execute:

$ bundle

Or install it yourself as:

$ gem install shields_io

## Command line Usage

Usage: ./generate_shield [options]

example:
./generate_shield --height=18 --vendor_text=Travis --vendor_width=40 --vendor_color=gray --status_text=Passing --status_color=green --format=png >travis.png

-v, Print the version
--font_size Font size
--height Shield height
--vendor_text Vendor text
--vendor_width Vendor width
--vendor_color Vendor Color gray,lightgray,darkgray,green,yellowgreen,yellow,red
--status_text Status Text
--status_width Status Width
--status_color Status Color gray,lightgray,darkgray,green,yellowgreen,yellow,red
--format Format svg, png etc..
-h, --help Display this help message.


## Use in code (ruby)
require "shields_io"
ShieldsIo::SVG.new(opts).render # will render an svg opts is a hash of options
ShieldsIo::SVG.new(opts).render_bitmap "gif" # will render a gif (default bitmap format is png) opts is a hash of options

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request


## Contributions
See [CONTRIBUTING.md](CONTRIBUTING.md).

Expand Down
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "bundler/gem_tasks"
require 'rake/testtask'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need both these requires, it'll double up on the bundler tasks (sadly). rake/testtask also loads bundler/gem_tasks.


Rake::TestTask.new do |t|
t.libs << "lib"
t.test_files = FileList['lib/test/**/*_spec.rb']

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't test files usually stored in {root}/(spec|test)/**/*_spec.rb?

end

task :default => [:test]
34 changes: 34 additions & 0 deletions bin/generate_shield
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env ruby
require "slop"
require_relative "../lib/shields_io"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to use require relative for a binary of a gem, just do require "shields_io".


opts = Slop.parse help: true do
banner 'Usage: ./generate_shield [options]
example:
./generate_shield --font_size=18 --height=18 --vendor_text=Travis --vendor_width=40 --vendor_color=gray --status_text=Passing --status_color=green --format=png >travis.png
'

on '-v', 'Print the version' do
puts "Version #{ShieldsIo::VERSION}"
end

on :height=, "Shield height", as: Integer
on :font_size=, "Font size", as: Integer
on :vendor_text=, "Vendor text"
on :vendor_width=, "Vendor width", as: Integer
on :vendor_color=, "Vendor Color #{ShieldsIo::SVG::Colors.join(",")}"
on :status_text=, "Status Text"
on :status_width=, "Status Width", as: Integer
on :status_color=, "Status Color #{ShieldsIo::SVG::Colors.join(",")}"
on :format=, "Format svg, png etc.."
end

opts = ShieldsIo::SVG::Defaults.merge(opts.to_h.delete_if { |k, v| v.nil? })

if opts[:format]=="svg"
$stdout.print ShieldsIo::SVG.new(opts).render
exit(0)
else
$stdout.print ShieldsIo::SVG.new(opts).render_bitmap opts[:format] unless opts[:format].nil?
exit(0)
end
42 changes: 42 additions & 0 deletions lib/shields_io.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require_relative "shields_io/version"
require "erubis"
require "mini_magick"

module ShieldsIo
class SVG
Colors = %w{gray lightgray darkgray green yellowgreen yellow red}
Defaults = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General accepted ruby standards suggest having all caps for constants, like COLORS and DEFAULTS to differentiate between classes, modules, and value constants. Just a thought.

font_size: 10,
height: 18,
vendor_text: "vendor",
vendor_width: 50,
vendor_color: "gray",
status_text: "status",
status_width: 70,
status_color: "lightgray"
}

def initialize(opts={})
@shield=opts
@template = File.read("#{File.dirname(__FILE__)}/template.svg.erb")
raise "unknown vendor color" unless ShieldsIo::SVG::Colors.include? @shield[:vendor_color]
raise "unknown status color" unless ShieldsIo::SVG::Colors.include? @shield[:status_color]
raise "height not an integer" unless @shield[:height].is_a? Integer
raise "vendor width not an integer" unless @shield[:vendor_width].is_a? Integer
raise "status width not an integer" unless @shield[:status_width].is_a? Integer
raise "font_size not an integer" unless @shield[:font_size].is_a? Integer

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd switch this to a case statement for readabilities sake.

I would also suggest raising an actual error, like ArgumentError.

end

def render
Erubis::Eruby.new(@template).result(@shield)
end

def render_bitmap(format="png")
buffer = StringIO.new(self.render)
img = MiniMagick::Image.read(buffer,".svg")
img.format format
img.to_blob
end

end
end
3 changes: 3 additions & 0 deletions lib/shields_io/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ShieldsIo
VERSION = "0.0.1"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gem seems complete enough, it should be 1.0.0.

end
73 changes: 73 additions & 0 deletions lib/template.svg.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<svg version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="<%=vendor_width + status_width + 4%>px"
height="<%= height %>px">
<defs>
<linearGradient id="gray" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="0" y2="<%= height %>">
<stop offset="0" style="stop-color:#AAAAAA" />
<stop offset="0.1" style="stop-color:#5A5A5A" />
<stop offset="0.96" style="stop-color:#3F3F3F" />
<stop offset="1" style="stop-color:#2E2E2E" />
</linearGradient>
<linearGradient id="lightgray" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="0" y2="<%= height %>">
<stop offset="0" style="stop-color:#D6D6D6" />
<stop offset="0.1" style="stop-color:#9F9F9F" />
<stop offset="0.96" style="stop-color:#707070" />
<stop offset="1" style="stop-color:#515151" />
</linearGradient>
<linearGradient id="darkgray" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="0" y2="<%= height %>">
<stop offset="0.0" style="stop-color:#909090" />
<stop offset="0.1" style="stop-color:#313131" />
<stop offset="0.96" style="stop-color:#232323" />
<stop offset="1" style="stop-color:#191919" />
</linearGradient>
<linearGradient id="green" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="0" y2="<%= height %>">
<stop offset="0" style="stop-color:#9DED7D"/>
<stop offset="0.1" style="stop-color:#4AD115"/>
<stop offset="0.96" style="stop-color:#31B70E"/>
<stop offset="1" style="stop-color:#23630A"/>
</linearGradient>
<linearGradient id="yellowgreen" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="0" y2="<%= height %>">
<stop offset="0" style="stop-color:#D1F171"/>
<stop offset="0.1" style="stop-color:#97CA00"/>
<stop offset="0.96" style="stop-color:#6B8F00"/>
<stop offset="1" style="stop-color:#4D6700"/>
</linearGradient>
<linearGradient id="yellow" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="0" y2="<%= height %>">
<stop offset="0" style="stop-color:#FFE37F"/>
<stop offset="0.1" style="stop-color:#E0B417"/>
<stop offset="0.96" style="stop-color:#9E7F10"/>
<stop offset="1" style="stop-color:#725C0C"/>
</linearGradient>
<linearGradient id="red" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="0" y2="<%= height %>">
<stop offset="0" style="stop-color:#FFAC9C"/>
<stop offset="0.1" style="stop-color:#E05D44"/>
<stop offset="0.96" style="stop-color:#9E4230"/>
<stop offset="1" style="stop-color:#722F22"/>
</linearGradient>
<filter id="dropshadow" height="100%">
<feGaussianBlur in="SourceAlpha" stdDeviation="0"/>
<feOffset dx="0" dy="1" result="offsetblur"/>
<feComponentTransfer xmlns="http://www.w3.org/2000/svg">
<feFuncA type="linear" slope="0.3"/>
</feComponentTransfer>
<feMerge>
<feMergeNode/>
<feMergeNode in="SourceGraphic"/>
</feMerge>
</filter>
</defs>

<g id="vendor">
<path d="M0,9 v-6 q0,-3,3,-3 v18 q-3,0,-3,-3 Z" fill="url(#<%= vendor_color %>)" />
<rect x="3" y="0" width="<%= vendor_width %>" height="<%= height%>" fill="url(#<%= vendor_color %>)" />
<text x="6" y="12" font-family="Open Sans" font-size="<%= font_size %>" fill="#FFFFFF" style="filter:url(#dropshadow)"><%= vendor_text %></text>
</g>

<g id="status">
<rect x="<%= vendor_width%>" y="0" width="<%= status_width %>" height="<%= height %>" fill="url(#<%= status_color %>)" />
<path fill="url(#<%= status_color %>)" d="M<%=vendor_width + status_width + 3%>,9 v-6 q0,-3,-3,-3 v<%=height%> q3,0,3,-3 Z" />
<text x="<%= vendor_width + 4%>" y="12" font-family="Open Sans" font-size="<%= font_size %>" fill="#FFFFFF" style="filter:url(#dropshadow)"><%= status_text%></text>
</g>
</svg>
36 changes: 36 additions & 0 deletions lib/test/test_shields_io_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "minitest/autorun"
require 'rake/testtask'
require "rexml/document"
require "mini_magick"
require_relative "../shields_io"

describe ShieldsIo::SVG do
before do
@shield = ShieldsIo::SVG.new(ShieldsIo::SVG::Defaults)
end

describe "render" do
it "must render svg" do
doc = REXML::Document.new @shield.render
doc.root.name.must_equal "svg"
end

it "must take into account default options" do
doc = REXML::Document.new @shield.render
doc.elements.to_a("//text").first.text.must_equal "vendor"
end

it "must take into account custom options" do
doc = REXML::Document.new ShieldsIo::SVG.new(ShieldsIo::SVG::Defaults.merge(vendor_text: "plop")).render
doc.elements.to_a("//text").first.text.must_equal "plop"
end
end

describe "render bitmap" do
it "renders a png" do
buffer = StringIO.new(@shield.render_bitmap)
img = MiniMagick::Image.read(buffer,".png")
img.mime_type.must_equal "image/png"
end
end
end
Binary file added lib/travis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions shields_io.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'shields_io/version'

Gem::Specification.new do |spec|
spec.name = "shields_io"
spec.version = ShieldsIo::VERSION
spec.authors = ["Ori Pekelman"]
spec.email = ["ori@pekelman.com"]
spec.description = %q{Small gem to generate shields (travis, code climate, etc..)}
spec.summary = %q{Small gem to generate shields (travis, code climate, etc..}
spec.homepage = "http://shields.io/"
spec.license = "Public Domain"

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "minitest"
spec.add_dependency "erubis", "~> 2.7.0"
spec.add_dependency "mini_magick", "~> 3.6.0"
spec.add_dependency "slop", "~> 3.4.0"
end