Skip to content

Instantly share code, notes, and snippets.

@choonkeat
Last active May 7, 2016 03:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save choonkeat/22f4d5de1f5183cba79e to your computer and use it in GitHub Desktop.
Save choonkeat/22f4d5de1f5183cba79e to your computer and use it in GitHub Desktop.
`html2sprockets:assets` is a quick way to add 3rd-party js/css to your rails app that is asset pipeline compatible; creates local gem in `vendor/gems` and apply them in your `Gemfile`
require 'fileutils'
namespace :html2sprockets do
desc "Setup env for gem creation"
task :env do
@gemname = ENV.fetch('NAME')
@gemdir = File.join(ENV.fetch('GEMDIR', 'vendor/gems'), @gemname)
@asset_dst_path = File.join(@gemdir, ENV.fetch('DST', 'vendor/assets'))
@asset_src_paths = ENV.fetch('SRC').each_line.collect(&:strip)
end
desc "Create a local, vendor gem and use it in Gemfile"
task gemdir: :env do
FileUtils.rm_rf(@gemdir) if File.exists?(@gemdir)
{
"Gemfile" => <<-EOM.strip_heredoc,
source 'https://rubygems.org'
gemspec
EOM
"README.md" => <<-EOM.strip_heredoc,
This gem is auto-generated by html2sprockets.
EOM
"Rakefile" => <<-EOM.strip_heredoc,
require "bundler/gem_tasks"
EOM
"lib/#{@gemname}.rb" => <<-EOM.strip_heredoc,
module #{@gemname.classify}
class Engine < ::Rails::Engine
initializer '#{@gemname}.assets.precompile' do |app|
Dir[root.join('vendor/assets/*').to_s].each do |dir|
app.config.assets.paths << dir
app.config.assets.precompile += Dir["\#{dir}/**/*.*"].
reject { |path|
case path
when /\\/test\\//
# whitelist dir/subdir here
true
else
case File.basename(path)
when 'WHITELIST'
# whitelist filename here
when /^_/, /\.json$/, /^Gruntfile/
# blacklist filename here
true
end
end
}.
collect { |path|
path[dir.to_s.length+1..-1].
gsub(/.(sass|scss)$/, '.css').
gsub(/.(erb|coffee)$/, '')
}
end
end
end
end
EOM
"#{@gemname}.gemspec" => <<-EOM.strip_heredoc,
$:.push File.expand_path("../lib", __FILE__)
Gem::Specification.new do |s|
s.name = "#{@gemname}"
s.version = "0.0.1"
s.authors = ["html2sprockets"]
s.summary = "#{@gemname}"
s.files = Dir["{lib,vendor}/**/*", "README.md"]
s.add_dependency "sprockets-rails", ">= 2.3.3"
end
EOM
}.each do |path, content|
relpath = File.join(@gemdir, path)
FileUtils.mkdir_p(File.dirname(File.expand_path(relpath)))
open(relpath, "wb") {|f| f.write(content) }
end
gemline = "gem '#{@gemname}', path: '#{@gemdir}'\n"
lines = IO.read('Gemfile').each_line.to_a - [gemline] + [gemline]
open('Gemfile', 'wb') {|f| f.write(lines.join) }
end
desc "Copy assets into gem's `vendor/assets`"
task assets: :gemdir do
@asset_src_paths.each do |source|
seen = {}
pattern_map = {
"#{source}/**/*.js" => 'javascripts',
"#{source}/**/*.{css,css.map,sass,scss}" => 'stylesheets',
"#{source}/**/*.{eot,svg,ttf,woff,woff2}" => 'fonts',
"#{source}/**/*.*" => 'images',
}
pattern_map.each do |pattern, subdir|
Dir[pattern].each do |src|
next if seen[src]
next if File.directory?(src)
seen[src] = true
dst = File.join(@asset_dst_path, subdir, @gemname, File.basename(source), src[source.length+1..-1])
FileUtils.mkdir_p(File.dirname(dst))
if src =~ /\.css\b/ && (not src =~ /\.map$/)
need_erb = false
newdata = IO.read(src).gsub(/\burl\s*\((.+?)\)/) {|match|
relpath = match.gsub(/url\s*\(['"]?|['"]?\)/, '')
if relpath =~ /^(\/\/|\w+:)/
match # url?
else
need_erb = true
abspath = File.expand_path(relpath, File.dirname(src))
refsrc = @asset_src_paths.find {|s| abspath.index(s) == 0 }
assetpath = File.join(@gemname, File.basename(refsrc), abspath[refsrc.length+1..-1])
"url(<%= asset_path '#{assetpath}' %>)"
end
}
dst = dst + (need_erb ? ".erb" : "")
open(dst, 'wb') {|f| f.write(newdata) }
else
FileUtils.cp src, dst
end
puts dst
end
end
end
end
end
# download and unzip https://github.com/hakimel/reveal.js/archive/3.1.0.zip
rake html2sprockets:assets NAME=revealjs SRC="/Users/choonkeat/Downloads/reveal.js-3.1.0/css
/Users/choonkeat/Downloads/reveal.js-3.1.0/js
/Users/choonkeat/Downloads/reveal.js-3.1.0/lib
/Users/choonkeat/Downloads/reveal.js-3.1.0/plugin"
# app/assets/stylesheets/reveal.css:/*
# app/assets/stylesheets/reveal.css: *= require revealjs/css/reveal.css
# app/assets/stylesheets/reveal.css: *= require revealjs/css/theme/black.css
# app/assets/stylesheets/reveal.css: *= require revealjs/lib/css/zenburn.css
# app/assets/stylesheets/reveal.css: */
# <%= javascript_include_tag "revealjs/lib/js/head.min.js", "revealjs/js/reveal.js" %>
# download and unzip https://github.com/twbs/bootstrap/releases/download/v3.3.5/bootstrap-3.3.5-dist.zip
rake html2sprockets:assets NAME=bootstrap SRC="/Users/choonkeat/Downloads/bootstrap-3.3.5-dist/css
/Users/choonkeat/Downloads/bootstrap-3.3.5-dist/fonts
/Users/choonkeat/Downloads/bootstrap-3.3.5-dist/js"
# app/assets/stylesheets/bootstrap.css:/*
# app/assets/stylesheets/bootstrap.css: *= require bootstrap/css/bootstrap.min
# app/assets/stylesheets/bootstrap.css: */
# app/assets/javascripts/bootstrap.js://= require bootstrap/js/bootstrap.min
# download and unzip http://galleria.io/static/galleria-1.4.2.zip
rake html2sprockets:assets NAME=galleria SRC="/Users/choonkeat/Downloads/galleria-1.4.2/js
/Users/choonkeat/Downloads/galleria-1.4.2/plugins
/Users/choonkeat/Downloads/galleria-1.4.2/themes"
# app/assets/javascripts/galleria.js://= require galleria/js/galleria-1.4.2.min
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment