Class: VBSMin

Inherits:
Object
  • Object
show all
Includes:
Version
Defined in:
lib/vbsmin.rb

Overview

Global VBSmin class

Constant Summary

Constants included from Version

Version::VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVBSMin

Instantiate a VBSmin object



29
30
31
32
33
34
35
# File 'lib/vbsmin.rb', line 29

def initialize
  @input_size = nil
  @output_size = nil
  @diff_size = nil
  @original_filepath = nil
  @min_filepath = nil
end

Instance Attribute Details

#diff_sizeInteger (readonly)

Returns Saved size (in bytes).

Returns:

  • (Integer)

    Saved size (in bytes)



20
21
22
# File 'lib/vbsmin.rb', line 20

def diff_size
  @diff_size
end

#input_sizeInteger (readonly)

Returns Size (in bytes) of the original file.

Returns:

  • (Integer)

    Size (in bytes) of the original file



14
15
16
# File 'lib/vbsmin.rb', line 14

def input_size
  @input_size
end

#min_filepathString (readonly)

Returns Path of the minified file.

Returns:

  • (String)

    Path of the minified file



26
27
28
# File 'lib/vbsmin.rb', line 26

def min_filepath
  @min_filepath
end

#original_filepathString (readonly)

Returns Path of the original file.

Returns:

  • (String)

    Path of the original file



23
24
25
# File 'lib/vbsmin.rb', line 23

def original_filepath
  @original_filepath
end

#output_sizeInteger (readonly)

Returns Size (in bytes) of the minified file.

Returns:

  • (Integer)

    Size (in bytes) of the minified file



17
18
19
# File 'lib/vbsmin.rb', line 17

def output_size
  @output_size
end

Instance Method Details

#minify(filepath) ⇒ String

Minify a VBScript file (make a minified copy: file.min.vbs)

Parameters:

  • filepath (String)

    Path to the VBS file to minify

Returns:

  • (String)

    Path of the minified file



40
41
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
# File 'lib/vbsmin.rb', line 40

def minify(filepath)
  @original_filepath = filepath
  # Count number of line of input
  input_lines = File.readlines(filepath).length
  # File streaming
  File.open(min_ext, 'w') do |output|
    File.foreach(filepath).with_index(1) do |line, i|
      eol = ':' # End of file char
      # Remove inline comment (must be before whitespace striping)
      line = inline_comment(line)
      # Remove leading and trailing whitespaces: null, horizontal tab, line feed,
      # vertical tab, form feed, carriage return, space
      line.strip!
      # Remove comments except inline ones (must be after whitespace striping)
      line = '' if /^(?:'|REM\b)/i =~ line
      # Remove space when several spaces between two keywords
      line = internal_space(line)
      # Remove line splitting
      line[-1] = '' && eol = '' if line[-2..] == ' _'
      # Write processed line unless it is a blank line or the last line
      unless line.empty?
        output.write(line)
        output.write(eol) unless i == input_lines
      end
    end
  end
  calc_size
  return @min_filepath
end