Method: VBSMin#minify

Defined in:
lib/vbsmin.rb

#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