Class: Luna::Binary::Uploader::SingleUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/luna/binary/uploader.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(podspecName, gitUrl, gitNode, binaryPath) ⇒ SingleUploader

Returns a new instance of SingleUploader.



23
24
25
26
27
28
29
30
# File 'lib/luna/binary/uploader.rb', line 23

def initialize(podspecName, gitUrl, gitNode, binaryPath)
  @podspecName = podspecName
  @gitUrl = gitUrl
  @gitNode = gitNode
  @binaryPath = binaryPath
  @rootName = Common.instance.tempLunaUploaderPath + "/temp/#{podspecName}/"
  validate!
end

Instance Attribute Details

#binaryPathObject (readonly)

Returns the value of attribute binaryPath.



17
18
19
# File 'lib/luna/binary/uploader.rb', line 17

def binaryPath
  @binaryPath
end

#gitNodeObject (readonly)

Returns the value of attribute gitNode.



16
17
18
# File 'lib/luna/binary/uploader.rb', line 16

def gitNode
  @gitNode
end

#gitUrlObject (readonly)

‘2.git地址 3.git节点 5.二进制缓存地址



15
16
17
# File 'lib/luna/binary/uploader.rb', line 15

def gitUrl
  @gitUrl
end

#local_pathObject

Returns the value of attribute local_path.



21
22
23
# File 'lib/luna/binary/uploader.rb', line 21

def local_path
  @local_path
end

#podspecNameObject (readonly)

Returns the value of attribute podspecName.



19
20
21
# File 'lib/luna/binary/uploader.rb', line 19

def podspecName
  @podspecName
end

#rootNameObject (readonly)

Returns the value of attribute rootName.



18
19
20
# File 'lib/luna/binary/uploader.rb', line 18

def rootName
  @rootName
end

#specificationObject

Returns the value of attribute specification.



20
21
22
# File 'lib/luna/binary/uploader.rb', line 20

def specification
  @specification
end

Instance Method Details

#binary_json_dirObject



372
373
374
# File 'lib/luna/binary/uploader.rb', line 372

def binary_json_dir 
  frameworkPath = rootName + "bin-json"
end

#binary_sourceObject



339
340
341
# File 'lib/luna/binary/uploader.rb', line 339

def binary_source
  { http: format(Common.instance.binary_download_url, specification.root.name, specification.version), type: Common.instance.download_file_type }
end

#clearTempFileObject



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/luna/binary/uploader.rb', line 428

def clearTempFile
  p rootName
  begin
    Common.instance.deleteDirectory("#{rootName}bin-json") 
  rescue => exception
    
  ensure
    begin
      Common.instance.deleteDirectory("#{rootName}#{podspecName}.zip")
    rescue => exception
      
    ensure
      begin
        Common.instance.deleteDirectory("#{rootName}#{podspecName}")
      rescue => exception
        
      ensure
        
      end
    end
  end          
end

#command(c) ⇒ Object



121
122
123
# File 'lib/luna/binary/uploader.rb', line 121

def command(c)
  return Common.instance.command(c)
end

#copyDependcyFramework(path, moduleName) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/luna/binary/uploader.rb', line 320

def copyDependcyFramework(path, moduleName)
  frameworkPath = rootName + '/**/' + path
  p "copyDependcyFramework " + frameworkPath
  frameworkpaths = Dir.glob(frameworkPath)
  if frameworkpaths.length 
    command = "mkdir -p #{rootName}#{moduleName}/depencyFrameworks;" 
    system command;
    p command;
  end
  frameworkDepencyFrameworks = []
  frameworkpaths.each { |item|
    system "cp -r #{item} #{rootName}#{moduleName}/depencyFrameworks"
    p "cp -r #{item} #{rootName}#{moduleName}/depencyFrameworks"
    p "#{moduleName}/depencyFrameworks/#{File.basename(item)}"
    frameworkDepencyFrameworks = frameworkDepencyFrameworks + ["#{moduleName}/depencyFrameworks/#{File.basename(item)}"]
  }
  return frameworkDepencyFrameworks
end

#createFrameworkSpecObject



197
198
199
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
228
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
261
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
# File 'lib/luna/binary/uploader.rb', line 197

