Class: Asca::REST::Provisioning::Profiles

Inherits:
Object
  • Object
show all
Defined in:
lib/asca/rest/provisioning/profiles.rb

Class Method Summary collapse

Class Method Details

.create_new_profile(options = {}) ⇒ Object

notion: bundle_id is not bundle identifier and device id is udid。They are the corresponding api id.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/asca/rest/provisioning/profiles.rb', line 44

def create_new_profile(options = {})
  if !options[:name]
    Asca::Tools::Asca::Tools::Log.error('No profile name specified')
    return false
  end
  if !options[:type]
    Asca::Tools::Asca::Tools::Log.error('No type specified')
    return false
  end
  if !options[:bundle_id]
    Asca::Tools::Asca::Tools::Log.error('No bundle id specfied')
    return false
  end
  if !options[:certificate_ids] || options[:certificate_ids].length == 0
    Asca::Tools::Asca::Tools::Log.error('No certificate id specified')
    return false
  end
    
  response = HTTP.auth('Bearer ' + Asca::Tools::Token.new_token).post(URI_PROFILES, :json => { "data" => {
    "type" => "profiles",
    "attributes" => {
        "name" => options[:name],
        "profileType" => options[:type],
    },
    "relationships" => {
      "bundleId" => {
        "data" => {
          "type" => "bundleIds",
          "id" => options[:bundle_id],
        }
      },
      "certificates" => {
        "data" => options[:certificate_ids].map { |certificate_id| { "type" => "certificates", "id" => certificate_id }}
      },
      "devices" => {
        "data" => options[:device_ids] ? options[:device_ids].map { |device_id| { "type" => "devices", "id" => device_id } } : nil
      },
    }
  }})
  if response.status.success?
      return true
  else
      puts response.body
      return false
  end      
end

.delete_profile(options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/asca/rest/provisioning/profiles.rb', line 91

def delete_profile(options = {})
  # query profile id
  response = HTTP.auth('Bearer ' + Asca::Tools::Token.new_token).get(URI_PROFILES, :params => { 'filter[name]' => options[:name] })
  if response.status.success?
    responseObj = JSON.parse(response.body)
    queried_profile_list = responseObj["data"]
    if queried_profile_list.length() > 0
      profile_id = queried_profile_list[0]["id"]
    end
  else
    Asca::Tools::Log.error(response.body)
    return
  end
  if !profile_id
    puts "No profile named #{options[:name]} found!"
    return
  end
    
  # delete profile
  response = HTTP.auth('Bearer ' + Asca::Tools::Token.new_token).delete(URI_PROFILES + "/#{profile_id}")
  if response.status.success?
    Asca::Tools::Log.info("Profile named #{options[:name]} deleted successfully!")
  else
    puts response.body
  end
  return response.status.success?
end

.download_profile(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/asca/rest/provisioning/profiles.rb', line 10

def download_profile(options = {})
  profile_name = options[:name] 
  out_put_dir = options[:out_put_dir]
  if !out_put_dir
    out_put_dir = Asca::Tools::Configuration.get_config('out_put_dir')
    if !out_put_dir
        puts "Please enter your out put dir:"
        out_put_dir = File.expand_path(gets.chomp)
        Asca::Tools::Configuration.update_config('out_put_dir', out_put_dir)
    end
  end
  response = HTTP.auth('Bearer ' + Asca::Tools::Token.new_token).get(URI_PROFILES, :params => { 'filter[name]' => profile_name })
  if response.status.success?
    profile_obj = JSON.parse(response.body)
    profile_content = profile_obj["data"][0]["attributes"]['profileContent']
    File.open(File.expand_path(profile_name + ".mobileprovision", out_put_dir), 'w') do |file|
      file.write(Base64.decode64(profile_content))
    end
  else
    Asca::Tools::Log.error(response.body)
  end
end

.install_profile(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/asca/rest/provisioning/profiles.rb', line 33

def install_profile(options = {})
  profile_name = options[:name]
  download_profile :name => profile_name, :out_put_dir => Asca::Tools::Configuration::CACHE_DIR
  profile_file_path = File.expand_path(profile_name + ".mobileprovision", Asca::Tools::Configuration::CACHE_DIR)
    
  # install profile
  FileUtils.cp(profile_file_path, File.expand_path('~/Library/MobileDevice/Provisioning Profiles'))
  Asca::Tools::Log.info("#{profile_name} installed successfully!")
end

.update_profile(options = {}) ⇒ Object

update profile. The new profile is almost the same as the old one, such as the same name, bundle id, certificate ids, with only exception that the new one always try to include all the currently reigstered devices.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/asca/rest/provisioning/profiles.rb', line 120

def update_profile(options = {}) 
  # query profile info
  response = HTTP.auth('Bearer ' + Asca::Tools::Token.new_token).get(URI_PROFILES, :params => { 'filter[name]' => options[:name] })
  if response.status.success?
    responseObj = JSON.parse(response.body)
    queried_profile_list = responseObj["data"]
    if queried_profile_list.length() > 0
      profile = queried_profile_list[0]
    end
  else
    Asca::Tools::Log.error(response.body)
    return
  end
  
  if !profile
    Asca::Tools::Log.error("No profile named #{options[:name]} found")
    return
  end
  # create new profile
  profile_type = profile["attributes"]["profileType"]
    
  # get bundle id
  response = HTTP.auth('Bearer ' + Asca::Tools::Token.new_token).get(profile["relationships"]["bundleId"]["links"]["self"])
  bundle_id = JSON.parse(response.body)["data"]["id"]
  response = HTTP.auth('Bearer ' + Asca::Tools::Token.new_token).get(profile["relationships"]["certificates"]["links"]["self"])
  certificate_ids = JSON.parse(response.body)["data"].map { |cer| cer["id"] }
    
  # get all device ids
  device_ids = Asca::REST::Provisioning::Devices.list_devices.map { |device|
    device["id"]
  }
    
  # delete old prifile
  delete_profile :name => options[:name]
  
  if profile_type.include? 'APP_STORE'
    create_new_profile :name => options[:name], :type => profile_type, :bundle_id => bundle_id, :certificate_ids => certificate_ids
  else
    create_new_profile :name => options[:name], :type => profile_type, :bundle_id => bundle_id, :device_ids => device_ids, :certificate_ids => certificate_ids
  end
  
  return true
end