Class: FrontEndArchitect::Blender

Inherits:
Object
  • Object
show all
Defined in:
lib/front_end_architect/blender.rb

Constant Summary collapse

VERSION =
'0.24'
ALPHA_REGEX =
/(-ms-)?filter:\s*(['"]?)progid:DXImageTransform\.Microsoft\.AlphaImageLoader\(\s*src=(['"])([^\?'"\)]+)(\?(?:[^'"\)]+)?)?\3,\s*sizingMethod=(['"])(image|scale|crop)\6\s*\)\2/im
IMPORT_REGEX =

shouldn’t the semicolon be optional?

/@import(?: url\(| )(['"]?)([^\?'"\)\s]+)(\?(?:[^'"\)]+)?)?\1\)?(?:[^?;]+)?;/im
URL_REGEX =
/url\((['"]?)([^\?'"\)]+)(\?(?:[^'"\)]+)?)?\1?\)/im
DEFAULT_OPTIONS =
{
  :blendfile => 'Blendfile.yaml',
  :data      => false,
  :force     => false,
  :root      => File.dirname('Blendfile.yaml'),
  :min       => :yui,
  :colored   => (Object.const_defined? :Colored),
}

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Blender

Returns a new instance of Blender.



38
39
40
# File 'lib/front_end_architect/blender.rb', line 38

def initialize(opts)
  @options = DEFAULT_OPTIONS.merge(opts)
end

Instance Method Details

#blendObject



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
# File 'lib/front_end_architect/blender.rb', line 42

def blend
  elapsed = Benchmark.realtime do
    unless File.exists? @options[:blendfile]
      raise "Couldn't find '#{@options[:blendfile]}'"
    end
    
    blendfile_mtime = File.mtime(@options[:blendfile])
    blender         = YAML::load_file @options[:blendfile]
    
    Dir.chdir(File.dirname(@options[:blendfile]))
    
    blender = flatten_blendfile(blender)
    
    blender.each do |output_name, sources|
      output_name = Pathname.new(output_name).cleanpath.to_s
      
      output_new       = false
      gzip_output_name = output_name + '.gz'
      
      # Checks the type flag and if the current file meets the type requirements continues
      if output_name.match '.' + @options[:file_type].to_s
        file_type = output_name.match(/\.css/) ? 'css' : 'js'
        
        # Checks if output file exists and checks the mtimes of the source files to the output file if new creates a new file
        if File.exists?(output_name) && (!@options[:gzip] || File.exists?(gzip_output_name))
          output_files = []
          output_files << File.mtime(output_name)
          output_files << File.mtime(gzip_output_name) if @options[:gzip] && File.exists?(gzip_output_name)
          
          oldest_output = output_files.sort.first
          
          if blendfile_mtime > oldest_output
            output_new = true
          else
            sources.each do |i|
              if File.mtime(i) > oldest_output
                output_new = true
                break
              end
            end
          end
          
          if output_new || @options[:force]
            if File.writable?(output_name) && !(@options[:gzip] && !File.writable?(gzip_output_name))
              create_output(output_name, sources, file_type)
            else
              puts_colored 'Permission Denied:' + ' ' + output_name,      :red
              puts_colored 'Permission Denied:' + ' ' + gzip_output_name, :red if @options[:gzip]
            end
          else
            puts_colored 'Skipping: ' + output_name,      :yellow
            puts_colored 'Skipping: ' + gzip_output_name, :yellow if @options[:gzip]
          end
        else
          create_output(output_name, sources, file_type)
        end
      end
    end
  end
  
  puts sprintf('%.5f', elapsed) + ' seconds'
end

#generateObject



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
# File 'lib/front_end_architect/blender.rb', line 105

def generate
  if File.exists?(@options[:blendfile]) && !@options[:force]
    raise "'#{@options[:blendfile]}' already exists"
  end
  
  blend_files = Hash.new
  
  Find.find(Dir.getwd) do |f|
    basename = File.basename(f)
    
    if FileTest.directory?(f) && (basename[0] == ?. || basename.match(/^(yui|tinymce|dojo|wp-includes|wp-admin|mint)$/i) || (File.basename(f).downcase == 'rails' && File.basename(File.dirname(f)).downcase == 'vendor'))
      Find.prune
    elsif !(basename.match(/(^|[-.])(pack|min)\.(css|js)$/i) || basename.match(/^(sifr\.js|ext\.js|mootools.*\.js)$/i))
      # TODO Check file contents instead of name for minification (port YSlow's isMinified)
      f.gsub!(Dir.getwd.to_s + '/', '')
      
      if File.extname(f).downcase == '.css' || File.extname(f).downcase == '.js'
        min_file  = basename.sub(/\.(css|js)$/i, '-min.\1')
        path      = File.dirname(f).split('/') # File::dirname depends on /
        
        path.push min_file
        path.push [basename]
        
        h = path.reverse.inject { |m,v| { v => m } }
        
        blend_files.deep_merge!(h).inspect
      end
    end
  end
  
  File.open(@options[:blendfile], 'w') do |blendfile|
    blendfile << blend_files.to_yaml
  end
end