Skip to content

Instantly share code, notes, and snippets.

@Sihui
Created July 30, 2014 20:08
Show Gist options
  • Save Sihui/0a7292cf52ad54a07d95 to your computer and use it in GitHub Desktop.
Save Sihui/0a7292cf52ad54a07d95 to your computer and use it in GitHub Desktop.
config.assets.paths += [
Rails.root.join('vendor', 'assets', 'fonts')
]
config.assets.precompile += [
'icons.eot',
'icons.svg',
'icons.ttf',
'icons.woff'
]
require 'rake'
namespace :icons do
desc "update the icon font files from a newly downloaded zip file"
task :update, [:zipfile] => [:environment] do |t, args|
begin
# Set absolute zipfile path in ~/Downloads
zipfile = File.join(ENV['HOME'], 'Downloads', args[:zipfile].shellescape)
# Set some dir vars
tmpdir = File.join(Rails.root, 'tmp', 'iconfont')
vendorfontsdir = File.join(Rails.root, 'vendor', 'assets', 'fonts')
vendorcssdir = File.join(Rails.root, 'vendor', 'assets', 'stylesheets')
# Create the tmp folder for extracted zip
unless File.directory? tmpdir
system "mkdir #{tmpdir}"
end
# Unzip that motherfucker
system "unzip -u #{zipfile} -d #{tmpdir}"
# Copy the font files
Dir.glob(File.join(tmpdir, 'fonts', '*')) do |file|
puts "Copying #{file} to #{vendorfontsdir}"
system "cp #{file} #{vendorfontsdir}"
end
# Copy the stylesheet
puts 'Copying the stylesheet'
stylesheet = File.join(vendorcssdir, 'icons.css.scss')
system "cp #{File.join(tmpdir, 'style.css')} #{stylesheet}"
# Replace the font urls in the stylesheet
puts 'Updating font URLs in stylesheet'
stylesheet_content = File.read(stylesheet)
stylesheet_content.gsub!("url('fonts/", "font-url('")
File.open(stylesheet, 'w') do |file|
file.write(stylesheet_content)
end
ensure
puts 'Deleting tmp folder'
if File.directory? tmpdir
system "rm -rf #{tmpdir}"
end
end
end
end
@Sihui
Copy link
Author

Sihui commented Jul 30, 2014

Download your custom icon font from icomoon (this script will look for the file in ~/Downloads/ or you can update the zipfile variable at the top of the task.

Put this file in your lib/tasks folder and use it with:

rake icons:update["icons.zip"]
The script will copy the new font files to vendor/assets/fonts, copy the stylesheet from style.css to vendor/assets/stylesheets as a SCSS file, then update the urls in the stylesheet to point at the font dir.

@Sihui
Copy link
Author

Sihui commented Jul 30, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment