Module: Pindo::XcodeCertHelper

Instance Method Summary collapse

Instance Method Details

#config_infoplist_cert(new_proj_name: nil, new_project_dir: nil, icloud_id: nil, group_id: nil, provisioning_info_array: nil) ⇒ Object



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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/pindo/module/cert/xcodecerthelper.rb', line 126

def config_infoplist_cert(new_proj_name:nil, new_project_dir:nil, icloud_id:nil, group_id:nil, provisioning_info_array:nil)

    new_proj_fullname = File.join(new_project_dir, new_proj_name) + ".xcodeproj"
    project_obj = Xcodeproj::Project.open(new_proj_fullname)
    entitlements_plist_path = File.join(new_project_dir, new_proj_name + ".entitlements")

        project_obj.targets.each do |target|

            if target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:framework]) || target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:dynamic_library]) || 
                target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:static_library]) || target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:bundle])
                next
            end

            temp_entitlements_file = target.build_configurations.first.build_settings['CODE_SIGN_ENTITLEMENTS']
            if !temp_entitlements_file.nil? && !temp_entitlements_file.empty?
                entitlements_plist_path = File.join(new_project_dir, temp_entitlements_file)

                if !File.exist?(entitlements_plist_path)
                  raise Informative, "Target: #{target.name.to_s} 找不到文件 #{entitlements_plist_path}"
                end

                if !group_id.nil?
                    modify_entitlements_plist(entitlements_plist_path:entitlements_plist_path, group_id:group_id)
                end

                if target.product_type.to_s.eql?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:application]) then
                    add_swark_entitlement(entitlements_plist_path:entitlements_plist_path, bundle_id_dict:provisioning_info_array)
                    if !icloud_id.nil?
                      modify_entitlements_plist(entitlements_plist_path:entitlements_plist_path, icloud_id:icloud_id)
                    end
                end

            end

            temp_info = target.build_configurations.first.build_settings['INFOPLIST_FILE']
            if !temp_info.nil? && !temp_info.empty?
                info_plist_path = File.join(new_project_dir, temp_info)
                if !File.exist?(info_plist_path)
                    raise Informative, "Missing Target #{target.name.to_s} Info.plist!!!  #{info_plist_path}  Modify Info.plist Error !!!"           
                end

                if target.product_type.to_s.eql?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:application]) then
                    if !icloud_id.nil?
                      modify_info_plist_icloud(plist_file_name:info_plist_path, icloud_id:icloud_id)
                    else
                      modify_info_plist_icloud(plist_file_name:info_plist_path, icloud_id:nil)
                    end
                end

            end

        end

end

#config_project_cert(new_proj_name: nil, new_project_dir: nil, cert_type: nil, team_id_vaule: nil, provisioning_info_array: nil) ⇒ Object



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
90
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
118
119
120
121
122
123
124
# File 'lib/pindo/module/cert/xcodecerthelper.rb', line 52

def config_project_cert(new_proj_name:nil, new_project_dir:nil, cert_type:nil, team_id_vaule:nil, provisioning_info_array:nil)


    new_proj_fullname = File.join(new_project_dir, new_proj_name) + ".xcodeproj"
    new_project_obj = Xcodeproj::Project.open(new_proj_fullname)

    new_project_obj.root_object.build_configuration_list.set_setting('CODE_SIGN_IDENTITY[sdk=iphoneos*]', "Apple Distribution")
    new_project_obj.root_object.build_configuration_list.set_setting('CODE_SIGN_IDENTITY*', "Apple Distribution")

    if cert_type == "development"
        new_project_obj.root_object.build_configuration_list.set_setting('CODE_SIGN_IDENTITY[sdk=iphoneos*]', "Apple Development")
        new_project_obj.root_object.build_configuration_list.set_setting('CODE_SIGN_IDENTITY*', "Apple Development")
    end

    new_project_obj.root_object.attributes['TargetAttributes'] = new_project_obj.root_object.attributes['TargetAttributes'] || {}
    target_atts_obj = new_project_obj.root_object.attributes['TargetAttributes']

    target_map = get_target_name_map

    new_project_obj.targets.each do |target|

        if target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:framework]) || target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:dynamic_library]) || 
            target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:static_library]) || target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:bundle])
            next
        end
        provisioning_info = nil

        if target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:application])
            bundle_id_map_key = target_map["MainTarget"]
            provisioning_info = provisioning_info_array.select { |s| s["type"].to_s.eql?(bundle_id_map_key.to_s) }.first
        else 

            target_map.each do |k, v|
                if target.name.to_s.end_with?(k)
                    bundle_id_map_key = v.to_s
                    provisioning_info = provisioning_info_array.select { |s| s["type"].to_s.eql?(bundle_id_map_key.to_s) }.first
                end
            end
        end

        # puts "=====1 target name: #{target.name.to_s}  bundle_id_map_key: #{bundle_id_map_key}  provisioning_info: #{provisioning_info}"

        if !provisioning_info.nil?
            target.build_configurations.each do |config|
                target_atts_obj[target.uuid] = target_atts_obj[target.uuid] || {}
                target_atts_obj[target.uuid]['DevelopmentTeam'] = team_id_vaule
                target_atts_obj[target.uuid]['ProvisioningStyle'] = "Manual"
                config.build_settings['DEVELOPMENT_TEAM'] = team_id_vaule
                config.build_settings['DEVELOPMENT_TEAM[sdk=iphoneos*]'] = team_id_vaule
                config.build_settings['CODE_SIGN_STYLE'] = "Manual"

                config.build_settings['CODE_SIGN_IDENTITY'] = "Apple Distribution"
                config.build_settings['CODE_SIGN_IDENTITY[sdk=iphoneos*]'] = "Apple Distribution"

                if cert_type == "development"
                    config.build_settings['CODE_SIGN_IDENTITY'] = "Apple Development"
                    config.build_settings['CODE_SIGN_IDENTITY[sdk=iphoneos*]'] = "Apple Development"
                end

                if !provisioning_info.nil?
                    config.build_settings['PRODUCT_BUNDLE_IDENTIFIER'] = provisioning_info["bundle_id"]
                    config.build_settings['PROVISIONING_PROFILE_SPECIFIER'] = provisioning_info['profile_name']
                    config.build_settings['PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]'] = provisioning_info['profile_name']
                    
                end
            end
        end
        

    end
    new_project_obj.save

end

#create_provisioning_info_array(build_type: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pindo/module/cert/xcodecerthelper.rb', line 27

def create_provisioning_info_array(build_type:nil)

  provisioning_info_array = []

  bundle_id_map = get_bundle_id_map
  
  bundle_id_map.each do |type, bundle_id_temp|
    provisioning_info = {}
    provisioning_info['type'] = type
    provisioning_info['bundle_id'] = bundle_id_temp
    name_temp = Match::Utils.environment_variable_name_profile_name(app_identifier: bundle_id_temp, type: build_type)
    provisioning_info['profile_name'] = ENV[name_temp]
    path_temp = Match::Utils.environment_variable_name_profile_path(app_identifier:bundle_id_temp,type: build_type)
    provisioning_info['profile_path'] = ENV[path_temp]
    signing_identity_key=Match::Utils.environment_variable_name_certificate_name(app_identifier: bundle_id_temp,type: build_type)
    provisioning_info["signing_identity"] = ENV[signing_identity_key]
    team_id_key =  Match::Utils.environment_variable_name_team_id(app_identifier: bundle_id_temp,type:build_type)
    provisioning_info["team_id"] = ENV[team_id_key]
    provisioning_info_array << provisioning_info
  end

    
    return provisioning_info_array
end

#create_upload_cert_info(apple_id: nil, cert_type: nil) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/pindo/module/cert/xcodecerthelper.rb', line 229

