Module: WebOptimizer

Extended by:
Common
Defined in:
lib/web_optimizer/compressor_css.rb,
lib/web_optimizer.rb,
lib/web_optimizer/common.rb,
lib/web_optimizer/version.rb,
lib/web_optimizer/compressor_js.rb,
lib/web_optimizer/optimize_images.rb

Overview

require “#File.dirname(__FILE__)/common.rb”

Defined Under Namespace

Modules: Common

Constant Summary collapse

VERSION =
"0.0.7"

Class Method Summary collapse

Methods included from Common

check_include_dir

Class Method Details

.compress_css(dir_path, ignore_paths = []) ⇒ Object


7
8
9
# File 'lib/web_optimizer/compressor_css.rb', line 7

def self.compress_css(dir_path, ignore_paths=[])
  self.compress_css_recursive(dir_path, ignore_paths, true)
end

.compress_css_recursive(dir_path, ignore_paths, trict = false) ⇒ Object

Raises:

  • (Exception)

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/web_optimizer/compressor_css.rb', line 11

def self.compress_css_recursive(dir_path, ignore_paths, trict=false)
  raise Exception.new("Invalid dir path") unless File.directory? dir

  ignore_paths = ignore_paths.map do |path|
    tmp_path = Pathname.new(path)
    tmp_path.absolute? ? path : File.join(dir_path, path)
  end
  unless check_include_dir(ignore_paths, dir_path)
    Dir[File.join(dir_path, "**/*.css")].each do |css_path|
      if File.directory?(css_path)
        compress_css_recursive(css_path, trict)
      elsif !css_path.end_with?("pack.css") && !css_path.end_with?("min.css") &&
        File.extname(css_path) == ".css" && !check_include_dir(ignore_paths, css_path)

        bak_css_path = "#{css_path}.bak"
        from_file = bak_css_path
        compile = true

        if File.exists?(bak_css_path)
          compile = trict
        else
          FileUtils.copy_file(css_path, bak_css_path)
        end

        if compile
          puts "+ Compress: #{css_path}"
          # puts  "java -jar /Users/tamvo/code/tools/yuicompressor-2.4.8pre.jar #{from_file} -o #{css_path} --type css"
          # sleep 2
          `java -jar /Users/tamvo/code/tools/yuicompressor-2.4.8pre.jar #{from_file} -o #{css_path} --type css`
        end

      end
    end
  else
    puts "- Ignore: #{dir_path}"
  end
end

.compress_img(dir, ignore_paths = []) ⇒ Object

Raises:

  • (Exception)

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/web_optimizer/optimize_images.rb', line 7

def self.compress_img(dir, ignore_paths=[])
  raise Exception.new("Invalid dir path") unless File.directory? dir

  ignore_paths = ignore_paths.map do |path|
    tmp_path = Pathname.new(path)
    tmp_path.absolute? ? path : File.join(dir, path)
  end

  unless check_include_dir(ignore_paths, dir)
    Dir[File.join(dir, "**/*")].each do |image_path|
      next if check_include_dir(ignore_paths, image_path)

      if File.directory?(image_path)
        compress_img(image_path)
      elsif (file_ext = File.extname(image_path)) == ".png"
        file_name = File.basename(image_path)
        `/usr/local/bin/pngquant #{image_path} --ext #{TMP_EXT_NAME}`
        new_file_path = image_path.gsub(file_ext, "-tmp.png")
        `mv #{new_file_path} #{image_path}`
        puts "[pngquant] #{image_path}"
      elsif ['png'].any? { |ext| file_ext == ".#{ext}" }
        puts "[image_optim] #{image_path}"
        `/Users/tamvo/.rvm/gems/[email protected]/bin/image_optim #{image_path}`
      end
    end
  end
end

.compress_js(dir_path, ignore_paths = []) ⇒ Object


7
8
9
# File 'lib/web_optimizer/compressor_js.rb', line 7

def self.compress_js(dir_path, ignore_paths=[])
  self.compress_js_recursive(dir_path, ignore_paths, false)
end

.compress_js_recursive(dir, ignore_paths, trict = false) ⇒ Object

Raises:

  • (Exception)

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/web_optimizer/compressor_js.rb', line 11

def self.compress_js_recursive(dir, ignore_paths, trict=false)
  raise Exception.new("Invalid dir path") unless File.directory? dir

  ignore_paths = ignore_paths.map do |path|
    tmp_path = Pathname.new(path)
    tmp_path.absolute? ? path : File.join(dir, path)
  end
  unless check_include_dir(ignore_paths, dir)
    Dir[File.join(dir, "**/*.js")].each do |js_path|
      if File.directory?(js_path)
        self.compress_js_recursive(js_path, trict)
      elsif !js_path.end_with?("pack.js") && !js_path.end_with?("min.js") &&
        File.extname(js_path) == ".js" && !check_include_dir(ignore_paths, js_path)

        bak_js_path = "#{js_path}.bak"
        from_file = bak_js_path
        compile = true

        if File.exists?(bak_js_path)
          compile = trict
        else
          FileUtils.copy_file(js_path, bak_js_path)
        end

        if compile
          puts "+ Compress: #{js_path}"
          # puts  "java -jar ~/code/tools/compiler-latest/compiler.jar --js #{from_file} --js_output_file #{js_path}"
          # sleep 2
          `java -jar /Users/tamvo/code/tools/compiler-latest/compiler.jar --js #{from_file} --js_output_file #{js_path}`
        end

      end
    end
  else
    puts "- Ignore: #{dir}"
  end
end