#!/usr/bin/env ruby require 'yaml' require 'optparse' require 'digest' require 'mini_magick' require 'fileutils' require 'erb' def copyimage(path, file, target) hash = Digest::MD5.file(file).hexdigest hash = Digest::MD5.hexdigest(hash.to_s + target) filename = "#{hash}.jpg" dest = File.join(path, filename) if File.exist?(dest) puts "Skipping" return filename end puts "Writing #{dest}" # Resize image and write it to destination image = MiniMagick::Image.open(file) image.resize(target) image.write(dest) return filename end def forImgs(imgs, &block) if imgs.respond_to?("each") imgs.each &block else block.call(imgs) end end options = {} OptionParser.new do |opts| opts.banner = "Usage: build.rb dest" opts.on("-d", "--dest PATH", "PATH to put build files") do |path| options[:path] = path end opts.on("-s", "--size SIZE", "Size to resize images to") do |size| options[:size] = size end opts.on("-g", "--commit HASH", "Commit to display on website") do |hash| options[:commit] = hash end opts.on("-c", "--clean", "Cleanup unused images") do |v| options[:clean] = true end end.parse! options[:path] = "build" unless options[:path] options[:size] = "1920x1080" unless options[:size] options[:clean] = false unless options[:clean] puts options # Create build dir FileUtils.mkdir_p(options[:path]) defs = YAML.load(File.read("imginfo.yml")) files = {} puts defs defs["groups"].each do |key, value| # For each image inside group forImgs(value["imgs"]) do |img| puts "Converting file #{img}" dest = copyimage(options[:path], img, options[:size]) files[img] = dest end end puts files template = File.read("index.tmpl") template = template.split("%BODY%") File.open(File.join(options[:path], "index.html"), "w") do |file| # Print preample file.write(template[0]) file.puts "

Original images can be found at wallpapers.

" defs["groups"].each do |key, value| file.puts "

#{key}

" forImgs(value["imgs"]) do |img| file.puts "" end if value["what"] file.puts "

#{value["what"]}" end end if options[:commit] file.puts "

Build from commit #{options[:commit]}

" end # Print postampleee? file.write(template[1]) end # Cleanup old files if options[:clean] Dir.glob(File.join(options[:path], "*.jpg")) do |file| hash = File.basename(file) if !files.values.include? hash puts "Cleaning #{hash}" File.delete(file) end end end