class LicenseFinder::CocoaPods

def acknowledgements_path

def acknowledgements_path
  if !ENV['ACKNOWLEDGEMENTS_PATH'].nil?
    result = Dir[*ENV['ACKNOWLEDGEMENTS_PATH']].first
  else
    search_paths = ['Pods/Pods-acknowledgements.plist',
                    'Pods/Target Support Files/Pods/Pods-acknowledgements.plist',
                    'Pods/Target Support Files/Pods-*/Pods-*-acknowledgements.plist']
    result = Dir[*search_paths.map { |path| File.join(project_path, path) }].first
    raise "Found a Podfile but no Pods directory in #{project_path}. Try running pod install before running license_finder." if result.nil?
  end
  result
end

def current_packages

def current_packages
  podfile = YAML.load_file(lockfile_path)
  podfile['PODS'].map do |pod|
    pod = pod.keys.first if pod.is_a?(Hash)
    name, version = pod.scan(/(.*)\s\((.*)\)/).flatten
    CocoaPodsPackage.new(
      name,
      version,
      license_texts[name],
      logger: logger
    )
  end
end

def license_texts

def license_texts
  # package name => license text
  @license_texts ||= read_plist(acknowledgements_path)['PreferenceSpecifiers']
                     .each_with_object({}) { |hash, memo| memo[hash['Title']] = hash['FooterText'] }
end

def lockfile_path

def lockfile_path
  project_path.join('Podfile.lock')
end

def package_management_command

def package_management_command
  LicenseFinder::Platform.darwin? ? 'pod' : nil
end

def possible_package_paths

def possible_package_paths
  [project_path.join('Podfile')]
end

def read_plist(pathname)

def read_plist(pathname)
  out, err, status = Open3.capture3('plutil', '-convert', 'json', '-o', '-', pathname)
  raise "#{out}\n\n#{err}" unless status.success?
  JSON.parse(out)
end