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:



171
172
173
# File 'lib/cocoapods-core/specification/linter.rb', line 171

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_app_host_name(n) ⇒ Object (private)



404
405
406
407
408
409
410
411
412
413
414
# File 'lib/cocoapods-core/specification/linter.rb', line 404

def _validate_app_host_name(n)
  unless consumer.requires_app_host?
    results.add_error('app_host_name', '`requires_app_host` must be set to ' \
      '`true` when `app_host_name` is specified.')
  end

  unless consumer.dependencies.map(&:name).include?(n)
    results.add_error('app_host_name', "The app host name (#{n}) specified by `#{consumer.spec.name}` could " \
      'not be found. You must explicitly declare a dependency on that app spec.')
  end
end

#_validate_authors(a) ⇒ Object (private)

Performs validations related to the authors attribute.



231
232
233
234
235
236
237
238
# File 'lib/cocoapods-core/specification/linter.rb', line 231

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_changelog(s) ⇒ Object (private)

Performs validations related to the changelog attribute.



535
536
537
538
539
540
# File 'lib/cocoapods-core/specification/linter.rb', line 535

def _validate_changelog(s)
  if s =~ %r{https://www.example.com/CHANGELOG}
    results.add_warning('changelog', 'The changelog has ' \
      'not been updated from the default.')
  end
end

#_validate_compiler_flags(flags) ⇒ Object (private)

Performs validations related to the compiler_flags attribute.



559
560
561
562
563
564
# File 'lib/cocoapods-core/specification/linter.rb', line 559

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.



387
388
389
390
391
392
# File 'lib/cocoapods-core/specification/linter.rb', line 387

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.



271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/cocoapods-core/specification/linter.rb', line 271

def _validate_description(d)
  if d == spec.summary
    results.add_warning('description', 'The description is equal to' \
     ' the summary.')
  end

  if d.strip.empty?
    results.add_error('description', 'The description is empty.')
  elsif spec.summary && 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.



297
298
299
300
301
302
# File 'lib/cocoapods-core/specification/linter.rb', line 297

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.



287
288
289
290
291
292
293
# File 'lib/cocoapods-core/specification/linter.rb', line 287

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

#_validate_info_plist(value) ⇒ Object (private)

Parameters:

  • value (Hash, Object)


544
545
546
547
548
549
# File 'lib/cocoapods-core/specification/linter.rb', line 544

def _validate_info_plist(value)
  return if value.empty?
  if consumer.spec.subspec? && consumer.spec.library_specification?
    results.add_error('info_plist', 'Info.plist configuration is not currently supported for subspecs.')
  end
end

#_validate_libraries(libs) ⇒ Object (private)

Performs validations related to the libraries attribute.



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/cocoapods-core/specification/linter.rb', line 315

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.



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/cocoapods-core/specification/linter.rb', line 339

def _validate_license(l)
  type = l[:type]
  file = l[:file]
  if type.nil?
    results.add_warning('license', 'Missing license type.')
  end
  if type && type.delete(' ').delete("\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.



250
251
252
253
254
255
# File 'lib/cocoapods-core/specification/linter.rb', line 250

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.



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/cocoapods-core/specification/linter.rb', line 210

def _validate_name(name)
  if name =~ %r{/}
    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_on_demand_resources(h) ⇒ Object (private)

Performs validations related to the on_demand_resources attribute.



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

def _validate_on_demand_resources(h)
  h.values.each do |value|
    unless Specification::ON_DEMAND_RESOURCES_CATEGORY_KEYS.include?(value[:category])
      results.add_error('on_demand_resources', "Invalid on demand resources category value `#{value[:category]}`. " \
        "Available options are `#{Specification::ON_DEMAND_RESOURCES_CATEGORY_KEYS.join(', ')}`.")
    end
  end
end

#_validate_readme(s) ⇒ Object (private)

Performs validations related to the readme attribute.



526
527
528
529
530
531
# File 'lib/cocoapods-core/specification/linter.rb', line 526

def _validate_readme(s)
  if s =~ %r{https://www.example.com/README}
    results.add_warning('readme', 'The readme has ' \
      'not been updated from the default.')
  end
end

#_validate_scheme(s) ⇒ Object (private)

Performs validation related to the scheme attribute.



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/cocoapods-core/specification/linter.rb', line 450

def _validate_scheme(s)
  unless s.empty?
    if consumer.spec.subspec? && consumer.spec.library_specification?
      results.add_error('scheme', 'Scheme configuration is not currently supported for subspecs.')
      return
    end
    if s.key?(:launch_arguments) && !s[:launch_arguments].is_a?(Array)
      results.add_error('scheme', 'Expected an array for key `launch_arguments`.')
    end
    if s.key?(:environment_variables) && !s[:environment_variables].is_a?(Hash)
      results.add_error('scheme', 'Expected a hash for key `environment_variables`.')
    end
    if s.key?(:code_coverage) && ![true, false].include?(s[:code_coverage])
      results.add_error('scheme', 'Expected a boolean for key `code_coverage`.')
    end
    if s.key?(:parallelizable) && ![true, false].include?(s[:parallelizable])
      results.add_error('scheme', 'Expected a boolean for key `parallelizable`.')
    end
    if s.key?(:build_configurations) && !s[:build_configurations].is_a?(Hash)
      results.add_error('scheme', 'Expected a hash for key `build_configurations`.')
    end
  end
end

#_validate_script_phases(s) ⇒ Object (private)

Performs validations related to the script_phases attribute.



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/cocoapods-core/specification/linter.rb', line 418

def _validate_script_phases(s)
  s.each do |script_phase|
    keys = script_phase.keys
    unrecognized_keys = keys - Specification::ALL_SCRIPT_PHASE_KEYS
    unless unrecognized_keys.empty?
      results.add_error('script_phases', "Unrecognized option(s) `#{unrecognized_keys.join(', ')}` in script phase `#{script_phase[:name]}`. " \
        "Available options are `#{Specification::ALL_SCRIPT_PHASE_KEYS.join(', ')}`.")
    end
    missing_required_keys = Specification::SCRIPT_PHASE_REQUIRED_KEYS - keys
    unless missing_required_keys.empty?
      results.add_error('script_phases', "Missing required shell script phase options `#{missing_required_keys.join(', ')}` in script phase `#{script_phase[:name]}`.")
    end
    unless Specification::EXECUTION_POSITION_KEYS.include?(script_phase[:execution_position])
      results.add_error('script_phases', "Invalid execution position value `#{script_phase[:execution_position]}` in shell script `#{script_phase[:name]}`. " \
      "Available options are `#{Specification::EXECUTION_POSITION_KEYS.join(', ')}`.")
    end
  end
end

#_validate_social_media_url(s) ⇒ Object (private)

Performs validations related to the social_media_url attribute.



517
518
519
520
521
522
# File 'lib/cocoapods-core/specification/linter.rb', line 517

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.



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/cocoapods-core/specification/linter.rb', line 358

def _validate_source(s)
  return unless s.is_a?(Hash)
  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.', true)
    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.



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

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_test_type(t) ⇒ Object (private)

Performs validations related to the test_type attribute.



396
397
398
399
400
401
402
# File 'lib/cocoapods-core/specification/linter.rb', line 396

def _validate_test_type(t)
  supported_test_types = Specification::DSL::SUPPORTED_TEST_TYPES.map(&:to_s)
  unless supported_test_types.include?(t.to_s)
    results.add_error('test_type', "The test type `#{t}` is not supported. " \
      "Supported test type values are #{supported_test_types}.")
  end
end

#_validate_version(v) ⇒ Object (private)

Performs validations related to the version attribute.



242
243
244
245
246
# File 'lib/cocoapods-core/specification/linter.rb', line 242

def _validate_version(v)
  if v.to_s.empty?
    results.add_error('version', 'A version is required.')
  end
end

#_validate_weak_frameworks(frameworks) ⇒ Object (private)

Performs validations related to the weak frameworks attribute.



306
307
308
309
310
311
# File 'lib/cocoapods-core/specification/linter.rb', line 306

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



505
506
507
508
509
510
511
512
513
# File 'lib/cocoapods-core/specification/linter.rb', line 505

def check_git_ssh_source(s)
  if git = s[:git]
    if git =~ %r{\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.', true)
    end
  end
end

#check_required_attributesvoid (private)

This method returns an undefined value.

Checks that every required attribute has a value.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cocoapods-core/specification/linter.rb', line 121

def check_required_attributes
  attributes = DSL.attributes.values.select(&:required?)
  attributes.each do |attr|
    begin
      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
    rescue => exception
      results.add_error('attributes', "Unable to parse attribute `#{attr.name}` due to error: #{exception}")
    end
  end
end

#check_requires_arc_attributevoid (private)

This method returns an undefined value.

Generates a warning if the requires_arc attribute has true or false string values.



107
108
109
110
111
112
113
114
115
# File 'lib/cocoapods-core/specification/linter.rb', line 107

def check_requires_arc_attribute
  attribute = DSL.attributes.values.find { |attr| attr.name == :requires_arc }
  if attribute
    value = spec.send(attribute.name)
    if value == 'true' || value == 'false'
      results.add_warning('requires_arc', value + ' is considered to be the name of a file.')
    end
  end
end

#errorsArray<Result>

Returns all the errors generated by the Linter.

Returns:

  • (Array<Result>)

    all the errors generated by the Linter.



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

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



574
575
576
577
578
579
# File 'lib/cocoapods-core/specification/linter.rb', line 574

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

#lintBoolean

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

Returns:

  • (Boolean)

    whether the specification passed validation.



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

def lint
  @results = Results.new
  if spec
    validate_root_name
    check_required_attributes
    check_requires_arc_attribute
    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.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/cocoapods-core/specification/linter.rb', line 154

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.



476
477
478
479
480
481
482
483
484
485
486
# File 'lib/cocoapods-core/specification/linter.rb', line 476

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)



488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/cocoapods-core/specification/linter.rb', line 488

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.', true)
  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.



177
178
179
180
# File 'lib/cocoapods-core/specification/linter.rb', line 177

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.



145
146
147
148
# File 'lib/cocoapods-core/specification/linter.rb', line 145

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.



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

def run_validation_hooks(attributes, target)
  attributes.each do |attr|
    validation_hook = "_validate_#{attr.name}"
    next unless respond_to?(validation_hook, true)
    begin
      value = target.send(attr.name)
      next unless value
      send(validation_hook, value)
    rescue => e
      results.add_error(attr.name, "Unable to validate due to exception: #{e}")
    end
  end
end

#validate_root_namevoid (private)

This method returns an undefined value.

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



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

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.



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

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