def createFrameworkSpec
  @spec = specification.dup
  moduleName = specification.root.name
  # Source Location
  @spec.source = binary_source
  # Source Code
  @spec.source_files = source_files("/Headers/**/*")
  @spec.public_header_files = source_files("/Headers/**/*")
  
  findFramework
  
  bundlePaths = findBundle
  spec_hash = @spec.to_hash
  framework_contents = [moduleFrameworkName]
  puts "framework_contents : #{framework_contents}"
  subspecsMapper = {
    'vendored_frameworks' => framework_contents
  }
  spec_hash['vendored_frameworks'] = framework_contents

  libA_contents = []
  if spec_hash["vendored_library"] != nil 
    libA_contents = libA_contents + copyDependcyFramework(spec_hash["vendored_library"], moduleName)  
  end

  if spec_hash["vendored_libraries"] != nil
    if spec_hash["vendored_libraries"].respond_to?(:each)
      spec_hash["vendored_libraries"].each { |framework|
        libA_contents = libA_contents + copyDependcyFramework(framework, moduleName)
      }
    else
      libA_contents = libA_contents + copyDependcyFramework(spec_hash["vendored_libraries"], moduleName)
    end
  end

  if libA_contents.length > 0 
    spec_hash['vendored_libraries'] = libA_contents
    subspecsMapper['vendored_libraries'] = libA_contents
  else
    spec_hash.delete('vendored_libraries')
  end

  
  spec_hash.delete('resource_bundles')
  spec_hash.delete('resources')
  spec_hash.delete('resource_bundle')
  spec_hash.delete('resource')
  if bundlePaths.length > 0
    spec_hash['resources'] = ["#{moduleName}/#{specification.root.name}.bundle"]
    subspecsMapper['resources'] = ["#{moduleName}/#{specification.root.name}.bundle"]
  end

  spec_hash.delete('exclude_files')
  spec_hash.delete('private_header_files')
  spec_hash.delete('preserve_paths')
  spec_hash.delete('pod_target_xcconfig')
  platforms = spec_hash['platforms']
  selected_platforms = platforms.select { |k, _v| platforms.include?(k) }
  spec_hash['platforms'] = selected_platforms.empty? ? platforms : selected_platforms
  if spec_hash['subspecs'] != nil
    tempSpec = []
    spec_hash['subspecs'].each { |item|
      mapper = item.to_hash
      mapper.delete('private_header_files')
      mapper.delete('exclude_files')
      mapper.delete('pod_target_xcconfig')
      mapper["name"] = item["name"]
      mapper["source_files"] = source_files("/Headers/**/*")
      mapper["public_header_files"] = source_files("/Headers/**/*") 
      
      framework_contents = [moduleFrameworkName]
      libA_contents = []
      if mapper["vendored_framework"] != nil 
        framework_contents = framework_contents + copyDependcyFramework(mapper["vendored_framework"], moduleName)
      end

      
      if mapper["vendored_frameworks"] != nil 
        if mapper["vendored_frameworks"].respond_to?(:each)
          mapper["vendored_frameworks"].each { |framework|
            framework_contents = framework_contents + copyDependcyFramework(framework, moduleName)
          }
        else
          framework_contents = framework_contents + copyDependcyFramework(mapper["vendored_frameworks"], moduleName)
        end
       
      end
      
      if mapper["vendored_library"] != nil 
        libA_contents = libA_contents + copyDependcyFramework(mapper["vendored_library"], moduleName)  
      end

      if mapper["vendored_libraries"] != nil
        if mapper["vendored_libraries"].respond_to?(:each)
          mapper["vendored_libraries"].each { |framework|
            libA_contents = libA_contents + copyDependcyFramework(framework, moduleName)
          }
        else
          libA_contents = libA_contents + copyDependcyFramework(mapper["vendored_libraries"], moduleName)
        end
      end

      if framework_contents.length > 0
        mapper["vendored_frameworks"] = framework_contents
      end

      if libA_contents.length > 0
        mapper["vendored_libraries"] = libA_contents
      end

      if subspecsMapper["resources"] != nil 
        mapper["resources"] = subspecsMapper["resources"]
      end

      
      tempSpec << mapper
    }
    spec_hash["subspecs"] = tempSpec
  end
  @spec1 = Pod::Specification.from_hash(spec_hash)
  @spec1
end

#createSpec(spec) ⇒ Object



363
364
365
366
367
368
369
370
# File 'lib/luna/binary/uploader.rb', line 363

def createSpec(spec)
  Pod::UI.message '生成二进制 podspec 内容: '
  spec.to_pretty_json.split("\n").each do |text|
    Pod::UI.message text
  end
  
  spec
