img-lint 0.0.17 → 0.0.18
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/img_lint/cli.rb +6 -0
- data/lib/img_lint/config.rb +5 -0
- data/lib/img_lint/linter.rb +30 -12
- data/lib/img_lint/rake_task.rb +2 -0
- data/lib/img_lint/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a93c829cbe2459dea77322211cf9adc461ca4bdc
|
4
|
+
data.tar.gz: 36c447665a022f2a6458a9c742dadae0192f5c7c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 957cefd40b9658c95bbb86ddcc91119c397a44d4df36390370467325f7ea8169b5d1a5bfe78c6db3060432ac1107dca9a577d3dfd377368df74272f532e6ba91
|
7
|
+
data.tar.gz: 00d00af5d27de640793fb5bf8df1ca38a0c90df7faaf8764ede44065fecf37abeb4dae4e007fb6eb90252e479db545c97639519de39d909c7b9440a52a13b092
|
data/lib/img_lint/cli.rb
CHANGED
@@ -16,6 +16,12 @@ module IMGLint
|
|
16
16
|
# linter.
|
17
17
|
#
|
18
18
|
class CLI
|
19
|
+
# CLI is used when running img-lint from command line (check bin/img-lint) file
|
20
|
+
# or from a Raketask (check lib/img_lint/rake_task file).
|
21
|
+
#
|
22
|
+
# In Raketask CLI#run is called without arguments (expected that config file is there)
|
23
|
+
# and in bin/img-lint ARGV is passed in.
|
24
|
+
#
|
19
25
|
def run(args = [])
|
20
26
|
if args.first == "install"
|
21
27
|
require "fileutils"
|
data/lib/img_lint/config.rb
CHANGED
@@ -6,11 +6,16 @@ require "img_lint/constants"
|
|
6
6
|
module IMGLint
|
7
7
|
# Config class is responsible to load img-lint either a user defined config
|
8
8
|
# or a default one (config/default.yml)
|
9
|
+
#
|
9
10
|
class Config
|
10
11
|
FILE_NAME = ".img-lint.yml".freeze
|
11
12
|
DEFAULT_FILE = File.join(IMG_LINT_HOME, "config", "default.yml")
|
12
13
|
|
13
14
|
class << self
|
15
|
+
# Be default gem will try to load config file in user's project folder.
|
16
|
+
# Then user's config (or empty object) will be merge with the default config
|
17
|
+
# from gem's folder.
|
18
|
+
#
|
14
19
|
def load
|
15
20
|
user_config = File.exist?(user_file) ? YAML.safe_load(File.read(user_file)) : {}
|
16
21
|
default_config = YAML.safe_load(File.read(DEFAULT_FILE))
|
data/lib/img_lint/linter.rb
CHANGED
@@ -6,12 +6,17 @@ module IMGLint
|
|
6
6
|
# suspicious images.
|
7
7
|
#
|
8
8
|
class Linter
|
9
|
-
|
10
|
-
|
9
|
+
# Be default gem will merge user-defined condif with the default one.
|
10
|
+
#
|
11
11
|
def initialize(config: {})
|
12
|
-
|
12
|
+
@config = IMGLint::Config.load.merge(config)
|
13
13
|
end
|
14
14
|
|
15
|
+
# The gem's core method which actually goes through all images files and
|
16
|
+
# checks whether the image size exceeds the max size from config.
|
17
|
+
#
|
18
|
+
# Verbose attribute is there to surpress output when running specs.
|
19
|
+
#
|
15
20
|
def lint(path: Dir.pwd, verbose: true)
|
16
21
|
path ||= Dir.pwd
|
17
22
|
|
@@ -25,21 +30,21 @@ module IMGLint
|
|
25
30
|
private
|
26
31
|
|
27
32
|
def find_heavy_images(path, verbose)
|
28
|
-
images = Dir.glob(%(#{path}/**/*.{#{config['image_formats']}}))
|
33
|
+
images = Dir.glob(%(#{path}/**/*.{#{@config['image_formats']}}))
|
29
34
|
|
30
|
-
puts "
|
35
|
+
puts "--> img-lint found no images in #{path}" if verbose && images.empty?
|
31
36
|
|
32
37
|
images.each_with_object({}) do |file, o|
|
33
38
|
next if excluded_file?(file)
|
34
39
|
|
35
40
|
image_size = File.new(file).size / 1024
|
36
41
|
|
37
|
-
o[file] = image_size if image_size > config["max_file_size"]
|
42
|
+
o[file] = image_size if image_size > @config["max_file_size"]
|
38
43
|
end
|
39
44
|
end
|
40
45
|
|
41
46
|
def exclude_patterns
|
42
|
-
@exclude_patterns ||= config.fetch("exclude", []).map! do |pattern|
|
47
|
+
@exclude_patterns ||= @config.fetch("exclude", []).map! do |pattern|
|
43
48
|
if pattern.start_with?("/")
|
44
49
|
pattern
|
45
50
|
else
|
@@ -54,18 +59,31 @@ module IMGLint
|
|
54
59
|
end
|
55
60
|
end
|
56
61
|
|
62
|
+
def longest_path_and_file_size(path, heavy_images)
|
63
|
+
longest_image_path = heavy_images.keys.max { |k| k.size }
|
64
|
+
longest_image_path = longest_image_path.sub(Dir.pwd, "") if Dir.pwd == path
|
65
|
+
|
66
|
+
longest_file_size = heavy_images.values.max { |v| v.to_s.size }.size
|
67
|
+
|
68
|
+
[longest_image_path.size, longest_file_size]
|
69
|
+
end
|
70
|
+
|
57
71
|
def print_report(path, heavy_images, verbose)
|
58
|
-
return
|
72
|
+
return unless verbose
|
73
|
+
|
74
|
+
if heavy_images.empty?
|
75
|
+
puts "--> img-lint detected no heavy images"
|
76
|
+
return
|
77
|
+
end
|
59
78
|
|
60
|
-
puts "
|
79
|
+
puts "--> img-lint detected heavy images:"
|
61
80
|
|
62
|
-
|
63
|
-
longest_file_size = heavy_images.values.max { |v| v.to_s.size }.size
|
81
|
+
longest_image_path_size, longest_file_size = longest_path_and_file_size(path, heavy_images)
|
64
82
|
|
65
83
|
heavy_images.sort_by(&:last).reverse.each do |image, file_size|
|
66
84
|
image = image.sub(Dir.pwd, "") if Dir.pwd == path
|
67
85
|
|
68
|
-
puts [image.ljust(
|
86
|
+
puts [image.ljust(longest_image_path_size), "#{file_size}Kb".rjust(longest_file_size)].join("\t")
|
69
87
|
end
|
70
88
|
end
|
71
89
|
end
|
data/lib/img_lint/rake_task.rb
CHANGED
data/lib/img_lint/version.rb
CHANGED