class StyleVacuum

Detects styles that are not used in a Flex application.

This needs to be provided with a source directory to search for used styles, and a directory containing css files which will be loaded and parsed for style definitions.

NOTE: This tool needs further work before it can be considerd to cover all

use cases.

Attributes

declared[R]
report[R]
undeclared[R]
unused[R]
used[R]

Public Class Methods

new(opt,out=STDOUT) click to toggle source
Calls superclass method Tool::new
# File lib/shed/style_vacuum.rb, line 20
def initialize(opt,out=STDOUT)
  super(opt,out)

  @css_dir = opt[:css_dir]

  do_exit unless valid_opts

  @style_regex = /styleName\s*=\s*["']\s*\{?\s*([\w.]+)\s*\}?\s*["']/
  @declared_regex = /^\.(\w+)/

  @declared, @used, @unused, @undeclared = [], [], [], []

  detect

  @report = describe

  to_disk(@report)
end

Public Instance Methods

detect() click to toggle source

Scans the project and detects styles referenced in the source and css files.

# File lib/shed/style_vacuum.rb, line 53
def detect
  puts "Scanning project for styles..."

  @declared = scan_dirs(/\.(css)/, @css_dir, @declared_regex)
  @used = scan_dirs(/\.(as|mxml)/, @src, @style_regex)

  #Find any style names used in the source which haven't been declared.
  @undeclared = @used-@declared

  #Find any style names declared in the css but not used in the src.
  @unused = @declared-@used

  summarise
end
valid_opts() click to toggle source

Valid if we can find .css files in the specifed css_dir.

# File lib/shed/style_vacuum.rb, line 42
def valid_opts
  return false unless File.exist?(@css_dir)

  found = Dir.chdir("#{@css_dir}") { Dir.glob('*.css') }

  found.length > 0
end

Private Instance Methods

describe() click to toggle source

Returns a string detailing the findings of the style detection.

# File lib/shed/style_vacuum.rb, line 81
def describe
  desc = "#{generated_at} by as-style-vacuum"
  desc << add_desc("Styles declared in CSS", @declared)
  desc << add_desc("Styles used in MXML",@used)
  desc << add_desc("Styles declared but not used (in a styleName property)",@unused)
  desc << add_desc("Styles used but not declared",@undeclared)
  desc
end
do_exit() click to toggle source

Log an error message and raise exit.

Calls superclass method Tool#do_exit
# File lib/shed/style_vacuum.rb, line 93
def do_exit
  super "The specified css directory does not exist, or does not contain css files."
end
summarise() click to toggle source

Summarise the collected data.

# File lib/shed/style_vacuum.rb, line 73
def summarise
  puts sprintf( "Declared styles: %d\nUndeclared styles: %d\nUsed styles: %d\nUnused styles: %d\n",
    @declared.length, @undeclared.length, @used.length, @unused.length)
end