Class: Pod::DyValidator

Inherits:
Object
  • Object
show all
Includes:
Config::Mixin
Defined in:
lib/validator.rb

Overview

Validates a Specification.

Extends the Linter from the Core to add additional which require the LocalPod and the Installer.

In detail it checks that the file patterns defined by the user match actually do match at least a file and that the Pod builds, by installing it without integration and building the project with xcodebuild.

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec_or_path, source_urls) ⇒ DyValidator

Initialize a new instance

Parameters:

  • spec_or_path (Specification, Pathname, String)

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

  • source_urls (Array<String>)

    the Source URLs to use in creating a Podfile.



30
31
32
33
34
35
36
37
# File 'lib/validator.rb', line 30

def initialize(spec_or_path, source_urls)
  @linter = Specification::Linter.new(spec_or_path)
  @source_urls = if @linter.spec && @linter.spec.dependencies.empty? && @linter.spec.recursive_subspecs.all? { |s| s.dependencies.empty? }
                   []
                 else
                   source_urls.map { |url| config.sources_manager.source_with_name_or_url(url) }.map(&:url)
                 end
end

Instance Attribute Details

#allow_warningsBool

Returns Whether the validator should fail on warnings, or only on errors.

Returns:

  • (Bool)

    Whether the validator should fail on warnings, or only on errors.



183
184
185
# File 'lib/validator.rb', line 183

def allow_warnings
  @allow_warnings
end

#fail_fastBool

Returns whether the linter should fail as soon as the first build variant causes an error. Helpful for i.e. multi-platforms specs, specs with subspecs.

Returns:

  • (Bool)

    whether the linter should fail as soon as the first build variant causes an error. Helpful for i.e. multi-platforms specs, specs with subspecs.



171
172
173
# File 'lib/validator.rb', line 171

def fail_fast
  @fail_fast
end

#file_accessorSandbox::FileAccessor

Returns the file accessor for the spec.

Returns:

  • (Sandbox::FileAccessor)

    the file accessor for the spec.



56
57
58
# File 'lib/validator.rb', line 56

def file_accessor
  @file_accessor
end

#ignore_public_only_resultsBoolean

Returns Whether attributes that affect only public sources Bool be skipped.

Returns:

  • (Boolean)

    Whether attributes that affect only public sources Bool be skipped.



204
205
206
# File 'lib/validator.rb', line 204

def ignore_public_only_results
  @ignore_public_only_results
end

#linterSpecification::Linter (readonly)

Returns the linter instance from CocoaPods Core.

Returns:

  • (Specification::Linter)

    the linter instance from CocoaPods Core.



20
21
22
# File 'lib/validator.rb', line 20

def linter
  @linter
end

#localBool Also known as: local?

Note:

Uses the ‘:path` option of the Podfile.

Returns whether the validation should be performed against the root of the podspec instead to its original source.

Returns:

  • (Bool)

    whether the validation should be performed against the root of the podspec instead to its original source.



178
179
180
# File 'lib/validator.rb', line 178

def local
  @local
end

#no_cleanBool

Returns whether the linter should not clean up temporary files for inspection.

Returns:

  • (Bool)

    whether the linter should not clean up temporary files for inspection.



165
166
167
# File 'lib/validator.rb', line 165

def no_clean
  @no_clean
end

#no_subspecsBool

Returns Whether the validator should validate all subspecs.

Returns:

  • (Bool)

    Whether the validator should validate all subspecs.



191
192
193
# File 'lib/validator.rb', line 191

def no_subspecs
  @no_subspecs
end

#only_subspecString

Returns name of the subspec to check, if nil all subspecs are checked.

Returns:

  • (String)

    name of the subspec to check, if nil all subspecs are checked.



187
188
189
# File 'lib/validator.rb', line 187

def only_subspec
  @only_subspec
end

#quickBool

Returns whether the validation should skip the checks that requires the download of the library.

Returns:

  • (Bool)

    whether the validation should skip the checks that requires the download of the library.



160
161
162
# File 'lib/validator.rb', line 160

def quick
  @quick
end

#resultsObject (readonly)

Returns the value of attribute results.



215
216
217
# File 'lib/validator.rb', line 215

def results
  @results
end

#skip_import_validationObject Also known as: skip_import_validation?

Returns the value of attribute skip_import_validation.



206
207
208
# File 'lib/validator.rb', line 206

def skip_import_validation
  @skip_import_validation
end

#skip_testsBool

Returns Whether the validator should skip building and running tests.

Returns:

  • (Bool)

    Whether the validator should skip building and running tests.



195
196
197
# File 'lib/validator.rb', line 195

def skip_tests
  @skip_tests
end

#swift_versionString

Returns the SWIFT_VERSION to use for validation.

Returns:

  • (String)

    the SWIFT_VERSION to use for validation.



254
255
256
# File 'lib/validator.rb', line 254

def swift_version
  @swift_version ||= dot_swift_version || '3.0'
end

#use_frameworksBool

Returns Whether frameworks should be used for the installation.

Returns:

  • (Bool)

    Whether frameworks should be used for the installation.



199
200
201
# File 'lib/validator.rb', line 199

def use_frameworks
  @use_frameworks
end

Instance Method Details

#dot_swift_versionString

Returns the SWIFT_VERSION in the .swift-version file or nil.

Returns:

  • (String)

    the SWIFT_VERSION in the .swift-version file or nil.



264
265
266
267
268
269
# File 'lib/validator.rb', line 264

def dot_swift_version
  return unless file
  swift_version_path = file.dirname + '.swift-version'
  return unless swift_version_path.exist?
  swift_version_path.read.strip
end

#failure_reasonObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/validator.rb', line 131

def failure_reason
  results_by_type = results.group_by(&:type)
  results_by_type.default = []
  return nil if validated?
  reasons = []
  if (size = results_by_type[:error].size) && size > 0
    reasons << "#{size} #{'error'.pluralize(size)}"
  end
  if !allow_warnings && (size = results_by_type[:warning].size) && size > 0
    reason = "#{size} #{'warning'.pluralize(size)}"
    pronoun = size == 1 ? 'it' : 'them'
    reason << " (but you can use `--allow-warnings` to ignore #{pronoun})" if reasons.empty?
    reasons << reason
  end
  if results.all?(&:public_only)
    reasons << 'all results apply only to public specs, but you can use ' \
               '`--private` to ignore them if linting the specification for a private pod'
  end

  reasons.to_sentence
end

#filePathname

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

Returns:

  • (Pathname)

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



50
51
52
# File 'lib/validator.rb', line 50

def file
  @linter.file
end

This method returns an undefined value.

Prints the result of the validation to the user.



94
95
96
# File 'lib/validator.rb', line 94

def print_results
  UI.puts results_message
end

#result_colorSymbol

Returns The color, which should been used to display the result. One of: ‘:green`, `:yellow`, `:red`.

