Class: Pod::Command::Whitelist

Inherits:
Pod::Command show all
Defined in:
lib/cocoapods-whitelist/command/whitelist.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Whitelist

Returns a new instance of Whitelist.



33
34
35
36
37
38
39
# File 'lib/cocoapods-whitelist/command/whitelist.rb', line 33

def initialize(argv)
  @whitelist_url = argv ? argv.option('config', DEFAULT_WHITELIST_URL) : DEFAULT_WHITELIST_URL
  @pospec_path = argv ? argv.option('podspec') : nil
  @fail_on_error = argv ? argv.flag?('fail-on-error') : false
  @failure = false
  super
end

Class Method Details

.optionsObject



26
27
28
29
30
31
# File 'lib/cocoapods-whitelist/command/whitelist.rb', line 26

def self.options
  [ ['--config=CONFIG', 'Config file or URL for the blacklist'],
    ['--podspec=PODSPEC', 'Podspec file to be lint'],
    ['--fail-on-error', 'Raise an exception in case of error']
  ].concat(super)
end

Instance Method Details

#get_podspec_specificationsObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cocoapods-whitelist/command/whitelist.rb', line 129

def get_podspec_specifications
  if @pospec_path
    return [Pod::Specification.from_file(@pospec_path)]
  end

  # Search .podspec in current directory
  podspecs = Dir.glob("./*.podspec")

  if podspecs.count == 0
    # Search .podspec in parent directory.
    # Some projects has Podfile into a subdirectory ("Example"), and run "pod install" from there.
    podspecs = Dir.glob("../*.podspec")
  end

  return podspecs.map { |path|  Pod::Specification.from_file(path) }
end

#get_whitelistObject



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cocoapods-whitelist/command/whitelist.rb', line 110

def get_whitelist
  begin
    open(@whitelist_url) { |io|
      buffer = io.read
      parse_whitelist(buffer)
    }
  rescue OpenURI::HTTPError => e
    status = e.io.status.join(' ')
    raise "Failed to fetch whitelist from '#{@whitelist_url}'.\n Error: #{status}"
  end
end

#parse_whitelist(raw_whitelist) ⇒ Object



122
123
124
125
126
127
# File 'lib/cocoapods-whitelist/command/whitelist.rb', line 122

def parse_whitelist(raw_whitelist)
  json = JSON.parse(raw_whitelist)
  return json["whitelist"].map { |dependencyJson|
    AllowedDependency.new(dependencyJson["name"], dependencyJson["version"])
  }
end

#runObject



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

def run
  whitelist = get_whitelist
  specifications = get_podspec_specifications

  if specifications.empty?
      UI.puts "No Podspec found".yellow
      return
  end

  specifications.map do |specification|
    validate_dependencies(JSON.parse(specification.to_json), whitelist)
  end

  show_result_message
end

#show_error_message(message) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/cocoapods-whitelist/command/whitelist.rb', line 146

def show_error_message(message)
  if @fail_on_error
    UI.puts message.red
  else
    UI.puts message.yellow
  end
end

#show_result_messageObject



61
62
63
64
65
66
67
68
69
# File 'lib/cocoapods-whitelist/command/whitelist.rb', line 61

def show_result_message
  return unless @failure
  message = "Please check your dependencies.\nYou can see the allowed dependencies at #{@whitelist_url}"
  if @fail_on_error
    raise Informative.new(message)
  else
    UI.puts message.yellow
  end
end

#validate!Object



41
42
43
# File 'lib/cocoapods-whitelist/command/whitelist.rb', line 41

def validate!
  help! "A whitelist file or URL is needed." unless @whitelist_url
end

#validate_dependencies(podspec, whitelist, parentName = nil) ⇒ Object

Checks the dependencies the project contains are in the whitelist



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
# File 'lib/cocoapods-whitelist/command/whitelist.rb', line 72

def validate_dependencies(podspec, whitelist, parentName = nil)
  pod_name = parentName ? "#{parentName}/#{podspec['name']}" : podspec['name']
  UI.puts "Verifying dependencies in #{pod_name}"

  dependencies = podspec["dependencies"] ? podspec["dependencies"] : []
  not_allowed = []

  dependencies.each do |name, versions|
    # Skip subspec dependency
    next if parentName && name.start_with?("#{parentName}/")

    allowedDependency = whitelist.select { |item|
       name == item.name && (versions.empty? || !item.version || versions.grep(/#{item.version}/).any?)
    }

    if allowedDependency.empty?
      not_allowed.push("#{name} (#{versions.join(", ")})")
      next
    end
  end

  if not_allowed.any?
    severity = @fail_on_error ? "Error" : "Warning"
    show_error_message(" #{severity}: Found dependencies not allowed:")
    not_allowed.each {|dependency| show_error_message("  - #{dependency}")}
    @failure = true
  else
    UI.puts " OK".green
  end

  # Validate subspecs dependencies
  if podspec["subspecs"]
    podspec["subspecs"].each do |subspec|
      validate_dependencies(subspec, whitelist, pod_name)
    end
  end
end