class AssetVacuum

This script scans all Actionscript classes and CSS files in a project to identify assets, like PNG files, that are in the project source tree but are no longer used by the application.

Attributes

assets[R]
declared[R]
unused[R]

Public Class Methods

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

  @src = opt[:src]
  @assets, @src_files, @declared, @unused = [], [], [], []

  do_exit unless valid_opts

  detect

  @report = describe

  to_disk(@report)
end

Public Instance Methods

detect() click to toggle source

Scans the project and detects usage statitics for the assets it's finds.

# File lib/shed/asset_vacuum.rb, line 39
def detect
  puts "Scanning project for assets that are unused..."

  scan_for_assets
  scan_for_declared

  @assets.each { |ass| @unused << ass unless is_asset_used(ass) }

  summarise
end
valid_opts() click to toggle source

Validates the given opts. Returns a boolean indicating if the src directory specified can be used.

# File lib/shed/asset_vacuum.rb, line 32
def valid_opts
  File.exist?(@src) rescue false
end

Private Instance Methods

describe() click to toggle source

Returns a string detailing the findings of the style detection.

# File lib/shed/asset_vacuum.rb, line 81
def describe
  desc = "#{generated_at} by as-asset-vacuum"
  desc << add_desc("Assets declared in src", @declared)
  desc << add_desc("Assets found in project but not referenced in source", @unused)
  desc
end
do_exit() click to toggle source

Log an error message to disk and raise exit.

Calls superclass method Tool#do_exit
# File lib/shed/asset_vacuum.rb, line 105
def do_exit
  super "The source directory does not exist."
end
is_asset_used(file) click to toggle source

Returns a boolean to indicate if the asset is referenced within the application.

# File lib/shed/asset_vacuum.rb, line 92
def is_asset_used(file)
  used = false
  @declared.each { |declaration|
    if File.basename(declaration) == File.basename(file)
      used = true
    end
  }
  used
end
scan_for_assets() click to toggle source

Finds all assets in the source directory.

# File lib/shed/asset_vacuum.rb, line 55
def scan_for_assets
  Search.find_all(/\.(jpg|jpeg|png|otf|ttf|swf|svg|mp3|gif)$/,@src,@excludes) do |path|
    @assets << path
  end
end
scan_for_declared() click to toggle source

Finds all asset declarations in the source files.

# File lib/shed/asset_vacuum.rb, line 64
def scan_for_declared
  @declared = scan_dirs(/\.(as|mxml|css)$/, @src, /Embed\(source=['"]([\w._\-\/]+)['"]/)
  @declared += scan_dirs(/\.(css)$/, @src, /:\s*url\(\s*['"](.*)['"]/)
  @declared += scan_dirs(/\.(mxml)$/, @src, /@Embed\(['"]([\w._\-\/]+)['"]/)
end
summarise() click to toggle source

Summarise the collected data and outputs a string detailing how many assets are used/unused in the project.

# File lib/shed/asset_vacuum.rb, line 74
def summarise
  puts sprintf("Used assets: %d\nUnused assets: %d", @declared.length, @unused.length)
end