[Extensions] Manifests don't have to be hard. Allow discovery of basic assets and follow conventions for templates.

This commit is contained in:
Chris Eppstein 2010-08-05 21:18:11 -07:00
parent 776b772c08
commit 03221b9e7c
2 changed files with 70 additions and 14 deletions

View File

@ -165,14 +165,26 @@ You may also see some real manifest files here:
### Manifest Declarations ### Manifest Declarations
There are six kinds of manifest declarations: **Easy Mode:** If you just have some basic files and nothing fancy going on, simply place this line in your manifest:
discover :all
This will cause compass to find all the files in your template and use the files' extension to determine where they should go. Alternatively, you can request that compass only discover files of a certain type. For example, the following will only discover javascript and image assets, you could then declare other file types on your own.
discover :javascripts
discover :images
The following types may be discovered: `:stylesheets`, `:images`, `:javascripts`, `:fonts`, `:html`, `:files`, and `:directories`
**Normal Mode:** There are seven kinds of manifest declarations:
1. `stylesheet` - Declares a sass file. 1. `stylesheet` - Declares a sass file.
2. `image` - Declares an image. 2. `image` - Declares an image.
3. `javascript` - Declares a javascript file. 3. `javascript` - Declares a javascript file.
4. `html` - Declares an html file. 4. `font` - Declares a font file.
5. `file` - Declares a random file. 5. `html` - Declares an html file.
6. `directory` - Declares a directory should be created. 6. `file` - Declares a random file.
7. `directory` - Declares a directory should be created.
All declarations take the path to the file as their first argument. Note that the All declarations take the path to the file as their first argument. Note that the
normal slash `/` can and should be used in a manifest. Compass will take care of normal slash `/` can and should be used in a manifest. Compass will take care of

View File

@ -20,7 +20,19 @@ module Compass
parse(manifest_file) if manifest_file parse(manifest_file) if manifest_file
end end
def self.type(t) 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 eval <<-END
def #{t}(from, options = {}) def #{t}(from, options = {})
@entries << Entry.new(:#{t}, from, options) @entries << Entry.new(:#{t}, from, options)
@ -34,13 +46,35 @@ module Compass
END END
end end
type :stylesheet type :stylesheet, :plural => :stylesheets, :extensions => %w(scss sass)
type :image type :image, :plural => :images, :extensions => %w(png gif jpg jpeg tiff gif)
type :javascript type :javascript, :plural => :javascripts, :extensions => %w(js)
type :font type :font, :plural => :fonts, :extensions => %w(otf woff ttf)
type :file type :html, :plural => :html, :extensions => %w(html haml)
type :html type :file :plural => :files
type :directory 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) def help(value = nil)
if value if value
@ -96,13 +130,23 @@ module Compass
@compile_after_generation = false @compile_after_generation = false
end 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 # parses a manifest file which is a ruby script
# evaluated in a Manifest instance context # evaluated in a Manifest instance context
def parse(manifest_file) def parse(manifest_file)
open(manifest_file) do |f| with_manifest(manifest_file) do
eval(f.read, instance_binding, manifest_file) open(manifest_file) do |f|
eval(f.read, instance_binding, manifest_file)
end
end end
end end
def instance_binding def instance_binding
binding binding
end end