Returns:

  • (Symbol)

    The color, which should been used to display the result. One of: ‘:green`, `:yellow`, `:red`.



239
240
241
242
243
244
# File 'lib/validator.rb', line 239

def result_color
  case result_type
  when :error   then :red
  when :warning then :yellow
  else :green end
end

#result_typeSymbol

Returns The type, which should been used to display the result. One of: ‘:error`, `:warning`, `:note`.

Returns:

  • (Symbol)

    The type, which should been used to display the result. One of: ‘:error`, `:warning`, `:note`.



226
227
228
229
230
231
232
233
234
# File 'lib/validator.rb', line 226

def result_type
  applicable_results = results
  applicable_results = applicable_results.reject(&:public_only?) if ignore_public_only_results
  types              = applicable_results.map(&:type).uniq
  if    types.include?(:error)   then :error
  elsif types.include?(:warning) then :warning
  else  :note
  end
end

#results_messageObject



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
# File 'lib/validator.rb', line 98

def results_message
  message = ''
  results.each do |result|
    if result.platforms == [:ios]
      platform_message = '[iOS] '
    elsif result.platforms == [:osx]
      platform_message = '[OSX] '
    elsif result.platforms == [:watchos]
      platform_message = '[watchOS] '
    elsif result.platforms == [:tvos]
      platform_message = '[tvOS] '
    end

    subspecs_message = ''
    if result.is_a?(Result)
      subspecs = result.subspecs.uniq
      if subspecs.count > 2
        subspecs_message = '[' + subspecs[0..2].join(', ') + ', and more...] '
      elsif subspecs.count > 0
        subspecs_message = '[' + subspecs.join(',') + '] '
      end
    end

    case result.type
    when :error   then type = 'ERROR'
    when :warning then type = 'WARN'
    when :note    then type = 'NOTE'
    else raise "#{result.type}" end
    message << "    - #{type.ljust(5)} | #{platform_message}#{subspecs_message}#{result.attribute_name}: #{result.message}\n"
  end
  message << "\n"
end

#specSpecification

Returns the specification to lint.

Returns:

  • (Specification)

    the specification to lint.



43
44
45
# File 'lib/validator.rb', line 43

def spec
  @linter.spec
end

#used_swift_versionString

Returns A string representing the Swift version used during linting or nil, if Swift was not used.

Returns:

  • (String)

    A string representing the Swift version used during linting or nil, if Swift was not used.



274
275
276
# File 'lib/validator.rb', line 274

def used_swift_version
  swift_version if @installer.pod_targets.any?(&:uses_swift?)
end

#validateBool

Note:

This method shows immediately which pod is being processed and overrides the printed line once the result is known.

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

Returns:

  • (Bool)

    whether the specification passed validation.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/validator.rb', line 68

def validate
  @results = []

  # Replace default spec with a subspec if asked for
  a_spec = spec
  if spec && @only_subspec
    subspec_name = @only_subspec.start_with?(spec.root.name) ? @only_subspec : "#{spec.root.name}/#{@only_subspec}"
    a_spec = spec.subspec_by_name(subspec_name, true, true)
    @subspec_name = a_spec.name
  end

  UI.print " -> #{a_spec ? a_spec.name : file.basename}\r" unless config.silent?
  $stdout.flush

  perform_linting
  perform_extensive_analysis(a_spec) if a_spec && !quick

  UI.puts ' -> '.send(result_color) << (a_spec ? a_spec.to_s : file.basename.to_s)
  print_results
  validated?
end

#validated?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/validator.rb', line 219

def validated?
  result_type != :error && (result_type != :warning || allow_warnings)
end

#validation_dirPathname

Returns the temporary directory used by the linter.

Returns:

  • (Pathname)

    the temporary directory used by the linter.



248
249
250
# File 'lib/validator.rb', line 248

def validation_dir
  @validation_dir ||= Pathname(Dir.mktmpdir(['CocoaPods-Lint-', "-#{spec.name}"]))
end