1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
module Compass
module Installers
class Manifest
include Enumerable
# A Manifest entry
class Entry < Struct.new(:type, :from, :options)
def to
options[:to] || from
end
end
attr_reader :options
def initialize(manifest_file = nil, options = {})
@entries = []
@options = options
@generate_config = true
@compile_after_generation = true
parse(manifest_file) if manifest_file
end
def self.known_extensions
@known_extensions ||= {}
end
def self.plural_types
@plural_types ||= {}
end
def self.type(t, options = {})
Array(options[:extensions]).each do |ext|
self.known_extensions[ext] = t
end
self.plural_types[options[:plural]] = t if options[:plural]
eval <<-END
def #{t}(from, options = {})
@entries << Entry.new(:#{t}, from, options)
end
def has_#{t}?
@entries.detect {|e| e.type == :#{t}}
end
def each_#{t}
@entries.select {|e| e.type == :#{t}}.each {|e| yield e}
end
END
end
type :stylesheet, :plural => :stylesheets, :extensions => %w(scss sass)
type :image, :plural => :images, :extensions => %w(png gif jpg jpeg tiff gif)
type :javascript, :plural => :javascripts, :extensions => %w(js)
type :font, :plural => :fonts, :extensions => %w(eot otf woff ttf)
type :html, :plural => :html, :extensions => %w(html haml)
type :file, :plural => :files
type :directory, :plural => :directories
def discover(type)
type = self.class.plural_types[type] || type
dir = File.dirname(@manifest_file)
Dir.glob("#{dir}/**/*").each do |file|
next if /manifest\.rb/ =~ file
short_name = file[(dir.length+1)..-1]
options = {}
ext = if File.extname(short_name) == ".erb"
options[:erb] = true
File.extname(short_name[0..-5])
else
File.extname(short_name)
end[1..-1]
file_type = self.class.known_extensions[ext]
file_type = :file if file_type.nil?
file_type = :directory if File.directory?(file)
if type == :all || type == file_type
send(file_type, short_name, options)
end
end
end
def help(value = nil)
if value
@help = value
else
@help
end
end
attr_reader :welcome_message_options
def welcome_message(value = nil, options = {})
if value
@welcome_message = value
@welcome_message_options = options
else
@welcome_message
end
end
def welcome_message_options
@welcome_message_options || {}
end
def description(value = nil)
if value
@description = value
else
@description
end
end
# Enumerates over the manifest files
def each
@entries.each {|e| yield e}
end
def generate_config?
@generate_config
end
def compile?
@compile_after_generation
end
protected
def no_configuration_file!
@generate_config = false
end
def skip_compilation!
@compile_after_generation = false
end
def with_manifest(manifest_file)
@manifest_file = manifest_file
yield
ensure
@manifest_file = nil
end
# parses a manifest file which is a ruby script
# evaluated in a Manifest instance context
def parse(manifest_file)
with_manifest(manifest_file) do
if File.exists?(manifest_file)
open(manifest_file) do |f|
eval(f.read, instance_binding, manifest_file)
end
else
eval("discover :all", instance_binding, manifest_file)
end
end
end
def instance_binding
binding
end
end
end
end
|