Module: Fastlane::Helper::SyncDevicesHelper::DevicesFile

Defined in:
lib/fastlane/plugin/sync_devices/helper/sync_devices_helper/devices_file.rb

Overview

Collection of methods that manipulates TSV or XML devices file.

Constant Summary collapse

MAX_DEVICE_NAME_LENGTH =

Maximum length of a device name that is permitted by Apple Developer Portal.

Returns:

  • (Integer)
50

Class Method Summary collapse

Class Method Details

.dump(devices, path, format: :tsv) ⇒ void

This method returns an undefined value.

Dumps devices to devices file specified by path. This method delegates to either of dump_tsv or dump_plist depending on format.

Parameters:

  • devices (Array<Spaceship::ConnectAPI::Device>)

    device objects to dump

  • path (String)

    path to the output file

  • format (:tsv, :plist) (defaults to: :tsv)

    output format



78
79
80
81
82
83
84
85
86
87
# File 'lib/fastlane/plugin/sync_devices/helper/sync_devices_helper/devices_file.rb', line 78

def self.dump(devices, path, format: :tsv)
  case format
  when :tsv
    dump_tsv(devices, path)
  when :plist
    dump_plist(devices, path)
  else
    raise "Unsupported format '#{format}'."
  end
end

.dump_plist(devices, path) ⇒ void

This method returns an undefined value.

Parameters:

  • devices (Array<Spaceship::ConnectAPI::Device>)

    device objects to dump

  • path (String)

    path to the output file



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/fastlane/plugin/sync_devices/helper/sync_devices_helper/devices_file.rb', line 106

def self.dump_plist(devices, path)
  require 'cfpropertylist'

  plist = CFPropertyList::List.new
  plist.value = CFPropertyList.guess(
    {
      'Device UDIDs' => devices.map do |device|
        {
          deviceIdentifier: device.udid,
          deviceName: device.name,
          devicePlatform: device.platform.downcase
        }
      end
    }
  )
  plist.save(path, CFPropertyList::List::FORMAT_XML)
end

.dump_tsv(devices, path) ⇒ void

This method returns an undefined value.

Parameters:

  • devices (Array<Spaceship::ConnectAPI::Device>)

    device objects to dump

  • path (String)

    path to the output file



92
93
94
95
96
97
98
99
100
101
# File 'lib/fastlane/plugin/sync_devices/helper/sync_devices_helper/devices_file.rb', line 92

def self.dump_tsv(devices, path)
  require 'csv'

  CSV.open(path, 'w', col_sep: "\t", headers: true, write_headers: true) do |csv|
    csv << HEADERS
    devices.each do |device|
      csv << [device.udid, device.name, device.platform]
    end
  end
end

.load(path) ⇒ Array<Spaceship::ConnectAPI::Device>

Loads a devices file and parse it as an array of devices. If the file extension is one of .deviceids, .plist and .xml, this method delegates to load_plist, otherwise to load_tsv.

Parameters:

  • path (String)

    path to the output file

Returns:

  • (Array<Spaceship::ConnectAPI::Device>)


16
17
18
19
20
# File 'lib/fastlane/plugin/sync_devices/helper/sync_devices_helper/devices_file.rb', line 16

def self.load(path)
  return load_plist(path) if %w[.deviceids .plist .xml].include?(File.extname(path))

  load_tsv(path)
end

.load_plist(path) ⇒ Array<Spaceship::ConnectAPI::Device>

Parameters:

  • path (String)

Returns:

  • (Array<Spaceship::ConnectAPI::Device>)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fastlane/plugin/sync_devices/helper/sync_devices_helper/devices_file.rb', line 49

def self.load_plist(path)
  require 'cfpropertylist'
  require 'spaceship/connect_api'

  plist = CFPropertyList::List.new(file: path)
  items = CFPropertyList.native_types(plist.value)['Device UDIDs']
  devices = items.map.with_index do |item, index|
    validate_dict_item(item, index, path)
    Spaceship::ConnectAPI::Device.new(
      nil,
      {
        name: item['deviceName'],
        udid: item['deviceIdentifier'],
        platform: parse_platform(item['devicePlatform'], path),
        status: Spaceship::ConnectAPI::Device::Status::ENABLED
      }
    )
  end
  validate_devices(devices, path)
  devices
end

.load_tsv(path) ⇒ Array<Spaceship::ConnectAPI::Device>

Parameters:

  • path (String)

Returns:

  • (Array<Spaceship::ConnectAPI::Device>)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fastlane/plugin/sync_devices/helper/sync_devices_helper/devices_file.rb', line 24

def self.load_tsv(path)
  require 'csv'
  require 'spaceship/connect_api'

  table = CSV.read(path, headers: true, col_sep: "\t")
  validate_headers(table.headers, path)

  devices = table.map.with_index(2) do |row, line_number|
    validate_row(row, path, line_number)
    Spaceship::ConnectAPI::Device.new(
      nil,
      {
        name: row['Device Name'],
        udid: row['Device ID'],
        platform: parse_platform(row['Device Platform'], path),
        status: Spaceship::ConnectAPI::Device::Status::ENABLED
      }
    )
  end
  validate_devices(devices, path)
  devices
end