-
Notifications
You must be signed in to change notification settings - Fork 350
/
Copy pathlinter.rb
582 lines (519 loc) · 19.7 KB
/
linter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
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
357
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
require 'cocoapods-core/specification/linter/result'
require 'cocoapods-core/specification/linter/analyzer'
module Pod
class Specification
# 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.
#
class Linter
# @return [Specification] the specification to lint.
#
attr_reader :spec
# @return [Pathname] the path of the `podspec` file where {#spec} is
# defined.
#
attr_reader :file
attr_reader :results
# @param [Specification, Pathname, String] spec_or_path
# the Specification or the path of the `podspec` file to lint.
#
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
# Lints the specification adding a {Result} for any failed check to the
# {#results} object.
#
# @return [Boolean] whether the specification passed validation.
#
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
#-----------------------------------------------------------------------#
# !@group Lint results
public
# @return [Array<Result>] all the errors generated by the Linter.
#
def errors
@errors ||= results.select { |r| r.type == :error }
end
# @return [Array<Result>] all the warnings generated by the Linter.
#
def warnings
@warnings ||= results.select { |r| r.type == :warning }
end
#-----------------------------------------------------------------------#
private
# !@group Lint steps
# Checks that the spec's root name matches the filename.
#
# @return [void]
#
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
# Generates a warning if the requires_arc attribute has true or false string values.
#
# @return [void]
#
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
# Checks that every required attribute has a value.
#
# @return [void]
#
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
# Runs the validation hook for root only attributes.
#
# @return [void]
#
def run_root_validation_hooks
attributes = DSL.attributes.values.select(&:root_only?)
run_validation_hooks(attributes, spec)
end
# Run validations for multi-platform attributes activating.
#
# @return [void]
#
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
# @return [Specification::Consumer] the current consumer.
#
attr_accessor :consumer
# Runs the validation hook for the attributes that are not root only.
#
# @return [void]
#
def run_all_specs_validation_hooks
attributes = DSL.attributes.values.reject(&:root_only?)
run_validation_hooks(attributes, consumer)
end
# Runs the validation hook for each attribute.
#
# @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.
#
# @return [void]
#
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
#-----------------------------------------------------------------------#
private
# Performs validations related to the `name` attribute.
#
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
# @!group Root spec validation helpers
# Performs validations related to the `authors` attribute.
#
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
# Performs validations related to the `version` attribute.
#
def _validate_version(v)
if v.to_s.empty?
results.add_error('version', 'A version is required.')
end
end
# Performs validations related to the `module_name` attribute.
#
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
# Performs validations related to the `summary` attribute.
#
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
# Performs validations related to the `description` attribute.
#
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
# Performs validations related to the `homepage` attribute.
#
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
# Performs validations related to the `frameworks` attribute.
#
def _validate_frameworks(frameworks)
if frameworks_invalid?(frameworks)
results.add_error('frameworks', 'A framework should only be' \
' specified by its name')
end
end
# Performs validations related to the `weak frameworks` attribute.
#
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
# Performs validations related to the `libraries` attribute.
#
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
# Performs validations related to the `license` attribute.
#
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
# Performs validations related to the `source` attribute.
#
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
# Performs validations related to the `deprecated_in_favor_of` attribute.
#
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
# Performs validations related to the `test_type` attribute.
#
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
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
# Performs validations related to the `script_phases` attribute.
#
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
# Performs validations related to the `on_demand_resources` attribute.
#
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
# Performs validation related to the `scheme` attribute.
#
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
# Performs validations related to github sources.
#
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
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
# Performs validations related to SSH sources
#
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
# Performs validations related to the `social_media_url` attribute.
#
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
# Performs validations related to the `readme` attribute.
#
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
# Performs validations related to the `changelog` attribute.
#
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
# @param [Hash,Object] value
#
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
#-----------------------------------------------------------------------#
# @!group All specs validation helpers
private
# Performs validations related to the `compiler_flags` attribute.
#
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
# Returns whether the frameworks are valid
#
# @param frameworks [Array<String>]
# The frameworks to be validated
#
# @return [Boolean] true if a framework contains any
# non-alphanumeric character or includes an extension.
#
def frameworks_invalid?(frameworks)
frameworks.any? do |framework|
framework_regex = /[^\w\-\+]/
framework =~ framework_regex
end
end
end
end
end