-
Notifications
You must be signed in to change notification settings - Fork 117
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
[feature] Add inline assets option #107
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
require "fileutils" | ||
require "digest/sha1" | ||
require "time" | ||
require "base64" | ||
|
||
# Ensure we are using a compatible version of SimpleCov | ||
major, minor, patch = SimpleCov::VERSION.scan(/\d+/).first(3).map(&:to_i) | ||
|
@@ -19,11 +20,15 @@ class HTMLFormatter | |
def initialize | ||
@branchable_result = SimpleCov.branch_coverage? | ||
@templates = {} | ||
@inline_assets = !ENV["SIMPLECOV_INLINE_ASSETS"].nil? | ||
@public_assets_dir = File.join(File.dirname(__FILE__), "../public/") | ||
end | ||
|
||
def format(result) | ||
Dir[File.join(File.dirname(__FILE__), "../public/*")].each do |path| | ||
FileUtils.cp_r(path, asset_output_path) | ||
unless @inline_assets | ||
Dir[File.join(@public_assets_dir, "*")].each do |path| | ||
FileUtils.cp_r(path, asset_output_path) | ||
end | ||
end | ||
|
||
File.open(File.join(output_path, "index.html"), "wb") do |file| | ||
|
@@ -71,9 +76,26 @@ def asset_output_path | |
end | ||
|
||
def assets_path(name) | ||
return asset_inline(name) if @inline_assets | ||
|
||
File.join("./assets", SimpleCov::Formatter::HTMLFormatter::VERSION, name) | ||
end | ||
|
||
def asset_inline(name) | ||
path = File.join(@public_assets_dir, name) | ||
|
||
# Only have a few content types, just hardcode them | ||
content_type = { | ||
".js" => "text/javascript", | ||
".png" => "image/png", | ||
".gif" => "image/gif", | ||
".css" => "text/css", | ||
}[File.extname(name)] | ||
|
||
base64_content = Base64.strict_encode64 File.open(path).read | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you put a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hey, sorry i missed this comment better 3 years late than never I guess? |
||
"data:#{content_type};base64,#{base64_content}" | ||
end | ||
|
||
# Returns the html for the given source_file | ||
def formatted_source_file(source_file) | ||
template("source_file").result(binding) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved @public_assets_dir here to fix ABC rubocop complaints