Class: Pod::Specification::Linter

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-core/specification/linter.rb,
lib/cocoapods-core/specification/linter/result.rb,
lib/cocoapods-core/specification/linter/analyzer.rb

Overview

The Linter check specifications for errors and warnings.

It is designed not only to guarantee the formal functionality of a specification, but also to support the maintenance of sources.

Defined Under Namespace

Classes: Analyzer, Results

Instance Attribute Summary collapse

Root spec validation helpers collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec_or_path) ⇒ Linter

Returns a new instance of Linter

Parameters:

  • spec_or_path (Specification, Pathname, String)

    the Specification or the path of the podspec file to lint.


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cocoapods-core/specification/linter.rb', line 26

def initialize(spec_or_path)
  if spec_or_path.is_a?(Specification)
    @spec = spec_or_path
    @file = @spec.defined_in_file
  else
    @file = Pathname.new(spec_or_path)
    begin
      @spec = Specification.from_file(@file)
    rescue => e
      @spec = nil
      @raise_message = e.message
    end
  end
end

Instance Attribute Details

#consumerSpecification::Consumer (private)

Returns the current consumer.

Returns:


152
153
154
# File 'lib/cocoapods-core/specification/linter.rb', line 152

def consumer
  @consumer
end

#filePathname (readonly)

Returns the path of the podspec file where #spec is defined.

Returns:

  • (Pathname)

    the path of the podspec file where #spec is defined.


19
20
21
# File 'lib/cocoapods-core/specification/linter.rb', line 19

def file
  @file
end

#resultsObject (readonly)

Returns the value of attribute results


21
22
23
# File 'lib/cocoapods-core/specification/linter.rb', line 21

def results
  @results
end

#specSpecification (readonly)

Returns the specification to lint.

Returns:


14
15
16
# File 'lib/cocoapods-core/specification/linter.rb', line 14

def spec
  @spec
end

Instance Method Details

#_validate_authors(a) ⇒ Object (private)


206
207
208
209
210
211
212
213
# File 'lib/cocoapods-core/specification/linter.rb', line 206

def _validate_authors(a)
  if a.is_a? Hash
    if a == { 'YOUR NAME HERE' => 'YOUR EMAIL HERE' }
      results.add_error('authors', 'The authors have not been updated ' \
        'from default')
    end
  end
end

#_validate_compiler_flags(flags) ⇒ Object (private)

Performs validations related to the compiler_flags attribute.


425
426
427
428
429
430
# File 'lib/cocoapods-core/specification/linter.rb', line 425

def _validate_compiler_flags(flags)
  if flags.join(' ').split(' ').any? { |flag| flag.start_with?('-Wno') }
    results.add_warning('compiler_flags', 'Warnings must not be disabled' \
    '(`-Wno compiler` flags).')
  end
end

#_validate_deprecated_in_favor_of(d) ⇒ Object (private)

Performs validations related to the deprecated_in_favor_of attribute.


360
361
362
363
364
365
# File 'lib/cocoapods-core/specification/linter.rb', line 360

def _validate_deprecated_in_favor_of(d)
  if spec.root.name == Specification.root_name(d)
    results.add_error('deprecated_in_favor_of', 'a spec cannot be ' \
      'deprecated in favor of itself')
  end
end

#_validate_description(d) ⇒ Object (private)

Performs validations related to the description attribute.


246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/cocoapods-core/specification/linter.rb', line 246

def _validate_description(d)
  if d =~ /An optional longer description of/
    results.add_warning('description', 'The description is not meaningful.')
  end
  if d == spec.summary
    results.add_warning('description', 'The description is equal to' \
     ' the summary.')
  end
  if d.length < spec.summary.length
    results.add_warning('description', 'The description is shorter ' \
    'than the summary.')
  end
end

#_validate_frameworks(frameworks) ⇒ Object (private)

Performs validations related to the frameworks attribute.


271
272
273
274
275
276
# File 'lib/cocoapods-core/specification/linter.rb', line 271

