Class: PodBuilder::Licenses

Inherits:
Object
  • Object
show all
Defined in:
lib/pod_builder/licenses.rb

Class Method Summary collapse

Class Method Details

.write(licenses, all_buildable_items) ⇒ Object



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
# File 'lib/pod_builder/licenses.rb', line 3

def self.write(licenses, all_buildable_items)
  puts "Writing licenses".yellow
  license_file_path = PodBuilder::project_path(Configuration.license_filename) + ".plist"

  current_licenses = []
  if File.exist?(license_file_path)
    plist = CFPropertyList::List.new(:file => license_file_path)
    dict = CFPropertyList.native_types(plist.value)  
    current_licenses = dict["PreferenceSpecifiers"]
  
    if current_licenses.count > 0 
      licenses_header = current_licenses.shift
      raise "\n\nUnexpected license found in header\n".red if licenses_header.has_key?("License")
    end
    if current_licenses.count > 0 
      license_footer = current_licenses.pop
      raise "\n\nUnexpected license found in footer\n".red if license_footer.has_key?("License")
    end
  end

  if licenses.count > 0
    licenses_header = licenses.shift
    raise "\n\nUnexpected license found in header\n".red if licenses_header.has_key?("License")
    license_footer = licenses.pop
    raise "\n\nUnexpected license found in footer\n".red if license_footer.has_key?("License")

    lincenses_titles = licenses.map { |x| x["Title"] }
    current_licenses.select! { |x| !lincenses_titles.include?(x["Title"]) }
  end

  licenses += current_licenses # merge with existing license
  licenses.uniq! { |x| x["Title"] }
  licenses.sort_by! { |x| x["Title"] }
  licenses.select! { |x| !Configuration.skip_licenses.include?(x["Title"]) }
  licenses.select! { |x| all_buildable_items.map(&:root_name).include?(x["Title"]) } # Remove items that are no longer included

  license_dict = {}
  license_dict["PreferenceSpecifiers"] = [licenses_header, licenses, license_footer].compact.flatten
  license_dict["StringsTable"] = "Acknowledgements"
  license_dict["Title"] = license_dict["StringsTable"]

  plist = CFPropertyList::List.new
  plist.value = CFPropertyList.guess(license_dict)
  plist.save(license_file_path, CFPropertyList::List::FORMAT_BINARY)

  if licenses.count > 0
    write_markdown(license_file_path)
  end
end