def create_upload_cert_info(apple_id:nil, cert_type:nil)

    cert_dest_dir = File.join(Dir.pwd, "cert")
    if !File.exist?(cert_dest_dir)
        FileUtils.mkdir_p(cert_dest_dir)
    end


    cert_git_url = pindo_single_config.deploy_cert_giturl
    if apple_id.eql?(pindo_single_config.demo_apple_id)
        cert_git_url = pindo_single_config.dev_cert_giturl
    end
    cert_reponame = cert_git_url.split("/").last.chomp(".git")
    certs_dir = getcode_to_dir(reponame:cert_reponame, remote_url:cert_git_url, path: pindo_single_config.pindo_dir, new_branch:apple_id)

    cert_git_dir = cert_type.downcase
    if !cert_type.downcase.include?("development")
        cert_git_dir = "distribution"
    end

    keys = Dir[File.join(certs_dir, "certs", cert_git_dir.to_s, "*.p12")]   
    decrypt_password = AESHelper.fetch_password(keychain_name:cert_git_url)
    output_dir = Dir.mktmpdir
    key_path = AESHelper.decrypt_specific_file(src_file: keys.first, password:decrypt_password, output_dir: output_dir)
    if key_path.nil? || key_path.empty? || !File.exist?(key_path)
        AESHelper.delete_password(keychain_name:cert_git_url)
        raise Informative, "证书解析失败,密码错误!"
    end

    FileUtils.copy(key_path, File.join(cert_dest_dir, "#{cert_type}.p12"))

end

#create_upload_provisioning_info(apple_id: nil, cert_type: nil, provisioning_info_array: nil) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/pindo/module/cert/xcodecerthelper.rb', line 262

def create_upload_provisioning_info(apple_id:nil, cert_type:nil, provisioning_info_array:nil)
    
    cert_dir = File.join(Dir.pwd, "cert")
    cert_json_file = File.join(cert_dir, "certs.json")

    if !File.exist?(cert_dir)
     FileUtils.mkdir_p(cert_dir)
    end

    cert_json = []
    begin
        cert_json = JSON.parse(File.read(cert_json_file)) if File.exist?(cert_json_file)
        cert_json = cert_json || []
    rescue StandardError => e
        cert_json = []
    end

    bundle_id = provisioning_info_array.select { |s| s["type"].to_s.eql?("bundle_id") }.first["bundle_id"]
    team_id_vaule = provisioning_info_array.first["team_id"]
    bundle_id_signing_identity = provisioning_info_array.first["signing_identity"]

     = {}
    select_result= cert_json.select { |x| x["account_name"].eql?(apple_id) }.first
    if select_result.nil?

     = {}
    else

     = select_result
    cert_json.delete_if { |x| x["account_name"].eql?(apple_id) }
    end

    ["account_name"] = apple_id
    ["team_id"] = team_id_vaule
    ["certs"] = ["certs"] || []

    cert_item = {}
    cert_item_result = ["certs"].select { |x| x["cert_id"].eql?(bundle_id_signing_identity)}.first
    if cert_item_result.nil?
    cert_item = {}
    else
    cert_item = cert_item_result
    ["certs"].delete_if { |x| x["cert_id"].eql?(bundle_id_signing_identity)}
    end

    cert_item["cert_id"] = cert_item["cert_id"] || bundle_id_signing_identity
    cert_item["password"] = "goodcert1"
    cert_item["cert_file"] = "#{cert_type}.p12"
    cert_item["cert_type"] = cert_type
    cert_item["cert_provisioning_group"] = cert_item["cert_provisioning_group"] || []



    cert_provisioning_group_item = {}
    provisioning_group_id = [cert_type, "group", bundle_id].join("_")

    provisioning_group_result = cert_item["cert_provisioning_group"].select { |x| x["provisioning_group_id"].eql?(provisioning_group_id)}.first
    if provisioning_group_result.nil?
    cert_provisioning_group_item = {}
    else
    cert_provisioning_group_item = provisioning_group_result
    cert_item["cert_provisioning_group"].delete_if { |x| x["provisioning_group_id"].eql?(provisioning_group_id)}
    end



    cert_provisioning_group_item["provisioning_group_id"] = provisioning_group_id
    cert_provisioning_group_item["provisioning_main_bundle_id"] = bundle_id
    cert_provisioning_group_item["provisioning_items"] = []

    group_id = [cert_type, "group", bundle_id].join("_")
    provisioning_info_array.each do |provisioning_info|
        bundle_id_temp = provisioning_info['bundle_id']
        provisioning_id = [cert_type, bundle_id_temp].join("_")
        
        cert_provisioning_group_item["provisioning_items"] << {
          "bundle_id" => bundle_id_temp,
          "provisioning_id" => provisioning_id,
          "group_id" => group_id,
          "provisioning_file" => provisioning_id + ".mobileprovision"
        }
        real_path = provisioning_info["profile_path"]
        FileUtils.cp_r(real_path, File.join(cert_dir, provisioning_id + ".mobileprovision"))

    end

    cert_item["cert_provisioning_group"] << cert_provisioning_group_item
    ["certs"] << cert_item
    cert_json << 
    File.open(cert_json_file, "w") do |f|
        f.write(JSON.pretty_generate(cert_json.compact))
    end

    return 
