Class: Boring::Favicon::BuildGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/boring/favicon/build/build_generator.rb

Constant Summary collapse

ICO_SIZES =
%w(16 32)
APPLE_SIZES =
%w(57 60 72 76 114 120 129 144 152)
APPLE_PRECOMPOSED_SIZES =
%w(120 129 152)
SMALL_BREAK =
50
LARGE_BREAK =
150
MS_TILE_SIZES =
%w(144)
FAVICON_DIR =
"favicons"
FILE_FAVICO_DIR =
"app/assets/images/#{FAVICON_DIR}"
DEFAULT_PRIMARY_COLOR =
"#082472"
DEFAULT_FAVICON_LETTER =

B for boring_generators :)

"B"
DEFAULT_IMAGE_PATH =
"#{FILE_FAVICO_DIR}/template.png"

Instance Method Summary collapse

Instance Method Details

#add_favicon_partialObject



111
112
113
114
115
116
117
# File 'lib/generators/boring/favicon/build/build_generator.rb', line 111

def add_favicon_partial
  say "Copying favicon layout partial", :green
  template("favicon.html.erb", "app/views/layouts/shared/_favicon.html.erb")
  insert_into_file "app/views/layouts/application.html.erb", <<~RUBY, after: /head.*\n/
    \t\t<%= render 'layouts/shared/favicon' %>
  RUBY
end

#build_faviconObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/boring/favicon/build/build_generator.rb', line 30

def build_favicon
  @application_name = options[:application_name]
  @primary_color = options[:primary_color]

  unless /Version/m =~ (`convert -version`)
    say <<~WARNING, :red
      ERROR: You do not have ImageMagick installed.
    WARNING
  end
end

#build_favicon_for_existing_template_imageObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/generators/boring/favicon/build/build_generator.rb', line 47

def build_favicon_for_existing_template_image
  return unless File.exist?(DEFAULT_IMAGE_PATH)

  say "Converting template image to favicons..."
  template_name = "#{FILE_FAVICO_DIR}/template.png"
  template_small_name = "#{FILE_FAVICO_DIR}/template-small.png"
  template_large_name = "#{FILE_FAVICO_DIR}/template-large.png"
  template_small_name = template_name unless File.file?(template_small_name)
  template_large_name = template_name unless File.file?(template_large_name)
  ICO_SIZES.each do |size|
    ico_template = template_name
    ico_template = template_small_name if size.to_i <= SMALL_BREAK
    ico_template = template_small_name if size.to_i >= LARGE_BREAK
    (`convert #{ico_template} -resize #{size}x#{size} #{FILE_FAVICO_DIR}/favicon-#{size}x#{size}.ico`)
  end
  APPLE_SIZES.each do |size|
    ico_template = template_name
    ico_template = template_small_name if size.to_i <= SMALL_BREAK
    ico_template = template_small_name if size.to_i >= LARGE_BREAK
    (`convert #{ico_template} -resize #{size}x#{size} #{FILE_FAVICO_DIR}/apple-touch-icon-#{size}x#{size}.png`)
  end
  APPLE_PRECOMPOSED_SIZES.each do |size|
    ico_template = template_name
    ico_template = template_small_name if size.to_i <= SMALL_BREAK
    ico_template = template_small_name if size.to_i >= LARGE_BREAK
    (`convert #{ico_template} -resize #{size}x#{size} #{FILE_FAVICO_DIR}/apple-touch-icon-#{size}x#{size}-precomposed.png`)
  end
  MS_TILE_SIZES.each do |size|
    ico_template = template_name
    ico_template = template_small_name if size.to_i <= SMALL_BREAK
    ico_template = template_small_name if size.to_i >= LARGE_BREAK
    (`convert #{ico_template} -resize #{size}x#{size} #{FILE_FAVICO_DIR}/mstile-#{size}x#{size}.png`)
  end
  ico_template = template_name
  ico_template = template_small_name if 152 <= SMALL_BREAK
  ico_template = template_small_name if 152 >= LARGE_BREAK
  (`convert #{ico_template} -resize 152x152 #{FILE_FAVICO_DIR}/apple-touch-icon.png`)
  (`convert #{ico_template} -resize 152x152 #{FILE_FAVICO_DIR}/apple-touch-icon-precomposed.png`)
end

#create_favicon_directoryObject



41
42
43
44
45
# File 'lib/generators/boring/favicon/build/build_generator.rb', line 41

def create_favicon_directory
  unless File.exist?(FILE_FAVICO_DIR)
    Dir.mkdir FILE_FAVICO_DIR
  end
end

#generate_new_favicon_using_favico_letterObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/generators/boring/favicon/build/build_generator.rb', line 87

def generate_new_favicon_using_favico_letter
  return if File.exist?(DEFAULT_IMAGE_PATH)
  say "Creating favicons from application...", :green

  favico_letter = options[:favico_letter] || @application_name.try(:first) || DEFAULT_FAVICON_LETTER
  font_file_path = options[:font_file_path]
  favicon_color = options[:primary_color] || DEFAULT_PRIMARY_COLOR

  ICO_SIZES.each do |size|
    (`convert -background "#{favicon_color}" -fill white -size #{size}x#{size} -gravity center #{font_file_path ? "-font #{font_file_path}" : ""} label:#{favico_letter} #{FILE_FAVICO_DIR}/favicon-#{size}x#{size}.ico`)
  end
  APPLE_SIZES.each do |size|
    (`convert -background "#{favicon_color}" -fill white -size #{size}x#{size} -gravity center #{font_file_path ? "-font #{font_file_path}" : ""} label:#{favico_letter} #{FILE_FAVICO_DIR}/apple-touch-icon-#{size}x#{size}.png`)
  end
  APPLE_PRECOMPOSED_SIZES.each do |size|
    (`convert -background "#{favicon_color}" -fill white -size #{size}x#{size} -gravity center #{font_file_path ? "-font #{font_file_path}" : ""} label:#{favico_letter} #{FILE_FAVICO_DIR}/apple-touch-icon-#{size}x#{size}-precomposed.png`)
  end
  MS_TILE_SIZES.each do |size|
    (`convert -background "#{favicon_color}" -fill white -size #{size}x#{size} -gravity center #{font_file_path ? "-font #{font_file_path}" : ""} label:#{favico_letter} #{FILE_FAVICO_DIR}/mstile-#{size}x#{size}.png`)
  end
  (`convert -background "#{favicon_color}" -fill white -size 152x152 -gravity center #{font_file_path ? "-font #{font_file_path}" : ""} label:#{favico_letter} #{FILE_FAVICO_DIR}/apple-touch-icon.png`)
  (`convert -background "#{favicon_color}" -fill white -size 152x152 -gravity center #{font_file_path ? "-font #{font_file_path}" : ""} label:#{favico_letter} #{FILE_FAVICO_DIR}/apple-touch-icon-precomposed.png`)
end