Class: Yui

Inherits:
Object
  • Object
show all
Includes:
Open3
Defined in:
lib/ruby-yui/yui.rb

Defined Under Namespace

Classes: NoInputFileException

Constant Summary collapse

MAJOR =
0
MINOR =
0
RELEASE =
5
YUI_ROOT =
File.join(File.dirname(__FILE__),'..','..')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inpath, options = {}) ⇒ Yui

Grabs a glob of files inpath + options will create a glob,

directory structure will be replicated in outputh

Examples:

Yui.new("./javascripts")
[/javascripts/main.js, /javascripts/folder/other.js]

Parameters:

  • inpath (String)

    path

  • options (Hash) (defaults to: {})

See Also:

  • defaults


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby-yui/yui.rb', line 37

def initialize(inpath,options={})
  @inpath   = inpath
  @options  = Yui.defaults.merge(options)
  @options[:suffix] = "" if @options[:suffix].nil?
  glob_path = File.join(@inpath,"**","*.#{@options[:type]}")
  @files    = Dir.glob(glob_path)
  
  if @options[:suffix].empty? && outpath == @inpath && @options[:stomp] == false
    raise Exception, "Your originals will be destroyed without a suffix or an outpath, run again with :stomp => true to allow this."
  end
  
  #Dont compress ruby-yui files.
  @files.delete_if {|file| file =~ /#{@options[:suffix]}/} unless @options[:suffix].empty?
  
  self.clobber if @options[:clobber]
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



53
54
55
# File 'lib/ruby-yui/yui.rb', line 53

def files
  @files
end

#inpathObject (readonly)

Returns the value of attribute inpath.



54
55
56
# File 'lib/ruby-yui/yui.rb', line 54

def inpath
  @inpath
end

#optionsObject (readonly)

Returns the value of attribute options.



55
56
57
# File 'lib/ruby-yui/yui.rb', line 55

def options
  @options
end

Class Method Details

.clobber(path, suffix, type) ⇒ Object

Clobber *.SUFFIX.(js|css)



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ruby-yui/yui.rb', line 153

def Yui.clobber(path,suffix,type)
  if suffix.empty?
    glob_path = File.join(path,"**.#{type}")
  else
    glob_path = File.join(path,"**","*#{suffix}.#{type}")
  end
  
  Dir.glob(glob_path).each do |file|
    puts "Clobbering #{file}..."
    FileUtils.rm file
  end
end

.compress(inpath, options = {}) ⇒ Boolean

Compresses inpath with options

Returns:

  • (Boolean)

    Was 100% of files compressed

See Also:



171
172
173
# File 'lib/ruby-yui/yui.rb', line 171

def Yui.compress(inpath,options={})
  Yui.new(inpath,options).minify
end

.versionObject



19
20
21
# File 'lib/ruby-yui/yui.rb', line 19

def Yui.version
  [MAJOR,MINOR,RELEASE].join('.')
end

Instance Method Details

#bundleString|nil

Bundles a set of files to

#{@options[:out_path]}/bundle.#{@options[:suffix]}.#{@options[:type]}

Returns:

  • (String|nil)

    Returns path to bundle if successful, otherwise nil

Raises:



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
# File 'lib/ruby-yui/yui.rb', line 73

def bundle
  raise(NoInputFileException, "Nothing to do, check input path.")if @files.empty?
  
  bundle_data = ""
  successful_compressions = 0
  @files.each do |file|
    cmd = Yui.gen_cmd(file,@options)
    stdin, stdout, stderr = popen3(cmd.join(' '))
    bundle_tmp = stdout.read
    
    if stderr.read.empty?
      puts "Bundling: #{file}"
      bundle_data += "\n#{bundle_tmp}"
      successful_compressions += 1
    else
      puts "Failed to bundle: #{file}"
    end
  end
  
  if successful_compressions == @files.length
    if @options[:suffix].empty?
      bundle_file_name = "bundle.#{@options[:type]}"
    else
      bundle_file_name = "bundle#{@options[:suffix]}.#{@options[:type]}"
    end
    bundle_path = File.join(outpath,bundle_file_name)
    File.open(bundle_path,'w'){|f| f.write(bundle_data)}
    return bundle_path
  else
    return nil
  end
end

#clobberObject

Clobbers files with the right suffix/type in the outpath

outpath defaults to inpath


63
64
65
# File 'lib/ruby-yui/yui.rb', line 63

def clobber
  Yui.clobber(outpath,@options[:suffix],@options[:type])
end

#minifyObject Also known as: compress



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
# File 'lib/ruby-yui/yui.rb', line 106

def minify
  raise(NoInputFileException, "Nothing to do, check input path.") if @files.empty?
  
  successful_compressions = 0
  
  @files.each do |file|
    #put the suffix on before the externsion if provided
    if @options[:suffix].empty?
      out_file = file.clone
    else
      out_file = file.split('.')
      out_file.pop #pop off extension
      out_file = out_file.join('.') << @options[:suffix] << ".#{@options[:type]}"
    end
    
    #Substitute the outpath
    out_file.sub!(@inpath,outpath)
    
    cmd = Yui.gen_cmd(file,@options) << "-o #{out_file}"
    
    FileUtils.mkdir_p File.dirname(out_file)

    puts "Compressing:\n\t #{file} =>\n\t #{out_file}"
    stdin, stdout, stderr = popen3(cmd.join(' '))

    stdout = stdout.read
    stderr = stderr.read

    if stdout.empty? && stderr.empty?
      successful_compressions += 1 
    else
      puts "Failed to compress: #{file}"
      if @options[:rename_on_fail] && !@options[:suffix].empty?
        puts "Copying:\n\t #{file} =>\n\t #{out_file}"
        FileUtils.cp file, out_file
      end

      puts "OUT: #{stdout}"
      puts "ERR: #{stderr}"
    end
  end
  
  return (successful_compressions == @files.length)
end

#outpathObject



57
58
59
# File 'lib/ruby-yui/yui.rb', line 57

def outpath
  @options[:out_path] || @inpath
end