end

#get_target_name_mapObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pindo/module/cert/xcodecerthelper.rb', line 9

def get_target_name_map
    return {
        "MainTarget" => "bundle_id",
        "Content" => "bundle_id_pushcontent",
        "Service" => "bundle_id_pushservice",
        "Keyboard" => "bundle_id_keyboard",
        "iMessage" => "bundle_id_imessage",
        "Siri" => "bundle_id_siri",
        "SiriUI" => "bundle_id_siriui",
        "Widget" => "bundle_id_widget",
        "Extension" => "bundle_id_extension",
        "ExtensionAd" => "bundle_id_extensionad",
        "ExtensionPorn" => "bundle_id_extensionporn",
        "WatchApp" => "bundle_id_watchapp",
        "WatchAppComplication" => "bundle_id_watchapp_extension"
    }
end

#modify_entitlements_plist(entitlements_plist_path: nil, group_id: nil, icloud_id: nil) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/pindo/module/cert/xcodecerthelper.rb', line 181

def modify_entitlements_plist(entitlements_plist_path:nil, group_id:nil, icloud_id:nil)
    if File.exist?(entitlements_plist_path)

        entitlements_plist_dict = Xcodeproj::Plist.read_from_path(entitlements_plist_path)

        if !group_id.nil? && !entitlements_plist_dict['com.apple.security.application-groups'].nil?
            entitlements_plist_dict['com.apple.security.application-groups'] = [group_id]
        end

        if !icloud_id.nil?
            entitlements_plist_dict['com.apple.developer.icloud-container-identifiers'] = [icloud_id]
            entitlements_plist_dict['com.apple.developer.ubiquity-container-identifiers'] = [icloud_id]
        end

        Xcodeproj::Plist.write_to_path(entitlements_plist_dict, entitlements_plist_path)
    end
end

#modify_info_plist_icloud(plist_file_name: nil, icloud_id: nil) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/pindo/module/cert/xcodecerthelper.rb', line 200

def modify_info_plist_icloud(plist_file_name:nil, icloud_id:nil)
      if ! File.exist? (plist_file_name)
        raise Informative, "修改Info.list 路径错误!!! #{plist_file_name}"
      end
      if File.exist?(plist_file_name)
          info_plist_dict = Xcodeproj::Plist.read_from_path(plist_file_name)

          if !icloud_id.nil?

              temp_value = {}
              if !info_plist_dict['NSUbiquitousContainers'].nil? && info_plist_dict['NSUbiquitousContainers'].is_a?(Hash)
                info_plist_dict['NSUbiquitousContainers'].each do |key, value|
                  if key.include?("iCloud.")
                    temp_value = value
                    break;
                  end
                end
              end
              info_plist_dict['NSUbiquitousContainers'] = {}
              info_plist_dict['NSUbiquitousContainers'][icloud_id] = temp_value || {}
          else
              info_plist_dict['NSUbiquitousContainers'] = nil
          end

          Xcodeproj::Plist.write_to_path(info_plist_dict, plist_file_name)

      end
end