Sha256: 79eb5a3bfefad965fc3428b6a89129b44712428104402d6b22e4bad0c79e4827

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

#!/usr/bin/env rake

require "bundler/gem_tasks"

desc "Update assets from Fotorama repo"
task :update do
  require "json"
  require "httpclient"

  def github_tags(repo)
    http = HTTPClient.new
    body = http.get("https://api.github.com/repos/#{repo}/tags").body
    response = JSON.parse(body)
    response.map { |tag| Gem::Version.new(tag['name']) }.sort
  end

  def get(url, to)
    to.open('w') do |io|
      http = HTTPClient.new
      http.transparent_gzip_decompression = true
      io << http.get(url).body
    end
  end

  def asset(file)
    assets = Pathname(__FILE__).dirname.join('vendor/assets')

    if file.end_with? '.css' or file.end_with? '.scss'
      assets.join('stylesheets', file)
    elsif file.end_with? '.js'
      assets.join('javascripts', file)
    else
      assets.join('images', file)
    end
  end

  def fetch(tag)
    cdn = "http://fotorama.s3.amazonaws.com/#{tag}"
    %w(fotorama.js fotorama.css fotorama.png fotorama@2x.png).each do |file|
      from = "#{cdn}/#{file}".sub(/(.css|.js)$/, '.uncompressed\\1')
      get from, asset(file)
    end
  end

  def convert
    css  = asset('fotorama.css')
    scss = asset('fotorama.css.scss')

    scss.open('w') do |io|
      io << css.read.gsub(/url\(/, 'image-url(')
    end

    css.delete
  end

  def update_version(tag)
    version_file = Pathname(__FILE__).dirname.join('lib/fotoramajs/version.rb')
    version_file.open('w') do |io|
      io << "module Fotoramajs\n  VERSION = \"#{tag}\"\nend\n"
    end
  end

  def update_gemfile_lock
    `#{Rake::DSL::RUBY} -S bundle`
  end

  puts "Fetching tags"
  tag = github_tags('artpolikarpov/fotorama').last

  puts "Load Fotorama #{tag}"
  fetch(tag)

  puts "Convert CSS to SCSS"
  convert

  puts "Update gem version"
  update_version(tag)
  update_gemfile_lock

  puts "Done"
end

desc 'Delete all generated files'
task :clobber do
  rm_r 'pkg' rescue nil
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fotoramajs-4.4.6 Rakefile
fotoramajs-4.4.5 Rakefile
fotoramajs-4.4.4 Rakefile
fotoramajs-4.4.3 Rakefile