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 =
7
YUI_ROOT =
File.join(File.dirname(__FILE__),'..','..')
FILE_TYPES =
[:js, :css]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Grabs a glob of files

Examples:

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

Parameters:

  • inpath (String)

    file path, directory or glob

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

See Also:

  • defaults


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

def initialize(inpath, options = {})    
  @inpath   = inpath
  @options  = Yui.defaults.merge(options)
  @options[:suffix] = "" if @options[:suffix].nil?
  
  @files = if File.file?(@inpath) #Processing a single file
    extension = File.extname(@inpath).delete!('.').to_sym
    # set the type automatically
    @options[:type] = extension if FILE_TYPES.member?(extension)
    
    [@inpath]
  elsif File.directory?(@inpath)
    Dir[File.join(@inpath,"**","*.#{@options[:type]}")]
  else # assume its a glob
    Dir[@inpath]
  end
      
  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
  
  #Try not to 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.



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

def files
  @files
end

#inpathObject (readonly)

Returns the value of attribute inpath.



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

def inpath
  @inpath
end

#optionsObject (readonly)

Returns the value of attribute options.



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

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

def Yui.clobber(path,suffix,type)
  glob_path = if File.file?(path)
    path
  elsif suffix.empty?
    File.join(path,"**.#{type}")
  else
    File.join(path,"**","*#{suffix}.#{type}")
  end
  
  Dir[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:



200
201
202
# File 'lib/ruby-yui/yui.rb', line 200

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

.compress_string(str, type, outpath = nil) ⇒ Array[Boolean,String]

@notes, this is done with the tempfile class

Parameters:

  • str (String)

    javascript/css to compress

  • type (Symbol)

    :js | :css

  • outpath (String) (defaults to: nil)

    path to (optionally) out put to

Returns:

  • (Array[Boolean,String])

    Success, compressed data



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/ruby-yui/yui.rb', line 177

def Yui.compress_string(str,type,outpath =  nil)
  temp_file = Tempfile.new("rubyui-#{rand(5000)}")
  temp_file.puts str
  temp_file.flush
  
  cmd = Yui.gen_cmd temp_file.path, Yui.defaults.merge({:type => type})
  stdin, stdout, stderr = Open3::popen3(cmd.join(' '))

  comp_ok = (stderr.read.empty?)
  stdout = stdout.read
  
  if comp_ok && outpath
    File.open(outpath,'w+'){ |fs| fs.puts stdout }
  end
  
  [comp_ok, stdout]
end

.versionObject



13
14
15
# File 'lib/ruby-yui/yui.rb', line 13

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



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

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