def _validate_frameworks(frameworks)
  if frameworks_invalid?(frameworks)
    results.add_error('frameworks', 'A framework should only be' \
    ' specified by its name')
  end
end

#_validate_homepage(h) ⇒ Object (private)

Performs validations related to the homepage attribute.


262
263
264
265
266
267
# File 'lib/cocoapods-core/specification/linter.rb', line 262

def _validate_homepage(h)
  if h =~ %r{http://EXAMPLE}
    results.add_warning('homepage', 'The homepage has not been updated' \
     ' from default')
  end
end

#_validate_libraries(libs) ⇒ Object (private)

Performs validations related to the libraries attribute.


289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/cocoapods-core/specification/linter.rb', line 289

def _validate_libraries(libs)
  libs.each do |lib|
    lib = lib.downcase
    if lib.end_with?('.a') || lib.end_with?('.dylib')
      results.add_error('libraries', 'Libraries should not include the' \
      ' extension ' \
      "(`#{lib}`)")
    end

    if lib.start_with?('lib')
      results.add_error('libraries', 'Libraries should omit the `lib`' \
      ' prefix ' \
      " (`#{lib}`)")
    end

    if lib.include?(',')
      results.add_error('libraries', 'Libraries should not include comas ' \
      "(`#{lib}`)")
    end
  end
end

#_validate_license(l) ⇒ Object (private)

Performs validations related to the license attribute.


313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/cocoapods-core/specification/linter.rb', line 313

def _validate_license(l)
  type = l[:type]
  file = l[:file]
  if type.nil?
    results.add_warning('license', 'Missing license type.')
  end
  if type && type.gsub(' ', '').gsub("\n", '').empty?
    results.add_warning('license', 'Invalid license type.')
  end
  if type && type =~ /\(example\)/
    results.add_error('license', 'Sample license type.')
  end
  if file && Pathname.new(file).extname !~ /^(\.(txt|md|markdown|))?$/i
    results.add_error('license', 'Invalid file type')
  end
end

#_validate_module_name(m) ⇒ Object (private)

Performs validations related to the module_name attribute.


225
226
227
228
229
230
# File 'lib/cocoapods-core/specification/linter.rb', line 225

def _validate_module_name(m)
  unless m.nil? || m =~ /^[a-z_][0-9a-z_]*$/i
    results.add_error('module_name', 'The module name of a spec' \
      ' should be a valid C99 identifier.')
  end
end

#_validate_name(name) ⇒ Object (private)

Performs validations related to the name attribute.


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/cocoapods-core/specification/linter.rb', line 187

def _validate_name(name)
  if name =~ /\//
    results.add_error('name', 'The name of a spec should not contain ' \
                   'a slash.')
  end

  if name =~ /\s/
    results.add_error('name', 'The name of a spec should not contain ' \
                   'whitespace.')
  end

  if name[0, 1] == '.'
    results.add_error('name', 'The name of a spec should not begin' \
    ' with a period.')
  end
end

#_validate_social_media_url(s) ⇒ Object (private)

Performs validations related to the social_media_url attribute.


410
411
412
413
414
415
# File 'lib/cocoapods-core/specification/linter.rb', line 410

def _validate_social_media_url(s)
  if s =~ %r{https://twitter.com/EXAMPLE}
    results.add_warning('social_media_url', 'The social media URL has ' \
      'not been updated from the default.')
  end
end

#_validate_source(s) ⇒ Object (private)

Performs validations related to the source attribute.


332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/cocoapods-core/specification/linter.rb', line 332

def _validate_source(s)
  if git = s[:git]
    tag, commit = s.values_at(:tag, :commit)
    version = spec.version.to_s

    if git =~ %r{http://EXAMPLE}
      results.add_error('source', 'The Git source still contains the ' \
      'example URL.')
    end
    if commit && commit.downcase =~ /head/
      results.add_error('source', 'The commit of a Git source cannot be' \
      ' `HEAD`.')
    end
    if tag && !tag.to_s.include?(version)
      results.add_warning('source', 'The version should be included in' \
       ' the Git tag.')
    end
    if tag.nil?
      results.add_warning('source', 'Git sources should specify a tag.')
    end
  end

  perform_github_source_checks(s)
  check_git_ssh_source(s)
end

#_validate_summary(s) ⇒ Object (private)

Performs validations related to the summary attribute.


234
235
236
237
238
239
240
241
242
# File 'lib/cocoapods-core/specification/linter.rb', line 234

def _validate_summary(s)
  if s.length > 140
    results.add_warning('summary', 'The summary should be a short ' \
      'version of `description` (max 140 characters).')
  end
  if s =~ /A short description of/
    results.add_warning('summary', 'The summary is not meaningful.')
  end
end

#_validate_version(v) ⇒ Object (private)


215
216
217
218
219
220
221
222
# File 'lib/cocoapods-core/specification/linter.rb', line 215

def _validate_version(v)
  if v.to_s.empty?
    results.add_error('version', 'A version is required.')
  elsif v <= Version::ZERO
    results.add_error('version', 'The version of the spec should be' \
    ' higher than 0.')
  end
end

#_validate_weak_frameworks(frameworks) ⇒ Object (private)

Performs validations related to the weak frameworks attribute.


280
281
282
283
284
285
# File 'lib/cocoapods-core/specification/linter.rb', line 280

def _validate_weak_frameworks(frameworks)
  if frameworks_invalid?(frameworks)
    results.add_error('weak_frameworks', 'A weak framework should only be' \
    ' specified by its name')
  end
end

#check_git_ssh_source(s) ⇒ Object (private)

Performs validations related to SSH sources


398
399
400
401
402
403
404
405
406
# File 'lib/cocoapods-core/specification/linter.rb', line 398

def check_git_ssh_source(s)
  if git = s[:git]
    if git =~ /\w+\@(\w|\.)+\:(\/\w+)*/
      results.add_warning('source', 'Git SSH URLs will NOT work for ' \
        'people behind firewalls configured to only allow HTTP, ' \
        'therefore HTTPS is preferred.')
    end
  end
end

#check_required_attributesvoid (private)

This method returns an undefined value.

Checks that every required attribute has a value.


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cocoapods-core/specification/linter.rb', line 106

def check_required_attributes
  attributes = DSL.attributes.values.select(&:required?)
  attributes.each do |attr|
    value = spec.send(attr.name)
    unless value && (!value.respond_to?(:empty?) || !value.empty?)
      if attr.name == :license
        results.add_warning('attributes', 'Missing required attribute ' \
        "`#{attr.name}`.")
      else
        results.add_error('attributes', 'Missing required attribute ' \
         "`#{attr.name}`.")
      end
    end
  end
end

#errorsArray<Result>

Returns all the errors generated by the Linter.

Returns:

  • (Array<Result>)

    all the errors generated by the Linter.


68
69
70
# File 'lib/cocoapods-core/specification/linter.rb', line 68

def errors
  @errors ||= results.select { |r| r.type == :error }
end

#frameworks_invalid?(frameworks) ⇒ Boolean (private)

Returns whether the frameworks are valid

The frameworks to be validated

non-alphanumeric character or includes an extension.

Parameters:

  • frameworks (Array<String>)

Returns:

  • (Boolean)

    true if a framework contains any


440
441
442
443
444
445
# File 'lib/cocoapods-core/specification/linter.rb', line 440

def frameworks_invalid?(frameworks)
  frameworks.any? do |framework|
    framework_regex = /[^\w\-\+]/
    framework =~ framework_regex
  end
end

#lintBool

Lints the specification adding a Result for any failed check to the #results object.

Returns:

  • (Bool)

    whether the specification passed validation.


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cocoapods-core/specification/linter.rb', line 46

def lint
  @results = Results.new
  if spec
    validate_root_name
    check_required_attributes
    run_root_validation_hooks
    perform_all_specs_analysis
  else
    results.add_error('spec', "The specification defined in `#{file}` "\
      "could not be loaded.\n\n#{@raise_message}")
  end
  results.empty?
end

#perform_all_specs_analysisvoid (private)

This method returns an undefined value.

Run validations for multi-platform attributes activating .


135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cocoapods-core/specification/linter.rb', line 135

def perform_all_specs_analysis
  all_specs = [spec, *spec.recursive_subspecs]
  all_specs.each do |current_spec|
    current_spec.available_platforms.each do |platform|
      @consumer = Specification::Consumer.new(current_spec, platform)
      results.consumer = @consumer
      run_all_specs_validation_hooks
      analyzer = Analyzer.new(@consumer, results)
      results = analyzer.analyze
      @consumer = nil
      results.consumer = nil
    end
  end
end

#perform_github_source_checks(s) ⇒ Object (private)

Performs validations related to github sources.


369
370
371
372
373
374
375
376
377
378
379
# File 'lib/cocoapods-core/specification/linter.rb', line 369

def perform_github_source_checks(s)
  require 'uri'

  if git = s[:git]
    return unless git =~ /^#{URI.regexp}$/
    git_uri = URI.parse(git)
    if git_uri.host
      perform_github_uri_checks(git, git_uri) if git_uri.host.end_with?('github.com')
    end
  end
end

#perform_github_uri_checks(git, git_uri) ⇒ Object (private)


381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/cocoapods-core/specification/linter.rb', line 381

def perform_github_uri_checks(git, git_uri)
  if git_uri.host.start_with?('www.')
    results.add_warning('github_sources', 'Github repositories should ' \
      'not use `www` in their URL.')
  end
  unless git.end_with?('.git')
    results.add_warning('github_sources', 'Github repositories ' \
      'should end in `.git`.')
  end
  unless git_uri.scheme == 'https'
    results.add_warning('github_sources', 'Github repositories ' \
      'should use an `https` link.')
  end
end

#run_all_specs_validation_hooksvoid (private)

This method returns an undefined value.

Runs the validation hook for the attributes that are not root only.


158
159
160
161
# File 'lib/cocoapods-core/specification/linter.rb', line 158

def run_all_specs_validation_hooks
  attributes = DSL.attributes.values.reject(&:root_only?)
  run_validation_hooks(attributes, consumer)
end

#run_root_validation_hooksvoid (private)

This method returns an undefined value.

Runs the validation hook for root only attributes.


126
127
128
129
# File 'lib/cocoapods-core/specification/linter.rb', line 126

def run_root_validation_hooks
  attributes = DSL.attributes.values.select(&:root_only?)
  run_validation_hooks(attributes, spec)
end

#run_validation_hooks(attributes, target) ⇒ void (private)

Note:

Hooks are called only if there is a value for the attribute as required attributes are already checked by the #check_required_attributes step.

This method returns an undefined value.

Runs the validation hook for each attribute.


171
172
173
174
175
176
177
178
179
# File 'lib/cocoapods-core/specification/linter.rb', line 171

def run_validation_hooks(attributes, target)
  attributes.each do |attr|
    validation_hook = "_validate_#{attr.name}"
    next unless respond_to?(validation_hook, true)
    value = target.send(attr.name)
    next unless value
    send(validation_hook, value)
  end
end

#validate_root_namevoid (private)

This method returns an undefined value.

Checks that the spec's root name matches the filename.


88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cocoapods-core/specification/linter.rb', line 88

def validate_root_name
  if spec.root.name && file
    acceptable_names = [
      spec.root.name + '.podspec',
      spec.root.name + '.podspec.json',
    ]
    names_match = acceptable_names.include?(file.basename.to_s)
    unless names_match
      results.add_error('name', 'The name of the spec should match the ' \
                        'name of the file.')
    end
  end
end

#warningsArray<Result>

Returns all the warnings generated by the Linter.

Returns:

  • (Array<Result>)

    all the warnings generated by the Linter.


74
75
76
# File 'lib/cocoapods-core/specification/linter.rb', line 74

def warnings
  @warnings ||= results.select { |r| r.type == :warning }
end