end

#download_gitObject



117
118
119
# File 'lib/luna/binary/uploader.rb', line 117

def download_git
  command("git clone #{gitUrl} #{@rootName}temp; cd #{@rootName}temp; git fetch --all;git stash; git checkout #{gitNode};") if gitUrl && gitUrl.length > 0
end

#filenameObject



376
377
378
# File 'lib/luna/binary/uploader.rb', line 376

def filename
  @filename ||= "#{binary_json_dir}/#{specification.root.name}.podspec.json"
end

#findBundleObject



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/luna/binary/uploader.rb', line 185

def findBundle
  bundlePaths = Dir.glob("#{binaryPath}/**/#{specification.root.name}.bundle")
  if bundlePaths.length > 1
    # raise "findBundle:#{bundlePaths},不知道取哪个" 
    puts "findBundle:#{bundlePaths},不知道取哪个,做合并"
  end
  bundlePaths.each { |item|
    system "cp -r #{item} #{moduleDirPath}"
  }
  return bundlePaths
end

#findFrameworkObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/luna/binary/uploader.rb', line 166

def findFramework
  frameworkPaths = Dir.glob("#{binaryPath}/**/#{specification.root.name.sub('-', '_')}.framework")
  if frameworkPaths.length > 1
    raise "findFramework:#{frameworkPaths},不知道取哪个" 
  end

  if frameworkPaths.length == 0
    raise "findFramework:#{frameworkPaths},没有" 
  end

  frameworkPaths.each { |item|
    command = "mkdir -p #{moduleDirPath}; cp -r #{item} #{moduleDirPath}"
    system command
    p command
    
  }
  return frameworkPaths
end

#isHasFrameworkInServiceObject



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/luna/binary/uploader.rb', line 402

