Module: CertStepper

Defined in:
lib/certstepper.rb

Defined Under Namespace

Classes: Cert

Class Method Summary collapse

Class Method Details

.copyToClipboard(content) ⇒ Object



242
243
244
# File 'lib/certstepper.rb', line 242

def self.copyToClipboard(content)
  system  ("echo #{content} | pbcopy")
end

.createDir(filePath) ⇒ Object



246
247
248
249
250
251
252
253
# File 'lib/certstepper.rb', line 246

def self.createDir(filePath)
   if !Dir.exist?  filePath
     Dir.mkdir filePath 
   else 
     FileUtils.rm_r filePath
     Dir.mkdir filePath 
   end
end

.createKeychain(de_path) ⇒ Object



263
264
265
266
# File 'lib/certstepper.rb', line 263

def self.createKeychain(de_path)
  @keychain_path = File.expand_path "#{de_path}/CertTempleContainor"
  system "security create-keychain -p 123456 #{@keychain_path}"
end

.dealCert(cert, de_path) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/certstepper.rb', line 268

def self.dealCert(cert,de_path)
  console_de_path = de_path.gsub /[\s]/ , "\\ "
  Dir.entries(de_path).each do |file_name|
    if file_name.end_with? '.cer' 
      #system "security add-trusted-cert -r unspecified -k 123456 #{File.expand_path('~')}/Downloads/ios_development.cer"
      system "security import #{console_de_path}/#{file_name} -k #{@keychain_path} -T `which codesign`"
      system "security export -k #{@keychain_path} -t certs -f pkcs12 -P 123 -o #{console_de_path}/#{cert.profile_name}.p12"
      system "security delete-keychain #{@keychain_path}"

    end 
  end
end

.dealCertByChrome(root_path, profile_name) ⇒ Object



282
283
284
285
286
287
# File 'lib/certstepper.rb', line 282

def self.dealCertByChrome(root_path , profile_name)
  system "security create-keychain -P 123456"
  system "security add-trusted-cert -r unspecified -k 123456 #{File.expand_path('~')}/Downloads/ios_development.cer"
  system "security export -k 123456 -t certs -f pkcs12 -o #{root_path}/#{profile_name}/cert.p12"
  system "security delete-keychain 123456"
end

.deleteUnusefulFile(de_path) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/certstepper.rb', line 174

def self.deleteUnusefulFile(de_path)
  console_de_path = de_path.gsub /[\s]/ , "\\ "
  Dir.entries(de_path).each do |file_name|
    if file_name.end_with? '.cer' 
      file_path = "#{de_path}/#{file_name}"
      system "rm #{console_de_path}/#{file_name}" 
      system "rm #{console_de_path}/#{File.basename(file_path,'.*')}.p12" 
      system "rm #{console_de_path}/#{File.basename(file_path,'.*')}.certSigningRequest" 
    end 
  end

end

.generateCertSuccessfully?(cert) ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/certstepper.rb', line 187

def self.generateCertSuccessfully?(cert)
  cert_path = @@root_path + "/#{cert.profile_name}"
  puts cert_path
  is_p12_exist = false 
  is_cer_exist = false
  is_provision_exist = false 
  if File.exists? cert_path 
    Dir.entries(cert_path).each do |file_name| 
      if file_name.end_with? ".p12"
        is_p12_exist = true
      elsif file_name.end_with? ".cer"
        is_cer_exist = true
      elsif file_name.end_with? ".mobileprovision"
        is_provision_exist = true
      end
    end
  end 

  return is_p12_exist && is_cer_exist && is_provision_exist
end

.getUserInputObject



255
256
257
258
259
260
261
# File 'lib/certstepper.rb', line 255

def self.getUserInput 
      begin
        return $stdin.gets
       rescue  => e
         
      end
end

.logMessage(message) ⇒ Object



290
291
292
293
294
295
# File 'lib/certstepper.rb', line 290

def self.logMessage(message)
  puts message
  #file = File.open("#{File.expand_path('~')}/certstepper.log", "w+")
  #file.write message
  #file.close
end

.parseData(start_path) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/certstepper.rb', line 208

def self.parseData(start_path)
  @@certs = Array.new
  index = 0
	file_name = start_path
  @@root_path = Pathname.new(file_name).parent.to_s
  @@file_path 
  new_cert = nil
 file = File.open(file_name , "r")
  line_array = file.readlines
  file_content= line_array.join
  file_content = file_content.gsub /[\r]/,"\n"
  file_content.each_line do |line|
  	 cert_prop_index = index%3
     line_content = line.gsub /[\s\n\t\r]/ ,""

     if  line_content !=nil && !line_content.empty?
        case cert_prop_index
        when 0
        	new_cert = Cert.new
        	@@certs << new_cert
        	new_cert.email = line_content.strip
        when 1
        	new_cert.password = line_content.strip
        when 2
        	new_cert.profile_id = line_content.strip
          new_cert.profile_name = line_content
        end
        index+=1
     end 
  end
 file.close

