img-lint 0.0.17 → 0.0.18

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 59e8ea52589642c88577a1c2ed8448884c17cb79
4
- data.tar.gz: f0afc1f20220878d0fe98c7c0ca134826b1eab63
3
+ metadata.gz: a93c829cbe2459dea77322211cf9adc461ca4bdc
4
+ data.tar.gz: 36c447665a022f2a6458a9c742dadae0192f5c7c
5
5
  SHA512:
6
- metadata.gz: 01b30b58d3bb1cfdfac948121b3217c698210eebd20c242fb5799b698714e7ad6e99398b0e62c2a82381919c93f5d926e7a052ce8ef5ab1c053faed774764faf
7
- data.tar.gz: 02a5c48975b61013fce63ace7a5736ef62578b9c7ae2c57cec49ebdedf8f5c424177a31078eb0470b71c10f01cc06adef1cc990739bb2969ab67a62c1151455d
6
+ metadata.gz: 957cefd40b9658c95bbb86ddcc91119c397a44d4df36390370467325f7ea8169b5d1a5bfe78c6db3060432ac1107dca9a577d3dfd377368df74272f532e6ba91
7
+ data.tar.gz: 00d00af5d27de640793fb5bf8df1ca38a0c90df7faaf8764ede44065fecf37abeb4dae4e007fb6eb90252e479db545c97639519de39d909c7b9440a52a13b092
@@ -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"
@@ -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))
@@ -6,12 +6,17 @@ module IMGLint
6
6
  # suspicious images.
7
7
  #
8
8
  class Linter
9
- attr_accessor :config
10
-
9
+ # Be default gem will merge user-defined condif with the default one.
10
+ #
11
11
  def initialize(config: {})
12
- self.config = IMGLint::Config.load.merge(config)
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 "No images found in #{path}" if verbose && images.empty?
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 if heavy_images.empty? || !verbose
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 "Suspicious images:"
79
+ puts "--> img-lint detected heavy images:"
61
80
 
62
- longest_image_path = heavy_images.keys.max { |k| k.size }.size
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(longest_image_path), "#{file_size}Kb".rjust(longest_file_size)].join("\t")
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
@@ -9,6 +9,8 @@ module IMGLint
9
9
  # and use in CI for example.
10
10
  #
11
11
  class RakeTask < Rake::TaskLib
12
+ # The only argument is the name of the rake task to register.
13
+ #
12
14
  def initialize(name = :img_lint)
13
15
  @name = name
14
16
 
@@ -3,5 +3,5 @@
3
3
  # Module to keep the gem's version in. Make sure to bump it before release.
4
4
  #
5
5
  module IMGLint
6
- VERSION = "0.0.17".freeze
6
+ VERSION = "0.0.18".freeze
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: img-lint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anatoli Makarevich