def isHasFrameworkInService
  command = "curl #{format(Common.instance.binary_download_url, specification.root.name, specification.version).sub("#{Common.instance.download_file_type}","")}"
  p command
  result = %x(#{command})
  resultHash = JSON.parse(result)
  isHasFrameWork = false
  if resultHash[specification.root.name] != nil 
    p "版本号#{resultHash[specification.root.name]}" 
    resultHash[specification.root.name].each { |item|
      if item == specification.version.version
        isHasFrameWork = true  
      end
    }
  end
  return isHasFrameWork
end

#isHasSpecInRepoObject



385
386
387
388
389
390
391
# File 'lib/luna/binary/uploader.rb', line 385

def isHasSpecInRepo
  moduleName = specification.root.name
  tag = specification.version
  p "#{repoPath}/#{moduleName}/#{tag}/*.podspec.json"
  paths = Dir.glob("#{repoPath}/#{moduleName}/#{tag}/**/*.podspec.json")
  return paths.length != 0
end

#isLocalHasFrameworkObject



153
154
155
# File 'lib/luna/binary/uploader.rb', line 153

def isLocalHasFramework
  return Dir.glob("#{rootName}#{moduleFrameworkName}").length > 0
end

#isLocalHasPodSpecObject



157
158
159
# File 'lib/luna/binary/uploader.rb', line 157

def isLocalHasPodSpec
  return Dir.glob("#{filename}").length > 0
end

#localSpecUpVersionObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/luna/binary/uploader.rb', line 87

def localSpecUpVersion
  nowTime = Time.now
  timeStamp = "#{nowTime.year}-#{nowTime.month}-#{nowTime.day}"
  specification.version = Util.writeVersionUp(local_path, specification.version)
  branchName = "lbu-#{timeStamp}"
  command("git checkout -b #{branchName}")
  # 保证最新节点防止push不上去
  command("git pull origin #{branchName}")
  command("git add #{local_path};")
  command("git commit -m 'Mod: 修改版本号为:#{specification.version.to_s} by LBU';")
  command("git push -f origin #{branchName}")
end

#moduleDirPathObject



149
150
151
# File 'lib/luna/binary/uploader.rb', line 149

def moduleDirPath
  return rootName + (specification.root.name)
end

#moduleFrameworkNameObject



161
162
163
164
# File 'lib/luna/binary/uploader.rb', line 161

def moduleFrameworkName
  moduleName = specification.root.name
  return "#{moduleName}/#{moduleName.sub('-', '_')}.framework"
end

#printPreUploadCommandObject



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/luna/binary/uploader.rb', line 134

def printPreUploadCommand
  if isLocalHasFramework && isLocalHasPodSpec
    if local_path
      puts "lbu single #{podspecName} #{local_path} #{binaryPath}".green  
    elsif gitUrl && gitUrl.length == 0
      puts "lbu single #{podspecName} #{specification.version} #{binaryPath}" .green   
    else
      puts "lbu single #{podspecName} #{gitUrl} #{gitNode} #{binaryPath}".green  
    end
  else
    raise "#{specification.name} #{specification.version} repo或framework缺失"
  end
end

#pushObject



125
126
127
128
129
130
131
132
# File 'lib/luna/binary/uploader.rb', line 125

def push
  if isLocalHasFramework && isLocalHasPodSpec 
    pushRepo
    uploadFramework
  else
    raise "#{specification.name} #{specification.version} repo或framework缺失"
  end
end

#pushRepoObject



393
394
395
396
397
398
399
400
# File 'lib/luna/binary/uploader.rb', line 393

def pushRepo
  moduleName = specification.root.name
  tag = specification.version
  targetDirname = "#{repoPath}/#{moduleName}/#{tag}"
  command = "cd #{repoPath};git stash;git checkout master;git pull origin master; mkdir -p #{targetDirname}; cp -r #{filename} #{targetDirname};cd #{repoPath};git add .; git commit -m 'MOD::#{moduleName}-#{tag} by LBU';git push -f origin master;"
  p command
  system command
end

#refresh_specification_workObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/luna/binary/uploader.rb', line 100

def refresh_specification_work
  begin
    clearTempFile
  rescue => exception
  
  ensure
    download_git
    
    if isHasSpecInRepo == true && isHasFrameworkInService == true
      @spec = createFrameworkSpec
      write_spec_file(@spec)
    else
      raise "repo or 二进制服务 不存在 #{specification.name} #{specification.version}"
    end
  end
end

#repoPathObject



381
382
383
# File 'lib/luna/binary/uploader.rb', line 381

def repoPath
  return Common.instance.repoPath
end

#source_files(suffix) ⇒ Object



343
344
345
346
# File 'lib/luna/binary/uploader.rb', line 343

def source_files(suffix)
  moduleName = specification.root.name
  ["#{moduleName}/#{moduleName.sub('-', '_')}.framework" + suffix]
end

#specificationWorkObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/luna/binary/uploader.rb', line 67

def specificationWork
  begin
    clearTempFile
  rescue => exception
  
  ensure
    download_git

    if local_path != nil
      localSpecUpVersion
    elsif isHasSpecInRepo == true || isHasFrameworkInService == true
      raise "已存在repo or 二进制服务 #{specification.name} #{specification.version}"
    end


    @spec = createFrameworkSpec
    write_spec_file(@spec)
  end
end

#uploadObject



39
40
41
# File 'lib/luna/binary/uploader.rb', line 39

def upload
  push
end

#uploadFrameworkObject



419
420
421
422
423
424
425
426
# File 'lib/luna/binary/uploader.rb', line 419

def uploadFramework
  zipCommand = "cd #{rootName};zip -r #{specification.root.name}.zip #{specification.root.name}"
  p zipCommand
  system zipCommand
  command = "cd #{rootName};curl #{Common.instance.binary_upload_url} -F \"name=#{specification.root.name}\" -F \"version=#{specification.version}\" -F \"annotate=#{specification.root.name}_#{specification.version}_log\" -F \"file=@#{specification.root.name}.zip\""
  p command 
  system command
end

#validate!Object



32
33
34
35
36
37
# File 'lib/luna/binary/uploader.rb', line 32

def validate!
  raise '缺少gitUrl参数' unless gitUrl
  raise '缺少gitNode参数' unless gitNode
  raise '缺少binaryPath参数' unless binaryPath
  raise '缺少podspecName参数' unless podspecName
end

#write_spec_file(specification) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/luna/binary/uploader.rb', line 348

def write_spec_file(specification)
  file = filename
  createSpec unless specification

  FileUtils.mkdir_p(binary_json_dir) unless File.exist?(binary_json_dir)
  FileUtils.rm_rf(file) if File.exist?(file)

  File.open(file, 'w+') do |f|
    # f.write("# MARK: converted automatically by plugin cocoapods-imy-bin @slj \r\n")
    f.write(specification.to_pretty_json)
  end

  @filename = file
end