Class: Gemfilelint::Linter

Inherits:
Object
  • Object
show all
Defined in:
lib/gemfilelint.rb

Defined Under Namespace

Modules: ANSIColor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ignore: [], logger: nil) ⇒ Linter

Returns a new instance of Linter.



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/gemfilelint.rb', line 118

def initialize(ignore: [], logger: nil)
  @ignore = ignore
  @logger =
    logger ||
      Logger
        .new($stdout)
        .tap do |creating|
          creating.level = :info
          creating.formatter = ->(*, message) { message }
        end
end

Instance Attribute Details

#ignoreObject (readonly)

Returns the value of attribute ignore.



116
117
118
# File 'lib/gemfilelint.rb', line 116

def ignore
  @ignore
end

#loggerObject (readonly)

Returns the value of attribute logger.



116
117
118
# File 'lib/gemfilelint.rb', line 116

def logger
  @logger
end

Instance Method Details

#lint(*paths) ⇒ Object

rubocop:disable Naming/PredicateMethod



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/gemfilelint.rb', line 130

def lint(*paths) # rubocop:disable Naming/PredicateMethod
  logger.info("Inspecting gemfiles at #{paths.join(", ")}\n")

  offenses = []
  paths.each do |path|
    Parser
      .for(path)
      .each_offense(ignore: ignore) do |offense|
        if offense
          offenses << offense
          logger.info("W".colorize(:magenta))
        else
          logger.info(".".colorize(:green))
        end
      end
  end

  logger.info("\n")

  if offenses.empty?
    true
  else
    messages =
      offenses.map do |offense|
        "#{offense.path.colorize(:cyan)}: " \
          "#{"W".colorize(:magenta)}: #{offense}"
      end

    logger.info("\nOffenses:\n\n#{messages.join("\n")}\n")
    false
  end
end