lib/tasks/silk_icons.rake in silk_icons-1.1.1 vs lib/tasks/silk_icons.rake in silk_icons-1.2.0

- old
+ new

@@ -1,47 +1,96 @@ require 'silk_icons/info' require 'pathname' require 'tmpdir' namespace :silk_icons do - docs_dir = Pathname('doc/silk_icons').expand_path - images_dir = Pathname('vendor/assets/images/silk_icons').expand_path + docs_dir = Pathname('doc/silk_icons') + images_dir = Pathname('vendor/assets/images/silk_icons') + sprite_image = images_dir + 'sprite.png' stylesheets_dir = Pathname('vendor/assets/stylesheets') + stylesheet = stylesheets_dir + 'silk_icons.css.scss' directory "#{docs_dir}" directory "#{images_dir}" directory "#{stylesheets_dir}" + task sprite: "#{sprite_image}" + task stylesheet: "#{stylesheet}" + task :dev_only do unless Pathname('silk_icons.gemspec').exist? puts 'This task is for the development of silk_icons gem itself.' exit end end - desc 'Unpack the archive and place files in appropriate locations' - task unpack: [ :dev_only, "#{docs_dir}", "#{images_dir}" ] do - basename = File.basename(SilkIcons::ARCHIVE_URL.path) - Dir.mktmpdir do |dir| - Dir.chdir dir do - sh 'curl -O %s' % SilkIcons::ARCHIVE_URL - sh 'unzip -q %s' % basename - %w(readme.html readme.txt).each do |file| - file = Pathname(file) - mv file, docs_dir + file.basename - end - Dir['icons/*'].each do |icon| - icon = Pathname(icon) - mv icon, images_dir + icon.basename - end + task fetch: :dev_only do + sh 'curl -O %s' % SilkIcons::ARCHIVE_URL + end + + desc 'Unpack the archive and place files in appropriate locations.' + task unpack: :dev_only do + archive_path = Pathname(File.basename(SilkIcons::ARCHIVE_URL.path)).expand_path + ENV['SILK_ICONS_TMP'] = dir = Dir.mktmpdir + at_exit { rm_r dir } + Dir.chdir dir do + sh 'unzip -q %s' % archive_path + end + end + + task docs: [ :unpack, "#{docs_dir}" ] do + %w(readme.html readme.txt).each do |file| + mv Pathname(ENV['SILK_ICONS_TMP']) + file, docs_dir + end + end + + file "#{sprite_image}" => [ :unpack, "#{images_dir}" ] do + dst = sprite_image.expand_path + + Dir.chdir ENV['SILK_ICONS_TMP'] do + pngs = FileList['icons/*.png'].sort + + pnms = pngs.pathmap('%X.pnm') + pngs.zip(pnms).each {|png, pnm| sh 'pngtopam %s > %s' % [png, pnm] } + + alpha_pnms = pnms.pathmap('%X-alpha.pnm') + pngs.zip(alpha_pnms).each {|png, pnm| sh 'pngtopam --alpha %s > %s' % [png, pnm] } + + row_pnms = pnms.each_slice(40).with_index.map do |row, i| + row_pnm = 'row%d.pnm' % i + sh 'pnmcat --leftright %s > %s' % [ row.join(' '), row_pnm] + row_pnm end + + row_alpha_pnms = alpha_pnms.each_slice(40).with_index.map do |row, i| + row_alpha_pnm = 'row%d-alpha.pnm' % i + sh 'pnmcat --leftright %s > %s' % [ row.join(' '), row_alpha_pnm] + row_alpha_pnm + end + + sh 'pnmcat --topbottom %s > %s' % [ row_pnms.join(' '), 'sprite.pnm'] + sh 'pnmcat --topbottom %s > %s' % [ row_alpha_pnms.join(' '), 'sprite-alpha.pnm'] + sh 'pnmtopng --compress 9 --alpha %s %s > %s' % [ 'sprite-alpha.pnm', 'sprite.pnm', dst ] end end - file "#{stylesheets_dir + 'silk_icons.css.scss'}" => [ :dev_only, "#{stylesheets_dir}" ] do |t| - icons = FileList["#{images_dir}/*.png"].pathmap('%n') - content = icons.map do |icon| - ".silk_icon-#{icon} { background: image-url('silk_icons/#{icon}.png') no-repeat; }" - end.join("\n") + file "#{stylesheet}" => [ :unpack, "#{stylesheets_dir}" ] do |t| + content = '' + Dir.chdir ENV['SILK_ICONS_TMP'] do + pngs = FileList['icons/*.png'].pathmap('%n').sort + pngs.each_slice(40).with_index do |row, y| + row.each.with_index do |png, x| + content += <<-STYLE.lstrip + .silk_icon-#{png} { + background: image-url('silk_icons/sprite.png') no-repeat -#{x*16}px -#{y*16}px; + width: 16px; + height: 16px; + overflow: hidden; + } + STYLE + end + end + end + File.write(t.name, content, encoding: 'US-ASCII') end end