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 =
3
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


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby-yui/yui.rb', line 30

def initialize(inpath,options={})
  @inpath   = inpath
  @options  = Yui.defaults.merge(options)
  glob_path = File.join(@inpath,"**","*.#{@options[:type]}")
  @files    = Dir.glob(glob_path)
  
  #Dont compress ruby-yui files.
  @files.delete_if {|file| file =~ /#{@options[:suffix]}/}
  
  self.clobber if @options[:clobber]
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



41
42
43
# File 'lib/ruby-yui/yui.rb', line 41

def files
  @files
end

#inpathObject (readonly)

Returns the value of attribute inpath.



42
43
44
# File 'lib/ruby-yui/yui.rb', line 42

def inpath
  @inpath
end

#optionsObject (readonly)

Returns the value of attribute options.



43
44
45
# File 'lib/ruby-yui/yui.rb', line 43

def options
  @options
end

Class Method Details

.clobber(path, suffix, type) ⇒ Object

Clobber *.SUFFIX.(js|css)



128
129
130
131
132
133
134
# File 'lib/ruby-yui/yui.rb', line 128

def Yui.clobber(path,suffix,type)
  files = Dir.glob(File.join(path,"**","*.#{suffix}.#{type}"))
  files.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:



141
142
143
144
# File 'lib/ruby-yui/yui.rb', line 141

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

.versionObject



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

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



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

def bundle
  if @files.empty?
    raise NoInputFileException, "Nothing to do, check input path."
  end
  
  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
    bundle_file_name = "bundle.#{@options[:suffix]}.#{@options[:type]}"
    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


51
52
53
# File 'lib/ruby-yui/yui.rb', line 51

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

#minifyObject Also known as: compress



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

def minify
  if @files.empty?
    raise NoInputFileException, "Nothing to do, check input path."
  end
  
  successful_compressions = 0
  
  @files.each do |file|
    out_file = file.split('.')      # split on '.'
    out_file.pop                    # pop off file extension
    out_file.push @options[:suffix] # add yui min suffix
    out_file.push @options[:type]   # add extension back on
    out_file = out_file.join('.')    # put it all together
    
    out_file.sub!(@inpath,outpath) if @options[:out_path]
    
    cmd = Yui.gen_cmd(file,@options)
    cmd << "-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(' '))

    if stdout.read.empty? && stderr.read.empty?
      successful_compressions += 1 
    else
      puts "Failed to compress: #{file}"
    end
  end
  
  return (successful_compressions == @files.length)
end

#outpathObject



45
46
47
# File 'lib/ruby-yui/yui.rb', line 45

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