end

.startStepObject



34
35
36
37
38
39
40
41
42
43
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/certstepper.rb', line 34

def self.startStep
 #    if ARGV.length==0 || ARGV == nil
 #      puts "error: Parameter does not match,there is not any parameter"
 #      return nil
 #    end
  
     #self.logMessage("#{ARGV}\n")


     begin
       if ARGV[0].start_with? "chrome-extension:"
         finished = false
         while !finished do 
           str = self.getUserInput[1..-1]
           str =str.strip_control_characters

           self.logMessage("#{str}\n")


           self.logMessage "start parse #{str.class}\n"
           my_hash = JSON.parse str
           self.logMessage("end parse#{my_hash}\n")

           start_path = my_hash["start_path"] 
           profile_name = my_hash["profile_name"]
           if start_path 
             self.logMessage("#start work start_path:#{start_path} : #{profile_name} \n")
             self.startWorkByChromeExtension start_path , profile_name
             finished = true
           end
         end
       else 

        check_xcode_select = `which xcode-select`.strip
        if !File.exists? check_xcode_select
          `xcode-select --install`
        end


         @options = {}
         option_parser = OptionParser.new do |opts|
           opts.banner = "certstepper File_Path [Options]命令行生成证书和配置文件!\n文件的内容:三行一个循环,依次是开发者账号、密码、唯一标识(bundle identifier)"
           @options[:cert_type] = ""
           @options[:profile_type] = "--adhoc" 
           @options[:force] = false

           opts.on('-t cert_type','--type cert_type','生成证书和配置文件的类型 development adhoc distribution') do |value|
             if value.start_with? 'ad' 
               @options[:cert_type] = "--adhoc"
               @options[:profile_type] = "" 
             elsif value.start_with? 'dev' 
               @options[:cert_type] = "--development"
               @options[:profile_type] = "--development" 
             elsif value.start_with? 'dis'
               @options[:cert_type] = ""
               @options[:profile_type] = "" 
             end
           end

          opts.on('-f', '--force', '强制重新获取证书,默认生成成功后不再重新获取') do 
            @options[:force] = true
          end
         end.parse!

         self.parseData ARGV[0]
         self.startWork
       end

     rescue => e 
       self.logMessage e.backtrace 
     end
     

end

.startWorkObject



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
163
164
165
166
167
168
169
170
171
172
# File 'lib/certstepper.rb', line 123

def self.startWork
      puts "总共要生成#{@@certs.length}个证书" 
      console_root_path = @@root_path.gsub /[\s]/ , "\\ "
      open_dir ="open #{console_root_path}"
      system("open #{console_root_path}")
      puts "Apple Cert Create Stepper\n"

      failed_cert_array = []

      @@certs.each do |cert|
         puts "----------- begin  #{cert.profile_id} Cert ----------- \n"
          
         if !generateCertSuccessfully?(cert) || @options[:force] 
              
           cert_path = console_root_path + "/#{cert.profile_name}"
           createDir cert_path

           keychain_entry = CredentialsManager::AccountManager.new(user:cert.email , password: cert.password)
           keychain_entry.add_to_keychain


           createKeychain cert_path
           system "cert -u #{cert.email} -o #{cert_path}  #{@options[:cert_type]}"
           dealCert cert , cert_path
            

           system "produce -u #{cert.email} -a #{cert.profile_id} --app_name #{cert.profile_name} --skip_itc"
           system "sigh -a #{cert.profile_id} -u #{cert.email} -o #{cert_path} #{@options[:profile_type]}"
           #system "sigh -a #{cert.profile_id} -u #{cert.email} -o #{cert_path} --adhoc"
           
           
           if !generateCertSuccessfully? cert
             failed_cert_array << cert.email
           end

           deleteUnusefulFile cert_path

        end 

        
        if failed_cert_array.length > 0 
          failed_content = "以下账号的证书和配置文件创建失败:\n" + failed_cert_array.join("\n") + "请重新尝试执行命令,如果多次不成功,请检查账号密码的正确性!"
          puts failed_content.colorize(:red) 
        else
          puts "证书和配置文件全部创建成功!".colorize(:green)
        end

      end 

end

.startWorkByChromeExtension(root_path, profile_name) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/certstepper.rb', line 109

def self.startWorkByChromeExtension(root_path,profile_name)
  console_root_path = root_path.gsub /[\s]/ , "\\ "
  system("open #{console_root_path}")
  self.createDir root_path + "/#{profile_name}"
  #self.dealCertByChrome root_path , profile_name
  mobileprovision_name = "#{profile_name}.mobileprovision"
  source_file = "#{File.expand_path('~')}/Downloads/#{mobileprovision_name}" 
  dest_file = "#{root_path}/#{profile_name}/#{mobileprovision_name}"
  if File.exist? source_file
     FileUtils.mv source_file , dest_file 
  end

end