tool-shed 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +45 -22
- data/bin/{as-asset-detector → as-asset-vacuum} +2 -2
- data/bin/{as-class-detector → as-class-vacuum} +2 -2
- data/bin/{as-style-detector → as-style-vacuum} +2 -2
- data/bin/as-vacuum +16 -0
- data/lib/shed/asdoc_package.rb +11 -11
- data/lib/shed/asset_vacuum.rb +85 -0
- data/lib/shed/{unused_class.rb → class_vacuum.rb} +18 -19
- data/lib/shed/manifest.rb +11 -12
- data/lib/shed/opts/asdoc_package_opts.rb +1 -1
- data/lib/shed/opts/asset_vacuum_opts.rb +22 -0
- data/lib/shed/opts/{unused_class_opts.rb → class_vacuum_opts.rb} +9 -12
- data/lib/shed/opts/manifest_opts.rb +2 -2
- data/lib/shed/opts/style_vacuum_opts.rb +31 -0
- data/lib/shed/opts/tool_opts.rb +8 -8
- data/lib/shed/style_vacuum.rb +92 -0
- data/lib/shed/tool.rb +43 -6
- data/lib/shed/version.rb +1 -1
- data/lib/tool_shed.rb +6 -6
- data/test/fixtures/unused-asset/assets/audio/referenced.mp3 +0 -0
- data/test/fixtures/unused-asset/assets/audio/un-referenced.mp3 +0 -0
- data/test/fixtures/unused-asset/assets/css/referenced-in-mxml.css +0 -0
- data/test/fixtures/unused-asset/assets/css/referenced.css +12 -0
- data/test/fixtures/unused-asset/assets/css/un-referenced.css +7 -0
- data/test/fixtures/unused-asset/assets/fonts/referenced-in-css.ttf +0 -0
- data/test/fixtures/unused-asset/assets/fonts/referenced-in-unused-css.ttf +0 -0
- data/test/fixtures/unused-asset/assets/img/referenced.gif +0 -0
- data/test/fixtures/unused-asset/assets/img/referenced.svg +0 -0
- data/test/fixtures/unused-asset/assets/img/un-referenced.gif +0 -0
- data/test/fixtures/unused-asset/assets/img/un-referenced.svg +0 -0
- data/test/fixtures/unused-asset/assets/swf/referenced-in-css.swf +0 -0
- data/test/fixtures/unused-asset/assets/swf/referenced.swf +0 -0
- data/test/fixtures/unused-asset/assets/swf/un-referenced.swf +0 -0
- data/test/fixtures/unused-asset/src/org/helvector/ToolBox.mxml +5 -0
- data/test/fixtures/unused-asset/src/org/helvector/ToolShed.as +7 -2
- data/test/unit/test_asdoc_package_opts.rb +1 -1
- data/test/unit/test_asset_vacuum.rb +77 -0
- data/test/unit/{test_unused_asset_opts.rb → test_asset_vacuum_opts.rb} +5 -7
- data/test/unit/{test_unused_class.rb → test_class_vacuum.rb} +7 -7
- data/test/unit/test_class_vacuum_opts.rb +51 -0
- data/test/unit/{test_unused_style.rb → test_style_vacuum.rb} +21 -15
- data/test/unit/test_style_vacuum_opts.rb +46 -0
- metadata +36 -22
- data/lib/shed/opts/unused_asset_opts.rb +0 -24
- data/lib/shed/opts/unused_style_opts.rb +0 -23
- data/lib/shed/unused_asset.rb +0 -93
- data/lib/shed/unused_style.rb +0 -114
- data/test/unit/test_unused_asset.rb +0 -69
- data/test/unit/test_unused_class_opts.rb +0 -59
- data/test/unit/test_unused_style_opts.rb +0 -31
data/README.md
CHANGED
@@ -3,46 +3,70 @@
|
|
3
3
|
A collection of utility scripts to help working with ActionScript and Flex
|
4
4
|
projects. These scripts are under development, so **don't** expect reliability.
|
5
5
|
|
6
|
+
All tools have help available on the command line, to view use:
|
7
|
+
|
8
|
+
as-tool-name -h
|
9
|
+
|
6
10
|
### as-docp
|
7
11
|
|
8
|
-
The `as-docp` tool
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
12
|
+
The `as-docp` tool can be used to generate a `flex-config.xml` file from package
|
13
|
+
level ASDoc files. This can then be used by the `asdoc` tool when generating
|
14
|
+
ASDocs for your project.
|
15
|
+
|
16
|
+
To document a package create a single file with a `.asdoc` suffix to contain
|
17
|
+
your ASDoc comments.
|
18
|
+
|
19
|
+
Example use:
|
20
|
+
|
21
|
+
as-docp -s src -o tmp/asdocp-config.xml
|
13
22
|
|
14
23
|
### as-manifest
|
15
24
|
|
16
25
|
Scans a specified source tree for ActionScript and MXML files and for each one
|
17
26
|
found creates a manifest entry. When complete writes the results to disk.
|
18
27
|
|
19
|
-
|
28
|
+
Example use:
|
29
|
+
|
30
|
+
as-manifest
|
31
|
+
as-manifest -s source/main -o project-manifest.xml
|
32
|
+
as-manifest -f 'org.helvector'
|
33
|
+
|
34
|
+
### as-class-vacuum
|
20
35
|
|
21
|
-
|
22
|
-
|
23
|
-
|
36
|
+
Compares a `mxmlc` generated `link-report.xml` against a `manifest.xml` file to
|
37
|
+
identify files that are in a project source tree but are no longer compiled
|
38
|
+
into the application.
|
24
39
|
|
25
|
-
|
26
|
-
files have been generated.
|
40
|
+
Example use:
|
27
41
|
|
28
|
-
|
42
|
+
as-class-vacuum -s src/main -o report/vacuum/class.xml -l report/link/app-link-report.xml -m manifest.xml
|
29
43
|
|
30
|
-
|
44
|
+
To generate a link report via mxmlc use `mxmlc -link-report=report.xml`
|
31
45
|
|
32
|
-
### as-
|
46
|
+
### as-style-vacuum
|
33
47
|
|
34
|
-
|
35
|
-
|
36
|
-
|
48
|
+
Loads all styles defined in **css** files (mxml isn't included) and identifies
|
49
|
+
styles that are not referenced in the source tree.
|
50
|
+
|
51
|
+
Example use:
|
52
|
+
|
53
|
+
as-style-vacuum -c source/assets -s source/main -o report/vacuum/style.xml
|
54
|
+
|
55
|
+
### as-asset-vacuum
|
56
|
+
|
57
|
+
This script scans a source tree for assets of types `jpg, jpeg, png, otf, ttf,
|
58
|
+
swf, svg, mp3, gif` then searches all ActionScript, CSS and MXML files to
|
59
|
+
identify which assets are not referenced in the source files.
|
60
|
+
|
61
|
+
Example use:
|
62
|
+
|
63
|
+
as-asset-vacuum
|
64
|
+
as-asset-vacuum -s source/main -o report/vacuum/asset.xml
|
37
65
|
|
38
66
|
## Install
|
39
67
|
|
40
68
|
gem install tool-shed
|
41
69
|
|
42
|
-
## Using
|
43
|
-
|
44
|
-
Examples will follow once the tools are in a stable state.
|
45
|
-
|
46
70
|
## Authors
|
47
71
|
|
48
72
|
[Simon Gregory](http://simongregory.com)
|
@@ -51,4 +75,3 @@ Examples will follow once the tools are in a stable state.
|
|
51
75
|
|
52
76
|
Released under the MIT License. Please see the accompanying [LICENSE](LICENSE) document for
|
53
77
|
details.
|
54
|
-
|
data/bin/as-vacuum
ADDED
data/lib/shed/asdoc_package.rb
CHANGED
@@ -23,16 +23,16 @@ class ASDocPackage < Tool
|
|
23
23
|
#
|
24
24
|
# Scan the given path and it's child directories for all .asdoc files.
|
25
25
|
#
|
26
|
-
def scan(
|
27
|
-
puts "Scanning '#{
|
26
|
+
def scan(dir)
|
27
|
+
puts "Scanning '#{dir}' for asdoc files..."
|
28
28
|
|
29
29
|
found = []
|
30
30
|
|
31
|
-
Search.find_all(@asdoc,
|
32
|
-
found << {:path =>
|
31
|
+
Search.find_all(@asdoc,dir,@excludes) do |path|
|
32
|
+
found << {:path => path, :package => ProjectTools.package(path)}
|
33
33
|
end
|
34
34
|
|
35
|
-
found.each { |
|
35
|
+
found.each { |file| log("Adding #{file[:path]}") }
|
36
36
|
|
37
37
|
found
|
38
38
|
end
|
@@ -46,8 +46,8 @@ class ASDocPackage < Tool
|
|
46
46
|
if asdocs.empty?
|
47
47
|
puts "No .asdoc files found."
|
48
48
|
else
|
49
|
-
|
50
|
-
to_disk(
|
49
|
+
create_xml(asdocs)
|
50
|
+
to_disk(xml)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
@@ -56,9 +56,9 @@ class ASDocPackage < Tool
|
|
56
56
|
# files.
|
57
57
|
#
|
58
58
|
def create_xml(asdocs)
|
59
|
-
|
60
|
-
asdocs.each { |
|
61
|
-
|
62
|
-
|
59
|
+
@xml = @header
|
60
|
+
asdocs.each { |asdoc| @xml << sprintf(@package, asdoc[:package], IO.read(asdoc[:path])) }
|
61
|
+
@xml << @footer
|
62
|
+
@xml
|
63
63
|
end
|
64
64
|
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#
|
4
|
+
# This script scans all Actionscript classes and CSS files in a project to
|
5
|
+
# identify assets, like PNG files, that are in the project source tree but are
|
6
|
+
# no longer used by the application.
|
7
|
+
#
|
8
|
+
class AssetVacuum < Tool
|
9
|
+
attr_reader :assets, :declared, :unused
|
10
|
+
|
11
|
+
def initialize(opt,out=STDOUT)
|
12
|
+
super(opt,out)
|
13
|
+
|
14
|
+
@src = opt[:src]
|
15
|
+
@assets, @src_files, @declared = [], [], []
|
16
|
+
|
17
|
+
unless valid_opts
|
18
|
+
@out.puts "#{INVALID_OPTS} The source directory does not exist."
|
19
|
+
return
|
20
|
+
end
|
21
|
+
|
22
|
+
detect
|
23
|
+
|
24
|
+
@report = describe
|
25
|
+
|
26
|
+
to_disk(@report)
|
27
|
+
end
|
28
|
+
|
29
|
+
#
|
30
|
+
# Valid if the specified project directory exists.
|
31
|
+
#
|
32
|
+
def valid_opts
|
33
|
+
File.exist?(@src) rescue false
|
34
|
+
end
|
35
|
+
|
36
|
+
def detect
|
37
|
+
puts "Scanning project for assets that are unused..."
|
38
|
+
|
39
|
+
Search.find_all(/\.(jpg|jpeg|png|otf|ttf|swf|svg|mp3|gif)$/,@src,@excludes) do |path|
|
40
|
+
@assets << path
|
41
|
+
end
|
42
|
+
|
43
|
+
src_dec = scan_dirs(/\.(as|mxml|css)$/, @src, /Embed\(source=['"]([\w._\-\/]+)['"]/)
|
44
|
+
css_dec = scan_dirs(/\.(css)$/, @src, /:\s*url\(\s*['"](.*)['"]/)
|
45
|
+
mxml_dec = scan_dirs(/\.(mxml)$/, @src, /@Embed\(['"]([\w._\-\/]+)['"]/)
|
46
|
+
|
47
|
+
@declared = src_dec + css_dec + mxml_dec
|
48
|
+
@unused = []
|
49
|
+
|
50
|
+
@assets.each { |ass| @unused << ass unless is_asset_used(ass) }
|
51
|
+
|
52
|
+
summarise
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
#
|
58
|
+
# Summarise the collected data.
|
59
|
+
#
|
60
|
+
def summarise
|
61
|
+
puts "Used assets: #{@declared.length.to_s}"
|
62
|
+
puts "Unused assets: #{@unused.length.to_s}"
|
63
|
+
end
|
64
|
+
|
65
|
+
#
|
66
|
+
# Returns a string detailing the findings of the style detection.
|
67
|
+
#
|
68
|
+
def describe
|
69
|
+
desc = "#{generated_at} by as-asset-vacuum"
|
70
|
+
desc << add_desc("Assets declared in src", @declared)
|
71
|
+
desc << add_desc("Assets found in project but not referenced in source", @unused)
|
72
|
+
desc
|
73
|
+
end
|
74
|
+
|
75
|
+
def is_asset_used(file)
|
76
|
+
used = false
|
77
|
+
@declared.each { |declaration|
|
78
|
+
if File.basename(declaration) == File.basename(file)
|
79
|
+
used = true
|
80
|
+
end
|
81
|
+
}
|
82
|
+
used
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
@@ -8,7 +8,7 @@
|
|
8
8
|
# Before executing this script make sure the relevant link reports and manifest
|
9
9
|
# files have been generated.
|
10
10
|
#
|
11
|
-
class
|
11
|
+
class ClassVacuum < Tool
|
12
12
|
attr_reader :report,
|
13
13
|
:empty_packages,
|
14
14
|
:unused_classes
|
@@ -22,7 +22,7 @@ class UnusedClass < Tool
|
|
22
22
|
@manifest_regex = /<component id="(\w+)"/
|
23
23
|
|
24
24
|
unless valid_opts
|
25
|
-
@out.puts "#{INVALID_OPTS}
|
25
|
+
@out.puts "#{INVALID_OPTS} The link report, manifest file, or source directory does not exist."
|
26
26
|
return
|
27
27
|
end
|
28
28
|
|
@@ -46,37 +46,36 @@ class UnusedClass < Tool
|
|
46
46
|
@unused_classes = @manifest_classes-@link_classes
|
47
47
|
@empty_packages = empties(@src)
|
48
48
|
|
49
|
-
|
50
|
-
puts "Empty packages: #{@empty_packages.length.to_s}"
|
49
|
+
summarise
|
51
50
|
end
|
52
51
|
|
53
52
|
private
|
54
53
|
|
55
54
|
#
|
56
|
-
#
|
55
|
+
# Summarise the collected data.
|
57
56
|
#
|
58
|
-
def
|
59
|
-
|
60
|
-
|
61
|
-
e
|
57
|
+
def summarise
|
58
|
+
puts "Unused classes: #{@unused_classes.length.to_s}"
|
59
|
+
puts "Empty packages: #{@empty_packages.length.to_s}"
|
62
60
|
end
|
63
61
|
|
64
62
|
#
|
65
|
-
#
|
63
|
+
# Scans the path for empty directories and lists them.
|
66
64
|
#
|
67
|
-
def
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
d
|
65
|
+
def empties(path)
|
66
|
+
empties = []
|
67
|
+
Search.for_empties(path) { |path| empties << path.sub( /^.*src\//, "") }
|
68
|
+
empties
|
72
69
|
end
|
73
70
|
|
74
71
|
#
|
75
|
-
#
|
72
|
+
# Returns a string detailing the findings of the class vacuumer.
|
76
73
|
#
|
77
|
-
def
|
78
|
-
|
79
|
-
"
|
74
|
+
def describe
|
75
|
+
desc = "#{generated_at} by as-class-vacuum"
|
76
|
+
desc << add_desc("Classes in the manifest but not the link report", @unused_classes)
|
77
|
+
desc << add_desc("Packages appear to be empty", @empty_packages)
|
78
|
+
desc
|
80
79
|
end
|
81
80
|
|
82
81
|
#
|
data/lib/shed/manifest.rb
CHANGED
@@ -34,16 +34,16 @@ class Manifest < Tool
|
|
34
34
|
# Search the provided path for as and mxml documents and return those found as
|
35
35
|
# a list.
|
36
36
|
#
|
37
|
-
def scan(
|
38
|
-
puts "Scanning '#{
|
37
|
+
def scan(dir)
|
38
|
+
puts "Scanning '#{dir}' for as and mxml files..."
|
39
39
|
|
40
40
|
found = []
|
41
41
|
|
42
|
-
Search.find_all(@filetypes,
|
43
|
-
ext = File.extname(
|
44
|
-
|
42
|
+
Search.find_all(@filetypes,dir,@excludes) do |path|
|
43
|
+
ext = File.extname(path)
|
44
|
+
name = File.basename(path, ext)
|
45
45
|
|
46
|
-
found << add(
|
46
|
+
found << add(path, name) if name =~ /^[A-Z]/
|
47
47
|
end
|
48
48
|
|
49
49
|
found = process(found) unless found.empty?
|
@@ -70,10 +70,10 @@ class Manifest < Tool
|
|
70
70
|
if @components.empty?
|
71
71
|
puts "No ActionScript or Mxml files found."
|
72
72
|
else
|
73
|
-
|
73
|
+
create_xml(@components)
|
74
74
|
|
75
75
|
#Open/Create the manifest file and write the output to it.
|
76
|
-
to_disk(
|
76
|
+
to_disk(xml)
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
@@ -82,9 +82,8 @@ class Manifest < Tool
|
|
82
82
|
# files.
|
83
83
|
#
|
84
84
|
def create_xml(comps)
|
85
|
-
|
86
|
-
comps.each { |
|
87
|
-
|
88
|
-
x
|
85
|
+
@xml = "<?xml version='1.0'?>\n<componentPackage>\n"
|
86
|
+
comps.each { |component| @xml << "\t#{component[:xml]}\n" }
|
87
|
+
@xml << "</componentPackage>"
|
89
88
|
end
|
90
89
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#
|
4
|
+
# Manages the command line interface for the assets vacuum tool.
|
5
|
+
#
|
6
|
+
class AssetVacuumOpts < ToolOpts
|
7
|
+
|
8
|
+
def self.name
|
9
|
+
"as-asset-vacuum"
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.description
|
13
|
+
"ActionScript Asset Vacuum Tool"
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.default_config
|
17
|
+
dc = superclass.default_config
|
18
|
+
dc[:output] = 'assets.txt'
|
19
|
+
dc
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -1,38 +1,35 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
#
|
4
|
-
# Manages the command line interface for the
|
4
|
+
# Manages the command line interface for the class vacuum tool.
|
5
5
|
#
|
6
|
-
class
|
6
|
+
class ClassVacuumOpts < ToolOpts
|
7
7
|
|
8
8
|
def self.name
|
9
|
-
"as-class-
|
9
|
+
"as-class-vacuum"
|
10
10
|
end
|
11
11
|
|
12
12
|
def self.description
|
13
|
-
"ActionScript
|
13
|
+
"ActionScript Class Vacuum Tool"
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.default_config
|
17
17
|
dc = superclass.default_config
|
18
|
-
dc[:output] = 'class-
|
18
|
+
dc[:output] = 'class-vacuum.txt'
|
19
19
|
dc[:manifest] = 'manifest.xml'
|
20
20
|
dc[:link_report] = 'link-report.xml'
|
21
21
|
dc
|
22
22
|
end
|
23
23
|
|
24
|
-
#
|
25
|
-
# Add all mandatory arguments to the options parser.
|
26
|
-
#
|
27
24
|
def self.add_mandatory(op,config)
|
28
25
|
superclass.add_mandatory(op,config)
|
29
26
|
|
30
|
-
op.on("-m", "--manifest FILE", "Path to the source manifest file.") do |
|
31
|
-
config[:manifest] =
|
27
|
+
op.on("-m", "--manifest FILE", "Path to the source manifest file.") do |value|
|
28
|
+
config[:manifest] = value
|
32
29
|
end
|
33
30
|
|
34
|
-
op.on("-l", "--link-report FILE", "Path to the compiler link-report file.") do |
|
35
|
-
config[:link_report] =
|
31
|
+
op.on("-l", "--link-report FILE", "Path to the compiler link-report file.") do |value|
|
32
|
+
config[:link_report] = value
|
36
33
|
end
|
37
34
|
end
|
38
35
|
|
@@ -21,8 +21,8 @@ class ManifestOpts < ToolOpts
|
|
21
21
|
|
22
22
|
def self.add_optional(op,config)
|
23
23
|
superclass.add_optional(op,config)
|
24
|
-
op.on("-f", "--filter [STRING]", String, "Package filter, in the form of 'org.helvector', to include in the generated manifest.") do |
|
25
|
-
config[:filter] =
|
24
|
+
op.on("-f", "--filter [STRING]", String, "Package filter, in the form of 'org.helvector', to include in the generated manifest.") do |value|
|
25
|
+
config[:filter] = value
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#
|
4
|
+
# Manages the command line interface for the style vacuum tool.
|
5
|
+
#
|
6
|
+
class StyleVacuumOpts < ToolOpts
|
7
|
+
|
8
|
+
def self.name
|
9
|
+
"as-style-vacuum"
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.description
|
13
|
+
"ActionScript Style Vacuum Tool"
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.default_config
|
17
|
+
dc = superclass.default_config
|
18
|
+
dc[:output] = 'styles.txt'
|
19
|
+
dc[:css_dir] = 'style'
|
20
|
+
dc
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.add_mandatory(op,config)
|
24
|
+
superclass.add_mandatory(op,config)
|
25
|
+
|
26
|
+
op.on("-c", "--css PATH", "Path to the directory containing css file/s.") do |value|
|
27
|
+
config[:css_dir] = value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/lib/shed/opts/tool_opts.rb
CHANGED
@@ -64,20 +64,20 @@ class ToolOpts
|
|
64
64
|
# Add all optional arguments to the options parser.
|
65
65
|
#
|
66
66
|
def self.add_optional(op,config)
|
67
|
-
op.on("-s", "--source [PATH]", String, "Path to source folder, defaults to current directory.") do |
|
68
|
-
config[:src] =
|
67
|
+
op.on("-s", "--source [PATH]", String, "Path to source folder, defaults to current directory.") do |value|
|
68
|
+
config[:src] = value
|
69
69
|
end
|
70
70
|
|
71
|
-
op.on("-o", "--output [FILE]", String, "Path to output file, defaults to #{config[:output]}.") do |
|
72
|
-
config[:output] =
|
71
|
+
op.on("-o", "--output [FILE]", String, "Path to output file, defaults to #{config[:output]}.") do |value|
|
72
|
+
config[:output] = value
|
73
73
|
end
|
74
74
|
|
75
|
-
op.on("-v", "--verbose", "Run verbosely.") do |
|
76
|
-
config[:verbose] =
|
75
|
+
op.on("-v", "--verbose", "Run verbosely.") do |value|
|
76
|
+
config[:verbose] = value
|
77
77
|
end
|
78
78
|
|
79
|
-
op.on("--silent", "Supress all output.") do |
|
80
|
-
config[:silent] =
|
79
|
+
op.on("--silent", "Supress all output.") do |value|
|
80
|
+
config[:silent] = value
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#
|
4
|
+
# Detects styles that are not used in a Flex application.
|
5
|
+
#
|
6
|
+
# This needs to be provided with a source directory to search for used styles,
|
7
|
+
# and a directory containing css files which will be loaded and parsed for
|
8
|
+
# style definitions.
|
9
|
+
#
|
10
|
+
# NOTE: This tool needs further work before it can be considerd to cover all
|
11
|
+
# use cases.
|
12
|
+
#
|
13
|
+
class StyleVacuum < Tool
|
14
|
+
attr_reader :report,
|
15
|
+
:declared,
|
16
|
+
:used,
|
17
|
+
:unused,
|
18
|
+
:undeclared
|
19
|
+
|
20
|
+
def initialize(opt,out=STDOUT)
|
21
|
+
super(opt,out)
|
22
|
+
|
23
|
+
@css_dir = opt[:css_dir]
|
24
|
+
|
25
|
+
@style_regex = /styleName\s*=\s*["']\s*\{?\s*([\w.]+)\s*\}?\s*["']/
|
26
|
+
@declared_regex = /^\.(\w+)/
|
27
|
+
|
28
|
+
@declared, @used, @unused, @undeclared = [], [], [], []
|
29
|
+
|
30
|
+
unless valid_opts
|
31
|
+
@out.puts "#{INVALID_OPTS} The specified css directory does not exist, or does not contain css files."
|
32
|
+
return
|
33
|
+
end
|
34
|
+
|
35
|
+
detect
|
36
|
+
|
37
|
+
@report = describe
|
38
|
+
|
39
|
+
to_disk(@report)
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# Valid if we can find .css files in the specifed css_dir.
|
44
|
+
#
|
45
|
+
def valid_opts
|
46
|
+
return false unless File.exist?(@css_dir)
|
47
|
+
|
48
|
+
found = Dir.chdir("#{@css_dir}") { Dir.glob('*.css') }
|
49
|
+
|
50
|
+
found.length > 0
|
51
|
+
end
|
52
|
+
|
53
|
+
def detect
|
54
|
+
puts "Scanning project for styles..."
|
55
|
+
|
56
|
+
@declared = scan_dirs(/\.(css)/, @css_dir, @declared_regex)
|
57
|
+
@used = scan_dirs(/\.(as|mxml)/, @src, @style_regex)
|
58
|
+
|
59
|
+
#Find any style names used in the source which haven't been declared.
|
60
|
+
@undeclared = @used-@declared
|
61
|
+
|
62
|
+
#Find any style names declared in the css but not used in the src.
|
63
|
+
@unused = @declared-@used
|
64
|
+
|
65
|
+
summarise
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
#
|
71
|
+
# Summarise the collected data.
|
72
|
+
#
|
73
|
+
def summarise
|
74
|
+
puts "Declared styles: #{@declared.length.to_s}"
|
75
|
+
puts "Undeclared styles: #{@undeclared.length.to_s}"
|
76
|
+
puts "Used styles: #{@used.length.to_s}"
|
77
|
+
puts "Unused styles: #{@unused.length.to_s}"
|
78
|
+
end
|
79
|
+
|
80
|
+
#
|
81
|
+
# Returns a string detailing the findings of the style detection.
|
82
|
+
#
|
83
|
+
def describe
|
84
|
+
desc = "#{generated_at} by as-style-vacuum"
|
85
|
+
desc << add_desc("Styles declared in CSS", @declared)
|
86
|
+
desc << add_desc("Styles used in MXML",@used)
|
87
|
+
desc << add_desc("Styles declared but not used (in a styleName property)",@unused)
|
88
|
+
desc << add_desc("Styles used but not declared",@undeclared)
|
89
|
+
desc
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|