Skip to content

Commit

Permalink
Merge pull request #898 from realm/seg-badge-generation
Browse files Browse the repository at this point in the history
Generate coverage badges locally
  • Loading branch information
jpsim committed Oct 27, 2017
2 parents 5a1b4c1 + 1b0713b commit 3895676
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,7 +2,11 @@

##### Breaking

* None.
* Generate documentation coverage badge locally. Since this avoids the failable
HTTP request to shields.io previously used to obtain the badge, we've removed
the `--[no-]download-badge` flag and the corresponding `download_badge`
YAML configuration key.
[Samuel Giddins](https://github.com/segiddins)

##### Enhancements

Expand Down
5 changes: 0 additions & 5 deletions lib/jazzy/config.rb
Expand Up @@ -328,11 +328,6 @@ def expand_path(path)
raise '--assets-directory is deprecated: use --theme instead.'
end

config_attr :download_badge,
command_line: '--[no-]download-badge',
description: 'Download documentation coverage badge from shields.io.',
default: true

# rubocop:enable Style/AlignParameters

def initialize
Expand Down
72 changes: 50 additions & 22 deletions lib/jazzy/doc_builder.rb
@@ -1,7 +1,5 @@
require 'fileutils'
require 'mustache'
require 'uri'
require 'net/http'
require 'pathname'
require 'sass'

Expand Down Expand Up @@ -131,7 +129,7 @@ def self.build_site(docs, coverage, options)

DocsetBuilder.new(output_dir, source_module).build!

download_badge(source_module.doc_coverage, options)
generate_badge(source_module.doc_coverage, options)

friendly_path = relative_path_if_inside(output_dir, Pathname.pwd)
puts "jam out ♪♫ to your fresh new docs in `#{friendly_path}`"
Expand Down Expand Up @@ -244,38 +242,68 @@ def self.document_markdown(source_module, doc_model, path_to_root)
# @param [Number] coverage The documentation coverage percentage
def self.color_for_coverage(coverage)
if coverage < 10
'red'
'e05d44' # red
elsif coverage < 30
'orange'
'fe7d37' # orange
elsif coverage < 60
'yellow'
'dfb317' # yellow
elsif coverage < 85
'yellowgreen'
'a4a61d' # yellowgreen
elsif coverage < 90
'green'
'97CA00' # green
else
'brightgreen'
'4c1' # brightgreen
end
end

# Downloads an SVG from shields.io displaying the documentation percentage
# rubocop:disable Metrics/MethodLength

# Generates an SVG similar to those from shields.io displaying the
# documentation percentage
# @param [Number] coverage The documentation coverage percentage
# @param [Config] options Build options
def self.download_badge(coverage, options)
return if options.hide_documentation_coverage || !options.download_badge
def self.generate_badge(coverage, options)
return if options.hide_documentation_coverage

coverage_length = coverage.to_s.size.succ
percent_string_length = coverage_length * 80 + 10
percent_string_offset = coverage_length * 40 + 975
width = coverage_length * 8 + 104
svg = <<-SVG.gsub(/^ {8}/, '')
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="#{width}" height="20">
<linearGradient id="b" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<clipPath id="a">
<rect width="#{width}" height="20" rx="3" fill="#fff"/>
</clipPath>
<g clip-path="url(#a)">
<path fill="#555" d="M0 0h93v20H0z"/>
<path fill="##{color_for_coverage(coverage)}" d="M93 0h#{percent_string_length / 10 + 10}v20H93z"/>
<path fill="url(#b)" d="M0 0h#{width}v20H0z"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110">
<text x="475" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="830">
documentation
</text>
<text x="475" y="140" transform="scale(.1)" textLength="830">
documentation
</text>
<text x="#{percent_string_offset}" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="#{percent_string_length}">
#{coverage}%
</text>
<text x="#{percent_string_offset}" y="140" transform="scale(.1)" textLength="#{percent_string_length}">
#{coverage}%
</text>
</g>
</svg>
SVG

warn 'downloading coverage badge'
badge_url = 'https://img.shields.io/badge/documentation-' \
"#{coverage}%25-#{color_for_coverage(coverage)}.svg"
badge_output = options.output + 'badge.svg'
system('curl', '-s', badge_url, '-o', badge_output.to_s)
unless $?.success?
warn 'Downloading documentation coverage badge failed.'
warn 'Please try again when connected to the Internet, or skip the ' \
'download by passing the `--no-download-badge` command flag.'
exit $?.exitstatus || 1
end
File.open(badge_output, 'w') { |f| f << svg }
end
# rubocop:enable Metrics/MethodLength

def self.should_link_to_github(file)
return unless file
Expand Down

0 comments on commit 3895676

Please sign in to comment.