Class: Kin::Sprites::RakeRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/kin/sprites/rake_runner.rb

Overview

A Rake helper which generates all the sprites for a given application.

RakeRunner expects a sprites.yml config file which defines the sprites and the icons in each sprite. The contents of each sprites are hashed and cached in a .hashes file (the the same directory as the saved sprites) so that re-running the task won’t result in them being re-generated. See README for examples.

Not auto-loaded by requiring either ‘kin’ or ‘kin/sprites’; if you want this Rake helper you need to require it explicitly.

Defined Under Namespace

Classes: SetInfo

Constant Summary collapse

TEMPLATE =
File.read(__FILE__).split(/^__END__/)[1]

Instance Method Summary collapse

Constructor Details

#initialize(sprites_yml, sprites_dir, sass_partial) ⇒ RakeRunner

Creates a new Generator instance.

Parameters:

  • sprites_yml (String)

    The path to the sprites.yml file which defines the sprites.

  • sprites_dir (String)

    The path to sprites directory, in which the generated sprites will be saved. The source icons will be assumed to be in a src/ subdirectory.

  • sass_partial (String)

    The path where the SASS partial should be saved.

Raises:

  • (SpriteError)

    Raised if the sprites.yml file wasn’t at the specified path.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kin/sprites/rake_runner.rb', line 39

def initialize(sprites_yml, sprites_dir, sass_partial)
  @sprites_yml  = sprites_yml
  @sprites_dir  = sprites_dir
  @sass_partial = sass_partial
  @source_dir   = File.join(@sprites_dir, 'src')
  @dot_hashes   = File.join(@sprites_dir, '.hashes')

  unless File.exists?(@sprites_yml)
    raise SpriteError,
      "sprites.yml not found; expected to be at: #{@sprites_yml}"
  end

  @sets = YAML.load(File.read(@sprites_yml)).map do |name, icons|
    SetInfo.new(name, Kin::Sprites::IconSet.new(icons), false)
  end

  # Sort the sets in alphanumeric order such that SASS partials don't
  # radically change when regenerated (since Ruby 1.8's hash is
  # unordered)
  @sets.sort! { |left, right| left.name <=> right.name }
end

Instance Method Details

#generate!(force = false) ⇒ Object

Generates the sprite images, and creates a _sprites.sass partial.

Parameters:

  • force (Boolean) (defaults to: false)

    Regenerate sprite images even if the existing image contains the exact same icons as the new one will.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/kin/sprites/rake_runner.rb', line 70

def generate!(force = false)
  @sets.each do |set|
    generate_sprite!(set, force)
  end

  if @sets.any? { |set| not set.skipped }
    generate_sass!
    dump_hashes!
  end

  $stdout.puts
  $stdout.puts("All done.")
end