Class: StyleVacuum

Inherits:
Tool
  • Object
show all
Defined in:
lib/shed/style_vacuum.rb

Overview

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.

Constant Summary

Constants inherited from Tool

Tool::INVALID_OPTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Tool

#add_sigint_handler, #generated_at, #log, #puts, #to_disk

Constructor Details

#initialize(opt, out = STDOUT) ⇒ StyleVacuum

Returns a new instance of StyleVacuum.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Attribute Details

#declaredObject (readonly)

Returns the value of attribute declared.



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

def declared
  @declared
end

#reportObject (readonly)

Returns the value of attribute report.



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

def report
  @report
end

#undeclaredObject (readonly)

Returns the value of attribute undeclared.



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

def undeclared
  @undeclared
end

#unusedObject (readonly)

Returns the value of attribute unused.



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

def unused
  @unused
end

#usedObject (readonly)

Returns the value of attribute used.



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

def used
  @used
end

Instance Method Details

#detectObject

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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_optsObject

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



42
43
44
45
46
47
48
# 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